Skip to content

Commit

Permalink
feat(storage): implements object data query
Browse files Browse the repository at this point in the history
  • Loading branch information
amimart committed Mar 3, 2023
1 parent 7c0eea3 commit 11267fa
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions contracts/cw-storage/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
match msg {
QueryMsg::Bucket {} => to_binary(&query::bucket(deps)?),
QueryMsg::Object { id } => to_binary(&query::object(deps, id)?),
QueryMsg::ObjectData { id } => to_binary(&query::data(deps, id)?),
_ => Err(StdError::generic_err("Not implemented")),
}
}
Expand Down Expand Up @@ -154,6 +155,10 @@ pub mod query {
.is_some(),
})
}

pub fn data(deps: Deps, id: ObjectId) -> StdResult<Binary> {
DATA.load(deps.storage, id).map(Binary::from)
}
}

#[cfg(test)]
Expand Down Expand Up @@ -458,4 +463,41 @@ mod tests {
assert!(!res.is_pinned);
assert_eq!(res.size.u128(), 4u128);
}

#[test]
fn object_data() {
let mut deps = mock_dependencies();
let info = mock_info("creator", &[]);

let msg = InstantiateMsg {
bucket: String::from("test"),
limits: BucketLimits::new(),
};
instantiate(deps.as_mut(), mock_env(), info.clone(), msg).unwrap();

match query::object(
deps.as_ref(),
ObjectId::from("315d0d9ab12c5f8884100055f79de50b72db4bd2c9bfd3df049d89640fed1fa6"),
)
.err()
.unwrap()
{
NotFound { .. } => (),
_ => panic!("assertion failed"),
}

let data = general_purpose::STANDARD.encode("okp4");
let msg = ExecuteMsg::StoreObject {
data: Binary::from_base64(data.as_str()).unwrap(),
pin: false,
};
execute(deps.as_mut(), mock_env(), info, msg).unwrap();

let res = query::data(
deps.as_ref(),
ObjectId::from("315d0d9ab12c5f8884100055f79de50b72db4bd2c9bfd3df049d89640fed1fa6"),
)
.unwrap();
assert_eq!(res, Binary::from_base64(data.as_str()).unwrap());
}
}

0 comments on commit 11267fa

Please sign in to comment.