Skip to content

Commit

Permalink
chore: update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chungquantin committed Dec 30, 2024
1 parent 24c0c2d commit 8e4322c
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 37 deletions.
133 changes: 100 additions & 33 deletions pallets/nfts/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@ use enumflags2::BitFlags;
use frame_support::{
assert_noop, assert_ok,
dispatch::{DispatchResultWithPostInfo, WithPostDispatchInfo},
pallet_prelude::MaxEncodedLen,
traits::{
tokens::nonfungibles_v2::{Create, Destroy, Inspect, Mutate},
Currency, Get,
},
Blake2_128Concat, StorageHasher,
};
use frame_system::pallet_prelude::BlockNumberFor;
use pallet_balances::Error as BalancesError;
Expand Down Expand Up @@ -374,20 +372,13 @@ fn destroy_with_bad_witness_should_not_work() {
fn destroy_should_work() {
new_test_ext().execute_with(|| {
Balances::make_free_balance_be(&account(1), 100);
Balances::make_free_balance_be(&account(2), 100);
assert_ok!(Nfts::create(
RuntimeOrigin::signed(account(1)),
account(1),
collection_config_with_all_settings_enabled()
));

assert_ok!(Nfts::mint(RuntimeOrigin::signed(account(1)), 0, 42, account(2), None));
assert_ok!(Nfts::approve_collection_transfer(
RuntimeOrigin::signed(account(2)),
0,
account(3),
None
));
assert_noop!(
Nfts::destroy(
RuntimeOrigin::signed(account(1)),
Expand All @@ -401,19 +392,6 @@ fn destroy_should_work() {
assert_eq!(Collection::<Test>::get(0).unwrap().item_configs, 1);
assert_eq!(ItemConfigOf::<Test>::iter_prefix(0).count() as u32, 1);
assert!(ItemConfigOf::<Test>::contains_key(0, 42));
assert_noop!(
Nfts::destroy(
RuntimeOrigin::signed(account(1)),
0,
Nfts::get_destroy_witness(&0).unwrap()
),
Error::<Test>::CollectionApprovalsExist
);
assert_ok!(Nfts::cancel_collection_approval(
RuntimeOrigin::signed(account(2)),
0,
account(3)
));
assert_ok!(Nfts::destroy(
RuntimeOrigin::signed(account(1)),
0,
Expand Down Expand Up @@ -576,8 +554,6 @@ fn transfer_should_work() {
default_item_config()
));
assert_ok!(Nfts::transfer(RuntimeOrigin::signed(account(2)), 0, 42, account(3)));
assert_eq!(AccountBalance::<Test>::get(0, account(2)), 0);
assert_eq!(AccountBalance::<Test>::get(0, account(3)), 1);
assert_eq!(items(), vec![(account(3), 0, 42)]);
assert_noop!(
Nfts::transfer(RuntimeOrigin::signed(account(2)), 0, 42, account(4)),
Expand All @@ -592,8 +568,6 @@ fn transfer_should_work() {
None
));
assert_ok!(Nfts::transfer(RuntimeOrigin::signed(account(2)), 0, 42, account(4)));
assert_eq!(AccountBalance::<Test>::get(0, account(3)), 0);
assert_eq!(AccountBalance::<Test>::get(0, account(4)), 1);
// validate we can't transfer non-transferable items
let collection_id = 1;
assert_ok!(Nfts::force_create(
Expand Down Expand Up @@ -4729,11 +4703,104 @@ fn check_approval_with_deadline_works() {
}

#[test]
fn ensure_collection_approval_bytes() {
let key = Blake2_128Concat::max_len::<<Test as crate::Config>::CollectionId>() +
Blake2_128Concat::max_len::<AccountId>() +
Blake2_128Concat::max_len::<AccountId>();
let value = Option::<BlockNumberFor<Test>>::max_encoded_len()
.saturating_add(BalanceOf::<Test>::max_encoded_len());
assert_eq!(key + value, 133);
fn mint_with_account_balance_updated_works() {
new_test_ext().execute_with(|| {
assert_ok!(Nfts::force_create(
RuntimeOrigin::root(),
account(1),
default_collection_config()
));

assert_ok!(Nfts::mint(RuntimeOrigin::signed(account(1)), 0, 42, account(1), None));
assert_eq!(AccountBalance::<Test>::get(0, account(1)), 1);

// Additive.
assert_ok!(Nfts::mint(RuntimeOrigin::signed(account(1)), 0, 43, account(1), None));
assert_eq!(AccountBalance::<Test>::get(0, account(1)), 2);

// Mint with witness data.
assert_ok!(Nfts::mint(
RuntimeOrigin::signed(account(1)),
0,
44,
account(2),
Some(MintWitness { mint_price: Some(1), ..Default::default() })
));
assert_eq!(AccountBalance::<Test>::get(0, account(2)), 1);
});
}

#[test]
fn transfer_with_account_balance_updated_works() {
new_test_ext().execute_with(|| {
assert_ok!(Nfts::force_create(
RuntimeOrigin::root(),
account(1),
default_collection_config()
));
assert_ok!(Nfts::force_mint(
RuntimeOrigin::signed(account(1)),
0,
42,
account(2),
default_item_config()
));
assert_ok!(Nfts::transfer(RuntimeOrigin::signed(account(2)), 0, 42, account(3)));
assert_eq!(AccountBalance::<Test>::get(0, account(2)), 0);
assert_eq!(AccountBalance::<Test>::get(0, account(3)), 1);

assert_ok!(Nfts::approve_transfer(
RuntimeOrigin::signed(account(3)),
0,
42,
account(2),
None
));
assert_ok!(Nfts::transfer(RuntimeOrigin::signed(account(2)), 0, 42, account(4)));
assert_eq!(AccountBalance::<Test>::get(0, account(3)), 0);
assert_eq!(AccountBalance::<Test>::get(0, account(4)), 1);
});
}

#[test]
fn destroy_with_collection_approvals_should_not_work() {
new_test_ext().execute_with(|| {
Balances::make_free_balance_be(&account(1), 100);
Balances::make_free_balance_be(&account(2), 100);
assert_ok!(Nfts::create(
RuntimeOrigin::signed(account(1)),
account(1),
collection_config_with_all_settings_enabled()
));

assert_ok!(Nfts::mint(RuntimeOrigin::signed(account(1)), 0, 42, account(2), None));
assert_ok!(Nfts::approve_collection_transfer(
RuntimeOrigin::signed(account(2)),
0,
account(3),
None
));
assert_ok!(Nfts::burn(RuntimeOrigin::signed(account(2)), 0, 42));

assert_noop!(
Nfts::destroy(
RuntimeOrigin::signed(account(1)),
0,
Nfts::get_destroy_witness(&0).unwrap()
),
Error::<Test>::CollectionApprovalsExist
);
assert_ok!(Nfts::cancel_collection_approval(
RuntimeOrigin::signed(account(2)),
0,
account(3)
));
assert_ok!(Nfts::destroy(
RuntimeOrigin::signed(account(1)),
0,
Nfts::get_destroy_witness(&0).unwrap()
));
assert!(!ItemConfigOf::<Test>::contains_key(0, 42));
assert_eq!(ItemConfigOf::<Test>::iter_prefix(0).count() as u32, 0);
});
}
20 changes: 18 additions & 2 deletions runtime/devnet/src/config/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ parameter_types! {
parameter_types! {
pub NftsPalletFeatures: PalletFeatures = PalletFeatures::all_enabled();
pub const NftsCollectionDeposit: Balance = 10 * UNIT;
// Key = 116 bytes (4+16+32+16+32+16), Value = 17 bytes (1+8+8)
pub const NftsCollectionApprovalDeposit: Balance = deposit(1, 133);
// Key = 116 bytes (4+16+32+16+32+16), Value = 21 bytes (1+4+16)
pub const NftsCollectionApprovalDeposit: Balance = deposit(1, 137);
pub const NftsItemDeposit: Balance = UNIT / 100;
pub const NftsMetadataDepositBase: Balance = deposit(1, 129);
pub const NftsAttributeDepositBase: Balance = deposit(1, 0);
Expand Down Expand Up @@ -123,3 +123,19 @@ impl pallet_assets::Config<TrustBackedAssetsInstance> for Runtime {
type StringLimit = AssetsStringLimit;
type WeightInfo = pallet_assets::weights::SubstrateWeight<Self>;
}

#[cfg(test)]
mod tests {
use frame_support::traits::StorageInfoTrait;

use super::*;

#[test]
fn ensure_collection_approval_deposit() {
let max_size = pallet_nfts::CollectionApprovals::<Runtime>::storage_info()
.first()
.and_then(|info| info.max_size)
.unwrap_or_default();
assert_eq!(deposit(1, max_size), NftsCollectionApprovalDeposit::get());
}
}
20 changes: 18 additions & 2 deletions runtime/testnet/src/config/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ parameter_types! {
parameter_types! {
pub NftsPalletFeatures: PalletFeatures = PalletFeatures::all_enabled();
pub const NftsCollectionDeposit: Balance = 10 * UNIT;
// Key = 116 bytes (4+16+32+16+32+16), Value = 17 bytes (1+8+8)
pub const NftsCollectionApprovalDeposit: Balance = deposit(1, 133);
// Key = 116 bytes (4+16+32+16+32+16), Value = 21 bytes (1+4+16)
pub const NftsCollectionApprovalDeposit: Balance = deposit(1, 137);
pub const NftsItemDeposit: Balance = UNIT / 100;
pub const NftsMetadataDepositBase: Balance = deposit(1, 129);
pub const NftsAttributeDepositBase: Balance = deposit(1, 0);
Expand Down Expand Up @@ -123,3 +123,19 @@ impl pallet_assets::Config<TrustBackedAssetsInstance> for Runtime {
type StringLimit = AssetsStringLimit;
type WeightInfo = pallet_assets::weights::SubstrateWeight<Self>;
}

#[cfg(test)]
mod tests {
use frame_support::traits::StorageInfoTrait;

use super::*;

#[test]
fn ensure_collection_approval_deposit() {
let max_size = pallet_nfts::CollectionApprovals::<Runtime>::storage_info()
.first()
.and_then(|info| info.max_size)
.unwrap_or_default();
assert_eq!(deposit(1, max_size), NftsCollectionApprovalDeposit::get());
}
}

0 comments on commit 8e4322c

Please sign in to comment.