Skip to content

Commit 7844b59

Browse files
committed
test common::subgraph
1 parent 8ce9dae commit 7844b59

File tree

13 files changed

+60
-35
lines changed

13 files changed

+60
-35
lines changed

crates/cli/src/commands/order/detail.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ impl Execute for CliOrderDetailArgs {
1717
async fn execute(&self) -> Result<()> {
1818
let subgraph_args: SubgraphArgs = self.subgraph_args.clone().into();
1919
let order = subgraph_args
20-
.to_subgraph_client()
21-
.await?
20+
.to_subgraph_client()?
2221
.order_detail(self.order_id.clone().into())
2322
.await?;
2423
let order_extended: OrderDetailExtended = order.try_into()?;

crates/cli/src/commands/order/list.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ impl Execute for CliOrderListArgs {
3030

3131
if self.pagination_args.csv {
3232
let csv_text = subgraph_args
33-
.to_subgraph_client()
34-
.await?
33+
.to_subgraph_client()?
3534
.orders_list_all()
3635
.await?
3736
.into_iter()
@@ -43,8 +42,7 @@ impl Execute for CliOrderListArgs {
4342
} else {
4443
let table = build_table(
4544
subgraph_args
46-
.to_subgraph_client()
47-
.await?
45+
.to_subgraph_client()?
4846
.orders_list(
4947
self.filter_args.clone().into(),
5048
self.pagination_args.clone().into(),

crates/cli/src/commands/order/remove.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ impl Execute for CliOrderRemoveArgs {
2525
async fn execute(&self) -> Result<()> {
2626
let subgraph_args: SubgraphArgs = self.subgraph_args.clone().into();
2727
let order = subgraph_args
28-
.to_subgraph_client()
29-
.await?
28+
.to_subgraph_client()?
3029
.order_detail(self.order_id.clone().into())
3130
.await?;
3231
let remove_order_args: RemoveOrderArgs = order.into();

crates/cli/src/commands/trade/detail.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ impl Execute for CliOrderTradeDetailArgs {
1919
async fn execute(&self) -> Result<()> {
2020
let subgraph_args: SubgraphArgs = self.subgraph_args.clone().into();
2121
let order_take = subgraph_args
22-
.to_subgraph_client()
23-
.await?
22+
.to_subgraph_client()?
2423
.order_trade_detail(self.id.clone().into())
2524
.await?;
2625
info!("{:#?}", order_take);

crates/cli/src/commands/trade/list.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ impl Execute for CliOrderTradesListArgs {
3030

3131
if self.pagination_args.csv {
3232
let csv_text = subgraph_args
33-
.to_subgraph_client()
34-
.await?
33+
.to_subgraph_client()?
3534
.order_trades_list_all(self.order_id.clone().into(), None, None)
3635
.await?
3736
.into_iter()
@@ -43,8 +42,7 @@ impl Execute for CliOrderTradesListArgs {
4342
} else {
4443
let table = build_table(
4544
subgraph_args
46-
.to_subgraph_client()
47-
.await?
45+
.to_subgraph_client()?
4846
.order_trades_list(
4947
self.order_id.clone().into(),
5048
self.pagination_args.clone().into(),

crates/cli/src/commands/vault/detail.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ impl Execute for CliVaultDetailArgs {
1717
async fn execute(&self) -> Result<()> {
1818
let subgraph_args: SubgraphArgs = self.subgraph_args.clone().into();
1919
let vault = subgraph_args
20-
.to_subgraph_client()
21-
.await?
20+
.to_subgraph_client()?
2221
.vault_detail(self.vault_id.clone().into())
2322
.await?;
2423
info!("{:#?}", vault);

crates/cli/src/commands/vault/list.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ impl Execute for CliVaultListArgs {
3131

3232
if self.pagination_args.csv {
3333
let vaults = subgraph_args
34-
.to_subgraph_client()
35-
.await?
34+
.to_subgraph_client()?
3635
.vaults_list_all()
3736
.await?;
3837
let vaults_flattened: Vec<TokenVaultFlattened> = vaults
@@ -46,8 +45,7 @@ impl Execute for CliVaultListArgs {
4645
let pagination_args: SgPaginationArgs = self.pagination_args.clone().into();
4746
let filter_args = self.filter_args.clone().into();
4847
let vaults = subgraph_args
49-
.to_subgraph_client()
50-
.await?
48+
.to_subgraph_client()?
5149
.vaults_list(filter_args, pagination_args)
5250
.await?;
5351
let vaults_flattened: Vec<TokenVaultFlattened> = vaults

crates/cli/src/commands/vault/list_balance_changes.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ impl Execute for CliVaultBalanceChangesList {
3030

3131
if self.pagination_args.csv {
3232
let csv_text = subgraph_args
33-
.to_subgraph_client()
34-
.await?
33+
.to_subgraph_client()?
3534
.vault_balance_changes_list_all(self.vault_id.clone().into())
3635
.await?
3736
.into_iter()
@@ -43,8 +42,7 @@ impl Execute for CliVaultBalanceChangesList {
4342
} else {
4443
let table = build_table(
4544
subgraph_args
46-
.to_subgraph_client()
47-
.await?
45+
.to_subgraph_client()?
4846
.vault_balance_changes_list(
4947
self.vault_id.clone().into(),
5048
self.pagination_args.clone().into(),

crates/common/src/subgraph.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,45 @@ pub struct SubgraphArgs {
88
}
99

1010
impl SubgraphArgs {
11-
pub async fn to_subgraph_client(&self) -> Result<OrderbookSubgraphClient, ParseError> {
11+
pub fn to_subgraph_client(&self) -> Result<OrderbookSubgraphClient, ParseError> {
1212
Ok(OrderbookSubgraphClient::new(Url::parse(self.url.as_str())?))
1313
}
1414
}
15+
16+
#[cfg(test)]
17+
mod tests {
18+
use super::*;
19+
20+
#[test]
21+
fn test_to_subgraph_client_ok() {
22+
let url = "https://api.thegraph.com/subgraphs/name/org1/sg1";
23+
let subgraph_args = SubgraphArgs {
24+
url: url.to_string(),
25+
};
26+
let subgraph_client = subgraph_args.to_subgraph_client().unwrap();
27+
assert_eq!(subgraph_client.url().as_str(), url);
28+
}
29+
30+
#[test]
31+
fn test_to_subgraph_client_err() {
32+
let url = "api.thegraph.com/subgraphs/name/org1/sg1".to_string();
33+
let subgraph_args = SubgraphArgs { url };
34+
let err = subgraph_args.to_subgraph_client().unwrap_err();
35+
assert_eq!(err, ParseError::RelativeUrlWithoutBase);
36+
37+
let url = "https:///".to_string();
38+
let subgraph_args = SubgraphArgs { url };
39+
let err = subgraph_args.to_subgraph_client().unwrap_err();
40+
assert_eq!(err, ParseError::EmptyHost);
41+
42+
let url = "".to_string();
43+
let subgraph_args = SubgraphArgs { url };
44+
let err = subgraph_args.to_subgraph_client().unwrap_err();
45+
assert_eq!(err, ParseError::RelativeUrlWithoutBase);
46+
47+
let url = ":".to_string();
48+
let subgraph_args = SubgraphArgs { url };
49+
let err = subgraph_args.to_subgraph_client().unwrap_err();
50+
assert_eq!(err, ParseError::RelativeUrlWithoutBase);
51+
}
52+
}

crates/subgraph/src/orderbook_client.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ impl From<OrderbookSubgraphClientError> for JsValue {
5656
}
5757
}
5858

59+
#[derive(Debug)]
5960
pub struct OrderbookSubgraphClient {
6061
url: Url,
6162
}
@@ -72,6 +73,10 @@ impl OrderbookSubgraphClient {
7273
Self { url }
7374
}
7475

76+
pub fn url(&self) -> &Url {
77+
&self.url
78+
}
79+
7580
/// Fetch single order
7681
pub async fn order_detail(&self, id: Id) -> Result<SgOrder, OrderbookSubgraphClientError> {
7782
let data = self

0 commit comments

Comments
 (0)