Skip to content

Commit

Permalink
better result and IO assertions in CLI tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AmbientTea committed Sep 20, 2024
1 parent 3ce6a8c commit a828579
Show file tree
Hide file tree
Showing 13 changed files with 118 additions and 133 deletions.
21 changes: 8 additions & 13 deletions partner-chains-cli/src/create_chain_spec/tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::config::CHAIN_CONFIG_FILE_PATH;
use crate::create_chain_spec::{CreateChainSpecCmd, INITIAL_PERMISSIONED_CANDIDATES_EXAMPLE};
use crate::tests::{MockIO, MockIOContext};
use crate::tests::{should_be_failure, should_be_success, MockIO, MockIOContext};
use crate::{config, CmdRun};
use anyhow::anyhow;
use colored::Colorize;
Expand All @@ -23,8 +23,7 @@ fn happy_path() {
show_outro(),
]);
let result = CreateChainSpecCmd.run(&mock_context);
mock_context.no_more_io_expected();
assert!(result.is_ok());
should_be_success!(result, mock_context)
}

#[test]
Expand Down Expand Up @@ -57,8 +56,7 @@ If you are the governance authority, please make sure you have run the `prepare-
If you are a validator, you can obtain the chain configuration file from the governance authority."),
]);
let result = CreateChainSpecCmd.run(&mock_context);
mock_context.no_more_io_expected();
assert!(result.is_err());
should_be_failure!(result, mock_context);
}

#[test]
Expand All @@ -72,8 +70,7 @@ If you are the governance authority, please make sure you have run the `prepare-
If you are a validator, you can obtain the chain configuration file from the governance authority."),
]);
let result = CreateChainSpecCmd.run(&mock_context);
mock_context.no_more_io_expected();
assert!(result.is_err());
should_be_failure!(result, mock_context);
}

#[test]
Expand All @@ -90,10 +87,9 @@ fn errors_if_chain_spec_is_missing() {
read_chain_spec_io(),
]);
let result = CreateChainSpecCmd.run(&mock_context);
mock_context.no_more_io_expected();
assert!(result.is_err());
let err = should_be_failure!(result, mock_context);
assert_eq!(
result.err().unwrap().to_string(),
err.to_string(),
"Could not read chain-spec.json file. File is expected to exists.".to_string()
);
}
Expand All @@ -113,9 +109,8 @@ fn forwards_build_spec_error_if_it_fails() {
run_build_spec_io(Err(error)),
]);
let result = CreateChainSpecCmd.run(&mock_context);
mock_context.no_more_io_expected();
assert!(result.is_err());
assert_eq!(result.err().unwrap().to_string(), "Failed miserably".to_string())
let err = should_be_failure!(result, mock_context);
assert_eq!(err.to_string(), "Failed miserably".to_string())
}

fn test_config_content() -> serde_json::Value {
Expand Down
16 changes: 5 additions & 11 deletions partner-chains-cli/src/generate_keys/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,7 @@ fn happy_path() {

let result = GenerateKeysCmd {}.run(&mock_context);

mock_context.no_more_io_expected();

assert!(result.is_ok());
should_be_success!(result, mock_context);
}

mod config_read {
Expand All @@ -199,7 +197,7 @@ mod config_read {

let result = GenerateKeysConfig::load(&context);

context.no_more_io_expected();
should_have_no_io_left!(context);

assert_eq!(result.chain_name, CHAIN_NAME);
assert_eq!(result.node_executable, EXECUTABLE_PATH);
Expand Down Expand Up @@ -227,7 +225,7 @@ mod config_read {

let result = GenerateKeysConfig::load(&context);

context.no_more_io_expected();
should_have_no_io_left!(context);

assert_eq!(result.chain_name, CHAIN_NAME);
assert_eq!(result.node_executable, EXECUTABLE_PATH);
Expand Down Expand Up @@ -301,9 +299,7 @@ mod generate_spo_keys {

let result = generate_spo_keys(&default_config(), &mock_context);

mock_context.no_more_io_expected();

assert!(result.is_ok());
should_be_success!(result, mock_context);
}

#[test]
Expand All @@ -328,9 +324,7 @@ mod generate_spo_keys {

let result = generate_spo_keys(&default_config(), &mock_context);

mock_context.no_more_io_expected();

assert!(result.is_ok());
should_be_success!(result, mock_context);
}
}

Expand Down
35 changes: 10 additions & 25 deletions partner-chains-cli/src/prepare_configuration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ pub mod tests {
use crate::config::{ConfigFieldDefinition, SelectOptions, RESOURCES_CONFIG_FILE_PATH};
use crate::prepare_configuration::PrepareConfigurationError::NetworkKeyNotFoundError;
use crate::prepare_configuration::Protocol::{Dns, Ipv4};
use crate::tests::{MockIO, MockIOContext};
use crate::tests::{should_be_failure, should_be_success, MockIO, MockIOContext};

const KEY: &str = "962515971a22aa95706c2109ba6e9502c7f39b33bdf63024f46f77894424f1fe";
pub const CHAIN_NAME: &str = "partner_chains_template";
Expand Down Expand Up @@ -327,9 +327,7 @@ pub mod tests {

let result = establish_bootnodes(&mock_context);

mock_context.no_more_io_expected();

assert!(result.is_ok());
should_be_success!(result, mock_context);
}

#[test]
Expand All @@ -344,9 +342,7 @@ pub mod tests {

let result = establish_bootnodes(&mock_context);

mock_context.no_more_io_expected();

assert!(result.is_ok());
should_be_success!(result, mock_context);
}

#[test]
Expand Down Expand Up @@ -374,9 +370,7 @@ pub mod tests {

let result = establish_bootnodes(&mock_context);

mock_context.no_more_io_expected();

assert!(result.is_ok());
should_be_success!(result, mock_context);
}

#[test]
Expand Down Expand Up @@ -404,9 +398,7 @@ pub mod tests {

let result = establish_bootnodes(&mock_context);

mock_context.no_more_io_expected();

assert!(result.is_ok());
should_be_success!(result, mock_context);
}

#[test]
Expand All @@ -417,13 +409,9 @@ pub mod tests {

let result = PrepareConfigurationCmd {}.run(&mock_context);

mock_context.no_more_io_expected();
let error = should_be_failure!(result, mock_context);

assert!(result.is_err());
assert_eq!(
result.err().unwrap().to_string(),
NetworkKeyNotFoundError(network_key_file()).to_string()
);
assert_eq!(error.to_string(), NetworkKeyNotFoundError(network_key_file()).to_string());
}

#[test]
Expand All @@ -445,8 +433,7 @@ pub mod tests {

let result = establish_bootnodes(&mock_context);

mock_context.no_more_io_expected();
assert!(result.is_ok());
should_be_success!(result, mock_context);
}

#[test]
Expand All @@ -464,10 +451,8 @@ pub mod tests {

let result = PrepareConfigurationCmd {}.run(&mock_context);

mock_context.no_more_io_expected();

assert!(result.is_err());
assert!(result.err().unwrap().to_string().contains("⚠️ Invalid IP address"));
let error = should_be_failure!(result, mock_context);
assert!(error.to_string().contains("⚠️ Invalid IP address"));
}

pub fn save_to_existing_file<T>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub mod tests {
use crate::config::config_fields::CARDANO_SECURITY_PARAMETER;
use crate::prepare_configuration::prepare_cardano_params::prepare_cardano_params;
use crate::prepare_configuration::prepare_cardano_params::tests::scenarios::save_cardano_params;
use crate::tests::MockIOContext;
use crate::tests::{should_be_success, MockIOContext};
use serde_json::Value;

const CUSTOM_CARDANO_PARAMS: CardanoParameters = CardanoParameters {
Expand Down Expand Up @@ -203,8 +203,8 @@ pub mod tests {
let mock_context = MockIOContext::new()
.with_json_file(CARDANO_SECURITY_PARAMETER.config_file, serde_json::json!({}))
.with_expected_io(vec![save_cardano_params(cardano_parameters.clone())]);
let params = prepare_cardano_params(&mock_context, cardano_network).unwrap();
mock_context.no_more_io_expected();
let result = prepare_cardano_params(&mock_context, cardano_network);
let params = should_be_success!(result, mock_context);
assert_eq!(params, cardano_parameters)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ mod tests {
prompt_and_save_to_existing_file, prompt_with_default_and_save_to_existing_file,
save_to_existing_file, save_to_new_file, CHAIN_CONFIG_PATH,
};
use crate::tests::should_be_success;
use crate::tests::{MockIO, MockIOContext};
use serde_json::Value;
use sidechain_domain::{MainchainAddressHash, UtxoId};
Expand Down Expand Up @@ -206,8 +207,7 @@ mod tests {

let result = prepare_chain_params(&mock_context);

assert!(result.is_ok());
mock_context.no_more_io_expected();
should_be_success!(result, mock_context);
}

#[test]
Expand Down Expand Up @@ -239,9 +239,7 @@ mod tests {

let result = prepare_chain_params(&mock_context);

mock_context.no_more_io_expected();

assert!(result.is_ok());
should_be_success!(result, mock_context);
}

#[test]
Expand Down Expand Up @@ -277,9 +275,7 @@ mod tests {

let result = prepare_chain_params(&mock_context);

mock_context.no_more_io_expected();

assert!(result.is_ok());
should_be_success!(result, mock_context);
}

#[test]
Expand Down Expand Up @@ -312,8 +308,7 @@ mod tests {

let result = prepare_chain_params(&mock_context);

mock_context.no_more_io_expected();
assert!(result.is_ok());
should_be_success!(result, mock_context);
}

#[test]
Expand Down Expand Up @@ -367,8 +362,7 @@ mod tests {

let result = prepare_chain_params(&mock_context);

mock_context.no_more_io_expected();
assert!(result.is_ok());
should_be_success!(result, mock_context);
}

fn test_chain_config() -> Value {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ use crate::config::{
get_cardano_network_from_file, CardanoNetwork, SidechainParams, PC_CONTRACTS_CLI_PATH,
};
use crate::io::IOContext;
use crate::prepare_configuration::prepare_cardano_params::prepare_cardano_params;
use crate::pc_contracts_cli_resources::{
establish_pc_contracts_cli_configuration, PcContractsCliResources,
};
use crate::prepare_configuration::prepare_cardano_params::prepare_cardano_params;
use crate::smart_contracts;
use anyhow::anyhow;
use serde_json::Value;
Expand Down Expand Up @@ -197,8 +197,8 @@ mod tests {
use crate::config::CHAIN_CONFIG_FILE_PATH;
use crate::prepare_configuration::prepare_cardano_params::PREPROD_CARDANO_PARAMS;
use crate::prepare_configuration::tests::save_to_existing_file;
use crate::tests::MockIO;
use crate::tests::MockIOContext;
use crate::tests::{should_be_success, MockIO};
use serde_json::Value;
use sidechain_domain::{MainchainAddressHash, UtxoId};
use std::str::FromStr;
Expand Down Expand Up @@ -289,8 +289,10 @@ mod tests {
),
MockIO::eprint(OUTRO),
]);
prepare_main_chain_config(&mock_context, test_sidechain_params()).unwrap();
mock_context.no_more_io_expected();
should_be_success!(
prepare_main_chain_config(&mock_context, test_sidechain_params()),
mock_context
);
}

#[test]
Expand Down Expand Up @@ -353,8 +355,10 @@ mod tests {
MockIO::file_read(INITIAL_PERMISSIONED_CANDIDATES.config_file),
MockIO::eprint(OUTRO),
]);
prepare_main_chain_config(&mock_context, test_sidechain_params()).unwrap();
mock_context.no_more_io_expected();
should_be_success!(
prepare_main_chain_config(&mock_context, test_sidechain_params()),
mock_context
);
}

#[test]
Expand Down
14 changes: 5 additions & 9 deletions partner-chains-cli/src/register/register1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,8 @@ fn parse_utxo_query_output(utxo_query_output: &UtxoQueryOutput) -> Vec<ValidUtxo
mod tests {
use super::*;
use crate::config::config_fields::{CARDANO_CLI, CARDANO_PAYMENT_VERIFICATION_KEY_FILE};
use crate::tests::{MockIO, MockIOContext};
use crate::tests::should_be_success;
use crate::tests::{should_be_failure, MockIO, MockIOContext};

#[test]
fn happy_path() {
Expand Down Expand Up @@ -316,8 +317,7 @@ mod tests {
);

let result = Register1Cmd {}.run(&mock_context);
mock_context.no_more_io_expected();
assert!(result.is_ok());
should_be_success!(result, mock_context);
}

#[test]
Expand All @@ -330,9 +330,7 @@ mod tests {
);

let result = Register1Cmd {}.run(&mock_context);
mock_context.no_more_io_expected();

assert!(result.is_err());
should_be_failure!(result, mock_context);
}

#[test]
Expand All @@ -351,9 +349,7 @@ mod tests {
);

let result = Register1Cmd {}.run(&mock_context);
mock_context.no_more_io_expected();

assert!(result.is_err());
should_be_failure!(result, mock_context);
}

#[test]
Expand Down
Loading

0 comments on commit a828579

Please sign in to comment.