From 701fbdac11848b6c0fb3a6334bf0fa84e1a8ac63 Mon Sep 17 00:00:00 2001 From: Nikhil Ranjan Date: Mon, 3 Apr 2023 11:08:44 +0200 Subject: [PATCH 1/3] e2e tests --- contracts/marketplace/lib.rs | 61 ++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/contracts/marketplace/lib.rs b/contracts/marketplace/lib.rs index 73d6ef1..1d5ea13 100644 --- a/contracts/marketplace/lib.rs +++ b/contracts/marketplace/lib.rs @@ -331,4 +331,65 @@ pub mod marketplace { AccountId::from([0x2; 32]) } } + + #[cfg(all(test, feature = "e2e-tests"))] + mod e2e_tests { + use super::*; + use ink_e2e::build_message; + + type E2EResult = std::result::Result>; + + #[ink_e2e::test] + async fn it_works(mut client: ink_e2e::Client) -> E2EResult<()> { + // given + let constructor = marketplaceRef::new(false); + let contract_acc_id = client + .instantiate("marketplace", &ink_e2e::alice(), constructor, 0, None) + .await + .expect("instantiate failed") + .account_id; + + let get = build_message::(contract_acc_id.clone()) + .call(|marketplace| marketplace.get()); + let get_res = client.call_dry_run(&ink_e2e::bob(), &get, 0, None).await; + assert!(matches!(get_res.return_value(), false)); + + // when + let flip = build_message::(contract_acc_id.clone()) + .call(|marketplace| marketplace.flip()); + let _flip_res = client + .call(&ink_e2e::bob(), flip, 0, None) + .await + .expect("flip failed"); + + // then + let get = build_message::(contract_acc_id.clone()) + .call(|marketplace| marketplace.get()); + let get_res = client.call_dry_run(&ink_e2e::bob(), &get, 0, None).await; + assert!(matches!(get_res.return_value(), true)); + + Ok(()) + } + + #[ink_e2e::test] + async fn default_works(mut client: ink_e2e::Client) -> E2EResult<()> { + // given + let constructor = marketplaceRef::new_default(); + + // when + let contract_acc_id = client + .instantiate("marketplace", &ink_e2e::bob(), constructor, 0, None) + .await + .expect("instantiate failed") + .account_id; + + // then + let get = build_message::(contract_acc_id.clone()) + .call(|marketplace| marketplace.get()); + let get_res = client.call_dry_run(&ink_e2e::bob(), &get, 0, None).await; + assert!(matches!(get_res.return_value(), false)); + + Ok(()) + } + } } From 93b708bb8ba00da18b40f5dd790f667bde9abd34 Mon Sep 17 00:00:00 2001 From: Nikhil Ranjan Date: Mon, 8 May 2023 09:50:14 +0200 Subject: [PATCH 2/3] dependencies --- contracts/marketplace/Cargo.toml | 3 + contracts/marketplace/lib.rs | 110 +++++++++++++++++++++---------- 2 files changed, 78 insertions(+), 35 deletions(-) diff --git a/contracts/marketplace/Cargo.toml b/contracts/marketplace/Cargo.toml index 6674601..103a85e 100644 --- a/contracts/marketplace/Cargo.toml +++ b/contracts/marketplace/Cargo.toml @@ -11,6 +11,9 @@ scale-info = { version = "2.3", default-features = false, features = ["derive"], openbrush = { tag = "3.0.0", git = "https://github.com/727-Ventures/openbrush-contracts", default-features = false, features = ["ownable", "psp34", "reentrancy_guard"] } pallet_marketplace = { path = "../../logics", default-features = false } +[dev-dependencies] +ink_e2e = "4.0.1" + [lib] path = "lib.rs" diff --git a/contracts/marketplace/lib.rs b/contracts/marketplace/lib.rs index 1d5ea13..605a62f 100644 --- a/contracts/marketplace/lib.rs +++ b/contracts/marketplace/lib.rs @@ -332,62 +332,102 @@ pub mod marketplace { } } + /// end-to-end (E2E) or integration tests for lottery. + /// + /// When running these you need to make sure that you: + /// - Compile the tests with the `e2e-tests` feature flag enabled (`--features e2e-tests`) + /// - Are running a Substrate node which contains `pallet-contracts` in the background #[cfg(all(test, feature = "e2e-tests"))] mod e2e_tests { + /// Imports all the definitions from the outer scope so we can use them here. use super::*; + + /// A helper function used for calling contract messages. use ink_e2e::build_message; + /// The End-to-End test `Result` type. type E2EResult = std::result::Result>; + /// We test that we can upload and instantiate the contract using its default constructor. #[ink_e2e::test] - async fn it_works(mut client: ink_e2e::Client) -> E2EResult<()> { - // given - let constructor = marketplaceRef::new(false); - let contract_acc_id = client - .instantiate("marketplace", &ink_e2e::alice(), constructor, 0, None) + async fn default_works(mut client: ink_e2e::Client) -> E2EResult<()> { + // Given + let constructor = LotteryRef::default(); + + // When + let contract_account_id = client + .instantiate("lottery", &ink_e2e::alice(), constructor, 0, None) .await .expect("instantiate failed") .account_id; - let get = build_message::(contract_acc_id.clone()) - .call(|marketplace| marketplace.get()); - let get_res = client.call_dry_run(&ink_e2e::bob(), &get, 0, None).await; - assert!(matches!(get_res.return_value(), false)); - - // when - let flip = build_message::(contract_acc_id.clone()) - .call(|marketplace| marketplace.flip()); - let _flip_res = client - .call(&ink_e2e::bob(), flip, 0, None) - .await - .expect("flip failed"); - - // then - let get = build_message::(contract_acc_id.clone()) - .call(|marketplace| marketplace.get()); - let get_res = client.call_dry_run(&ink_e2e::bob(), &get, 0, None).await; - assert!(matches!(get_res.return_value(), true)); + // Then + let owner = build_message::(contract_account_id.clone()) + .call(|lottery| lottery.owner()); + let owner_result = client.call_dry_run(&ink_e2e::alice(), &owner, 0, None).await; + assert!(matches!(owner_result.return_value(), &ink_e2e::alice())); Ok(()) } + /// We test that we can run lottery. #[ink_e2e::test] - async fn default_works(mut client: ink_e2e::Client) -> E2EResult<()> { - // given - let constructor = marketplaceRef::new_default(); - - // when - let contract_acc_id = client - .instantiate("marketplace", &ink_e2e::bob(), constructor, 0, None) + async fn it_works(mut client: ink_e2e::Client) -> E2EResult<()> { + // Given + let constructor = LotteryRef::new(false); + let contract_account_id = client + .instantiate("lottery", &ink_e2e::bob(), constructor, 0, None) .await .expect("instantiate failed") .account_id; - // then - let get = build_message::(contract_acc_id.clone()) - .call(|marketplace| marketplace.get()); - let get_res = client.call_dry_run(&ink_e2e::bob(), &get, 0, None).await; - assert!(matches!(get_res.return_value(), false)); + let owner = build_message::(contract_account_id.clone()) + .call(|lottery| lottery.owner()); + let owner_result = client.call_dry_run(&ink_e2e::bob(), &owner, 0, None).await; + assert!(matches!(get_result.return_value(), &ink_e2e::bob())); + + // When + let start_lottery = build_message::(contract_account_id.clone()) + .call(|lottery| lottery.start_lottery()); + let _start_lottery_result = client + .call(&ink_e2e::bob(), start_lottery, 0, None) + .await + .expect("start_lottery failed"); + + // Then + let running = build_message::(contract_account_id.clone()) + .call(|lottery| lottery.is_running()); + let running_result = client.call_dry_run(&ink_e2e::bob(), &running, 0, None).await; + assert!(matches!(running_result.return_value(), true)); + + // When + let stop_lottery = build_message::(contract_account_id.clone()) + .call(|lottery| lottery.stop_lottery()); + let _stop_lottery_result = client + .call(&ink_e2e::bob(), stop_lottery, 0, None) + .await + .expect("start_lottery failed"); + + // Then + let running_after_stop = build_message::(contract_account_id.clone()) + .call(|lottery| lottery.is_running()); + let running_after_stop_result = client.call_dry_run(&ink_e2e::bob(), &running_after_stop, 0, None).await; + assert!(matches!(running_result.return_value(), false)); + + + // When + let enter = build_message::(contract_account_id.clone()) + .call(|lottery| lottery.enter()); + let _enter_result = client + .call(&ink_e2e::bob(), enter, 1, None) + .await + .expect("start_lottery failed"); + + // Then + let pot = build_message::(contract_account_id.clone()) + .call(|lottery| lottery.pot()); + let pot_result = client.call_dry_run(&ink_e2e::bob(), &pot, 0, None).await; + assert!(matches!(pot_result.return_value(), 1)); Ok(()) } From 4311536bedfcd70ee4a3025de251ecc0750dd037 Mon Sep 17 00:00:00 2001 From: Nikhil Ranjan Date: Mon, 15 May 2023 10:18:46 +0200 Subject: [PATCH 3/3] fix --- contracts/marketplace/Cargo.toml | 1 + contracts/marketplace/lib.rs | 78 ++++---------------------------- contracts/psp34/Cargo.toml | 2 +- contracts/rmrk/Cargo.toml | 2 +- 4 files changed, 13 insertions(+), 70 deletions(-) diff --git a/contracts/marketplace/Cargo.toml b/contracts/marketplace/Cargo.toml index 103a85e..3f81404 100644 --- a/contracts/marketplace/Cargo.toml +++ b/contracts/marketplace/Cargo.toml @@ -27,6 +27,7 @@ std = [ "pallet_marketplace/std", ] ink-as-dependency = [] +e2e-tests = [] [profile.dev] overflow-checks = false diff --git a/contracts/marketplace/lib.rs b/contracts/marketplace/lib.rs index 605a62f..46c6f02 100644 --- a/contracts/marketplace/lib.rs +++ b/contracts/marketplace/lib.rs @@ -332,7 +332,7 @@ pub mod marketplace { } } - /// end-to-end (E2E) or integration tests for lottery. + /// end-to-end (E2E) or integration tests for marketplace. /// /// When running these you need to make sure that you: /// - Compile the tests with the `e2e-tests` feature flag enabled (`--features e2e-tests`) @@ -350,86 +350,28 @@ pub mod marketplace { /// We test that we can upload and instantiate the contract using its default constructor. #[ink_e2e::test] - async fn default_works(mut client: ink_e2e::Client) -> E2EResult<()> { + async fn init_works(mut client: ink_e2e::Client) -> E2EResult<()> { // Given - let constructor = LotteryRef::default(); + let constructor = MarketplaceRef::new(fee_recipient()); // When let contract_account_id = client - .instantiate("lottery", &ink_e2e::alice(), constructor, 0, None) + .instantiate("marketplace", &ink_e2e::alice(), constructor, 0, None) .await .expect("instantiate failed") .account_id; // Then - let owner = build_message::(contract_account_id.clone()) - .call(|lottery| lottery.owner()); - let owner_result = client.call_dry_run(&ink_e2e::alice(), &owner, 0, None).await; - assert!(matches!(owner_result.return_value(), &ink_e2e::alice())); + let marketplace_fee = build_message::(contract_account_id.clone()) + .call(|marketplace| marketplace.get_marketplace_fee()); + let marketplace_fee_result = client.call_dry_run(&ink_e2e::alice(), &owner, 0, None).await; + assert!(matches!(marketplace_fee_result.return_value(), 100)); Ok(()) } - /// We test that we can run lottery. - #[ink_e2e::test] - async fn it_works(mut client: ink_e2e::Client) -> E2EResult<()> { - // Given - let constructor = LotteryRef::new(false); - let contract_account_id = client - .instantiate("lottery", &ink_e2e::bob(), constructor, 0, None) - .await - .expect("instantiate failed") - .account_id; - - let owner = build_message::(contract_account_id.clone()) - .call(|lottery| lottery.owner()); - let owner_result = client.call_dry_run(&ink_e2e::bob(), &owner, 0, None).await; - assert!(matches!(get_result.return_value(), &ink_e2e::bob())); - - // When - let start_lottery = build_message::(contract_account_id.clone()) - .call(|lottery| lottery.start_lottery()); - let _start_lottery_result = client - .call(&ink_e2e::bob(), start_lottery, 0, None) - .await - .expect("start_lottery failed"); - - // Then - let running = build_message::(contract_account_id.clone()) - .call(|lottery| lottery.is_running()); - let running_result = client.call_dry_run(&ink_e2e::bob(), &running, 0, None).await; - assert!(matches!(running_result.return_value(), true)); - - // When - let stop_lottery = build_message::(contract_account_id.clone()) - .call(|lottery| lottery.stop_lottery()); - let _stop_lottery_result = client - .call(&ink_e2e::bob(), stop_lottery, 0, None) - .await - .expect("start_lottery failed"); - - // Then - let running_after_stop = build_message::(contract_account_id.clone()) - .call(|lottery| lottery.is_running()); - let running_after_stop_result = client.call_dry_run(&ink_e2e::bob(), &running_after_stop, 0, None).await; - assert!(matches!(running_result.return_value(), false)); - - - // When - let enter = build_message::(contract_account_id.clone()) - .call(|lottery| lottery.enter()); - let _enter_result = client - .call(&ink_e2e::bob(), enter, 1, None) - .await - .expect("start_lottery failed"); - - // Then - let pot = build_message::(contract_account_id.clone()) - .call(|lottery| lottery.pot()); - let pot_result = client.call_dry_run(&ink_e2e::bob(), &pot, 0, None).await; - assert!(matches!(pot_result.return_value(), 1)); - - Ok(()) + fn fee_recipient() -> AccountId { + AccountId::from([0x1; 32]) } } } diff --git a/contracts/psp34/Cargo.toml b/contracts/psp34/Cargo.toml index abdfd3d..f1ab7d0 100644 --- a/contracts/psp34/Cargo.toml +++ b/contracts/psp34/Cargo.toml @@ -23,4 +23,4 @@ std = [ "openbrush/std", ] -ink-as-dependency = [] \ No newline at end of file +ink-as-dependency = [] diff --git a/contracts/rmrk/Cargo.toml b/contracts/rmrk/Cargo.toml index 259be1b..41304e0 100644 --- a/contracts/rmrk/Cargo.toml +++ b/contracts/rmrk/Cargo.toml @@ -29,4 +29,4 @@ std = [ "rmrk/std", ] -ink-as-dependency = [] \ No newline at end of file +ink-as-dependency = []