Skip to content

Commit

Permalink
tests for 1155 WIP #2
Browse files Browse the repository at this point in the history
  • Loading branch information
coreggon11 committed Oct 28, 2021
1 parent b4069a7 commit 95d6541
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 24 deletions.
54 changes: 45 additions & 9 deletions contracts/token/psp1155/tests/burnable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>,
) -> 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<u8>,
) -> 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::<ink_env::DefaultEnvironment>().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);
}
}

Expand All @@ -38,6 +64,7 @@ mod burnable {
let accounts = ink_env::test::default_accounts::<ink_env::DefaultEnvironment>().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);
Expand All @@ -61,6 +88,7 @@ mod burnable {
let accounts = ink_env::test::default_accounts::<ink_env::DefaultEnvironment>().expect("Cannot get accounts");

let mut nft = PSP1155Struct::new();
nft.init(accounts.alice, accounts.bob);

nft.burn_batch(
burn_ids
Expand Down Expand Up @@ -130,6 +158,7 @@ mod burnable {
let accounts = ink_env::test::default_accounts::<ink_env::DefaultEnvironment>().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);
}
Expand All @@ -145,6 +174,7 @@ mod burnable {
let accounts = ink_env::test::default_accounts::<ink_env::DefaultEnvironment>().expect("Cannot get accounts");

let mut nft = PSP1155Struct::new();
nft.init(accounts.alice, accounts.bob);

nft.burn_batch_from(
accounts.bob,
Expand All @@ -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::<ink_env::DefaultEnvironment>().expect("Cannot get accounts");

let mut nft = PSP1155Struct::new();
nft.init(accounts.alice, accounts.bob);

nft.burn(token_id_1, burn_amount);
}
Expand All @@ -175,6 +207,7 @@ mod burnable {
let accounts = ink_env::test::default_accounts::<ink_env::DefaultEnvironment>().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);
Expand All @@ -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::<ink_env::DefaultEnvironment>().expect("Cannot get accounts");

let mut nft = PSP1155Struct::new();
nft.init(accounts.alice, accounts.bob);

nft.burn_batch(
burn_ids
Expand All @@ -215,6 +250,7 @@ mod burnable {
let accounts = ink_env::test::default_accounts::<ink_env::DefaultEnvironment>().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);
Expand Down
32 changes: 27 additions & 5 deletions contracts/token/psp1155/tests/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
#[brush::contract]
mod metadata {
use ink_lang as ink;
use ink_env::{
call,
test,
};
use psp1155::{
extensions::metadata::*,
traits::*,
Expand All @@ -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<u8>,
) -> 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<u8>,
) -> Result<(), psp1155::traits::PSP1155Error> {
Ok(())
}
}

impl PSP1155Metadata for PSP1155Struct {}

Expand All @@ -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/")));
}
}
29 changes: 27 additions & 2 deletions contracts/token/psp1155/tests/mintable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>,
) -> 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<u8>,
) -> Result<(), psp1155::traits::PSP1155Error> {
Ok(())
}
}

impl PSP1155Struct {
#[ink(constructor)]
pub fn new() -> Self {
Expand Down
17 changes: 9 additions & 8 deletions contracts/token/psp1155/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<ink_env::DefaultEnvironment>().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);
}
}

Expand Down

0 comments on commit 95d6541

Please sign in to comment.