Skip to content

Commit

Permalink
test(starknet_gateway): move mock dependancies to test utils
Browse files Browse the repository at this point in the history
  • Loading branch information
ArniStarkware committed Dec 25, 2024
1 parent a7f0ee7 commit 63a13ec
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 27 deletions.
27 changes: 1 addition & 26 deletions crates/starknet_gateway/src/gateway_test.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
30 changes: 30 additions & 0 deletions crates/starknet_gateway/src/test_utils.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) };
Expand Down Expand Up @@ -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(()));
}
}

0 comments on commit 63a13ec

Please sign in to comment.