Browse Source

Move helpers from happy_path tests to the test harness module

These helpers will be useful in all the tests
shutdown-taker-if-no-maker-present
Mariusz Klochowicz 3 years ago
parent
commit
d40c695da8
No known key found for this signature in database GPG Key ID: 470C865699C8D4D
  1. 59
      daemon/tests/happy_path.rs
  2. 54
      daemon/tests/harness/mod.rs

59
daemon/tests/happy_path.rs

@ -1,17 +1,9 @@
use crate::harness::flow::{is_next_none, next_cfd, next_order, next_some}; use crate::harness::flow::{is_next_none, next_cfd, next_order, next_some};
use crate::harness::mocks::{ use crate::harness::{assert_is_same_order, dummy_new_order, init_tracing, start_both};
mock_oracle_annoucement, mock_party_params, mock_wallet_sign_and_broadcast, use daemon::model::cfd::CfdState;
}; use daemon::model::Usd;
use crate::harness::start_both;
use daemon::maker_cfd;
use daemon::model::cfd::{CfdState, Order, Origin};
use daemon::model::{Price, Usd};
use maia::secp256k1_zkp::schnorrsig; use maia::secp256k1_zkp::schnorrsig;
use rust_decimal_macros::dec; use rust_decimal_macros::dec;
use tracing::subscriber::DefaultGuard;
use tracing_subscriber::filter::LevelFilter;
use tracing_subscriber::util::SubscriberInitExt;
use tracing_subscriber::EnvFilter;
mod harness; mod harness;
#[tokio::test] #[tokio::test]
@ -106,48 +98,3 @@ async fn taker_takes_order_and_maker_accepts_and_contract_setup() {
assert!(matches!(taker_cfd.state, CfdState::PendingOpen { .. })); assert!(matches!(taker_cfd.state, CfdState::PendingOpen { .. }));
assert!(matches!(maker_cfd.state, CfdState::PendingOpen { .. })); assert!(matches!(maker_cfd.state, CfdState::PendingOpen { .. }));
} }
/// The order cannot be directly compared in tests as the origin is different,
/// therefore wrap the assertion macro in a code that unifies the 'Origin'
fn assert_is_same_order(a: &Order, b: &Order) {
// Assume the same origin
let mut a = a.clone();
let mut b = b.clone();
a.origin = Origin::Ours;
b.origin = Origin::Ours;
assert_eq!(a, b);
}
fn dummy_new_order() -> maker_cfd::NewOrder {
maker_cfd::NewOrder {
price: Price::new(dec!(50_000)).expect("unexpected failure"),
min_quantity: Usd::new(dec!(5)),
max_quantity: Usd::new(dec!(100)),
}
}
fn init_tracing() -> DefaultGuard {
let filter = EnvFilter::from_default_env()
// apply warning level globally
.add_directive(format!("{}", LevelFilter::WARN).parse().unwrap())
// log traces from test itself
.add_directive(
format!("happy_path={}", LevelFilter::DEBUG)
.parse()
.unwrap(),
)
.add_directive(format!("taker={}", LevelFilter::DEBUG).parse().unwrap())
.add_directive(format!("maker={}", LevelFilter::DEBUG).parse().unwrap())
.add_directive(format!("daemon={}", LevelFilter::DEBUG).parse().unwrap())
.add_directive(format!("rocket={}", LevelFilter::WARN).parse().unwrap());
let guard = tracing_subscriber::fmt()
.with_env_filter(filter)
.with_test_writer()
.set_default();
tracing::info!("Running version: {}", env!("VERGEN_GIT_SEMVER_LIGHTWEIGHT"));
guard
}

54
daemon/tests/harness/mod.rs

@ -3,15 +3,20 @@ use crate::harness::mocks::oracle::OracleActor;
use crate::harness::mocks::wallet::WalletActor; use crate::harness::mocks::wallet::WalletActor;
use crate::schnorrsig; use crate::schnorrsig;
use daemon::maker_cfd::CfdAction; use daemon::maker_cfd::CfdAction;
use daemon::model::cfd::{Cfd, Order}; use daemon::model::cfd::{Cfd, Order, Origin};
use daemon::model::Usd; use daemon::model::{Price, Usd};
use daemon::seed::Seed; use daemon::seed::Seed;
use daemon::{connection, db, maker_cfd, maker_inc_connections, taker_cfd}; use daemon::{connection, db, maker_cfd, maker_inc_connections, taker_cfd};
use rust_decimal_macros::dec;
use sqlx::SqlitePool; use sqlx::SqlitePool;
use std::net::SocketAddr; use std::net::SocketAddr;
use std::str::FromStr; use std::str::FromStr;
use std::task::Poll; use std::task::Poll;
use tokio::sync::watch; use tokio::sync::watch;
use tracing::subscriber::DefaultGuard;
use tracing_subscriber::filter::LevelFilter;
use tracing_subscriber::util::SubscriberInitExt;
use tracing_subscriber::EnvFilter;
use xtra::spawn::TokioGlobalSpawnExt; use xtra::spawn::TokioGlobalSpawnExt;
use xtra::Actor; use xtra::Actor;
@ -208,3 +213,48 @@ async fn in_memory_db() -> SqlitePool {
pool pool
} }
/// The order cannot be directly compared in tests as the origin is different,
/// therefore wrap the assertion macro in a code that unifies the 'Origin'
pub fn assert_is_same_order(a: &Order, b: &Order) {
// Assume the same origin
let mut a = a.clone();
let mut b = b.clone();
a.origin = Origin::Ours;
b.origin = Origin::Ours;
assert_eq!(a, b);
}
pub fn dummy_new_order() -> maker_cfd::NewOrder {
maker_cfd::NewOrder {
price: Price::new(dec!(50_000)).expect("unexpected failure"),
min_quantity: Usd::new(dec!(5)),
max_quantity: Usd::new(dec!(100)),
}
}
pub fn init_tracing() -> DefaultGuard {
let filter = EnvFilter::from_default_env()
// apply warning level globally
.add_directive(format!("{}", LevelFilter::WARN).parse().unwrap())
// log traces from test itself
.add_directive(
format!("happy_path={}", LevelFilter::DEBUG)
.parse()
.unwrap(),
)
.add_directive(format!("taker={}", LevelFilter::DEBUG).parse().unwrap())
.add_directive(format!("maker={}", LevelFilter::DEBUG).parse().unwrap())
.add_directive(format!("daemon={}", LevelFilter::DEBUG).parse().unwrap())
.add_directive(format!("rocket={}", LevelFilter::WARN).parse().unwrap());
let guard = tracing_subscriber::fmt()
.with_env_filter(filter)
.with_test_writer()
.set_default();
tracing::info!("Running version: {}", env!("VERGEN_GIT_SEMVER_LIGHTWEIGHT"));
guard
}

Loading…
Cancel
Save