Skip to content

Commit

Permalink
chore(sequencer_node): add cli_args to test util + general cleanup
Browse files Browse the repository at this point in the history
commit-id:b608a7d6
  • Loading branch information
Itay-Tsabary-Starkware committed Nov 1, 2024
1 parent 51eea64 commit e01b741
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 36 deletions.
3 changes: 2 additions & 1 deletion crates/mempool_node/src/config/config_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ fn test_default_config_file_is_up_to_date() {
/// Tests parsing a node config without additional args.
#[test]
fn test_config_parsing() {
let args = create_test_config_load_args(&REQUIRED_PARAM_CONFIG_POINTERS);
let required_params = RequiredParams::create_for_testing();
let args = create_test_config_load_args(required_params);
let config = SequencerNodeConfig::load_and_process(args);
let config = config.expect("Parsing function failed.");

Expand Down
48 changes: 13 additions & 35 deletions crates/mempool_node/src/config/test_utils.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::vec::Vec; // Used by #[gen_field_names_fn].

use papyrus_config::{ParamPath, SerializationType, SerializedContent, SerializedParam};
use papyrus_proc_macros::gen_field_names_fn;
use starknet_api::core::ChainId;

use crate::config::node_command;

/// Required parameters utility struct.
#[gen_field_names_fn]
pub struct RequiredParams {
Expand All @@ -14,42 +15,19 @@ impl RequiredParams {
pub fn create_for_testing() -> Self {
Self { chain_id: ChainId::create_for_testing() }
}

pub fn cli_args(&self) -> Vec<String> {
let args = vec!["--chain_id".to_string(), self.chain_id.to_string()];
// Verify all arguments and their values are present.
assert!(args.len() == Self::field_names().len() * 2, "Missing required parameters.");
args
}
}

// TODO(Tsabary): Bundle required config values in a struct, detailing whether they are pointer
// targets or not. Then, derive their values in the config (struct and pointers).
// Also, add functionality to derive them for testing.
// Creates a vector of strings with the command name and required parameters that can be used as
// arguments to load a config.
#[cfg(any(feature = "testing", test))]
pub fn create_test_config_load_args(pointers: &Vec<(ParamPath, SerializedParam)>) -> Vec<String> {
use crate::config::node_command;

let mut dummy_values = Vec::new();

// Command name.
dummy_values.push(node_command().to_string());

// Iterate over required config parameters and add them as args with suitable arbitrary values.
for (target_param, serialized_pointer) in pointers {
// Param name.
let required_param_name_as_arg = format!("--{}", target_param);
dummy_values.push(required_param_name_as_arg);

// Param value.
let serialization_type = match &serialized_pointer.content {
SerializedContent::ParamType(serialization_type) => serialization_type,
_ => panic!("Required parameters have to be of type ParamType."),
};
let arbitrary_value = match serialization_type {
SerializationType::Boolean => "false",
SerializationType::Float => "15.2",
SerializationType::NegativeInteger => "-30",
SerializationType::PositiveInteger => "17",
SerializationType::String => "ArbitraryString",
}
.to_string();
dummy_values.push(arbitrary_value);
}
dummy_values
pub fn create_test_config_load_args(required_params: RequiredParams) -> Vec<String> {
let mut cli_args = vec![node_command().to_string()];
cli_args.extend(required_params.cli_args());
cli_args
}

0 comments on commit e01b741

Please sign in to comment.