From d1d6bacedb64a97597d1752e91d2ca23ec8e6f1b Mon Sep 17 00:00:00 2001 From: Joshua Oladele Date: Tue, 17 Oct 2023 11:11:46 +0100 Subject: [PATCH] Fix contract state delete test There seems to be a race condition for the contract state migrations in tests. The commit attempts to fix this by setting up all migrations for the test suite exactly once. --- chaindexing-tests/src/lib.rs | 2 +- chaindexing-tests/src/main.rs | 4 ++- chaindexing-tests/src/tests.rs | 4 +++ .../src/tests/contract_states.rs | 30 +++++++------------ 4 files changed, 18 insertions(+), 22 deletions(-) diff --git a/chaindexing-tests/src/lib.rs b/chaindexing-tests/src/lib.rs index 763249d..99fa282 100644 --- a/chaindexing-tests/src/lib.rs +++ b/chaindexing-tests/src/lib.rs @@ -1,4 +1,4 @@ pub mod db; pub mod factory; pub mod test_runner; -mod tests; +pub mod tests; diff --git a/chaindexing-tests/src/main.rs b/chaindexing-tests/src/main.rs index 734575d..c05e946 100644 --- a/chaindexing-tests/src/main.rs +++ b/chaindexing-tests/src/main.rs @@ -1,5 +1,5 @@ use chaindexing::{Chaindexing, ChaindexingRepo, HasRawQueryClient, Repo}; -use chaindexing_tests::db; +use chaindexing_tests::{db, tests}; #[tokio::main] async fn main() { @@ -7,4 +7,6 @@ async fn main() { let repo = ChaindexingRepo::new(db::database_url().as_str()); let raw_query_client = repo.get_raw_query_client().await; Chaindexing::run_internal_migrations(&raw_query_client).await; + + tests::setup().await; } diff --git a/chaindexing-tests/src/tests.rs b/chaindexing-tests/src/tests.rs index ecc9310..0d2218b 100644 --- a/chaindexing-tests/src/tests.rs +++ b/chaindexing-tests/src/tests.rs @@ -1,2 +1,6 @@ mod contract_states; mod events_ingester; + +pub async fn setup() { + contract_states::setup().await; +} diff --git a/chaindexing-tests/src/tests/contract_states.rs b/chaindexing-tests/src/tests/contract_states.rs index 7bc79d3..e67c7b2 100644 --- a/chaindexing-tests/src/tests/contract_states.rs +++ b/chaindexing-tests/src/tests/contract_states.rs @@ -1,6 +1,6 @@ #[cfg(test)] mod tests { - use chaindexing::{Chaindexing, ChaindexingRepo, EventContext, HasRawQueryClient}; + use chaindexing::{ChaindexingRepo, EventContext, HasRawQueryClient}; use super::*; use crate::factory::{bayc_contract, transfer_event_with_contract}; @@ -9,13 +9,7 @@ mod tests { #[tokio::test] pub async fn creates_state() { let bayc_contract = bayc_contract().add_state_migrations(NftStateMigrations); - let mut raw_query_client = test_runner::new_repo().get_raw_query_client().await; - Chaindexing::run_migrations_for_contract_states( - &raw_query_client, - &vec![bayc_contract.clone()], - ) - .await; let raw_query_txn_client = ChaindexingRepo::get_raw_query_txn_client(&mut raw_query_client).await; let event_context = EventContext::new( @@ -40,13 +34,7 @@ mod tests { #[tokio::test] pub async fn updates_state() { let bayc_contract = bayc_contract().add_state_migrations(NftStateMigrations); - let mut raw_query_client = test_runner::new_repo().get_raw_query_client().await; - Chaindexing::run_migrations_for_contract_states( - &raw_query_client, - &vec![bayc_contract.clone()], - ) - .await; let raw_query_txn_client = ChaindexingRepo::get_raw_query_txn_client(&mut raw_query_client).await; let event_context = EventContext::new( @@ -77,13 +65,7 @@ mod tests { #[tokio::test] pub async fn deletes_state() { let bayc_contract = bayc_contract().add_state_migrations(NftStateMigrations); - let mut raw_query_client = test_runner::new_repo().get_raw_query_client().await; - Chaindexing::run_migrations_for_contract_states( - &raw_query_client, - &vec![bayc_contract.clone()], - ) - .await; let raw_query_txn_client = ChaindexingRepo::get_raw_query_txn_client(&mut raw_query_client).await; let event_context = EventContext::new( @@ -104,9 +86,11 @@ mod tests { } } -use chaindexing::{ContractState, ContractStateMigrations}; +use chaindexing::{Chaindexing, ContractState, ContractStateMigrations, HasRawQueryClient}; use serde::{Deserialize, Serialize}; +use crate::{factory::bayc_contract, test_runner}; + #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] struct NftState { token_id: i32, @@ -126,3 +110,9 @@ impl ContractStateMigrations for NftStateMigrations { ] } } + +pub async fn setup() { + let bayc_contract = bayc_contract().add_state_migrations(NftStateMigrations); + let raw_query_client = test_runner::new_repo().get_raw_query_client().await; + Chaindexing::run_migrations_for_contract_states(&raw_query_client, &vec![bayc_contract]).await; +}