From dcbbdba65ab7f69c21eb6f3dd37b3f40d514c34e Mon Sep 17 00:00:00 2001 From: jpike Date: Mon, 3 Mar 2025 14:48:18 -0500 Subject: [PATCH] Adding validate_str_contains adding new validation method Change-Id: I86a5464b259e9e8bb8533d9eefe48e16bf3619ed --- framework/validation/validation.py | 80 +++++++++++++------ .../framework/validation/validaton_test.py | 27 +++++++ 2 files changed, 81 insertions(+), 26 deletions(-) create mode 100644 unit_tests/framework/validation/validaton_test.py diff --git a/framework/validation/validation.py b/framework/validation/validation.py index be615f11..c3ef478b 100644 --- a/framework/validation/validation.py +++ b/framework/validation/validation.py @@ -10,14 +10,16 @@ def validate_equals(observed_value: Any, expected_value: Any, validation_descrip This function will validate if the observed value matches the expected value with associated logging. Args: - observed_value: Value that we see on the system. - expected_value: Value that is expected and against which we are asserting. - validation_description: Description of this validation for logging purposes. + observed_value (Any): Value that we see on the system. + expected_value (Any): Value that is expected and against which we are asserting. + validation_description (str): Description of this validation for logging purposes. Returns: None - """ + Raises: + Exception: raised when validate fails + """ if observed_value == expected_value: get_logger().log_info(f"Validation Successful - {validation_description}") else: @@ -27,23 +29,27 @@ def validate_equals(observed_value: Any, expected_value: Any, validation_descrip raise Exception("Validation Failed") -def validate_equals_with_retry(function_to_execute: Callable[[], Any], - expected_value: Any, - validation_description: str, - timeout: int = 30, - polling_sleep_time: int = 5,) -> None: +def validate_equals_with_retry( + function_to_execute: Callable[[], Any], + expected_value: Any, + validation_description: str, + timeout: int = 30, + polling_sleep_time: int = 5, +) -> None: """ - Validates that function_to_execute will return the expected value in the specified amount of time. + Validates that function_to_execute will return the expected value in the specified amount of time. - Args: - function_to_execute: The function to be executed repeatedly, taking no arguments and returning any value. - expected_value: The expected return value of the function. - validation_description: Description of this validation for logging purposes. - timeout: The maximum time (in seconds) to wait for the match. - polling_sleep_time: The interval of time to wait between calls to function_to_execute. + Args: + function_to_execute (Callable[[], Any]): The function to be executed repeatedly, taking no arguments and returning any value. + expected_value (Any): The expected return value of the function. + validation_description (str): Description of this validation for logging purposes. + timeout (int): The maximum time (in seconds) to wait for the match. + polling_sleep_time (int): The interval of time to wait between calls to function_to_execute. + + Raises: + TimeoutError: raised when validate does not equal in the required time """ - get_logger().log_info(f"Attempting Validation - {validation_description}") end_time = time.time() + timeout @@ -69,22 +75,44 @@ def validate_equals_with_retry(function_to_execute: Callable[[], Any], raise TimeoutError(f"Timeout performing validation - {validation_description}") -def validate_list_contains(observed_value: Any, expected_values: Any, validation_description: str) -> None: +def validate_str_contains(observed_value: str, expected_value: str, validation_description: str) -> None: """ - This function validates if the observed value matches ANY of the expected values - with associated logging. + This function will validate if the observed value contains the expected value. Args: - observed_value: Value that we see on the system. - expected_values: A LIST of values that are expected. The observed value - is checked against each of these. - validation_description: Description of this validation for logging purposes. + observed_value(str): Value that we see on the system. + expected_value(str): the value we are expecting to see in the observed value str. + validation_description (str): Description of this validation for logging purposes. Returns: None - Raises: Exception if the validation fails. - """ + Raises: + Exception: when validate fails + """ + if expected_value in observed_value: + get_logger().log_info(f"Validation Successful - {validation_description}") + else: + get_logger().log_error(f"Validation Failed - {validation_description}") + get_logger().log_error(f"Expected: {expected_value}") + get_logger().log_error(f"Observed: {observed_value}") + raise Exception("Validation Failed") + + +def validate_list_contains(observed_value: Any, expected_values: Any, validation_description: str) -> None: + """ + This function validates if the observed value matches ANY of the expected values with associated logging. + + Args: + observed_value (Any): Value that we see on the system. + expected_values (Any): A LIST of values that are expected. The observed value is checked against each of these. + validation_description (str): Description of this validation for logging purposes. + + Returns: None + + Raises: + Exception: if the validation fails. + """ if observed_value in expected_values: get_logger().log_info(f"Validation Successful - {validation_description}") else: diff --git a/unit_tests/framework/validation/validaton_test.py b/unit_tests/framework/validation/validaton_test.py new file mode 100644 index 00000000..f0f8f45e --- /dev/null +++ b/unit_tests/framework/validation/validaton_test.py @@ -0,0 +1,27 @@ +from config.configuration_file_locations_manager import ( + ConfigurationFileLocationsManager, +) +from config.configuration_manager import ConfigurationManager +from framework.validation.validation import validate_str_contains + + +def test_validate_str_contains(): + """ + Validates function validate_str_contains + """ + configuration_locations_manager = ConfigurationFileLocationsManager() + ConfigurationManager.load_configs(configuration_locations_manager) + validate_str_contains("observed value contains: success", "success", "Test that the word success appears") + + +def test_validate_str_contains_fails(): + """ + Validates function validate_str_contains fails when expected + """ + configuration_locations_manager = ConfigurationFileLocationsManager() + ConfigurationManager.load_configs(configuration_locations_manager) + try: + validate_str_contains("observed value contains: ", "success", "Test that the word success appears") + assert False, "Validation passed when it should not have" # if test succeeds, we should never get to this line + except Exception as e: + assert e.__str__() == "Validation Failed", "Validation failed as expected."