Skip to content

Commit

Permalink
test(sdk): add query and group tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shumkov committed Feb 3, 2025
1 parent 22212aa commit 21de3a4
Show file tree
Hide file tree
Showing 9 changed files with 531 additions and 10 deletions.
11 changes: 1 addition & 10 deletions packages/rs-sdk/tests/fetch/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! This module contains [Config] struct that can be used to configure dash-platform-sdk.
//! It's mainly used for testing.
use dpp::platform_value::string_encoding::Encoding;
use crate::fetch::generated_data::*;
use dpp::{
dashcore::{hashes::Hash, ProTxHash},
prelude::Identifier,
Expand All @@ -13,15 +13,6 @@ use serde::Deserialize;
use std::path::PathBuf;
use zeroize::Zeroizing;

/// Existing document ID
///
// TODO: this is copy-paste from drive-abci `packages/rs-sdk/tests/fetch/main.rs` where it's private,
// consider defining it in `data-contracts` crate
const DPNS_DASH_TLD_DOCUMENT_ID: [u8; 32] = [
215, 242, 197, 63, 70, 169, 23, 171, 110, 91, 57, 162, 215, 188, 38, 11, 100, 146, 137, 69, 55,
68, 209, 224, 212, 242, 106, 141, 142, 255, 55, 207,
];

#[derive(Debug, Deserialize)]
/// Configuration for dash-platform-sdk.
///
Expand Down
45 changes: 45 additions & 0 deletions packages/rs-sdk/tests/fetch/generated_data.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use dash_sdk::platform::Identifier;
use dpp::tokens::calculate_token_id;
use std::sync::LazyLock;

/// Existing document ID
///
// TODO: this is copy-paste from drive-abci `packages/rs-sdk/tests/fetch/main.rs` where it's private,
// consider defining it in `data-contracts` crate
pub const DPNS_DASH_TLD_DOCUMENT_ID: [u8; 32] = [
215, 242, 197, 63, 70, 169, 23, 171, 110, 91, 57, 162, 215, 188, 38, 11, 100, 146, 137, 69, 55,
68, 209, 224, 212, 242, 106, 141, 142, 255, 55, 207,
];

/// Data contract with groups and tokens created by init chain for testing
/// See `/packages/rs-drive-abci/src/execution/platform_events/initialization/create_genesis_state/mod.rs#L49`
pub const DATA_CONTRACT_ID: Identifier = Identifier::new([3; 32]);
/// Identity used in the data contract above created by init chain for testing
/// See `/packages/rs-drive-abci/src/execution/platform_events/initialization/create_genesis_state/mod.rs#L49`
pub const IDENTITY_ID_1: Identifier = Identifier::new([1; 32]);
/// Second identity used in the data contract above created by init chain for testing
/// See `/packages/rs-drive-abci/src/execution/platform_events/initialization/create_genesis_state/mod.rs#L49`
pub const IDENTITY_ID_2: Identifier = Identifier::new([2; 32]);
/// Third identity used in the data contract above created by init chain for testing
/// See `/packages/rs-drive-abci/src/execution/platform_events/initialization/create_genesis_state/mod.rs#L49`
pub const IDENTITY_ID_3: Identifier = Identifier::new([3; 32]);
/// Token ID that doesn't exist
pub const UNKNOWN_TOKEN_ID: Identifier = Identifier::new([1; 32]);
/// Identity ID that doesn't exist
pub const UNKNOWN_IDENTITY_ID: Identifier = Identifier::new([255; 32]);
/// Group action ID that burns some tokens of the data contract above by the first identity
/// This group action is created by init chain for testing
/// See `/packages/rs-drive-abci/src/execution/platform_events/initialization/create_genesis_state/mod.rs#L49`
pub const GROUP_ACTION_ID: Identifier = Identifier::new([32; 32]);
/// The first token ID from the data contract above created by init chain for testing
/// See `/packages/rs-drive-abci/src/execution/platform_events/initialization/create_genesis_state/mod.rs#L49`
pub static TOKEN_ID_0: LazyLock<Identifier> =
LazyLock::new(|| Identifier::new(calculate_token_id(&DATA_CONTRACT_ID.to_buffer(), 0)));
/// The second token ID from the data contract above created by init chain for testing
/// See `/packages/rs-drive-abci/src/execution/platform_events/initialization/create_genesis_state/mod.rs#L49`
pub static TOKEN_ID_1: LazyLock<Identifier> =
LazyLock::new(|| Identifier::new(calculate_token_id(&DATA_CONTRACT_ID.to_buffer(), 1)));
/// The third token ID from the data contract above created by init chain for testing
/// See `/packages/rs-drive-abci/src/execution/platform_events/initialization/create_genesis_state/mod.rs#L49`
pub static TOKEN_ID_2: LazyLock<Identifier> =
LazyLock::new(|| Identifier::new(calculate_token_id(&DATA_CONTRACT_ID.to_buffer(), 2)));
211 changes: 211 additions & 0 deletions packages/rs-sdk/tests/fetch/group_actions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
use crate::fetch::common::setup_logs;
use crate::fetch::config::Config;
use crate::fetch::generated_data::*;
use assert_matches::assert_matches;
use dash_sdk::platform::group_actions::{
GroupActionSignersQuery, GroupActionsQuery, GroupInfosQuery, GroupQuery,
};
use dash_sdk::platform::{Fetch, FetchMany};
use dpp::data_contract::group::v0::GroupV0;
use dpp::data_contract::group::{Group, GroupMemberPower};
use dpp::group::action_event::GroupActionEvent;
use dpp::group::group_action::v0::GroupActionV0;
use dpp::group::group_action::GroupAction;
use dpp::group::group_action_status::GroupActionStatus;
use dpp::tokens::token_event::TokenEvent;
use std::collections::BTreeMap;

/// Fetches non-existing group
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn test_group_not_found() {
setup_logs();

let cfg = Config::new();
let sdk = cfg.setup_api("test_group_not_found").await;

let query = GroupQuery {
contract_id: DATA_CONTRACT_ID,
group_contract_position: 99,
};

let group = Group::fetch(&sdk, query).await.expect("fetch group");

assert_eq!(group, None);
}

/// Fetches existing group
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn test_group_fetch() {
setup_logs();

let cfg = Config::new();
let sdk = cfg.setup_api("test_group_fetch").await;

let query = GroupQuery {
contract_id: DATA_CONTRACT_ID,
group_contract_position: 0,
};

let group = Group::fetch(&sdk, query).await.expect("fetch group");

assert_matches!(
group,
Some(Group::V0(GroupV0 {
members,
required_power: 1
})) if members == BTreeMap::from([(IDENTITY_ID_1, 1), (IDENTITY_ID_2, 1)])
);
}

/// Fetches one group since first one exclusive
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn test_fetch_1_groups_since_0() {
setup_logs();

let cfg = Config::new();
let sdk = cfg.setup_api("test_fetch_1_groups_since_0").await;

let query = GroupInfosQuery {
contract_id: DATA_CONTRACT_ID,
start_group_contract_position: Some((0, false)),
limit: Some(1),
};

let groups = Group::fetch_many(&sdk, query).await.expect("fetch group");

assert_eq!(groups.len(), 1);

dbg!(&groups);

assert_matches!(
groups.get(&1),
Some(Some(Group::V0(GroupV0 {
members,
required_power: 3
}))) if members == &BTreeMap::from([(IDENTITY_ID_1, 1), (IDENTITY_ID_2, 1), (IDENTITY_ID_3, 1)])
);
}

/// Fetches all groups since second one inclusive
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn test_fetch_all_groups_since_1_inclusive() {
setup_logs();

let cfg = Config::new();
let sdk = cfg
.setup_api("test_fetch_all_groups_since_1_inclusive")
.await;

let query = GroupInfosQuery {
contract_id: DATA_CONTRACT_ID,
start_group_contract_position: Some((1, true)),
limit: None,
};

let groups = Group::fetch_many(&sdk, query).await.expect("fetch group");

assert_eq!(groups.len(), 2);

assert_matches!(
groups.get(&1),
Some(Some(Group::V0(GroupV0 {
members,
required_power: 3
}))) if members == &BTreeMap::from([(IDENTITY_ID_1, 1), (IDENTITY_ID_2, 1), (IDENTITY_ID_3, 1)])
);

assert_matches!(
groups.get(&2),
Some(Some(Group::V0(GroupV0 {
members,
required_power: 2
}))) if members == &BTreeMap::from([(IDENTITY_ID_1, 1), (IDENTITY_ID_3, 1)])
);
}

/// Fetches all group actions
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn test_fetch_all_group_actions() {
setup_logs();

let cfg = Config::new();
let sdk = cfg.setup_api("test_fetch_all_group_actions").await;

let query = GroupActionsQuery {
contract_id: DATA_CONTRACT_ID,
group_contract_position: 2,
status: GroupActionStatus::ActionActive,
limit: None,
start_at_action_id: None,
};

let group_actions = GroupAction::fetch_many(&sdk, query)
.await
.expect("fetch group");

assert_eq!(group_actions.len(), 1);

dbg!(&group_actions);

assert_matches!(
group_actions.get(&GROUP_ACTION_ID),
Some(Some(GroupAction::V0(GroupActionV0 {
event: GroupActionEvent::TokenEvent(TokenEvent::Burn(10, Some(note))),
}))) if note == "world on fire"
);
}

/// Fetches one group action since specific one
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn test_fetch_one_group_action_since_existing_one_with_limit() {
setup_logs();

let cfg = Config::new();
let sdk = cfg
.setup_api("test_fetch_one_group_action_since_existing_one_with_limit")
.await;

let query = GroupActionsQuery {
contract_id: DATA_CONTRACT_ID,
group_contract_position: 2,
status: GroupActionStatus::ActionActive,
limit: Some(1),
start_at_action_id: Some((GROUP_ACTION_ID, true)),
};

let group_actions = GroupAction::fetch_many(&sdk, query)
.await
.expect("fetch group");

assert_eq!(group_actions.len(), 1);

assert_matches!(
group_actions.get(&GROUP_ACTION_ID),
Some(Some(GroupAction::V0(GroupActionV0 {
event: GroupActionEvent::TokenEvent(TokenEvent::Burn(10, Some(note))),
}))) if note == "world on fire"
);
}

/// Fetches group action signers
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn test_fetch_group_action_signers() {
setup_logs();

let cfg = Config::new();
let sdk = cfg.setup_api("test_fetch_group_action_signers").await;

let query = GroupActionSignersQuery {
contract_id: DATA_CONTRACT_ID,
group_contract_position: 2,
status: GroupActionStatus::ActionActive,
action_id: GROUP_ACTION_ID,
};

let group_actions = GroupMemberPower::fetch_many(&sdk, query)
.await
.expect("fetch group");

assert_eq!(group_actions.len(), 1);
assert_eq!(group_actions.get(&IDENTITY_ID_1), Some(&Some(1)));
}
3 changes: 3 additions & 0 deletions packages/rs-sdk/tests/fetch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ mod data_contract;
mod document;
mod epoch;
mod evonode;
mod generated_data;
mod group_actions;
mod identity;
mod identity_contract_nonce;
mod mock_fetch;
mod mock_fetch_many;
mod prefunded_specialized_balance;
mod protocol_version_vote_count;
mod protocol_version_votes;
mod tokens;
Loading

0 comments on commit 21de3a4

Please sign in to comment.