Skip to content

Commit

Permalink
refactor(api/nonfungibles): pallet tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chungquantin committed Oct 18, 2024
1 parent c41134f commit a32e801
Showing 1 changed file with 143 additions and 100 deletions.
243 changes: 143 additions & 100 deletions pallets/api/src/nonfungibles/tests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use codec::Encode;
use frame_support::assert_ok;
use frame_support::{assert_noop, assert_ok};
use frame_system::pallet_prelude::BlockNumberFor;
use pallet_nfts::{AccountBalance, CollectionConfig, CollectionSettings, MintSettings};
use pallet_nfts::{
AccountBalance, CollectionConfig, CollectionDetails, CollectionSettings, ItemDeposit,
ItemDetails, MintSettings,
};
use sp_runtime::{BoundedBTreeMap, BoundedVec, DispatchError::BadOrigin};

use super::types::{AccountIdOf, CollectionIdOf, ItemIdOf};
use crate::{
Expand All @@ -12,10 +16,9 @@ use crate::{

const ITEM: u32 = 1;

mod encoding_read_result {
use pallet_nfts::{CollectionDetails, ItemDeposit, ItemDetails};
use sp_runtime::{BoundedBTreeMap, BoundedVec};
type NftsError = pallet_nfts::Error<Test>;

mod encoding_read_result {
use super::*;

#[test]
Expand Down Expand Up @@ -107,53 +110,72 @@ mod encoding_read_result {
}

#[test]
fn mint_works() {
fn transfer() {
new_test_ext().execute_with(|| {
let owner = account(ALICE);
let collection = create_collection(owner.clone());
// Successfully mint a new collection item.
let balance_before_mint = AccountBalance::<Test>::get(collection.clone(), owner.clone());
//
assert_ok!(NonFungibles::mint(
signed(owner.clone()),
owner.clone(),
collection,
ITEM,
None
));
let balance_after_mint = AccountBalance::<Test>::get(collection.clone(), owner.clone());
assert_eq!(balance_after_mint, 1);
assert_eq!(balance_after_mint - balance_before_mint, 1);
let owner = ALICE;
let dest = BOB;

let (collection, item) = nfts::create_collection_mint(owner, ITEM);
for origin in vec![root(), none()] {
assert_noop!(
NonFungibles::transfer(origin, collection, item, account(dest)),
BadOrigin
);
}
// Successfully burn an existing new collection item.
let balance_before_transfer = AccountBalance::<Test>::get(collection, &account(dest));
assert_ok!(NonFungibles::transfer(signed(owner), collection, ITEM, account(dest)));
let balance_after_transfer = AccountBalance::<Test>::get(collection, &account(dest));
assert_eq!(AccountBalance::<Test>::get(collection, &account(owner)), 0);
assert_eq!(balance_after_transfer - balance_before_transfer, 1);
System::assert_last_event(
Event::Transfer { collection, item: ITEM, from: None, to: Some(owner), price: None }
.into(),
Event::Transfer {
collection,
item,
from: Some(account(owner)),
to: Some(account(dest)),
price: None,
}
.into(),
);
});
}

#[test]
fn burn_works() {
fn mint_works() {
new_test_ext().execute_with(|| {
let owner = account(ALICE);
let (collection, item) = create_collection_mint(owner.clone(), ITEM);
// Successfully burn an existing new collection item.
assert_ok!(NonFungibles::burn(signed(owner.clone()), collection, ITEM));
let owner = ALICE;
let collection = nfts::create_collection(owner);

// Successfully mint a new collection item.
let balance_before_mint = AccountBalance::<Test>::get(collection, account(owner));
assert_ok!(NonFungibles::mint(signed(owner), account(owner), collection, ITEM, None));
let balance_after_mint = AccountBalance::<Test>::get(collection, account(owner));
assert_eq!(balance_after_mint, 1);
assert_eq!(balance_after_mint - balance_before_mint, 1);
System::assert_last_event(
Event::Transfer { collection, item, from: Some(owner), to: None, price: None }.into(),
Event::Transfer {
collection,
item: ITEM,
from: None,
to: Some(account(owner)),
price: None,
}
.into(),
);
});
}

#[test]
fn transfer() {
fn burn_works() {
new_test_ext().execute_with(|| {
let owner = account(ALICE);
let dest = account(BOB);
let (collection, item) = create_collection_mint(owner.clone(), ITEM);
let owner = ALICE;

// Successfully burn an existing new collection item.
assert_ok!(NonFungibles::transfer(signed(owner.clone()), collection, ITEM, dest.clone()));
let (collection, item) = nfts::create_collection_mint(owner, ITEM);
assert_ok!(NonFungibles::burn(signed(owner), collection, ITEM));
System::assert_last_event(
Event::Transfer { collection, item, from: Some(owner), to: Some(dest), price: None }
Event::Transfer { collection, item, from: Some(account(owner)), to: None, price: None }
.into(),
);
});
Expand All @@ -162,59 +184,63 @@ fn transfer() {
#[test]
fn approve_works() {
new_test_ext().execute_with(|| {
let owner = account(ALICE);
let operator = account(BOB);
let (collection, item) = create_collection_mint(owner.clone(), ITEM);
// Successfully approve `spender` to transfer the collection item.
let owner = ALICE;
let operator = BOB;
let (collection, item) = nfts::create_collection_mint(owner, ITEM);
// Successfully approve `oeprator` to transfer the collection item.
assert_ok!(NonFungibles::approve(
signed(owner.clone()),
signed(owner),
collection,
Some(item),
operator.clone(),
account(operator),
true
));
System::assert_last_event(
Event::Approval {
collection,
item: Some(item),
owner,
operator: operator.clone(),
owner: account(owner),
operator: account(operator),
approved: true,
}
.into(),
);
// Successfully transfer the item by the delegated account `spender`.
assert_ok!(Nfts::transfer(signed(operator.clone()), collection, item, operator));
// Successfully transfer the item by the delegated account `operator`.
assert_ok!(Nfts::transfer(signed(operator), collection, item, account(operator)));
});
}

#[test]
fn cancel_approval_works() {
new_test_ext().execute_with(|| {
let owner = account(ALICE);
let spender = account(BOB);
let (collection, item) =
create_collection_mint_and_approve(owner.clone(), ITEM, spender.clone());
// Successfully cancel the transfer approval of `spender` by `owner`.
let owner = ALICE;
let operator = BOB;
let (collection, item) = nfts::create_collection_mint_and_approve(owner, ITEM, operator);
// Successfully cancel the transfer approval of `operator` by `owner`.
assert_ok!(NonFungibles::approve(
signed(owner),
collection,
Some(item),
spender.clone(),
account(operator),
false
));
// Failed to transfer the item by `spender` without permission.
assert!(Nfts::transfer(signed(spender.clone()), collection, item, spender).is_err());
// Failed to transfer the item by `operator` without permission.
assert_noop!(
Nfts::transfer(signed(operator), collection, item, account(operator)),
NftsError::NoPermission
);
});
}

#[test]
fn owner_of_works() {}
fn owner_of_works() {
unimplemented!()
}

#[test]
fn collection_owner_works() {
new_test_ext().execute_with(|| {
let collection = create_collection(account(ALICE));
let collection = nfts::create_collection(ALICE);
assert_eq!(
NonFungibles::read(CollectionOwner(collection)).encode(),
Nfts::collection_owner(collection).encode()
Expand All @@ -225,7 +251,7 @@ fn collection_owner_works() {
#[test]
fn total_supply_works() {
new_test_ext().execute_with(|| {
let (collection, _) = create_collection_mint(account(ALICE), ITEM);
let (collection, _) = nfts::create_collection_mint(ALICE, ITEM);
assert_eq!(
NonFungibles::read(TotalSupply(collection)).encode(),
Nfts::collection_items(collection).unwrap_or_default().encode()
Expand All @@ -236,7 +262,7 @@ fn total_supply_works() {
#[test]
fn collection_works() {
new_test_ext().execute_with(|| {
let (collection, _) = create_collection_mint(account(ALICE), ITEM);
let (collection, _) = nfts::create_collection_mint(ALICE, ITEM);
assert_eq!(
NonFungibles::read(Collection(collection)).encode(),
pallet_nfts::Collection::<Test>::get(&collection).encode(),
Expand All @@ -247,76 +273,93 @@ fn collection_works() {
#[test]
fn balance_of_works() {
new_test_ext().execute_with(|| {
let owner = account(ALICE);
let (collection, _) = create_collection_mint(owner.clone(), ITEM);
let owner = ALICE;
let (collection, _) = nfts::create_collection_mint(owner, ITEM);
assert_eq!(
NonFungibles::read(BalanceOf { collection, owner: owner.clone() }).encode(),
AccountBalance::<Test>::get(collection, owner).encode()
NonFungibles::read(BalanceOf { collection, owner: account(owner) }).encode(),
AccountBalance::<Test>::get(collection, account(owner)).encode()
);
});
}

#[test]
fn allowance_works() {
new_test_ext().execute_with(|| {
let owner = account(ALICE);
let operator = account(BOB);
let (collection, item) =
create_collection_mint_and_approve(owner.clone(), ITEM, operator.clone());
let owner = ALICE;
let operator = BOB;
let (collection, item) = nfts::create_collection_mint_and_approve(owner, ITEM, operator);
assert_eq!(
NonFungibles::read(Allowance {
collection,
item: Some(item),
owner: owner.clone(),
operator: operator.clone(),
owner: account(owner),
operator: account(operator),
})
.encode(),
Nfts::check_allowance(&collection, &Some(item), &owner, &operator)
Nfts::check_allowance(&collection, &Some(item), &account(owner), &account(operator))
.is_ok()
.encode()
);
});
}

fn signed(account: AccountId) -> RuntimeOrigin {
RuntimeOrigin::signed(account)
fn signed(account_id: u8) -> RuntimeOrigin {
RuntimeOrigin::signed(account(account_id))
}

fn create_collection_mint_and_approve(
owner: AccountIdOf<Test>,
item: ItemIdOf<Test>,
spender: AccountIdOf<Test>,
) -> (u32, u32) {
let (collection, item) = create_collection_mint(owner.clone(), item);
assert_ok!(Nfts::approve_transfer(signed(owner), collection, Some(item), spender, None));
(collection, item)
fn root() -> RuntimeOrigin {
RuntimeOrigin::root()
}

fn create_collection_mint(owner: AccountIdOf<Test>, item: ItemIdOf<Test>) -> (u32, u32) {
let collection = create_collection(owner.clone());
assert_ok!(Nfts::mint(signed(owner.clone()), collection, item, owner, None));
(collection, item)
fn none() -> RuntimeOrigin {
RuntimeOrigin::none()
}

fn create_collection(owner: AccountIdOf<Test>) -> u32 {
let next_id = next_collection_id();
assert_ok!(Nfts::create(
signed(owner.clone()),
owner.clone(),
collection_config_with_all_settings_enabled()
));
next_id
}
mod nfts {
use super::*;

fn next_collection_id() -> u32 {
pallet_nfts::NextCollectionId::<Test>::get().unwrap_or_default()
}
pub(super) fn create_collection_mint_and_approve(
owner: u8,
item: ItemIdOf<Test>,
operator: u8,
) -> (u32, u32) {
let (collection, item) = create_collection_mint(owner, item);
assert_ok!(Nfts::approve_transfer(
signed(owner),
collection,
Some(item),
account(operator),
None
));
(collection, item)
}

pub(super) fn create_collection_mint(owner: u8, item: ItemIdOf<Test>) -> (u32, u32) {
let collection = create_collection(owner);
assert_ok!(Nfts::mint(signed(owner), collection, item, account(owner), None));
(collection, item)
}

pub(super) fn create_collection(owner: u8) -> u32 {
let next_id = next_collection_id();
assert_ok!(Nfts::create(
signed(owner),
account(owner),
collection_config_with_all_settings_enabled()
));
next_id
}

pub(super) fn next_collection_id() -> u32 {
pallet_nfts::NextCollectionId::<Test>::get().unwrap_or_default()
}

fn collection_config_with_all_settings_enabled(
) -> CollectionConfig<Balance, BlockNumberFor<Test>, CollectionIdOf<Test>> {
CollectionConfig {
settings: CollectionSettings::all_enabled(),
max_supply: None,
mint_settings: MintSettings::default(),
pub(super) fn collection_config_with_all_settings_enabled(
) -> CollectionConfig<Balance, BlockNumberFor<Test>, CollectionIdOf<Test>> {
CollectionConfig {
settings: CollectionSettings::all_enabled(),
max_supply: None,
mint_settings: MintSettings::default(),
}
}
}

0 comments on commit a32e801

Please sign in to comment.