|
| 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 | +} |
0 commit comments