From e28ec75a3c576b4c8d27da0141b25598fef4c045 Mon Sep 17 00:00:00 2001 From: ninokeldishvili Date: Tue, 19 Nov 2024 17:46:10 +0400 Subject: [PATCH] Add test with mock call and check output balance --- Cargo.lock | 29 ++++++++++++ Cargo.toml | 1 + crates/cli/Cargo.toml | 1 + crates/cli/src/balance.rs | 95 ++++++++++++++++++++++++++++++++++++++- 4 files changed, 125 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 1f29cb825..d83ab9a3e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2201,6 +2201,16 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d3fd119d74b830634cea2a0f58bbd0d54540518a14397557951e79340abc28c0" +[[package]] +name = "colored" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cbf2150cce219b664a8a70df7a1f933836724b503f8a413af9365b4dcc4d90b8" +dependencies = [ + "lazy_static", + "windows-sys 0.48.0", +] + [[package]] name = "combine" version = "3.8.1" @@ -5246,6 +5256,24 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "mockito" +version = "0.31.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "80f9fece9bd97ab74339fe19f4bcaf52b76dcc18e5364c7977c1838f76b38de9" +dependencies = [ + "assert-json-diff", + "colored", + "httparse", + "lazy_static", + "log", + "rand", + "regex", + "serde_json", + "serde_urlencoded", + "similar", +] + [[package]] name = "native-tls" version = "0.2.12" @@ -6373,6 +6401,7 @@ dependencies = [ "csv", "cynic", "httpmock", + "mockito", "rain-metadata 0.0.2-alpha.6", "rain_orderbook_app_settings", "rain_orderbook_bindings", diff --git a/Cargo.toml b/Cargo.toml index c34ca10bf..dad4b8c1e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,6 +32,7 @@ comfy-table = "7.1.0" cynic-codegen = { version = "3.4.0", features = ["rkyv"] } cynic = "3.7.3" chrono = "0.4.31" +mockito = "0.31" typeshare = { git = "https://github.com/tomjw64/typeshare", rev = "556b44aafd5304eedf17206800f69834e3820b7c" } thiserror = "1.0.56" strict-yaml-rust = "0.1.2" diff --git a/crates/cli/Cargo.toml b/crates/cli/Cargo.toml index 2514323f6..677c41c07 100644 --- a/crates/cli/Cargo.toml +++ b/crates/cli/Cargo.toml @@ -29,6 +29,7 @@ comfy-table = { workspace = true } chrono = { workspace = true } csv = { workspace = true } cynic = { workspace = true } +mockito = { workspace = true } [target.'cfg(not(target_family = "wasm"))'.dependencies] tokio = { workspace = true, features = ["full"] } diff --git a/crates/cli/src/balance.rs b/crates/cli/src/balance.rs index 02bb5a7f7..84f02480a 100644 --- a/crates/cli/src/balance.rs +++ b/crates/cli/src/balance.rs @@ -18,6 +18,8 @@ async fn fetch_vault_balance(url: &str, variables: OrdersListQueryVariables) -> .await?; let text = req.text().await?; + + let response: Value = serde_json::from_str(&text)?; Ok(serde_json::from_str(&text)?) } @@ -32,7 +34,7 @@ async fn get_data(url: &str, variables: OrdersListQueryVariables) -> Result Result { let variables = OrdersListQueryVariables { skip: Some(0), - first: Some(25), + first: Some(1), filters: None, }; @@ -43,6 +45,7 @@ pub async fn get_balances(subgraph_url: &str) -> Result { #[cfg(test)] mod tests { use super::*; + use mockito::mock; #[tokio::test] async fn test_get_balances() { @@ -56,4 +59,94 @@ mod tests { println!("Fetched balance data: {:?}", data); } } + + #[tokio::test] + async fn test_get_balances_data() { + // Define the mock response JSON + let mock_response = serde_json::json!({ + "data": { + "orders": [ + { + "orderBytes": "0x1234", + "orderHash": "0x5678", + "owner": "0xabcdef", + "outputs": [ + { + "token": { + "id": "1", + "address": "0xdeadbeef", + "name": "Token A", + "symbol": "TKA", + "decimals": 18 + }, + "balance": "1000", + "vaultId": "vault-123" + } + ], + "inputs": [], + "orderbook": { + "id": "orderbook-1" + }, + "active": true, + "timestampAdded": "2024-11-19T12:00:00Z", + "addEvents": [ + { + "transaction": { + "blockNumber": 12345, + "timestamp": "2024-11-19T12:00:00Z" + } + } + ], + "trades": [] + } + ] + } + }); + + // Start a mock server + let _mock = mock("POST", "/") + .with_header("content-type", "application/json") + .with_body(mock_response.to_string()) + .create(); + + let mock_url = &mockito::server_url(); + + // Call the function under test + let result = get_balances(mock_url).await; + + // Assert the function call was successful + assert!(result.is_ok(), "Failed to fetch balances: {:?}", result); + + // Validate the returned data structure and values + if let Ok(data) = result { + // Ensure "data" key exists + let orders = data.get("data").and_then(|d| d.get("orders")); + assert!(orders.is_some(), "Orders data missing in response"); + + if let Some(order_array) = orders { + assert_eq!( + order_array.as_array().unwrap().len(), + 1, + "Unexpected number of orders" + ); + + let first_order = &order_array[0]; + assert_eq!(first_order.get("owner").unwrap(), "0xabcdef"); + assert_eq!(first_order.get("active").unwrap(), true); + + // Validate the `outputs` -> `balance` + let outputs = first_order.get("outputs").unwrap().as_array().unwrap(); + assert_eq!(outputs.len(), 1, "Unexpected number of outputs"); + + let first_output = &outputs[0]; + assert_eq!( + first_output.get("balance").unwrap(), + "1000", + "Unexpected balance in output" + ); + } + + println!("Fetched balance data: {:?}", data); + } + } }