From 02084593dec92a5f059ddf3b044e79359051e8e7 Mon Sep 17 00:00:00 2001 From: veeso Date: Fri, 10 Nov 2023 18:44:27 +0100 Subject: [PATCH] feat: working on app tests --- src/dilazionato/src/app.rs | 110 +++++++++++++++++- src/dilazionato/src/app/configuration.rs | 10 +- src/dilazionato/src/app/storage/contracts.rs | 2 +- src/dilazionato/src/app/storage/tx_history.rs | 2 +- src/dilazionato/src/app/test_utils.rs | 62 ++++++++++ src/dilazionato/src/constants.rs | 4 +- src/dilazionato/src/lib.rs | 2 - src/dilazionato/src/test.rs | 26 ----- src/dilazionato/src/utils.rs | 13 ++- 9 files changed, 191 insertions(+), 40 deletions(-) create mode 100644 src/dilazionato/src/app/test_utils.rs delete mode 100644 src/dilazionato/src/test.rs diff --git a/src/dilazionato/src/app.rs b/src/dilazionato/src/app.rs index cf30d0f..81f4f32 100644 --- a/src/dilazionato/src/app.rs +++ b/src/dilazionato/src/app.rs @@ -6,6 +6,8 @@ mod configuration; mod memory; mod minter; mod storage; +#[cfg(test)] +mod test_utils; use async_trait::async_trait; use candid::{Nat, Principal}; @@ -347,7 +349,7 @@ impl Dip721 for Dilazionato { /// Returns canister cycles fn cycles() -> Nat { - ic_cdk::api::canister_balance().into() + crate::utils::cycles() } /// Returns total unique holders of tokens @@ -557,12 +559,106 @@ impl Dip721 for Dilazionato { #[cfg(test)] mod test { - use crate::test::mock_token; + use std::time::Duration; + + use crate::{ + app::test_utils::mock_token, + constants::{DEFAULT_LOGO, DEFAULT_NAME, DEFAULT_SYMBOL}, + }; - use super::*; + use super::{test_utils::store_mock_contract, *}; use pretty_assertions::assert_eq; + #[test] + fn test_should_init_canister() { + Dilazionato::init(DilazionatoInitData { + custodians: vec![caller()], + fly_canister: caller(), + marketplace_canister: caller(), + }); + + assert_eq!(Dilazionato::custodians(), vec![caller()]); + assert_eq!(Configuration::get_fly_canister(), caller()); + assert_eq!(Configuration::get_marketplace_canister(), caller()); + } + + #[test] + fn test_should_set_upgrade_time_on_post_upgrade() { + init_canister(); + let metadata = Dilazionato::metadata(); + assert!(metadata.upgraded_at == metadata.created_at); + std::thread::sleep(Duration::from_millis(100)); + Dilazionato::post_upgrade(); + let metadata = Dilazionato::metadata(); + assert!(metadata.upgraded_at > metadata.created_at); + } + + #[test] + fn test_should_get_metadata() { + init_canister(); + let metadata = Dilazionato::metadata(); + assert_eq!(metadata.custodians, vec![caller()]); + assert_eq!(metadata.logo.as_deref(), Some(DEFAULT_LOGO)); + assert_eq!(metadata.name.as_deref(), Some(DEFAULT_NAME)); + assert_eq!(metadata.symbol.as_deref(), Some(DEFAULT_SYMBOL)); + } + + #[test] + fn test_should_get_stats() { + init_canister(); + let stats = Dilazionato::stats(); + assert_eq!(stats.cycles, crate::utils::cycles()); + assert_eq!(stats.total_supply, 0); + assert_eq!(stats.total_transactions, 0); + assert_eq!(stats.total_unique_holders, 0); + } + + #[test] + fn test_should_set_logo() { + init_canister(); + let logo = "logo"; + Dilazionato::set_logo(logo.to_string()); + assert_eq!(Dilazionato::logo().as_deref(), Some(logo)); + } + + #[test] + fn test_should_set_name() { + init_canister(); + let name = "name"; + Dilazionato::set_name(name.to_string()); + assert_eq!(Dilazionato::name().as_deref(), Some(name)); + } + + #[test] + fn test_should_set_symbol() { + init_canister(); + let symbol = "symbol"; + Dilazionato::set_symbol(symbol.to_string()); + assert_eq!(Dilazionato::symbol().as_deref(), Some(symbol)); + } + + #[test] + fn test_should_set_custodians() { + init_canister(); + let custodians = vec![caller(), Principal::management_canister()]; + Dilazionato::set_custodians(custodians.clone()); + assert_eq!(Dilazionato::custodians(), custodians); + } + + #[test] + fn test_should_get_cycles() { + init_canister(); + assert_eq!(Dilazionato::cycles(), crate::utils::cycles()); + } + + #[test] + fn test_should_get_unique_holders() { + init_canister(); + store_mock_contract(&[1, 2], 1); + assert_eq!(Dilazionato::total_unique_holders(), Nat::from(1)); + } + #[test] fn test_should_get_tx() { assert!(Dilazionato::transaction(Nat::from(1)).is_err()); @@ -576,4 +672,12 @@ mod test { let _ = TxHistory::register_token_mint(&mock_token(1, 1)); assert_eq!(Dilazionato::total_transactions(), Nat::from(1)); } + + fn init_canister() { + Dilazionato::init(DilazionatoInitData { + custodians: vec![caller()], + fly_canister: caller(), + marketplace_canister: caller(), + }); + } } diff --git a/src/dilazionato/src/app/configuration.rs b/src/dilazionato/src/app/configuration.rs index ba84f00..5239986 100644 --- a/src/dilazionato/src/app/configuration.rs +++ b/src/dilazionato/src/app/configuration.rs @@ -50,8 +50,8 @@ thread_local! { ); /// Contract last upgrade timestamp - static UPGRADED_AT: RefCell>> = - RefCell::new(StableCell::new(MEMORY_MANAGER.with(|mm| mm.get(UPGRADED_AT_MEMORY_ID)), crate::utils::time()).unwrap() + static UPGRADED_AT: RefCell, VirtualMemory>> = + RefCell::new(StableCell::new(MEMORY_MANAGER.with(|mm| mm.get(UPGRADED_AT_MEMORY_ID)), None).unwrap() ); } @@ -145,12 +145,14 @@ impl Configuration { } pub fn get_upgraded_at() -> u64 { - UPGRADED_AT.with_borrow(|cell| *cell.get()) + UPGRADED_AT + .with_borrow(|cell| *cell.get()) + .unwrap_or(Self::get_created_at()) } pub fn set_upgraded_at() -> DilazionatoResult<()> { UPGRADED_AT - .with_borrow_mut(|cell| cell.set(crate::utils::time())) + .with_borrow_mut(|cell| cell.set(Some(crate::utils::time()))) .map_err(|_| DilazionatoError::StorageError)?; Ok(()) diff --git a/src/dilazionato/src/app/storage/contracts.rs b/src/dilazionato/src/app/storage/contracts.rs index 14728b0..3363cc6 100644 --- a/src/dilazionato/src/app/storage/contracts.rs +++ b/src/dilazionato/src/app/storage/contracts.rs @@ -261,7 +261,7 @@ mod test { use did::dilazionato::BuildingData; use pretty_assertions::assert_eq; - use crate::test::mock_token; + use crate::app::test_utils::mock_token; use super::*; diff --git a/src/dilazionato/src/app/storage/tx_history.rs b/src/dilazionato/src/app/storage/tx_history.rs index c52ca27..ffa6cb9 100644 --- a/src/dilazionato/src/app/storage/tx_history.rs +++ b/src/dilazionato/src/app/storage/tx_history.rs @@ -125,7 +125,7 @@ mod test { use pretty_assertions::assert_eq; - use crate::test::mock_token; + use crate::app::test_utils::mock_token; use super::*; diff --git a/src/dilazionato/src/app/test_utils.rs b/src/dilazionato/src/app/test_utils.rs new file mode 100644 index 0000000..8bf22ca --- /dev/null +++ b/src/dilazionato/src/app/test_utils.rs @@ -0,0 +1,62 @@ +use candid::Principal; +use did::{ + dilazionato::{BuildingData, Contract, Token}, + ID, +}; +use dip721::TokenIdentifier; + +use crate::utils::caller; + +use super::storage::ContractStorage; + +pub fn mock_token(id: u64, contract_id: u64) -> Token { + Token { + id: TokenIdentifier::from(id), + contract_id: ID::from(contract_id), + owner: Some( + Principal::from_text("zrrb4-gyxmq-nx67d-wmbky-k6xyt-byhmw-tr5ct-vsxu4-nuv2g-6rr65-aae") + .unwrap(), + ), + transferred_at: None, + transferred_by: None, + approved_at: None, + approved_by: None, + mfly_reward: 4000, + burned_at: None, + burned_by: None, + minted_at: 0, + value: 100, + operator: None, + is_burned: false, + minted_by: Principal::anonymous(), + } +} + +fn mock_contract(id: u64, token_ids: &[u64]) -> Contract { + Contract { + id: id.into(), + seller: caller(), + buyers: vec![Principal::management_canister()], + tokens: token_ids + .iter() + .map(|id| TokenIdentifier::from(*id)) + .collect(), + expiration: "2040-06-01".to_string(), + initial_value: 250_000, + value: 250_000, + currency: "EUR".to_string(), + building: BuildingData { + city: "Rome".to_string(), + }, + } +} + +pub fn store_mock_contract(token_ids: &[u64], contract_id: u64) { + let mut tokens = Vec::new(); + for id in token_ids { + tokens.push(mock_token(*id, contract_id)); + } + assert!( + ContractStorage::insert_contract(mock_contract(contract_id, token_ids), tokens).is_ok() + ); +} diff --git a/src/dilazionato/src/constants.rs b/src/dilazionato/src/constants.rs index 1660c7a..b72e2dd 100644 --- a/src/dilazionato/src/constants.rs +++ b/src/dilazionato/src/constants.rs @@ -1,3 +1,3 @@ pub const DEFAULT_LOGO: &str = ""; -pub const DEFAULT_NAME: &str = "Sell Contract"; -pub const DEFAULT_SYMBOL: &str = "MHSC"; +pub const DEFAULT_NAME: &str = "Dilazionato"; +pub const DEFAULT_SYMBOL: &str = "DSC"; diff --git a/src/dilazionato/src/lib.rs b/src/dilazionato/src/lib.rs index e51ce0a..fb1ced0 100644 --- a/src/dilazionato/src/lib.rs +++ b/src/dilazionato/src/lib.rs @@ -8,8 +8,6 @@ mod app; mod client; mod constants; mod inspect; -#[cfg(test)] -mod test; mod utils; use app::Dilazionato; diff --git a/src/dilazionato/src/test.rs b/src/dilazionato/src/test.rs deleted file mode 100644 index 0e19339..0000000 --- a/src/dilazionato/src/test.rs +++ /dev/null @@ -1,26 +0,0 @@ -use candid::Principal; -use did::{dilazionato::Token, ID}; -use dip721::TokenIdentifier; - -pub fn mock_token(id: u64, contract_id: u64) -> Token { - Token { - id: TokenIdentifier::from(id), - contract_id: ID::from(contract_id), - owner: Some( - Principal::from_text("zrrb4-gyxmq-nx67d-wmbky-k6xyt-byhmw-tr5ct-vsxu4-nuv2g-6rr65-aae") - .unwrap(), - ), - transferred_at: None, - transferred_by: None, - approved_at: None, - approved_by: None, - mfly_reward: 4000, - burned_at: None, - burned_by: None, - minted_at: 0, - value: 100, - operator: None, - is_burned: false, - minted_by: Principal::anonymous(), - } -} diff --git a/src/dilazionato/src/utils.rs b/src/dilazionato/src/utils.rs index ec38333..3a4b587 100644 --- a/src/dilazionato/src/utils.rs +++ b/src/dilazionato/src/utils.rs @@ -1,4 +1,4 @@ -use candid::Principal; +use candid::{Nat, Principal}; /// Returns current time in nanoseconds pub fn time() -> u64 { @@ -15,6 +15,17 @@ pub fn time() -> u64 { } } +pub fn cycles() -> Nat { + #[cfg(not(target_arch = "wasm32"))] + { + Nat::from(30_000_000_000_u64) + } + #[cfg(target_arch = "wasm32")] + { + ic_cdk::api::canister_balance().into() + } +} + pub fn caller() -> Principal { #[cfg(not(target_arch = "wasm32"))] {