From 95d65413d8a3eee70adb0c99a4f7717adbfa3332 Mon Sep 17 00:00:00 2001 From: "kriko.eth" <43150707+coreggon11@users.noreply.github.com> Date: Thu, 28 Oct 2021 20:54:39 +0200 Subject: [PATCH] tests for 1155 WIP #2 --- contracts/token/psp1155/tests/burnable.rs | 54 +++++++++++++++++++---- contracts/token/psp1155/tests/metadata.rs | 32 +++++++++++--- contracts/token/psp1155/tests/mintable.rs | 29 +++++++++++- contracts/token/psp1155/tests/tests.rs | 17 +++---- 4 files changed, 108 insertions(+), 24 deletions(-) diff --git a/contracts/token/psp1155/tests/burnable.rs b/contracts/token/psp1155/tests/burnable.rs index c1476e8c2..4fd314c97 100644 --- a/contracts/token/psp1155/tests/burnable.rs +++ b/contracts/token/psp1155/tests/burnable.rs @@ -14,20 +14,46 @@ mod burnable { psp1155: PSP1155Data, } - impl PSP1155 for PSP1155Struct {} - impl PSP1155Burnable for PSP1155Struct {} + impl PSP1155 for PSP1155Struct { + // Don't do cross call in test + fn _do_safe_transfer_acceptance_check( + &mut self, + _operator: AccountId, + _from: AccountId, + _to: AccountId, + _id: Id, + _amount: Balance, + _data: Vec, + ) -> Result<(), psp1155::traits::PSP1155Error> { + Ok(()) + } + + // Don't do cross call in test + fn _do_batch_safe_transfer_acceptance_check( + &mut self, + _operator: AccountId, + _from: AccountId, + _to: AccountId, + _ids_to_amounts: Vec<(Id, Balance)>, + _data: Vec, + ) -> Result<(), psp1155::traits::PSP1155Error> { + Ok(()) + } + } + impl PSP1155Struct { #[ink(constructor)] pub fn new() -> Self { - let mut instance = Self::default(); - let accounts = - ink_env::test::default_accounts::().expect("Cannot get accounts"); - instance._mint(accounts.alice, [1; 32], 20); - instance._mint(accounts.bob, [1; 32], 20); - instance._mint(accounts.bob, [2; 32], 20); - instance + Self::default() + } + + #[ink(message)] + pub fn init(&mut self, acc1: AccountId, acc2: AccountId) { + self._mint(acc1.clone(), [1; 32], 20); + self._mint(acc2.clone(), [1; 32], 20); + self._mint(acc2.clone(), [2; 32], 20); } } @@ -38,6 +64,7 @@ mod burnable { let accounts = ink_env::test::default_accounts::().expect("Cannot get accounts"); let mut nft = PSP1155Struct::new(); + nft.init(accounts.alice, accounts.bob); assert_eq!(nft.balance_of(accounts.alice, token_id_1), token_1_amount); assert_eq!(nft.balance_of(accounts.bob, token_id_1), token_1_amount); @@ -61,6 +88,7 @@ mod burnable { let accounts = ink_env::test::default_accounts::().expect("Cannot get accounts"); let mut nft = PSP1155Struct::new(); + nft.init(accounts.alice, accounts.bob); nft.burn_batch( burn_ids @@ -130,6 +158,7 @@ mod burnable { let accounts = ink_env::test::default_accounts::().expect("Cannot get accounts"); let mut nft = PSP1155Struct::new(); + nft.init(accounts.alice, accounts.bob); nft.burn_from(accounts.bob, token_id_1, token_1_amount); } @@ -145,6 +174,7 @@ mod burnable { let accounts = ink_env::test::default_accounts::().expect("Cannot get accounts"); let mut nft = PSP1155Struct::new(); + nft.init(accounts.alice, accounts.bob); nft.burn_batch_from( accounts.bob, @@ -161,8 +191,10 @@ mod burnable { fn burn_insufficient_balance() { let token_id_1 = [1; 32]; let burn_amount = 2; + let accounts = ink_env::test::default_accounts::().expect("Cannot get accounts"); let mut nft = PSP1155Struct::new(); + nft.init(accounts.alice, accounts.bob); nft.burn(token_id_1, burn_amount); } @@ -175,6 +207,7 @@ mod burnable { let accounts = ink_env::test::default_accounts::().expect("Cannot get accounts"); let mut nft = PSP1155Struct::new(); + nft.init(accounts.alice, accounts.bob); // switch to bob, give allowance to alice so we can burn_from bob by alice change_callee(accounts.bob); @@ -192,8 +225,10 @@ mod burnable { let token_amount = 2; let burn_ids = vec![token_id_1, token_id_2]; let burn_amounts = vec![token_amount, token_amount]; + let accounts = ink_env::test::default_accounts::().expect("Cannot get accounts"); let mut nft = PSP1155Struct::new(); + nft.init(accounts.alice, accounts.bob); nft.burn_batch( burn_ids @@ -215,6 +250,7 @@ mod burnable { let accounts = ink_env::test::default_accounts::().expect("Cannot get accounts"); let mut nft = PSP1155Struct::new(); + nft.init(accounts.alice, accounts.bob); // switch to bob, give allowance to alice so we can burn_from bob by alice change_callee(accounts.bob); diff --git a/contracts/token/psp1155/tests/metadata.rs b/contracts/token/psp1155/tests/metadata.rs index 43c202ab8..b7a47a231 100644 --- a/contracts/token/psp1155/tests/metadata.rs +++ b/contracts/token/psp1155/tests/metadata.rs @@ -2,10 +2,6 @@ #[brush::contract] mod metadata { use ink_lang as ink; - use ink_env::{ - call, - test, - }; use psp1155::{ extensions::metadata::*, traits::*, @@ -20,6 +16,32 @@ mod metadata { metadata: PSP1155MetadataData, } + impl PSP1155 for PSP1155Struct { + // Don't do cross call in test + fn _do_safe_transfer_acceptance_check( + &mut self, + _operator: AccountId, + _from: AccountId, + _to: AccountId, + _id: Id, + _amount: Balance, + _data: Vec, + ) -> Result<(), psp1155::traits::PSP1155Error> { + Ok(()) + } + + // Don't do cross call in test + fn _do_batch_safe_transfer_acceptance_check( + &mut self, + _operator: AccountId, + _from: AccountId, + _to: AccountId, + _ids_to_amounts: Vec<(Id, Balance)>, + _data: Vec, + ) -> Result<(), psp1155::traits::PSP1155Error> { + Ok(()) + } + } impl PSP1155Metadata for PSP1155Struct {} @@ -36,6 +58,6 @@ mod metadata { fn metadata_works() { let nft = PSP1155Struct::new(Some(String::from("https://www.supercolony.net/"))); - assert_eq!(nft.uri([0;32]), Some(String::from("https://www.supercolony.net/"))); + assert_eq!(nft.uri([0; 32]), Some(String::from("https://www.supercolony.net/"))); } } diff --git a/contracts/token/psp1155/tests/mintable.rs b/contracts/token/psp1155/tests/mintable.rs index b7990d03b..cdf859066 100644 --- a/contracts/token/psp1155/tests/mintable.rs +++ b/contracts/token/psp1155/tests/mintable.rs @@ -14,10 +14,35 @@ mod mintable { psp1155: PSP1155Data, } - impl PSP1155 for PSP1155Struct {} - impl PSP1155Mintable for PSP1155Struct {} + impl PSP1155 for PSP1155Struct { + // Don't do cross call in test + fn _do_safe_transfer_acceptance_check( + &mut self, + _operator: AccountId, + _from: AccountId, + _to: AccountId, + _id: Id, + _amount: Balance, + _data: Vec, + ) -> Result<(), psp1155::traits::PSP1155Error> { + Ok(()) + } + + // Don't do cross call in test + fn _do_batch_safe_transfer_acceptance_check( + &mut self, + _operator: AccountId, + _from: AccountId, + _to: AccountId, + _ids_to_amounts: Vec<(Id, Balance)>, + _data: Vec, + ) -> Result<(), psp1155::traits::PSP1155Error> { + Ok(()) + } + } + impl PSP1155Struct { #[ink(constructor)] pub fn new() -> Self { diff --git a/contracts/token/psp1155/tests/tests.rs b/contracts/token/psp1155/tests/tests.rs index 70c28d4ee..34eb6da85 100644 --- a/contracts/token/psp1155/tests/tests.rs +++ b/contracts/token/psp1155/tests/tests.rs @@ -116,14 +116,15 @@ mod tests { impl PSP1155Struct { #[ink(constructor)] pub fn new() -> Self { - let mut instance = Self::default(); - let accounts = - ink_env::test::default_accounts::().expect("Cannot get accounts"); - instance._mint(accounts.alice, [1; 32], 1); - instance._mint(accounts.alice, [2; 32], 20); - instance._mint(accounts.bob, [1; 32], 1); - instance._mint(accounts.bob, [2; 32], 20); - instance + Self::default() + } + + #[ink(message)] + pub fn init(&mut self, acc1: AccountId, acc2: AccountId) { + self._mint(acc1, [1; 32], 1); + self._mint(acc1, [2; 32], 20); + self._mint(acc2, [1; 32], 1); + self._mint(acc2, [2; 32], 20); } }