Skip to content

Commit

Permalink
Merge pull request #787 from rainlanguage/2024-08-26-cli-order-take-c…
Browse files Browse the repository at this point in the history
…ommands-tests

Backfill tests for cli crate order take commands
  • Loading branch information
hardyjosh authored Aug 27, 2024
2 parents dc1b02e + 8a6f237 commit bfed539
Show file tree
Hide file tree
Showing 3 changed files with 253 additions and 17 deletions.
95 changes: 95 additions & 0 deletions crates/cli/src/commands/order_take/detail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,98 @@ impl Execute for CliOrderTakeDetailArgs {
Ok(())
}
}

#[cfg(test)]
mod tests {
use super::*;
use alloy::{
hex::encode_prefixed,
primitives::{Address, B256},
};
use httpmock::MockServer;
use serde_json::{json, Value};

#[tokio::test]
async fn test_execute_happy() {
let sg_server = MockServer::start();
sg_server.mock(|_when, then| {
then.json_body_obj(&get_sg_response());
});

let cli_order_take_detail_args = CliOrderTakeDetailArgs {
subgraph_args: CliSubgraphArgs {
subgraph_url: sg_server.url("/sg"),
},
id: encode_prefixed(B256::random()),
};

// should succeed
assert!(cli_order_take_detail_args.execute().await.is_ok());
}

#[tokio::test]
async fn test_execute_unhappy() {
// mock sg with corrupt response
let sg_server = MockServer::start();
sg_server.mock(|_when, then| {
then.json_body_obj(&json!({"data": {"trade": null}}));
});

let cli_order_take_detail_args = CliOrderTakeDetailArgs {
subgraph_args: CliSubgraphArgs {
subgraph_url: sg_server.url("/sg"),
},
id: encode_prefixed(B256::random()),
};

// should error
assert!(cli_order_take_detail_args.execute().await.is_err());
}

// helper function that returns mocked sg response in json
fn get_sg_response() -> Value {
json!({
"data": {
"trade": {
"id": encode_prefixed(B256::random()),
"order": {
"orderHash": encode_prefixed(B256::random()),
"timestampAdded": "0",
},
"outputVaultBalanceChange": {
"amount": "0",
"vault": {
"token": {
"name": "T1",
"symbol": "T1",
"id": encode_prefixed(Address::random()),
"address": encode_prefixed(Address::random()),
"decimals": "6"
}
}
},
"inputVaultBalanceChange": {
"amount": "0",
"vault": {
"token": {
"name": "T2",
"symbol": "T2",
"id": encode_prefixed(Address::random()),
"address": encode_prefixed(Address::random()),
"decimals": "18"
}
},
},
"timestamp": "0",
"tradeEvent": {
"sender": encode_prefixed(Address::random()),
"transaction": {
"id": encode_prefixed(B256::random()),
"from": encode_prefixed(Address::random()),
}
},
}
}
})
}
}
164 changes: 147 additions & 17 deletions crates/cli/src/commands/order_take/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use rain_orderbook_common::{
subgraph::SubgraphArgs,
types::{FlattenError, OrderTakeFlattened, NO_SYMBOL},
};
use rain_orderbook_subgraph_client::PaginationArgs;
use tracing::info;

#[derive(Args, Clone)]
Expand All @@ -30,31 +29,32 @@ impl Execute for CliOrderTakesListArgs {
let subgraph_args: SubgraphArgs = self.subgraph_args.clone().into();

if self.pagination_args.csv {
let order_takes = subgraph_args
let csv_text = subgraph_args
.to_subgraph_client()
.await?
.order_takes_list_all(self.order_id.clone().into())
.await?;
let order_takes_flattened: Vec<OrderTakeFlattened> = order_takes
.await?
.into_iter()
.map(|o| o.try_into())
.collect::<Result<Vec<OrderTakeFlattened>, FlattenError>>()?;
.collect::<Result<Vec<OrderTakeFlattened>, FlattenError>>()?
.try_into_csv()?;

let csv_text = order_takes_flattened.try_into_csv()?;
println!("{}", csv_text);
} else {
let pagination_args: PaginationArgs = self.pagination_args.clone().into();
let order_takes = subgraph_args
.to_subgraph_client()
.await?
.order_takes_list(self.order_id.clone().into(), pagination_args)
.await?;
let order_takes_flattened: Vec<OrderTakeFlattened> = order_takes
.into_iter()
.map(|o| o.try_into())
.collect::<Result<Vec<OrderTakeFlattened>, FlattenError>>()?;
let table = build_table(
subgraph_args
.to_subgraph_client()
.await?
.order_takes_list(
self.order_id.clone().into(),
self.pagination_args.clone().into(),
)
.await?
.into_iter()
.map(|o| o.try_into())
.collect::<Result<Vec<OrderTakeFlattened>, FlattenError>>()?,
)?;

let table = build_table(order_takes_flattened)?;
info!("\n{}", table);
}

Expand Down Expand Up @@ -89,3 +89,133 @@ fn build_table(order_take: Vec<OrderTakeFlattened>) -> Result<Table> {

Ok(table)
}

#[cfg(test)]
mod tests {
use super::*;
use alloy::{
hex::encode_prefixed,
primitives::{Address, B256},
};
use httpmock::MockServer;
use serde_json::{json, Value};

#[tokio::test]
async fn test_csv_execute_happy() {
// mock subgraph with pagination
let sg_server = MockServer::start();
sg_server.mock(|when, then| {
when.body_contains("\"skip\":0");
then.json_body_obj(&get_sg_response());
});
sg_server.mock(|_when, then| {
then.json_body_obj(&json!({"data": {"trades": []}}));
});

let cli_order_take_list_args = CliOrderTakesListArgs {
order_id: encode_prefixed(B256::random()),
subgraph_args: CliSubgraphArgs {
subgraph_url: sg_server.url("/sg"),
},
pagination_args: CliPaginationArgs {
csv: true,
page_size: 25,
page: 1,
},
};

// should succeed
assert!(cli_order_take_list_args.execute().await.is_ok());
}

#[tokio::test]
async fn test_no_csv_execute_happy() {
// mock subgraph
let sg_server = MockServer::start();
sg_server.mock(|_when, then| {
then.json_body_obj(&get_sg_response());
});

let cli_order_take_list_args = CliOrderTakesListArgs {
order_id: encode_prefixed(B256::random()),
subgraph_args: CliSubgraphArgs {
subgraph_url: sg_server.url("/sg"),
},
pagination_args: CliPaginationArgs {
csv: false,
page_size: 25,
page: 1,
},
};

// should succeed
assert!(cli_order_take_list_args.execute().await.is_ok());
}

#[tokio::test]
async fn test_execute_unhappy() {
let cli_order_take_list_args = CliOrderTakesListArgs {
order_id: encode_prefixed(B256::random()),
subgraph_args: CliSubgraphArgs {
subgraph_url: "https://bad-url".to_string(),
},
pagination_args: CliPaginationArgs {
csv: false,
page_size: 25,
page: 1,
},
};

// should error
assert!(cli_order_take_list_args.execute().await.is_err());
}

// helper function that returns mocked sg response in json
fn get_sg_response() -> Value {
json!({
"data": {
"trades": [{
"id": encode_prefixed(B256::random()),
"order": {
"id": encode_prefixed(B256::random()),
"orderHash": encode_prefixed(B256::random()),
"timestampAdded": "0",
},
"outputVaultBalanceChange": {
"amount": "0",
"vault": {
"token": {
"name": "T1",
"symbol": "T1",
"id": encode_prefixed(Address::random()),
"address": encode_prefixed(Address::random()),
"decimals": "6"
}
}
},
"inputVaultBalanceChange": {
"amount": "0",
"vault": {
"token": {
"name": "T2",
"symbol": "T2",
"id": encode_prefixed(Address::random()),
"address": encode_prefixed(Address::random()),
"decimals": "18"
}
},
},
"timestamp": "0",
"tradeEvent": {
"sender": encode_prefixed(Address::random()),
"transaction": {
"id": encode_prefixed(B256::random()),
"from": encode_prefixed(Address::random()),
"timestamp": "0",
}
},
}]
}
})
}
}
11 changes: 11 additions & 0 deletions crates/cli/src/commands/order_take/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,14 @@ impl Execute for OrderTake {
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use clap::CommandFactory;

#[test]
fn verify_command() {
OrderTake::command().debug_assert();
}
}

0 comments on commit bfed539

Please sign in to comment.