From d40c695da8ca785b4a89bb683dee03dd8f3ff855 Mon Sep 17 00:00:00 2001 From: Mariusz Klochowicz Date: Wed, 10 Nov 2021 16:50:59 +1030 Subject: [PATCH] Move helpers from happy_path tests to the test harness module These helpers will be useful in all the tests --- daemon/tests/happy_path.rs | 59 ++----------------------------------- daemon/tests/harness/mod.rs | 54 +++++++++++++++++++++++++++++++-- 2 files changed, 55 insertions(+), 58 deletions(-) diff --git a/daemon/tests/happy_path.rs b/daemon/tests/happy_path.rs index 5f34413..1009019 100644 --- a/daemon/tests/happy_path.rs +++ b/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::mocks::{ - mock_oracle_annoucement, mock_party_params, mock_wallet_sign_and_broadcast, -}; -use crate::harness::start_both; -use daemon::maker_cfd; -use daemon::model::cfd::{CfdState, Order, Origin}; -use daemon::model::{Price, Usd}; +use crate::harness::{assert_is_same_order, dummy_new_order, init_tracing, start_both}; +use daemon::model::cfd::CfdState; +use daemon::model::Usd; use maia::secp256k1_zkp::schnorrsig; 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; #[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!(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 -} diff --git a/daemon/tests/harness/mod.rs b/daemon/tests/harness/mod.rs index 0f2c095..62fa043 100644 --- a/daemon/tests/harness/mod.rs +++ b/daemon/tests/harness/mod.rs @@ -3,15 +3,20 @@ use crate::harness::mocks::oracle::OracleActor; use crate::harness::mocks::wallet::WalletActor; use crate::schnorrsig; use daemon::maker_cfd::CfdAction; -use daemon::model::cfd::{Cfd, Order}; -use daemon::model::Usd; +use daemon::model::cfd::{Cfd, Order, Origin}; +use daemon::model::{Price, Usd}; use daemon::seed::Seed; use daemon::{connection, db, maker_cfd, maker_inc_connections, taker_cfd}; +use rust_decimal_macros::dec; use sqlx::SqlitePool; use std::net::SocketAddr; use std::str::FromStr; use std::task::Poll; 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::Actor; @@ -208,3 +213,48 @@ async fn in_memory_db() -> SqlitePool { 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 +}