From 98b410423cad72eb1a78b3d4473ded2a082fa3e7 Mon Sep 17 00:00:00 2001 From: aaravm Date: Sat, 29 Jun 2024 17:31:13 +0530 Subject: [PATCH] added unit test for init and new request --- .../coordinator/src/communication/mod.rs | 2 +- .../src/coordinator/verify_bond.rs | 8 +-- .../coordinator/src/database/mod.rs | 64 +++++++++++++++++++ 3 files changed, 69 insertions(+), 5 deletions(-) diff --git a/taptrade-cli-demo/coordinator/src/communication/mod.rs b/taptrade-cli-demo/coordinator/src/communication/mod.rs index 0258a26..f634da5 100755 --- a/taptrade-cli-demo/coordinator/src/communication/mod.rs +++ b/taptrade-cli-demo/coordinator/src/communication/mod.rs @@ -89,7 +89,7 @@ pub async fn api_server(database: CoordinatorDB, wallet: CoordinatorWallet) -> R let app = Router::new() .route("/create-offer", post(receive_order)) .route("/submit-maker-bond", post(submit_maker_bond)) - .route("/fetch-available-offers", post(fetch_available_offers)) + // .route("/fetch-available-offers", post(fetch_available_offers)) .layer(Extension(database)) .layer(Extension(wallet)); // add other routes here diff --git a/taptrade-cli-demo/coordinator/src/coordinator/verify_bond.rs b/taptrade-cli-demo/coordinator/src/coordinator/verify_bond.rs index eaf6370..225ac9b 100644 --- a/taptrade-cli-demo/coordinator/src/coordinator/verify_bond.rs +++ b/taptrade-cli-demo/coordinator/src/coordinator/verify_bond.rs @@ -25,10 +25,10 @@ pub fn verify_and_respond( } // Create the response (you may need additional logic to generate order_id_hex and timestamp) - let response = OrderActivatedResponse { - order_id_hex: generate_order_id(&tx)?, // Assuming you have a function to generate this - bond_locked_until_timestamp: calculate_bond_lock_time()?, // Assuming you have a function for this - }; + // let response = OrderActivatedResponse { + // order_id_hex: generate_order_id(&tx)?, // Assuming you have a function to generate this + // bond_locked_until_timestamp: calculate_bond_lock_time()?, // Assuming you have a function for this + // }; Ok(response) } diff --git a/taptrade-cli-demo/coordinator/src/database/mod.rs b/taptrade-cli-demo/coordinator/src/database/mod.rs index e7cfc31..6e50681 100644 --- a/taptrade-cli-demo/coordinator/src/database/mod.rs +++ b/taptrade-cli-demo/coordinator/src/database/mod.rs @@ -2,6 +2,7 @@ use anyhow::Context; use super::*; use sqlx::{sqlite::SqlitePoolOptions, Pool, Row, Sqlite}; +use std::env; #[derive(Clone, Debug)] pub struct CoordinatorDB { @@ -201,3 +202,66 @@ impl CoordinatorDB { Ok(Some(available_offers)) } } +#[cfg(test)] +mod tests { + use anyhow::Ok; + +use super::*; + async fn create_coordinator()-> Result { + // Set up the in-memory database + env::set_var("DATABASE_PATH", ":memory:"); + + // Initialize the database + let database= CoordinatorDB::init().await?; + Ok(database) + + } + #[tokio::test] + async fn test_init() -> Result<()> { + let database = create_coordinator().await?; + // Verify the table creation + let table_exists = sqlx::query("SELECT name FROM sqlite_master WHERE type='table' AND name='maker_requests'") + .fetch_optional(&*database.db_pool) + .await? + .is_some(); + assert!(table_exists, "The maker_requests table should exist."); + Ok(()) + } + #[tokio::test] + async fn test_insert_new_maker_request() -> Result<()> { + let database = create_coordinator().await?; + + // Create a sample order request and bond requirement response + let order_request = OrderRequest { + robohash_hex: "a3f1f1f0e2f3f4f5".to_string(), + is_buy_order: true, + amount_satoshi: 1000, + bond_ratio: 50, + offer_duration_ts: 1234567890, + }; + + let bond_requirement_response = BondRequirementResponse { + bond_address: "1BitcoinAddress".to_string(), + locking_amount_sat: 500, + }; + + // Insert the new maker request + database.insert_new_maker_request(&order_request, &bond_requirement_response).await?; + + // Verify the insertion + let row = sqlx::query("SELECT * FROM maker_requests WHERE robohash = ?") + .bind(hex::decode(&order_request.robohash_hex)?) + .fetch_one(&*database.db_pool) + .await?; + + assert!(row.get::("is_buy_order")); + assert_eq!(row.get::("amount_sat"), 1000); + assert_eq!(row.get::("bond_ratio"), 50); + assert_eq!(row.get::("offer_duration_ts"), 1234567890); + assert_eq!(row.get::("bond_address"), "1BitcoinAddress"); + assert_eq!(row.get::("bond_amount_sat"), 500); + + Ok(()) + } +} +