Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(starknet_gateway): move mock dependancies to test utils #2940

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(()));
}
}