Skip to content

Commit

Permalink
Add test with mock call and check output balance
Browse files Browse the repository at this point in the history
  • Loading branch information
ninokeldishvili committed Nov 19, 2024
1 parent c5a8c46 commit e28ec75
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 1 deletion.
29 changes: 29 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
95 changes: 94 additions & 1 deletion crates/cli/src/balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?)
}

Expand All @@ -32,7 +34,7 @@ async fn get_data(url: &str, variables: OrdersListQueryVariables) -> Result<Valu
pub async fn get_balances(subgraph_url: &str) -> Result<Value> {
let variables = OrdersListQueryVariables {
skip: Some(0),
first: Some(25),
first: Some(1),
filters: None,
};

Expand All @@ -43,6 +45,7 @@ pub async fn get_balances(subgraph_url: &str) -> Result<Value> {
#[cfg(test)]
mod tests {
use super::*;
use mockito::mock;

#[tokio::test]
async fn test_get_balances() {
Expand All @@ -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);
}
}
}

0 comments on commit e28ec75

Please sign in to comment.