Chromia
Start Building

14 / 15Module 3: Create the marketplaceAdvanced

Test our marketplace

Write a test to confirm the marketplace behaves correctly. You'll assemble it piece by piece with the Rell test framework. Open src/rell_marketplace_test/blockchain_rell_marketplace_test.rell and add these imports:

src/rell_marketplace_test/blockchain_rell_marketplace_test.rell
@test module;import rell_marketplace.*;import lib.ft4.test.core.{ ft_auth_operation_for };import lib.ft4.assets.{ get_asset_balance };import lib.ft4.external.accounts.{ get_accounts_by_signer };

The test also needs a query that returns the last account and your NFTs. You'll reuse this query in a later lesson when testing with the Chromia CLI.

src/rell_marketplace/queries.rell
query get_last_account() = account @ {} ( $, @omit @sort_desc .rowid ) limit 1;query get_all_nfts() {    return nft_card @* {} (        nft_card_dto(            strength = .strength,            health = .health,            owner_id = .nft.owner.id,            nft_id = .nft.rowid        )    );}struct nft_card_dto {    strength: integer;    health: integer;    owner_id: byte_array;    nft_id: rowid;}

Next, define a test function called test_list_and_buy_nft. Inside it, create two test accounts along with the economy_account.

src/rell_marketplace_test/blockchain_rell_marketplace_test.rell
function test_list_and_buy_nft() {    val alice = rell.test.keypairs.alice;    val trudy = rell.test.keypairs.trudy;    val economy_account = rell.test.keypairs.frank;    rell.test.tx()        .op(create_user(alice.pub)).sign(alice)        .run();    val alice_account = get_last_account();    rell.test.tx()        .op(create_user(trudy.pub)).sign(trudy)        .run();    val trudy_account = get_last_account();}

Once Alice owns a mystery card, add the step that puts it up for sale. First, retrieve the card minted by the earlier operation; it is the first and only one. Then call the list_nft operation with Alice as the signer.

src/rell_marketplace_test/blockchain_rell_marketplace_test.rell
    rell.test.tx()        .op(ft_auth_operation_for(alice.pub))        .op(buy_mystery_card()).sign(alice)        .run();    val nft = get_all_nfts()[0];    rell.test.tx()        .op(ft_auth_operation_for(alice.pub))        .op(list_nft(nft.nft_id.to_integer(), 20))        .sign(alice)        .run();

With the card listed, Trudy can now purchase it from the marketplace at the listed price.

src/rell_marketplace_test/blockchain_rell_marketplace_test.rell
    rell.test.tx()        .op(ft_auth_operation_for(trudy.pub))        .op(buy_nft(nft.nft_id.to_integer()))        .sign(trudy)        .run();

Finally, check the outcome: compare the account balances and confirm who now owns the minted NFT.

src/rell_marketplace_test/blockchain_rell_marketplace_test.rell
    val nft_after_trade = get_all_nfts()[0];    assert_equals(get_asset_balance(alice_account, dapp_meta.asset), 920);    assert_equals(get_asset_balance(trudy_account, dapp_meta.asset), 980);    assert_equals(nft_after_trade.owner_id, trudy_account.id);

The test is now complete. Run it from your terminal with the chr test command. A passing run confirms that the marketplace works as intended.

The next lesson walks through a comparable test using the Chromia CLI.

Written for this site from Chromia’s public course. Code samples are the course’s own.