Skip to content

Commit

Permalink
feat: working on app tests
Browse files Browse the repository at this point in the history
  • Loading branch information
veeso committed Nov 10, 2023
1 parent 0257626 commit 0208459
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 40 deletions.
110 changes: 107 additions & 3 deletions src/dilazionato/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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());
Expand All @@ -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(),
});
}
}
10 changes: 6 additions & 4 deletions src/dilazionato/src/app/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ thread_local! {
);

/// Contract last upgrade timestamp
static UPGRADED_AT: RefCell<StableCell<u64, VirtualMemory<DefaultMemoryImpl>>> =
RefCell::new(StableCell::new(MEMORY_MANAGER.with(|mm| mm.get(UPGRADED_AT_MEMORY_ID)), crate::utils::time()).unwrap()
static UPGRADED_AT: RefCell<StableCell<Option<u64>, VirtualMemory<DefaultMemoryImpl>>> =
RefCell::new(StableCell::new(MEMORY_MANAGER.with(|mm| mm.get(UPGRADED_AT_MEMORY_ID)), None).unwrap()
);

}
Expand Down Expand Up @@ -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(())
Expand Down
2 changes: 1 addition & 1 deletion src/dilazionato/src/app/storage/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;

Expand Down
2 changes: 1 addition & 1 deletion src/dilazionato/src/app/storage/tx_history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;

Expand Down
62 changes: 62 additions & 0 deletions src/dilazionato/src/app/test_utils.rs
Original file line number Diff line number Diff line change
@@ -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()
);
}
4 changes: 2 additions & 2 deletions src/dilazionato/src/constants.rs
Original file line number Diff line number Diff line change
@@ -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";
2 changes: 0 additions & 2 deletions src/dilazionato/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ mod app;
mod client;
mod constants;
mod inspect;
#[cfg(test)]
mod test;
mod utils;

use app::Dilazionato;
Expand Down
26 changes: 0 additions & 26 deletions src/dilazionato/src/test.rs

This file was deleted.

13 changes: 12 additions & 1 deletion src/dilazionato/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use candid::Principal;
use candid::{Nat, Principal};

/// Returns current time in nanoseconds
pub fn time() -> u64 {
Expand All @@ -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"))]
{
Expand Down

0 comments on commit 0208459

Please sign in to comment.