From d93385eb1c66fb7cb74f6bbb3355cd6e22c7f0c9 Mon Sep 17 00:00:00 2001 From: piotr-iohk <42900201+piotr-iohk@users.noreply.github.com> Date: Wed, 4 Dec 2024 12:31:31 +0100 Subject: [PATCH] Reuse network_id accross tests against devnet (#79) --- src/test.rs | 12 +++++++++++ tests/account_balance.rs | 17 ++++----------- tests/block.rs | 12 ++++------- tests/compare_to_ocaml.rs | 2 +- tests/fixtures/account_balance.rs | 7 +++++-- tests/fixtures/block.rs | 7 +++++-- tests/fixtures/mempool.rs | 7 +++++-- tests/fixtures/mod.rs | 9 -------- tests/fixtures/network.rs | 3 ++- tests/fixtures/search_transactions.rs | 13 +++++++----- tests/network_options.rs | 7 ++----- tests/search_transactions.rs | 21 ++++++++++--------- .../snapshots/network_list__network_list.snap | 2 +- tests/validate_network.rs | 17 +++++++++++---- 14 files changed, 73 insertions(+), 63 deletions(-) diff --git a/src/test.rs b/src/test.rs index a45bc37..d0f9f5b 100644 --- a/src/test.rs +++ b/src/test.rs @@ -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}; @@ -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()) +} diff --git a/tests/account_balance.rs b/tests/account_balance.rs index 4780aaf..85f714d 100644 --- a/tests/account_balance.rs +++ b/tests/account_balance.rs @@ -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, }; @@ -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(); @@ -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(_)))); diff --git a/tests/block.rs b/tests/block.rs index 7f0695a..dab11db 100644 --- a/tests/block.rs +++ b/tests/block.rs @@ -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; @@ -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::>(); let mut maybe_prev: Option> = None; while let Some(resolved) = futures.next().await { @@ -44,17 +45,12 @@ fn specified_identifiers() -> &'static [PartialBlockIdentifier; 3] { }) } -fn network_identifier() -> &'static NetworkIdentifier { - static NETWORK_IDENTIFIER: OnceLock = 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(()) diff --git a/tests/compare_to_ocaml.rs b/tests/compare_to_ocaml.rs index bd9dd59..9a2ef78 100644 --- a/tests/compare_to_ocaml.rs +++ b/tests/compare_to_ocaml.rs @@ -34,7 +34,7 @@ async fn assert_responses_contain(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 } diff --git a/tests/fixtures/account_balance.rs b/tests/fixtures/account_balance.rs index 5cdf565..894e7c4 100644 --- a/tests/fixtures/account_balance.rs +++ b/tests/fixtures/account_balance.rs @@ -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![ diff --git a/tests/fixtures/block.rs b/tests/fixtures/block.rs index 16e4d53..a42c87f 100644 --- a/tests/fixtures/block.rs +++ b/tests/fixtures/block.rs @@ -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> { diff --git a/tests/fixtures/mempool.rs b/tests/fixtures/mempool.rs index 48fb1e6..ea3016f 100644 --- a/tests/fixtures/mempool.rs +++ b/tests/fixtures/mempool.rs @@ -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()))]) diff --git a/tests/fixtures/mod.rs b/tests/fixtures/mod.rs index 350eff8..b7e5e7f 100644 --- a/tests/fixtures/mod.rs +++ b/tests/fixtures/mod.rs @@ -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>); - -pub fn network_id() -> NetworkIdentifier { - NetworkIdentifier::new("mina".to_string(), "devnet".to_string()) -} - -pub fn network_request() -> NetworkRequest { - NetworkRequest::new(network_id()) -} diff --git a/tests/fixtures/network.rs b/tests/fixtures/network.rs index 3698dfd..61ba3d4 100644 --- a/tests/fixtures/network.rs +++ b/tests/fixtures/network.rs @@ -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; diff --git a/tests/fixtures/search_transactions.rs b/tests/fixtures/search_transactions.rs index 303ddfb..486676d 100644 --- a/tests/fixtures/search_transactions.rs +++ b/tests/fixtures/search_transactions.rs @@ -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 @@ -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(), diff --git a/tests/network_options.rs b/tests/network_options.rs index c5cd5e1..0e422b0 100644 --- a/tests/network_options.rs +++ b/tests/network_options.rs @@ -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(()) diff --git a/tests/search_transactions.rs b/tests/search_transactions.rs index e8cec90..5fbff98 100644 --- a/tests/search_transactions.rs +++ b/tests/search_transactions.rs @@ -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, }; @@ -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), @@ -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), @@ -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), @@ -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 @@ -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(), @@ -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(), @@ -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(), @@ -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(), @@ -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(), diff --git a/tests/snapshots/network_list__network_list.snap b/tests/snapshots/network_list__network_list.snap index 6fb6b0a..08a1841 100644 --- a/tests/snapshots/network_list__network_list.snap +++ b/tests/snapshots/network_list__network_list.snap @@ -5,7 +5,7 @@ expression: "&response.network_identifiers" [ NetworkIdentifier { blockchain: "mina", - network: "devnet", + network: "testnet", sub_network_identifier: None, }, ] diff --git a/tests/validate_network.rs b/tests/validate_network.rs index ce03e1e..5b72067 100644 --- a/tests/validate_network.rs +++ b/tests/validate_network.rs @@ -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<()> { @@ -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"); } @@ -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) +}