8000 Unit testing by wahlbergkyle · Pull Request #191 · securesecrets/shade · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Unit testing #191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8000
71 changes: 71 additions & 0 deletions contracts/mint/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ pub mod tests {
}
}
*/

#[test]
fn capture_calc() {
let amount = Uint128(1_000_000_000_000_000_000);
Expand All @@ -368,6 +369,9 @@ pub mod tests {
let value = calculate_portion(amount, capture);
assert_eq!(value, expected);
}


/**
#[test]
fn mint_algorithm_simple() {
// In this example the "sent" value is 1 with 6 decimal places
Expand Down Expand Up @@ -405,4 +409,71 @@ pub mod tests {

assert_eq!(value, expected_value);
}
**/
macro_rules! mint_algorithm_tests {
($($name:ident: $value:expr,)*) => {
$(
#[test]
fn $name() {
let (in_price, in_amount, in_decimals, target_price, target_decimals, expected_value) = $value;
assert_eq!(calculate_mint(in_price, in_amount, in_decimals, target_price, target_decimals), expected_value);
}
)*
}
}

mint_algorithm_tests!{
mint_simple_0: (
// In this example the "sent" value is 1 with 6 decimal places
// The mint value will be 1 with 3 decimal places
Uint128(1_000_000_000_000_000_000), //Burn price
Uint128(1_000_000), //Burn amount
6u8, //Burn decimals
Uint128(1_000_000_000_000_000_000), //Mint price
3u8, //Mint decimals
Uint128(1_000), //Expected value
),
mint_complex_0: (
// In this example the "sent" value is 1.8 with 6 decimal places
// The mint value will be 3.6 with 12 decimal places
Uint128(2_000_000_000_000_000_000),
Uint128(1_800_000),
6u8,
Uint128(1_000_000_000_000_000_000),
12u8,
Uint128(3_600_000_000_000),
),
mint_complex_1: (
// In amount is 50.000 valued at 20
// target price is 100$ with 6 decimals
Uint128(20_000_000_000_000_000_000),
Uint128(50_000),
3u8,
Uint128(100_000_000_000_000_000_000),
6u8,
Uint128(10_000_000),
),
mint_complex_2: (
// In amount is 10,000,000 valued at 100
// Target price is $10 with 6 decimals
Uint128(1_000_000_000_000_000_000_000),
Uint128(100_000_000_000_000),
8u8,
Uint128(10_000_000_000_000_000_000),
6u8,
Uint128(100_000_000_000_000),
),
}
/*
mint_overflow_0: (
// In amount is 1,000,000,000,000,000,000,000,000 valued at 1,000
// Target price is $5 with 6 decimals
Uint128(1_000_000_000_000_000_000_000),
Uint128(100_000_000_000_000_000_000_000_000_000_000),
8u8,
Uint128(5_000_000_000_000_000_000),
6u8,
Uint128(500_000_000_000_000_000_000_000_000_000_000_000),
),
*/
}
49 changes: 45 additions & 4 deletions contracts/oracle/src/test.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#[cfg(test)]
mod tests {
use crate::contract;
use cosmwasm_std::{coins, from_binary, testing::{mock_dependencies, mock_env, MockApi, MockQuerier, MockStorage}, Extern, StdError, Uint128, HumanAddr};
use shade_protocol::{utils::asset::Contract, oracle::{self, OracleConfig, QueryAnswer}};
use crate::query;

use cosmwasm_std::Uint128;

macro_rules! normalize_price_tests {
($($name:ident: $value:expr,)*) => {
Expand Down Expand Up @@ -33,10 +35,10 @@ mod tests {
normalize_2: (
// amount of TKN received for 1 sSCRT
Uint128(1_000_000),
// TKN 6 decimals
6u8,
// TKN 8 decimals
8u8,
// price * 10^18
Uint128(1_000_000_000_000_000_000)
Uint128(10_000_000_000_000_000)
),
}

Expand Down Expand Up @@ -86,4 +88,43 @@ mod tests {
Uint128(50_000_000_000_000_000_000),
),
}

#[test]
fn test_config(){
let mut deps = mock_dependencies(20, &coins(0, ""));

// Initialize oracle contract
let env = mock_env("creator", &coins(0, ""));
let oracle_init_msg = oracle::InitMsg{
admin: None,
band: Contract{
address: HumanAddr::from(""),
code_hash: String::from(""),
},
sscrt: Contract{
address: HumanAddr::from(""),
code_hash: String::from("")
},
};
let res = contract::init(&mut deps, env, oracle_init_msg).unwrap();
assert_eq!(0, res.messages.len());

let check_state = OracleConfig{
admin: HumanAddr::from("creator"),
band: Contract{
address: HumanAddr::from(""),
code_hash: String::from("")
},
sscrt: Contract{
address: HumanAddr::from(""),
code_hash: String::from("")
}
};
let query_answer = query::config(&mut deps).unwrap();
let query_result = match query_answer{
QueryAnswer::Config{config} => config == check_state,
_ => false,
};
assert_eq!(true, query_result);
}
}
0