Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
larry0x committed Oct 19, 2022
1 parent 259c844 commit 90ff373
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 22 deletions.
12 changes: 10 additions & 2 deletions contracts/hub/tests/creating.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use sg_std::Response;
use badge_hub::error::ContractError;
use badge_hub::state::*;
use badge_hub::{execute, query};
use badges::{Badge, MintRule};
use badges::{Badge, MintRule, FeeRate};

mod utils;

Expand All @@ -19,7 +19,15 @@ fn setup_test() -> OwnedDeps<MockStorage, MockApi, MockQuerier, Empty> {

// here we test the creation of badges without fees
// fee-related logics are tested in a separate file
FEE_PER_BYTE.save(deps.as_mut().storage, &Decimal::zero()).unwrap();
FEE_RATE
.save(
deps.as_mut().storage,
&FeeRate {
metadata: Decimal::zero(),
key: Decimal::zero(),
},
)
.unwrap();

deps
}
Expand Down
20 changes: 7 additions & 13 deletions contracts/hub/tests/deployment.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use cosmwasm_std::testing::{mock_dependencies};
use cosmwasm_std::{attr, Addr, Decimal, SubMsg, WasmMsg, to_binary};

use badges::FeeRate;

use badge_hub::error::ContractError;
use badge_hub::state::{BADGE_COUNT, NFT, DEVELOPER};
use badge_hub::execute;
Expand All @@ -12,25 +14,17 @@ fn instantiating() {
let res = execute::init(
deps.as_mut(),
Addr::unchecked("larry"),
Decimal::from_ratio(10u128, 1u128),
FeeRate {
metadata: Decimal::from_ratio(10u128, 1u128),
key: Decimal::from_ratio(2u128, 1u128),
},
)
.unwrap();
assert_eq!(res.messages, vec![]);
assert_eq!(
res.attributes,
vec![
attr("action", "badges/hub/init"),
attr("contract_name", "crates.io:badge-hub"),
attr("contract_version", env!("CARGO_PKG_VERSION"))
]
);
assert_eq!(res.attributes, vec![attr("action", "badges/hub/init")]);

let badge_count = BADGE_COUNT.load(deps.as_ref().storage).unwrap();
assert_eq!(badge_count, 0);

let version = cw2::get_contract_version(deps.as_ref().storage).unwrap();
assert_eq!(version.contract, "crates.io:badge-hub");
assert_eq!(version.version, env!("CARGO_PKG_VERSION"));
}

#[test]
Expand Down
17 changes: 10 additions & 7 deletions contracts/hub/tests/fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@ use sg_std::{create_fund_fairburn_pool_msg, Response, NATIVE_DENOM};
use badge_hub::error::ContractError;
use badge_hub::{execute, query};
use badge_hub::state::*;
use badges::{Badge, MintRule};
use badges::{Badge, MintRule, FeeRate};

mod utils;

// 10 ustars per bytes
fn mock_fee_per_byte() -> Decimal {
Decimal::from_ratio(10u128, 1u128)
fn mock_fee_rate() -> FeeRate {
FeeRate {
metadata: Decimal::from_ratio(10u128, 1u128),
key: Decimal::from_ratio(2u128, 1u128),
}
}

fn setup_test() -> OwnedDeps<MockStorage, MockApi, MockQuerier, Empty> {
Expand All @@ -28,7 +31,7 @@ fn setup_test() -> OwnedDeps<MockStorage, MockApi, MockQuerier, Empty> {
DEVELOPER.save(deps.as_mut().storage, &Addr::unchecked("larry")).unwrap();
NFT.save(deps.as_mut().storage, &Addr::unchecked("nft")).unwrap();
BADGE_COUNT.save(deps.as_mut().storage, &0).unwrap();
FEE_PER_BYTE.save(deps.as_mut().storage, &mock_fee_per_byte()).unwrap();
FEE_RATE.save(deps.as_mut().storage, &mock_fee_rate()).unwrap();

deps
}
Expand Down Expand Up @@ -85,7 +88,7 @@ fn badge_creation_fee() {
};

let bytes = to_binary(&mock_badge).unwrap();
let fee_amount = (Uint128::from(bytes.len() as u128) * mock_fee_per_byte()).u128();
let fee_amount = (Uint128::from(bytes.len() as u128) * mock_fee_rate().metadata).u128();

// try create without sending a fee, should fail
{
Expand Down Expand Up @@ -194,7 +197,7 @@ fn badge_editing_fee() {
// calculate the expected fee amount
let old_bytes = to_binary(&old_metadata).unwrap().len() as u128;
let new_bytes = to_binary(&new_metadata).unwrap().len() as u128;
let fee_amount = (Uint128::new(new_bytes - old_bytes) * mock_fee_per_byte()).u128();
let fee_amount = (Uint128::new(new_bytes - old_bytes) * mock_fee_rate().metadata).u128();

// not sending sufficient fee, should fail
{
Expand Down Expand Up @@ -241,7 +244,7 @@ fn key_adding_fee() {
.collect::<BTreeSet<_>>();

let bytes = to_binary(&mock_keys).unwrap().len() as u128;
let fee_amount = (Uint128::new(bytes) * mock_fee_per_byte()).u128();
let fee_amount = (Uint128::new(bytes) * mock_fee_rate().key).u128();

fn add(
deps: DepsMut,
Expand Down

0 comments on commit 90ff373

Please sign in to comment.