From 63a13ec45b0764beaa703887998cf7e5d8baed29 Mon Sep 17 00:00:00 2001 From: Arni Hod Date: Tue, 24 Dec 2024 14:17:45 +0200 Subject: [PATCH] test(starknet_gateway): move mock dependancies to test utils --- crates/starknet_gateway/src/gateway_test.rs | 27 +---------------- .../stateful_transaction_validator_test.rs | 2 +- crates/starknet_gateway/src/test_utils.rs | 30 +++++++++++++++++++ 3 files changed, 32 insertions(+), 27 deletions(-) diff --git a/crates/starknet_gateway/src/gateway_test.rs b/crates/starknet_gateway/src/gateway_test.rs index e8502f43ae..e4a95dcd1e 100644 --- a/crates/starknet_gateway/src/gateway_test.rs +++ b/crates/starknet_gateway/src/gateway_test.rs @@ -1,10 +1,7 @@ -use std::sync::Arc; - use assert_matches::assert_matches; use blockifier::context::ChainInfo; use blockifier::test_utils::{CairoVersion, RunnableCairo1}; use mempool_test_utils::starknet_api_test_utils::{declare_tx, invoke_tx}; -use mockall::predicate::eq; use papyrus_network_types::network_types::BroadcastedMessageMetadata; use papyrus_test_utils::{get_rng, GetTestInstance}; use rstest::{fixture, rstest}; @@ -22,8 +19,8 @@ use crate::config::{ StatefulTransactionValidatorConfig, StatelessTransactionValidatorConfig, }; -use crate::gateway::Gateway; use crate::state_reader_test_utils::{local_test_state_reader_factory, TestStateReaderFactory}; +use crate::test_utils::MockDependencies; #[fixture] fn config() -> GatewayConfig { @@ -54,28 +51,6 @@ fn mock_dependencies( MockDependencies { config, compiler, state_reader_factory, mock_mempool_client } } -struct MockDependencies { - config: GatewayConfig, - compiler: GatewayCompiler, - state_reader_factory: TestStateReaderFactory, - mock_mempool_client: MockMempoolClient, -} - -impl MockDependencies { - fn gateway(self) -> Gateway { - Gateway::new( - self.config, - Arc::new(self.state_reader_factory), - self.compiler, - Arc::new(self.mock_mempool_client), - ) - } - - fn expect_add_tx(&mut self, args: AddTransactionArgsWrapper) { - self.mock_mempool_client.expect_add_tx().once().with(eq(args)).return_once(|_| Ok(())); - } -} - type SenderAddress = ContractAddress; fn create_tx() -> (RpcTransaction, SenderAddress) { diff --git a/crates/starknet_gateway/src/stateful_transaction_validator_test.rs b/crates/starknet_gateway/src/stateful_transaction_validator_test.rs index de734b71cd..366ccd4431 100644 --- a/crates/starknet_gateway/src/stateful_transaction_validator_test.rs +++ b/crates/starknet_gateway/src/stateful_transaction_validator_test.rs @@ -88,7 +88,7 @@ fn test_instantiate_validator(stateful_validator: StatefulTransactionValidator) let mut mock_state_reader_factory = MockStateReaderFactory::new(); - // Make sure stateful_validator uses the latest block in the initiall call. + // Make sure stateful_validator uses the latest block in the initial call. let latest_state_reader = state_reader_factory.get_state_reader_from_latest_block(); mock_state_reader_factory .expect_get_state_reader_from_latest_block() diff --git a/crates/starknet_gateway/src/test_utils.rs b/crates/starknet_gateway/src/test_utils.rs index f92bb5bce8..645ef336b1 100644 --- a/crates/starknet_gateway/src/test_utils.rs +++ b/crates/starknet_gateway/src/test_utils.rs @@ -1,3 +1,6 @@ +use std::sync::Arc; + +use mockall::predicate::eq; use starknet_api::block::GasPrice; use starknet_api::core::ContractAddress; use starknet_api::data_availability::DataAvailabilityMode; @@ -17,9 +20,14 @@ use starknet_api::transaction::fields::{ ValidResourceBounds, }; use starknet_api::{declare_tx_args, deploy_account_tx_args, felt, invoke_tx_args}; +use starknet_mempool_types::communication::{AddTransactionArgsWrapper, MockMempoolClient}; use starknet_types_core::felt::Felt; +use crate::compilation::GatewayCompiler; use crate::compiler_version::VersionId; +use crate::config::GatewayConfig; +use crate::gateway::Gateway; +use crate::state_reader_test_utils::TestStateReaderFactory; pub const NON_EMPTY_RESOURCE_BOUNDS: ResourceBounds = ResourceBounds { max_amount: GasAmount(1), max_price_per_unit: GasPrice(1) }; @@ -136,3 +144,25 @@ pub fn rpc_tx_for_testing( )), } } + +pub(crate) struct MockDependencies { + pub config: GatewayConfig, + pub compiler: GatewayCompiler, + pub state_reader_factory: TestStateReaderFactory, + pub mock_mempool_client: MockMempoolClient, +} + +impl MockDependencies { + pub fn gateway(self) -> Gateway { + Gateway::new( + self.config, + Arc::new(self.state_reader_factory), + self.compiler, + Arc::new(self.mock_mempool_client), + ) + } + + pub fn expect_add_tx(&mut self, args: AddTransactionArgsWrapper) { + self.mock_mempool_client.expect_add_tx().once().with(eq(args)).return_once(|_| Ok(())); + } +}