Skip to content

Commit

Permalink
[Ignition]: Add a test for reciprocal price.
Browse files Browse the repository at this point in the history
  • Loading branch information
0xOmarA committed Feb 8, 2024
1 parent cf20c39 commit 79f6d37
Showing 1 changed file with 42 additions and 12 deletions.
54 changes: 42 additions & 12 deletions libraries/adapters-interface/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,18 +118,27 @@ impl Price {
mod test {
use super::*;

const BITCOIN: ResourceAddress = ResourceAddress::new_or_panic([
93, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1,
]);
const USD: ResourceAddress = ResourceAddress::new_or_panic([
93, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 2,
]);

#[test]
fn percentage_difference_with_opposite_base_and_quote_is_calculated_correctly(
) {
// Arrange
let p1 = Price {
base: ACCOUNT_OWNER_BADGE,
quote: VALIDATOR_OWNER_BADGE,
base: BITCOIN,
quote: USD,
price: dec!(100),
};
let p2 = Price {
base: VALIDATOR_OWNER_BADGE,
quote: ACCOUNT_OWNER_BADGE,
base: USD,
quote: BITCOIN,
price: dec!(1) / dec!(100),
};

Expand All @@ -144,13 +153,13 @@ mod test {
fn simple_percentage_difference_is_calculated_correctly() {
// Arrange
let p1 = Price {
base: ACCOUNT_OWNER_BADGE,
quote: VALIDATOR_OWNER_BADGE,
base: BITCOIN,
quote: USD,
price: dec!(100),
};
let p2 = Price {
base: ACCOUNT_OWNER_BADGE,
quote: VALIDATOR_OWNER_BADGE,
base: BITCOIN,
quote: USD,
price: dec!(50),
};

Expand All @@ -164,8 +173,8 @@ mod test {
#[test]
fn exchange_method_calculates_as_expected1() {
// Arrange
let btc = ACCOUNT_OWNER_BADGE;
let usd = VALIDATOR_OWNER_BADGE;
let btc = BITCOIN;
let usd = USD;

let price = Price {
base: btc,
Expand All @@ -184,8 +193,8 @@ mod test {
#[test]
fn exchange_method_calculates_as_expected2() {
// Arrange
let btc = ACCOUNT_OWNER_BADGE;
let usd = VALIDATOR_OWNER_BADGE;
let btc = BITCOIN;
let usd = USD;

let price = Price {
base: btc,
Expand All @@ -201,4 +210,25 @@ mod test {
assert_eq!(out_address, btc);
assert_eq!(out_amount, dec!(1));
}

#[test]
fn price_reciprocal_is_what_we_expect_it_to_be() {
// Arrange
let btc = BITCOIN;
let usd = USD;

let price = Price {
base: btc,
quote: usd,
price: dec!(2),
};

// Act
let reciprocal = price.reciprocal();

// Assert
assert_eq!(reciprocal.base, price.quote);
assert_eq!(reciprocal.quote, price.base);
assert_eq!(reciprocal.price, dec!(1) / price.price);
}
}

0 comments on commit 79f6d37

Please sign in to comment.