Skip to content

Commit 21de3a4

Browse files
committed
test(sdk): add query and group tests
1 parent 22212aa commit 21de3a4

File tree

9 files changed

+531
-10
lines changed

9 files changed

+531
-10
lines changed

packages/rs-sdk/tests/fetch/config.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! This module contains [Config] struct that can be used to configure dash-platform-sdk.
44
//! It's mainly used for testing.
55
6-
use dpp::platform_value::string_encoding::Encoding;
6+
use crate::fetch::generated_data::*;
77
use dpp::{
88
dashcore::{hashes::Hash, ProTxHash},
99
prelude::Identifier,
@@ -13,15 +13,6 @@ use serde::Deserialize;
1313
use std::path::PathBuf;
1414
use zeroize::Zeroizing;
1515

16-
/// Existing document ID
17-
///
18-
// TODO: this is copy-paste from drive-abci `packages/rs-sdk/tests/fetch/main.rs` where it's private,
19-
// consider defining it in `data-contracts` crate
20-
const DPNS_DASH_TLD_DOCUMENT_ID: [u8; 32] = [
21-
215, 242, 197, 63, 70, 169, 23, 171, 110, 91, 57, 162, 215, 188, 38, 11, 100, 146, 137, 69, 55,
22-
68, 209, 224, 212, 242, 106, 141, 142, 255, 55, 207,
23-
];
24-
2516
#[derive(Debug, Deserialize)]
2617
/// Configuration for dash-platform-sdk.
2718
///
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
use dash_sdk::platform::Identifier;
2+
use dpp::tokens::calculate_token_id;
3+
use std::sync::LazyLock;
4+
5+
/// Existing document ID
6+
///
7+
// TODO: this is copy-paste from drive-abci `packages/rs-sdk/tests/fetch/main.rs` where it's private,
8+
// consider defining it in `data-contracts` crate
9+
pub const DPNS_DASH_TLD_DOCUMENT_ID: [u8; 32] = [
10+
215, 242, 197, 63, 70, 169, 23, 171, 110, 91, 57, 162, 215, 188, 38, 11, 100, 146, 137, 69, 55,
11+
68, 209, 224, 212, 242, 106, 141, 142, 255, 55, 207,
12+
];
13+
14+
/// Data contract with groups and tokens created by init chain for testing
15+
/// See `/packages/rs-drive-abci/src/execution/platform_events/initialization/create_genesis_state/mod.rs#L49`
16+
pub const DATA_CONTRACT_ID: Identifier = Identifier::new([3; 32]);
17+
/// Identity used in the data contract above created by init chain for testing
18+
/// See `/packages/rs-drive-abci/src/execution/platform_events/initialization/create_genesis_state/mod.rs#L49`
19+
pub const IDENTITY_ID_1: Identifier = Identifier::new([1; 32]);
20+
/// Second identity used in the data contract above created by init chain for testing
21+
/// See `/packages/rs-drive-abci/src/execution/platform_events/initialization/create_genesis_state/mod.rs#L49`
22+
pub const IDENTITY_ID_2: Identifier = Identifier::new([2; 32]);
23+
/// Third identity used in the data contract above created by init chain for testing
24+
/// See `/packages/rs-drive-abci/src/execution/platform_events/initialization/create_genesis_state/mod.rs#L49`
25+
pub const IDENTITY_ID_3: Identifier = Identifier::new([3; 32]);
26+
/// Token ID that doesn't exist
27+
pub const UNKNOWN_TOKEN_ID: Identifier = Identifier::new([1; 32]);
28+
/// Identity ID that doesn't exist
29+
pub const UNKNOWN_IDENTITY_ID: Identifier = Identifier::new([255; 32]);
30+
/// Group action ID that burns some tokens of the data contract above by the first identity
31+
/// This group action is created by init chain for testing
32+
/// See `/packages/rs-drive-abci/src/execution/platform_events/initialization/create_genesis_state/mod.rs#L49`
33+
pub const GROUP_ACTION_ID: Identifier = Identifier::new([32; 32]);
34+
/// The first token ID from the data contract above created by init chain for testing
35+
/// See `/packages/rs-drive-abci/src/execution/platform_events/initialization/create_genesis_state/mod.rs#L49`
36+
pub static TOKEN_ID_0: LazyLock<Identifier> =
37+
LazyLock::new(|| Identifier::new(calculate_token_id(&DATA_CONTRACT_ID.to_buffer(), 0)));
38+
/// The second token ID from the data contract above created by init chain for testing
39+
/// See `/packages/rs-drive-abci/src/execution/platform_events/initialization/create_genesis_state/mod.rs#L49`
40+
pub static TOKEN_ID_1: LazyLock<Identifier> =
41+
LazyLock::new(|| Identifier::new(calculate_token_id(&DATA_CONTRACT_ID.to_buffer(), 1)));
42+
/// The third token ID from the data contract above created by init chain for testing
43+
/// See `/packages/rs-drive-abci/src/execution/platform_events/initialization/create_genesis_state/mod.rs#L49`
44+
pub static TOKEN_ID_2: LazyLock<Identifier> =
45+
LazyLock::new(|| Identifier::new(calculate_token_id(&DATA_CONTRACT_ID.to_buffer(), 2)));
Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
use crate::fetch::common::setup_logs;
2+
use crate::fetch::config::Config;
3+
use crate::fetch::generated_data::*;
4+
use assert_matches::assert_matches;
5+
use dash_sdk::platform::group_actions::{
6+
GroupActionSignersQuery, GroupActionsQuery, GroupInfosQuery, GroupQuery,
7+
};
8+
use dash_sdk::platform::{Fetch, FetchMany};
9+
use dpp::data_contract::group::v0::GroupV0;
10+
use dpp::data_contract::group::{Group, GroupMemberPower};
11+
use dpp::group::action_event::GroupActionEvent;
12+
use dpp::group::group_action::v0::GroupActionV0;
13+
use dpp::group::group_action::GroupAction;
14+
use dpp::group::group_action_status::GroupActionStatus;
15+
use dpp::tokens::token_event::TokenEvent;
16+
use std::collections::BTreeMap;
17+
18+
/// Fetches non-existing group
19+
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
20+
async fn test_group_not_found() {
21+
setup_logs();
22+
23+
let cfg = Config::new();
24+
let sdk = cfg.setup_api("test_group_not_found").await;
25+
26+
let query = GroupQuery {
27+
contract_id: DATA_CONTRACT_ID,
28+
group_contract_position: 99,
29+
};
30+
31+
let group = Group::fetch(&sdk, query).await.expect("fetch group");
32+
33+
assert_eq!(group, None);
34+
}
35+
36+
/// Fetches existing group
37+
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
38+
async fn test_group_fetch() {
39+
setup_logs();
40+
41+
let cfg = Config::new();
42+
let sdk = cfg.setup_api("test_group_fetch").await;
43+
44+
let query = GroupQuery {
45+
contract_id: DATA_CONTRACT_ID,
46+
group_contract_position: 0,
47+
};
48+
49+
let group = Group::fetch(&sdk, query).await.expect("fetch group");
50+
51+
assert_matches!(
52+
group,
53+
Some(Group::V0(GroupV0 {
54+
members,
55+
required_power: 1
56+
})) if members == BTreeMap::from([(IDENTITY_ID_1, 1), (IDENTITY_ID_2, 1)])
57+
);
58+
}
59+
60+
/// Fetches one group since first one exclusive
61+
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
62+
async fn test_fetch_1_groups_since_0() {
63+
setup_logs();
64+
65+
let cfg = Config::new();
66+
let sdk = cfg.setup_api("test_fetch_1_groups_since_0").await;
67+
68+
let query = GroupInfosQuery {
69+
contract_id: DATA_CONTRACT_ID,
70+
start_group_contract_position: Some((0, false)),
71+
limit: Some(1),
72+
};
73+
74+
let groups = Group::fetch_many(&sdk, query).await.expect("fetch group");
75+
76+
assert_eq!(groups.len(), 1);
77+
78+
dbg!(&groups);
79+
80+
assert_matches!(
81+
groups.get(&1),
82+
Some(Some(Group::V0(GroupV0 {
83+
members,
84+
required_power: 3
85+
}))) if members == &BTreeMap::from([(IDENTITY_ID_1, 1), (IDENTITY_ID_2, 1), (IDENTITY_ID_3, 1)])
86+
);
87+
}
88+
89+
/// Fetches all groups since second one inclusive
90+
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
91+
async fn test_fetch_all_groups_since_1_inclusive() {
92+
setup_logs();
93+
94+
let cfg = Config::new();
95+
let sdk = cfg
96+
.setup_api("test_fetch_all_groups_since_1_inclusive")
97+
.await;
98+
99+
let query = GroupInfosQuery {
100+
contract_id: DATA_CONTRACT_ID,
101+
start_group_contract_position: Some((1, true)),
102+
limit: None,
103+
};
104+
105+
let groups = Group::fetch_many(&sdk, query).await.expect("fetch group");
106+
107+
assert_eq!(groups.len(), 2);
108+
109+
assert_matches!(
110+
groups.get(&1),
111+
Some(Some(Group::V0(GroupV0 {
112+
members,
113+
required_power: 3
114+
}))) if members == &BTreeMap::from([(IDENTITY_ID_1, 1), (IDENTITY_ID_2, 1), (IDENTITY_ID_3, 1)])
115+
);
116+
117+
assert_matches!(
118+
groups.get(&2),
119+
Some(Some(Group::V0(GroupV0 {
120+
members,
121+
required_power: 2
122+
}))) if members == &BTreeMap::from([(IDENTITY_ID_1, 1), (IDENTITY_ID_3, 1)])
123+
);
124+
}
125+
126+
/// Fetches all group actions
127+
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
128+
async fn test_fetch_all_group_actions() {
129+
setup_logs();
130+
131+
let cfg = Config::new();
132+
let sdk = cfg.setup_api("test_fetch_all_group_actions").await;
133+
134+
let query = GroupActionsQuery {
135+
contract_id: DATA_CONTRACT_ID,
136+
group_contract_position: 2,
137+
status: GroupActionStatus::ActionActive,
138+
limit: None,
139+
start_at_action_id: None,
140+
};
141+
142+
let group_actions = GroupAction::fetch_many(&sdk, query)
143+
.await
144+
.expect("fetch group");
145+
146+
assert_eq!(group_actions.len(), 1);
147+
148+
dbg!(&group_actions);
149+
150+
assert_matches!(
151+
group_actions.get(&GROUP_ACTION_ID),
152+
Some(Some(GroupAction::V0(GroupActionV0 {
153+
event: GroupActionEvent::TokenEvent(TokenEvent::Burn(10, Some(note))),
154+
}))) if note == "world on fire"
155+
);
156+
}
157+
158+
/// Fetches one group action since specific one
159+
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
160+
async fn test_fetch_one_group_action_since_existing_one_with_limit() {
161+
setup_logs();
162+
163+
let cfg = Config::new();
164+
let sdk = cfg
165+
.setup_api("test_fetch_one_group_action_since_existing_one_with_limit")
166+
.await;
167+
168+
let query = GroupActionsQuery {
169+
contract_id: DATA_CONTRACT_ID,
170+
group_contract_position: 2,
171+
status: GroupActionStatus::ActionActive,
172+
limit: Some(1),
173+
start_at_action_id: Some((GROUP_ACTION_ID, true)),
174+
};
175+
176+
let group_actions = GroupAction::fetch_many(&sdk, query)
177+
.await
178+
.expect("fetch group");
179+
180+
assert_eq!(group_actions.len(), 1);
181+
182+
assert_matches!(
183+
group_actions.get(&GROUP_ACTION_ID),
184+
Some(Some(GroupAction::V0(GroupActionV0 {
185+
event: GroupActionEvent::TokenEvent(TokenEvent::Burn(10, Some(note))),
186+
}))) if note == "world on fire"
187+
);
188+
}
189+
190+
/// Fetches group action signers
191+
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
192+
async fn test_fetch_group_action_signers() {
193+
setup_logs();
194+
195+
let cfg = Config::new();
196+
let sdk = cfg.setup_api("test_fetch_group_action_signers").await;
197+
198+
let query = GroupActionSignersQuery {
199+
contract_id: DATA_CONTRACT_ID,
200+
group_contract_position: 2,
201+
status: GroupActionStatus::ActionActive,
202+
action_id: GROUP_ACTION_ID,
203+
};
204+
205+
let group_actions = GroupMemberPower::fetch_many(&sdk, query)
206+
.await
207+
.expect("fetch group");
208+
209+
assert_eq!(group_actions.len(), 1);
210+
assert_eq!(group_actions.get(&IDENTITY_ID_1), Some(&Some(1)));
211+
}

packages/rs-sdk/tests/fetch/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@ mod data_contract;
1818
mod document;
1919
mod epoch;
2020
mod evonode;
21+
mod generated_data;
22+
mod group_actions;
2123
mod identity;
2224
mod identity_contract_nonce;
2325
mod mock_fetch;
2426
mod mock_fetch_many;
2527
mod prefunded_specialized_balance;
2628
mod protocol_version_vote_count;
2729
mod protocol_version_votes;
30+
mod tokens;

0 commit comments

Comments
 (0)