Skip to content

Commit

Permalink
wip: moving test into same file
Browse files Browse the repository at this point in the history
  • Loading branch information
NanezX committed Oct 16, 2023
1 parent 71aa55c commit 8049f00
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@ async fn orderbook_entity_test() -> Result<()> {
// .expect("cannot deploy expression_deployer");

// ///////////////////////////////////////////////
// // Deploy ERC20 token contract (A)
// // Deploy ERC20 token contract (A)command) and convert to Bytes
// let ob_meta_hashed = Bytes::from(keccak256(read_orderbook_meta()));

// assert_eq!(response.id, orderbook.address());
// assert_eq!(response.address, orderbook.address());
// assert_eq!(response.deployer, wallet_0.address());
// assert_eq!(response.meta, ob_meta_hashed);
// let token_a = deploy_erc20_mock(None)
// .await
// .expect("failed on deploy erc20 token A");
Expand Down Expand Up @@ -106,3 +112,41 @@ async fn orderbook_entity_test() -> Result<()> {

Ok(())
}

#[tokio::main]
#[test]
async fn rain_meta_v1_entity_test() -> Result<()> {
// Always checking if OB is deployed, so we attemp to obtaing it
let _ = get_orderbook().await.expect("cannot get OB");

// Wait for Subgraph sync
wait().await.expect("cannot get SG sync status");

// Read meta from root repository (output from nix command) and convert to Bytes
let ob_meta = read_orderbook_meta();
let ob_meta_bytes = Bytes::from(ob_meta.clone());
let ob_meta_hashed = Bytes::from(keccak256(ob_meta));

// Query the RainMetaV1 entity
let response = Query::rain_meta_v1(ob_meta_hashed.clone())
.await
.expect("cannot get the rain meta query response");

assert_eq!(response.id, ob_meta_hashed);
assert_eq!(response.meta_bytes, ob_meta_bytes);
// assert_eq!(response.content, ob_meta_bytes);

println!("response.content: {:?}", response.content);

// let response = Query::rain_meta_v1(orderbook.address())
// .await
// .expect("cannot get the rain meta query response");

// // This wallet is used to deploy the OrderBook at initialization, so it is the deployer
// let wallet_0 = utils::get_wallet(0);

// // Read meta from root repository (output from nix command) and convert to Bytes
// let ob_meta_hashed = Bytes::from(keccak256(read_orderbook_meta()));

Ok(())
}
8 changes: 7 additions & 1 deletion subgraph/tests/subgraph/query/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
pub(crate) mod orderbook;
pub(crate) mod rain_meta_v1;

use anyhow::Result;
use ethers::types::Address;
use ethers::types::{Address, Bytes};
use orderbook::{get_orderbook_query, OrderBookResponse};
use rain_meta_v1::{get_rain_meta_v1, RainMetaV1Response};

pub struct Query;

impl Query {
pub async fn orderbook(address: Address) -> Result<OrderBookResponse> {
get_orderbook_query(address).await
}

pub async fn rain_meta_v1(id: Bytes) -> Result<RainMetaV1Response> {
get_rain_meta_v1(id).await
}
}
5 changes: 3 additions & 2 deletions subgraph/tests/subgraph/query/orderbook/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use self::order_book::ResponseData;
use crate::subgraph::wait::wait;
use crate::subgraph::wait;
use anyhow::{anyhow, Result};
use ethers::types::{Address, Bytes};
use graphql_client::{GraphQLQuery, Response};
Expand Down Expand Up @@ -27,7 +27,7 @@ pub struct OrderBookResponse {

impl OrderBookResponse {
pub fn from(response: ResponseData) -> OrderBookResponse {
let orderbook = response.order_book.unwrap();
let orderbook: order_book::OrderBookOrderBook = response.order_book.unwrap();

let meta_bytes = orderbook
.meta
Expand Down Expand Up @@ -55,6 +55,7 @@ pub async fn get_orderbook_query(orderbook_address: Address) -> Result<OrderBook
let variables = order_book::Variables {
orderbook: format!("{:?}", orderbook_address).to_string().into(),
};

let request_body = OrderBook::build_query(variables);
let client = reqwest::Client::new();
let res = client.post(url.clone()).json(&request_body).send().await?;
Expand Down
67 changes: 67 additions & 0 deletions subgraph/tests/subgraph/query/rain_meta_v1/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use self::rain_meta_v1::{RainMetaV1RainMetaV1Content, ResponseData};
use crate::subgraph::wait;
use anyhow::{anyhow, Result};
use ethers::types::Bytes;
use graphql_client::{GraphQLQuery, Response};
use reqwest::Url;
use serde::{Deserialize, Serialize};
use std::str::FromStr;

#[derive(GraphQLQuery)]
#[graphql(
schema_path = "tests/subgraph/query/schema.json",
query_path = "tests/subgraph/query/rain_meta_v1/rain_meta_v1.graphql",
reseponse_derives = "Debug, Serialize, Deserialize"
)]
pub struct RainMetaV1;

#[derive(Serialize, Deserialize, Debug)]
pub struct RainMetaV1Response {
pub id: Bytes,
pub meta_bytes: Bytes,
pub content: Vec<Bytes>,
}

impl RainMetaV1Response {
pub fn from(response: ResponseData) -> RainMetaV1Response {
let data = response.rain_meta_v1.unwrap();

let content_data: Vec<RainMetaV1RainMetaV1Content> =
data.content.unwrap_or(vec![RainMetaV1RainMetaV1Content {
id: Bytes::from([0u8, 32]),
}]);

let content: Vec<Bytes> = content_data.iter().map(|meta| meta.id.clone()).collect();

RainMetaV1Response {
id: data.id,
meta_bytes: data.meta_bytes,
content,
}
}
}

pub async fn get_rain_meta_v1(rain_meta_id: Bytes) -> Result<RainMetaV1Response> {
wait().await?;

let url = Url::from_str(&"http://localhost:8000/subgraphs/name/test/test")
.expect("cannot get the sg url");

let variables = rain_meta_v1::Variables {
rain_meta: rain_meta_id.to_string().into(),
};

let request_body = RainMetaV1::build_query(variables);
let client = reqwest::Client::new();
let res = client.post(url.clone()).json(&request_body).send().await?;

let response_body: Response<rain_meta_v1::ResponseData> = res.json().await?;

match response_body.data {
Some(data) => {
let response: RainMetaV1Response = RainMetaV1Response::from(data);
Ok(response)
}
None => Err(anyhow!("Failed to get metaboard")),
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
query RainMetaV1($rain_meta: String) {
rainMetaV1(id: $rain_meta) {
id
metaBytes
content {
id
}
}
}
2 changes: 2 additions & 0 deletions subgraph/tests/subgraph/wait/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ pub struct SyncStatus;
pub async fn wait() -> anyhow::Result<bool> {
let block_number = get_block_number().await;

// let _ = get_orderbook().await.expect("cannot get OB in waiting");

let url = Url::from_str(&"http://localhost:8030/graphql")?;

let variables = sync_status::Variables {};
Expand Down
2 changes: 1 addition & 1 deletion subgraph/tests/utils/deploy/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod erc20_mock;
mod meta_getter;
mod orderbook;
pub mod orderbook;
mod registry1820;
mod touch_deployer;

Expand Down
5 changes: 1 addition & 4 deletions subgraph/tests/utils/deploy/orderbook/setup.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
subgraph::{deploy, Config},
generated::OrderBook,
subgraph::{deploy, Config},
utils::get_block_number,
};
use anyhow::Result;
Expand Down Expand Up @@ -36,9 +36,6 @@ pub async fn init_orderbook(
.await
.expect("cannot deploy OB at setup initialization");

// let addrr = orderbook.address();
// let ob_address = format!("{:?}", orderbook.address());

let sg_config = Config {
contract_address: &format!("{:?}", orderbook.address()),
block_number: get_block_number().await.as_u64(),
Expand Down

0 comments on commit 8049f00

Please sign in to comment.