From 8e4322ca259774e65a0752a9b689fdfa6603dc60 Mon Sep 17 00:00:00 2001 From: chungquantin <56880684+chungquantin@users.noreply.github.com> Date: Mon, 30 Dec 2024 11:02:18 +0700 Subject: [PATCH] chore: update tests --- pallets/nfts/src/tests.rs | 133 ++++++++++++++++++++------- runtime/devnet/src/config/assets.rs | 20 +++- runtime/testnet/src/config/assets.rs | 20 +++- 3 files changed, 136 insertions(+), 37 deletions(-) diff --git a/pallets/nfts/src/tests.rs b/pallets/nfts/src/tests.rs index f1c36c698..7a5a518d7 100644 --- a/pallets/nfts/src/tests.rs +++ b/pallets/nfts/src/tests.rs @@ -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; @@ -374,7 +372,6 @@ 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), @@ -382,12 +379,6 @@ fn destroy_should_work() { )); 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)), @@ -401,19 +392,6 @@ fn destroy_should_work() { assert_eq!(Collection::::get(0).unwrap().item_configs, 1); assert_eq!(ItemConfigOf::::iter_prefix(0).count() as u32, 1); assert!(ItemConfigOf::::contains_key(0, 42)); - assert_noop!( - Nfts::destroy( - RuntimeOrigin::signed(account(1)), - 0, - Nfts::get_destroy_witness(&0).unwrap() - ), - Error::::CollectionApprovalsExist - ); - assert_ok!(Nfts::cancel_collection_approval( - RuntimeOrigin::signed(account(2)), - 0, - account(3) - )); assert_ok!(Nfts::destroy( RuntimeOrigin::signed(account(1)), 0, @@ -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::::get(0, account(2)), 0); - assert_eq!(AccountBalance::::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)), @@ -592,8 +568,6 @@ fn transfer_should_work() { None )); assert_ok!(Nfts::transfer(RuntimeOrigin::signed(account(2)), 0, 42, account(4))); - assert_eq!(AccountBalance::::get(0, account(3)), 0); - assert_eq!(AccountBalance::::get(0, account(4)), 1); // validate we can't transfer non-transferable items let collection_id = 1; assert_ok!(Nfts::force_create( @@ -4729,11 +4703,104 @@ fn check_approval_with_deadline_works() { } #[test] -fn ensure_collection_approval_bytes() { - let key = Blake2_128Concat::max_len::<::CollectionId>() + - Blake2_128Concat::max_len::() + - Blake2_128Concat::max_len::(); - let value = Option::>::max_encoded_len() - .saturating_add(BalanceOf::::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::::get(0, account(1)), 1); + + // Additive. + assert_ok!(Nfts::mint(RuntimeOrigin::signed(account(1)), 0, 43, account(1), None)); + assert_eq!(AccountBalance::::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::::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::::get(0, account(2)), 0); + assert_eq!(AccountBalance::::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::::get(0, account(3)), 0); + assert_eq!(AccountBalance::::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::::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::::contains_key(0, 42)); + assert_eq!(ItemConfigOf::::iter_prefix(0).count() as u32, 0); + }); } diff --git a/runtime/devnet/src/config/assets.rs b/runtime/devnet/src/config/assets.rs index 0ff2a0cf6..e46d7dbc8 100644 --- a/runtime/devnet/src/config/assets.rs +++ b/runtime/devnet/src/config/assets.rs @@ -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); @@ -123,3 +123,19 @@ impl pallet_assets::Config for Runtime { type StringLimit = AssetsStringLimit; type WeightInfo = pallet_assets::weights::SubstrateWeight; } + +#[cfg(test)] +mod tests { + use frame_support::traits::StorageInfoTrait; + + use super::*; + + #[test] + fn ensure_collection_approval_deposit() { + let max_size = pallet_nfts::CollectionApprovals::::storage_info() + .first() + .and_then(|info| info.max_size) + .unwrap_or_default(); + assert_eq!(deposit(1, max_size), NftsCollectionApprovalDeposit::get()); + } +} diff --git a/runtime/testnet/src/config/assets.rs b/runtime/testnet/src/config/assets.rs index 0ff2a0cf6..e46d7dbc8 100644 --- a/runtime/testnet/src/config/assets.rs +++ b/runtime/testnet/src/config/assets.rs @@ -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); @@ -123,3 +123,19 @@ impl pallet_assets::Config for Runtime { type StringLimit = AssetsStringLimit; type WeightInfo = pallet_assets::weights::SubstrateWeight; } + +#[cfg(test)] +mod tests { + use frame_support::traits::StorageInfoTrait; + + use super::*; + + #[test] + fn ensure_collection_approval_deposit() { + let max_size = pallet_nfts::CollectionApprovals::::storage_info() + .first() + .and_then(|info| info.max_size) + .unwrap_or_default(); + assert_eq!(deposit(1, max_size), NftsCollectionApprovalDeposit::get()); + } +}