Skip to content

Commit

Permalink
test: add NFT mint unit test to pop api extension
Browse files Browse the repository at this point in the history
  • Loading branch information
peterwht committed Mar 2, 2024
1 parent 3da22e9 commit 0bf56b6
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pop-api/src/v0/balances.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ parachain-info = { workspace = true }
[dev-dependencies]
env_logger = "0.11.2"
hex = "0.4.3"
enumflags2 = "0.7.9"

[features]
default = ["std"]
Expand Down
43 changes: 38 additions & 5 deletions runtime/src/extensions/pop_api_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,10 @@ where
mod tests {
pub use super::*;
pub use crate::*;
use enumflags2::BitFlags;
pub use pallet_contracts::Code;
use pallet_nfts::{CollectionConfig, CollectionSetting, CollectionSettings, MintSettings};
use parachains_common::CollectionId;
pub use sp_runtime::{traits::Hash, AccountId32};

pub const DEBUG_OUTPUT: pallet_contracts::DebugInfo = pallet_contracts::DebugInfo::UnsafeDebug;
Expand Down Expand Up @@ -163,6 +166,21 @@ mod tests {
[hash[0..4].to_vec()].concat()
}

// NFT helper functions
fn collection_config_from_disabled_settings(
settings: BitFlags<CollectionSetting>,
) -> CollectionConfig<Balance, BlockNumber, CollectionId> {
CollectionConfig {
settings: CollectionSettings::from_disabled(settings),
max_supply: None,
mint_settings: MintSettings::default(),
}
}

fn default_collection_config() -> CollectionConfig<Balance, BlockNumber, CollectionId> {
collection_config_from_disabled_settings(CollectionSetting::DepositRequired.into())
}

#[test]
fn test_dispatch() {
new_test_ext().execute_with(|| {
Expand Down Expand Up @@ -263,9 +281,25 @@ mod tests {

let addr = result.account_id;

let function = function_selector("mint_through_runtime");
let collection_id: u32 = 1;
let collection_id: u32 = 0;
let item_id: u32 = 1;

// create nft collection
assert_eq!(
Nfts::force_create(
RuntimeOrigin::root(),
ALICE.into(),
default_collection_config()
),
Ok(())
);

assert_eq!(Nfts::collection_owner(collection_id), Some(ALICE.into()));
// assert that the item does not exist yet
assert_eq!(Nfts::owner(collection_id, item_id), None);

let function = function_selector("mint_through_runtime");

let params = [
function,
collection_id.encode(),
Expand All @@ -274,9 +308,6 @@ mod tests {
]
.concat();

let bob_balance_before = Balances::free_balance(&BOB);
assert_eq!(bob_balance_before, INITIAL_AMOUNT);

let result = Contracts::bare_call(
ALICE,
addr.clone(),
Expand All @@ -299,6 +330,8 @@ mod tests {

// check for revert
assert!(!result.result.unwrap().did_revert(), "Contract reverted!");

assert_eq!(Nfts::owner(collection_id, item_id), Some(BOB.into()));
});
}
}

0 comments on commit 0bf56b6

Please sign in to comment.