Skip to content

Commit

Permalink
Reuse network_id accross tests against devnet (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-iohk authored Dec 4, 2024
1 parent 3fbb2a2 commit d93385e
Show file tree
Hide file tree
Showing 14 changed files with 73 additions and 63 deletions.
12 changes: 12 additions & 0 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use axum::{
response::IntoResponse,
Router,
};
use coinbase_mesh::models::{NetworkIdentifier, NetworkRequest};
use pretty_assertions::assert_eq;
use reqwest::Client;
use serde_json::{Map, Value};
Expand Down Expand Up @@ -160,3 +161,14 @@ fn remove_empty_related_transactions(value: &mut Value) {
_ => {}
}
}

pub const DEVNET_BLOCKCHAIN_ID: &str = "mina";
pub const DEVNET_NETWORK_ID: &str = "testnet";

pub fn network_id() -> NetworkIdentifier {
NetworkIdentifier::new(DEVNET_BLOCKCHAIN_ID.to_string(), DEVNET_NETWORK_ID.to_string())
}

pub fn network_request() -> NetworkRequest {
NetworkRequest::new(network_id())
}
17 changes: 4 additions & 13 deletions tests/account_balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ use anyhow::Result;
use futures::future::try_join_all;
use insta::assert_debug_snapshot;
use mina_mesh::{
models::{
AccountBalanceRequest, AccountBalanceResponse, AccountIdentifier, NetworkIdentifier, PartialBlockIdentifier,
},
models::{AccountBalanceRequest, AccountBalanceResponse, AccountIdentifier, PartialBlockIdentifier},
test::network_id,
MinaMeshConfig, MinaMeshError,
};

Expand All @@ -25,11 +24,7 @@ async fn responses() -> Result<()> {
account_identifier: Box::new(AccountIdentifier { address: address.into(), sub_account: None, metadata: None }),
block_identifier: Some(Box::new(PartialBlockIdentifier { index: Some(6265), hash: None })),
currencies: None,
network_identifier: Box::new(NetworkIdentifier {
blockchain: "mina".into(),
network: "devnet".into(),
sub_network_identifier: None,
}),
network_identifier: Box::new(network_id()),
})
})
.collect();
Expand All @@ -51,11 +46,7 @@ async fn account_not_found_error() -> Result<()> {
}),
block_identifier: None,
currencies: None,
network_identifier: Box::new(NetworkIdentifier {
blockchain: "mina".into(),
network: "devnet".into(),
sub_network_identifier: None,
}),
network_identifier: Box::new(network_id()),
})
.await;
assert!(matches!(response, Err(MinaMeshError::AccountNotFound(_))));
Expand Down
12 changes: 4 additions & 8 deletions tests/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use anyhow::Result;
use futures::{stream::FuturesUnordered, StreamExt};
use insta::assert_debug_snapshot;
use mina_mesh::{
models::{BlockRequest, BlockResponse, NetworkIdentifier, PartialBlockIdentifier},
models::{BlockRequest, BlockResponse, PartialBlockIdentifier},
test::network_id,
MinaMeshConfig, MinaMeshError,
};
use pretty_assertions::assert_eq;
Expand All @@ -14,7 +15,7 @@ async fn specified() -> Result<()> {
let mina_mesh = MinaMeshConfig::from_env().to_mina_mesh().await?;
let mut futures = specified_identifiers()
.iter()
.map(|item| mina_mesh.block(BlockRequest::new(network_identifier().to_owned(), item.to_owned())))
.map(|item| mina_mesh.block(BlockRequest::new(network_id(), item.to_owned())))
.collect::<FuturesUnordered<_>>();
let mut maybe_prev: Option<Result<BlockResponse, MinaMeshError>> = None;
while let Some(resolved) = futures.next().await {
Expand Down Expand Up @@ -44,17 +45,12 @@ fn specified_identifiers() -> &'static [PartialBlockIdentifier; 3] {
})
}

fn network_identifier() -> &'static NetworkIdentifier {
static NETWORK_IDENTIFIER: OnceLock<NetworkIdentifier> = OnceLock::new();
NETWORK_IDENTIFIER.get_or_init(|| NetworkIdentifier::new("mina".to_string(), "devnet".to_string()))
}

#[tokio::test]
async fn unspecified() -> Result<()> {
let response = MinaMeshConfig::from_env()
.to_mina_mesh()
.await?
.block(BlockRequest::new(network_identifier().to_owned(), PartialBlockIdentifier::new()))
.block(BlockRequest::new(network_id(), PartialBlockIdentifier::new()))
.await;
assert!(response.is_ok());
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion tests/compare_to_ocaml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ async fn assert_responses_contain<T: Serialize>(subpath: &str, reqs: &[T], fragm
}

#[tokio::test]
async fn search_transactions() -> Result<()> {
async fn search_transactions_test() -> Result<()> {
let (subpath, reqs) = fixtures::search_transactions();
assert_responses_eq(subpath, &reqs).await
}
Expand Down
7 changes: 5 additions & 2 deletions tests/fixtures/account_balance.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use mina_mesh::models::{AccountBalanceRequest, AccountIdentifier, PartialBlockIdentifier};
use mina_mesh::{
models::{AccountBalanceRequest, AccountIdentifier, PartialBlockIdentifier},
test::network_id,
};

use super::{network_id, CompareGroup};
use super::CompareGroup;

pub fn account_balance<'a>() -> CompareGroup<'a> {
("/account/balance", vec![
Expand Down
7 changes: 5 additions & 2 deletions tests/fixtures/block.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use mina_mesh::models::{BlockRequest, PartialBlockIdentifier};
use mina_mesh::{
models::{BlockRequest, PartialBlockIdentifier},
test::network_id,
};

use super::{network_id, CompareGroup};
use super::CompareGroup;

#[allow(dead_code)]
pub fn block<'a>() -> CompareGroup<'a> {
Expand Down
7 changes: 5 additions & 2 deletions tests/fixtures/mempool.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use mina_mesh::models::{MempoolTransactionRequest, NetworkRequest, TransactionIdentifier};
use mina_mesh::{
models::{MempoolTransactionRequest, NetworkRequest, TransactionIdentifier},
test::network_id,
};

use super::{network_id, CompareGroup};
use super::CompareGroup;

pub fn mempool<'a>() -> CompareGroup<'a> {
("/mempool", vec![Box::new(NetworkRequest::new(network_id()))])
Expand Down
9 changes: 0 additions & 9 deletions tests/fixtures/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,7 @@ pub use account_balance::*;
#[allow(unused_imports)]
pub use block::*;
pub use mempool::*;
use mina_mesh::models::{NetworkIdentifier, NetworkRequest};
pub use network::*;
pub use search_transactions::*;

pub type CompareGroup<'a> = (&'a str, Vec<Box<dyn ErasedSerialize>>);

pub fn network_id() -> NetworkIdentifier {
NetworkIdentifier::new("mina".to_string(), "devnet".to_string())
}

pub fn network_request() -> NetworkRequest {
NetworkRequest::new(network_id())
}
3 changes: 2 additions & 1 deletion tests/fixtures/network.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use mina_mesh::test::network_request;
use serde::{ser::SerializeStruct, Serialize, Serializer};

use super::{network_request, CompareGroup};
use super::CompareGroup;

struct EmptyPayload;

Expand Down
13 changes: 8 additions & 5 deletions tests/fixtures/search_transactions.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
use mina_mesh::models::{NetworkIdentifier, SearchTransactionsRequest, TransactionIdentifier};
use mina_mesh::{
models::{SearchTransactionsRequest, TransactionIdentifier},
test::network_id,
};

use super::CompareGroup;

pub fn search_transactions<'a>() -> CompareGroup<'a> {
("/search/transactions", vec![
Box::new(SearchTransactionsRequest {
network_identifier: Box::new(NetworkIdentifier::new("mina".to_string(), "devnet".to_string())),
network_identifier: Box::new(network_id()),
address: Some("B62qkd6yYALkQMq2SFd5B57bJbGBMA2QuGtLPMzRhhnvexRtVRycZWP".to_string()),
limit: Some(5),
offset: Some(0),
..Default::default()
}),
Box::new(SearchTransactionsRequest {
network_identifier: Box::new(NetworkIdentifier::new("mina".to_string(), "devnet".to_string())),
network_identifier: Box::new(network_id()),
max_block: Some(44),
status: Some("failed".to_string()),
limit: Some(5),
..Default::default()
}),
Box::new(SearchTransactionsRequest {
network_identifier: Box::new(NetworkIdentifier::new("mina".to_string(), "devnet".to_string())),
network_identifier: Box::new(network_id()),
max_block: Some(44),
transaction_identifier: Some(Box::new(TransactionIdentifier::new(
// cspell:disable-next-line
Expand All @@ -29,7 +32,7 @@ pub fn search_transactions<'a>() -> CompareGroup<'a> {
..Default::default()
}),
Box::new(SearchTransactionsRequest {
network_identifier: Box::new(NetworkIdentifier::new("mina".to_string(), "devnet".to_string())),
network_identifier: Box::new(network_id()),
transaction_identifier: Some(Box::new(TransactionIdentifier::new(
// cspell:disable-next-line
"5JvFoEyvuPu9zmi4bDGbhqsakre2SPQU1KKbeh2Lk5uC9eYrc2h2".to_string(),
Expand Down
7 changes: 2 additions & 5 deletions tests/network_options.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
use anyhow::Result;
use insta::assert_debug_snapshot;
use mina_mesh::{
models::{NetworkIdentifier, NetworkRequest},
MinaMeshConfig,
};
use mina_mesh::{test::network_request, MinaMeshConfig};

#[tokio::test]
async fn test_network_options() -> Result<()> {
let req = NetworkRequest::new(NetworkIdentifier::new("mina".to_string(), "devnet".to_string()));
let req = network_request();
let response = MinaMeshConfig::from_env().to_mina_mesh().await?.network_options(req).await?;
assert_debug_snapshot!(&response.allow);
Ok(())
Expand Down
21 changes: 11 additions & 10 deletions tests/search_transactions.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use anyhow::Result;
use insta::assert_debug_snapshot;
use mina_mesh::{
models::{AccountIdentifier, NetworkIdentifier, SearchTransactionsRequest, TransactionIdentifier},
models::{AccountIdentifier, SearchTransactionsRequest, TransactionIdentifier},
test::network_id,
MinaMeshConfig,
};

Expand All @@ -12,7 +13,7 @@ async fn search_transactions_specified() -> Result<()> {
let address = "B62qkd6yYALkQMq2SFd5B57bJbGBMA2QuGtLPMzRhhnvexRtVRycZWP";

let request_addr = SearchTransactionsRequest {
network_identifier: Box::new(NetworkIdentifier::new("mina".to_string(), "devnet".to_string())),
network_identifier: Box::new(network_id()),
address: Some(address.to_string()),
limit: Some(5),
offset: Some(0),
Expand All @@ -22,7 +23,7 @@ async fn search_transactions_specified() -> Result<()> {
let response_addr = mina_mesh.search_transactions(request_addr).await;

let request_acct_id = SearchTransactionsRequest {
network_identifier: Box::new(NetworkIdentifier::new("mina".to_string(), "devnet".to_string())),
network_identifier: Box::new(network_id()),
account_identifier: Some(Box::new(AccountIdentifier::new(address.to_string()))),
limit: Some(5),
offset: Some(0),
Expand All @@ -42,7 +43,7 @@ async fn search_transactions_failed() -> Result<()> {
let mina_mesh = MinaMeshConfig::from_env().to_mina_mesh().await?;

let request = SearchTransactionsRequest {
network_identifier: Box::new(NetworkIdentifier::new("mina".to_string(), "devnet".to_string())),
network_identifier: Box::new(network_id()),
max_block: Some(44),
status: Some("failed".to_string()),
limit: Some(5),
Expand All @@ -61,7 +62,7 @@ async fn search_transactions_internal_command() -> Result<()> {
let mina_mesh = MinaMeshConfig::from_env().to_mina_mesh().await?;

let request = SearchTransactionsRequest {
network_identifier: Box::new(NetworkIdentifier::new("mina".to_string(), "devnet".to_string())),
network_identifier: Box::new(network_id()),
max_block: Some(44),
transaction_identifier: Some(Box::new(TransactionIdentifier::new(
// cspell:disable-next-line
Expand All @@ -83,7 +84,7 @@ async fn search_transactions_zkapp_success() -> Result<()> {
let mina_mesh = MinaMeshConfig::from_env().to_mina_mesh().await?;

let request = SearchTransactionsRequest {
network_identifier: Box::new(NetworkIdentifier::new("mina".to_string(), "devnet".to_string())),
network_identifier: Box::new(network_id()),
transaction_identifier: Some(Box::new(TransactionIdentifier::new(
// cspell:disable-next-line
"5JvFoEyvuPu9zmi4bDGbhqsakre2SPQU1KKbeh2Lk5uC9eYrc2h2".to_string(),
Expand All @@ -104,7 +105,7 @@ async fn search_transactions_zkapp_failed() -> Result<()> {
let mina_mesh = MinaMeshConfig::from_env().to_mina_mesh().await?;

let request = SearchTransactionsRequest {
network_identifier: Box::new(NetworkIdentifier::new("mina".to_string(), "devnet".to_string())),
network_identifier: Box::new(network_id()),
transaction_identifier: Some(Box::new(TransactionIdentifier::new(
// cspell:disable-next-line
"5JujBt8rnKheA7CHBnTwUDXrHtQxqPB9LL5Q8y4KwLjPBsBSJuSE".to_string(),
Expand Down Expand Up @@ -132,7 +133,7 @@ async fn search_transactions_zkapp_tokens_account_identifier() -> Result<()> {
let metadata = serde_json::json!({ "token_id": token_id });

let request_address1_token = SearchTransactionsRequest {
network_identifier: Box::new(NetworkIdentifier::new("mina".to_string(), "devnet".to_string())),
network_identifier: Box::new(network_id()),
max_block: Some(max_block),
account_identifier: Some(Box::new(AccountIdentifier {
address: address1.to_string(),
Expand All @@ -145,7 +146,7 @@ async fn search_transactions_zkapp_tokens_account_identifier() -> Result<()> {
let response_address1_token = mina_mesh.search_transactions(request_address1_token).await;

let request_address2_token = SearchTransactionsRequest {
network_identifier: Box::new(NetworkIdentifier::new("mina".to_string(), "devnet".to_string())),
network_identifier: Box::new(network_id()),
max_block: Some(max_block),
account_identifier: Some(Box::new(AccountIdentifier {
address: address2.to_string(),
Expand All @@ -168,7 +169,7 @@ async fn search_transactions_zkapp_tokens_tx_hash() -> Result<()> {
let mina_mesh = MinaMeshConfig::from_env().to_mina_mesh().await?;

let request_tx_hash = SearchTransactionsRequest {
network_identifier: Box::new(NetworkIdentifier::new("mina".to_string(), "devnet".to_string())),
network_identifier: Box::new(network_id()),
transaction_identifier: Some(Box::new(TransactionIdentifier::new(
// cspell:disable-next-line
"5JuotEHhjuYbu2oucyTiVhJX3Abx5DPL4NXnM7CP9hfJZLE5G8n9".to_string(),
Expand Down
2 changes: 1 addition & 1 deletion tests/snapshots/network_list__network_list.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ expression: "&response.network_identifiers"
[
NetworkIdentifier {
blockchain: "mina",
network: "devnet",
network: "testnet",
sub_network_identifier: None,
},
]
17 changes: 13 additions & 4 deletions tests/validate_network.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
use anyhow::Result;
use mina_mesh::{models::NetworkIdentifier, CacheKey::NetworkId, MinaMeshConfig, MinaMeshError};
use mina_mesh::{
models::NetworkIdentifier,
test::{network_id, DEVNET_BLOCKCHAIN_ID, DEVNET_NETWORK_ID},
CacheKey::NetworkId,
MinaMeshConfig, MinaMeshError,
};

#[tokio::test]
async fn genesis_block_identifier() -> Result<()> {
Expand All @@ -15,13 +20,13 @@ async fn genesis_block_identifier() -> Result<()> {
#[tokio::test]
async fn validate_network_ok() -> Result<()> {
let mina_mesh = MinaMeshConfig::from_env().to_mina_mesh().await?;
let network = NetworkIdentifier::new("mina".to_string(), "devnet".to_string());
let network = network_id();

assert!(mina_mesh.get_from_cache(NetworkId).is_none(), "Cache should be empty");
let result = mina_mesh.validate_network(&network).await;
assert!(result.is_ok(), "validate_network failed");
if let Some(cached_network_id) = mina_mesh.get_from_cache(NetworkId) {
assert_eq!(cached_network_id, "mina:devnet", "Cached network_id does not match");
assert_eq!(cached_network_id, current_network_id_string(), "Cached network_id does not match");
} else {
panic!("Cache was not updated after validate_network");
}
Expand All @@ -36,10 +41,14 @@ async fn validate_network_err() -> Result<()> {
assert!(result.is_err(), "validate_network should have failed");
if let Err(MinaMeshError::NetworkDne(expected, actual)) = result {
assert_eq!(expected, "mina:unknown");
assert_eq!(actual, "mina:devnet");
assert_eq!(actual, current_network_id_string());
} else {
panic!("Unexpected error type");
}

Ok(())
}

fn current_network_id_string() -> String {
format!("{}:{}", DEVNET_BLOCKCHAIN_ID, DEVNET_NETWORK_ID)
}

0 comments on commit d93385e

Please sign in to comment.