-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery_asset_definitions.rs
121 lines (113 loc) · 4.76 KB
/
query_asset_definitions.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
use cosmwasm_std::{to_json_binary, Binary, Deps};
use result_extensions::ResultExtensions;
use crate::core::state::list_asset_definitions_v3;
use crate::util::aliases::AssetResult;
/// A query that fetches all [AssetDefinitionV3s](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.
pub fn query_asset_definitions(deps: &Deps) -> AssetResult<Binary> {
let asset_definitions = list_asset_definitions_v3(deps.storage);
to_json_binary(&asset_definitions)?.to_ok()
}
#[cfg(test)]
mod tests {
use cosmwasm_std::{from_json, Uint128};
use provwasm_mocks::mock_provenance_dependencies;
use crate::core::types::asset_definition::{AssetDefinitionInputV3, AssetDefinitionV3};
use crate::core::types::verifier_detail::VerifierDetailV2;
use crate::testutil::{
test_constants::DEFAULT_VERIFIER_ADDRESS,
test_utilities::{get_default_asset_definition, test_instantiate_success, InstArgs},
};
use crate::util::traits::OptionExtensions;
use super::query_asset_definitions;
#[test]
fn test_empty_result() {
let deps = mock_provenance_dependencies();
let response_bin = query_asset_definitions(&deps.as_ref())
.expect("expected the query to execute appropriately");
let query_response = from_json::<Vec<AssetDefinitionV3>>(&response_bin)
.expect("expected the query to deserialize from binary correctly");
assert!(
query_response.is_empty(),
"expected no asset definitions to exist due to the contract not being instantiated"
);
}
#[test]
fn test_default_instantiation_result() {
let mut deps = mock_provenance_dependencies();
test_instantiate_success(deps.as_mut(), &InstArgs::default());
let response_bin = query_asset_definitions(&deps.as_ref())
.expect("expected the query to execute appropriately");
let query_response = from_json::<Vec<AssetDefinitionV3>>(&response_bin)
.expect("expected the query to deserialize from binary correctly");
assert_eq!(
1,
query_response.len(),
"expected only one asset definition to be in the response",
);
let default_definition = get_default_asset_definition();
assert_eq!(
&default_definition,
query_response.first().unwrap(),
"expected the response to include the default asset definition used in default instantiation",
);
}
#[test]
fn test_many_definitions_result() {
let mut deps = mock_provenance_dependencies();
let mut def_ids = Vec::with_capacity(20);
// Populate a vec with 0-19 just for different asset definitions
def_ids.extend(0..20);
let asset_definition_inputs = def_ids
.into_iter()
.map(|id| {
AssetDefinitionInputV3::new(
format!("asset_type_{}", id),
format!("Display Name for {}", id).to_some(),
vec![VerifierDetailV2::new(
DEFAULT_VERIFIER_ADDRESS,
Uint128::new(150),
"nhash",
vec![],
None,
None,
None,
)],
true.to_some(),
true.to_some(),
)
})
.collect::<Vec<AssetDefinitionInputV3>>();
test_instantiate_success(
deps.as_mut(),
&InstArgs {
asset_definitions: asset_definition_inputs.clone(),
..Default::default()
},
);
let response_bin = query_asset_definitions(&deps.as_ref())
.expect("expected the query to execute appropriately");
let query_response = from_json::<Vec<AssetDefinitionV3>>(&response_bin)
.expect("expected the query to deserialize from binary correctly");
assert_eq!(
20,
query_response.len(),
"expected all 20 asset definitions to be included in the response",
);
asset_definition_inputs
.into_iter()
.map(|input| input.into_asset_definition())
.for_each(|asset_definition| {
assert!(
query_response.iter().any(|def| def == &asset_definition),
"expected the asset definition of type [{}] to be found in the query response",
asset_definition.asset_type,
);
});
}
}