-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery_asset_definition.rs
84 lines (77 loc) · 3.66 KB
/
query_asset_definition.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use crate::core::state::may_load_asset_definition_by_type_v3;
use crate::util::aliases::AssetResult;
use cosmwasm_std::{to_json_binary, Binary, Deps};
use result_extensions::ResultExtensions;
/// A query that fetches a target [AssetDefinitionV3](crate::core::types::asset_definition::AssetDefinitionV3)
/// from the contract's internal storage.
///
/// # Parameters
///
/// * `deps` A dependencies object provided by the cosmwasm framework. Allows access to useful
/// resources like contract internal storage and a querier to retrieve blockchain objects.
/// * `asset_type` The asset type corresponding to the stored
/// [AssetDefinitionV3](crate::core::types::asset_definition::AssetDefinitionV3) to look up.
pub fn query_asset_definition(deps: &Deps, asset_type: &str) -> AssetResult<Binary> {
let asset_definition = may_load_asset_definition_by_type_v3(deps.storage, asset_type)?;
to_json_binary(&asset_definition)?.to_ok()
}
#[cfg(test)]
mod tests {
use crate::core::state::insert_asset_definition_v3;
use crate::core::types::asset_definition::AssetDefinitionV3;
use crate::query::query_asset_definition::query_asset_definition;
use crate::testutil::test_utilities::{
get_default_asset_definition, test_instantiate_success, InstArgs,
};
use cosmwasm_std::{from_json, Deps};
use provwasm_mocks::mock_provenance_dependencies;
#[test]
fn test_successful_query_from_instantiation_for_asset_type() {
let mut deps = mock_provenance_dependencies();
test_instantiate_success(deps.as_mut(), &InstArgs::default());
// This is the same value that gets added when the contract is instantiated with InstArgs::default()
let default_asset_definition = get_default_asset_definition();
let from_query_def = get_asset_from_query_by_asset_type(
&deps.as_ref(),
&default_asset_definition.asset_type,
);
assert_eq!(
default_asset_definition, from_query_def,
"expected the query value to equate to the value added during instantiation",
);
}
#[test]
fn test_successful_query_from_direct_serialization_for_asset_type() {
let mut deps = mock_provenance_dependencies();
let asset_def = get_default_asset_definition();
insert_asset_definition_v3(deps.as_mut().storage, &asset_def)
.expect("expected the asset definition to be properly saved to state");
let query_def = get_asset_from_query_by_asset_type(&deps.as_ref(), &asset_def.asset_type);
assert_eq!(
asset_def, query_def,
"expected the query value to equate to the value directly added to the state",
);
}
#[test]
fn test_none_is_returned_when_asset_definition_is_not_found_by_asset_type() {
let binary = query_asset_definition(&mock_provenance_dependencies().as_ref(), "fakeloan")
.expect("the query should execute without error");
let result = from_json::<Option<AssetDefinitionV3>>(&binary)
.expect("expected the binary to deserialize appropriately");
assert!(
result.is_none(),
"the resulting binary should be an empty Option",
);
}
fn get_asset_from_query_by_asset_type<S: Into<String>>(
deps: &Deps,
asset_type: S,
) -> AssetDefinitionV3 {
let bin = query_asset_definition(deps, &asset_type.into()).expect(
"the query should successfully serialize the value in storage as binary without error",
);
from_json::<Option<AssetDefinitionV3>>(&bin)
.expect("binary deserialization should succeed")
.expect("expected the deserialized option to be populated")
}
}