def _test_get_referenced_test_data( self, filename: str, testsuite: str, testcase: str) -> dict: """ Call get_referenced_test_data and return results Args: filename (str): Name of YAML file to read testsuite (str): Name of testsuite to retrieve testcase (str): Name of testcase to retrieve Returns: Dict: test case definition that meet the provide parameters """ data_file = get_data_file( test_dir_name=self.TESTS_SUBDIR, data_dir_name=self.DATA_SUBDIR, filename=filename) yaml_obj = YamlInputFile(input_file=data_file) ref_yaml_obj = ReferentialYAML(yaml_input=yaml_obj) tc_data = ref_yaml_obj.get_referenced_test_data( yaml_input=ref_yaml_obj, testsuite=testsuite, testcase=testcase) return tc_data
def test_add_referenced_tc_to_ts_where_tc_is_new(self): tc_data = { YAMLConsts.DESCRIPTION: "Added test", YAMLConsts.STEPS: { 'STEP_A': {}, 'STEP_B': {} } } test_case_name = "NEW_TEST_CASE" data_file = get_data_file( test_dir_name=self.TESTS_SUBDIR, data_dir_name=self.DATA_SUBDIR, filename=self.SIMPLE_REF) yaml_obj = YamlInputFile(input_file=data_file) ref_yaml_obj = ReferentialYAML(yaml_input=yaml_obj) ref_yaml_obj.add_referenced_tc_to_ts( target_ts=self.TEST_SUITE, target_tc=test_case_name, tc_data=tc_data) updated_tc_yaml = ref_yaml_obj.data[0][self.TEST_SUITE][test_case_name] logging.info(f"Data added: {tc_data}") logging.info(f"Updated YAML Data: {updated_tc_yaml}") assert_equals(tc_data.get(YAMLConsts.STEPS, {}), updated_tc_yaml.get(YAMLConsts.STEPS, {}))
def _read_and_update_source_yaml_file( self, filename: str, testsuite: str, testcase: str, element: str, value: typing.Any, ref_yaml: ReferentialYAML = None) -> ReferentialYAML: """ Read the specified file, and update the corresponding testsuite/testcase element with the provided value. Args: filename (str): Name of the YAML file testsuite (str): Testsuite to update testcase (str): Testcase to update element (str): Testcase element (dict key) to update value (any): Value to update the testcase element ref_yaml (ReferentialYAML): Populated Referential Yaml File Obj Returns: Updated ReferentialYAML Object """ if ref_yaml is None: # Read YAML File data_file = get_data_file( test_dir_name=self.TESTS_SUBDIR, data_dir_name=self.DATA_SUBDIR, filename=filename) yaml_obj = YamlInputFile(input_file=data_file) else: yaml_obj = ref_yaml.yaml ref_yaml_obj = ref_yaml # Insert the MOD_STEP data to the YAML structure self._update_yaml( yaml=yaml_obj, test_suite=testsuite, test_case=testcase, element=element, value=value) # Build reference YAML object and add the data from # the primary reference if ref_yaml is None: ref_yaml_obj = ReferentialYAML(yaml_input=yaml_obj) assert_true(ref_yaml_obj.check_if_file_references_another_file()) ref_yaml_obj._add_referenced_paths() logging.info(f"Updated YAML:\n{pprint.pformat(ref_yaml_obj.data)}") return ref_yaml_obj
def _update_yaml( yaml: YamlInputFile, test_suite: str, test_case: str, element: str, value: typing.Any) -> None: """ Provided a listed YAML structure, the testsuite and testcase, the attribute name to update, and the value to update the attribute to, find the attribute and update the structure. Args: yaml (ReferentialYAML): YAML Structure test_suite (str): Test suite name test_case (str): Test case name element (str): Name of test case attribute to update value (Any): Value or structure to update attribute to Returns: None """ tc_data = ReferentialYAML.get_referenced_test_data( yaml_input=yaml, testsuite=test_suite, testcase=test_case) if element not in tc_data: logging.warn(f"Unable to find requested element: " f"'{element}' in yaml_file") logging.warn(f"Test case found: {', '.join(tc_data.keys())}") logging.warn(f"Adding attribute '{element}' to structure.") logging.info(f"Inserting value: {value}") else: logging.info(f"ELEMENT: {element}") logging.info(f"UPDATED VALUE: {value}") tc_data[element] = value logging.info(f"Updated YAML: {pprint.pformat(yaml)}")
def _get_testcase_ids( yaml_data: typing.Union[ReferentialYAML, YamlInputFile], test_suite: str, test_case: str) -> list: """ Get the list of step ids within test_suite:test_case structure (yaml data) Args: yaml_data (YamlInputFile): YAML data structure test_suite (str): Name of test suite in data test_case (str): Name of test case in data Returns: (list) List of ids """ # Get the test case definition tc_data = ReferentialYAML.get_referenced_test_data( yaml_input=yaml_data, testsuite=test_suite, testcase=test_case) logging.debug(f"TEST CASE DATA:\n{pprint.pformat(tc_data)}") # Get the step definitions from the test case definition steps = tc_data[YAMLConsts.STEPS] # Get the ids of the steps (maintained in order) step_ids = [str(list(x.values())[0][YAMLConsts.ID]) for x in steps] return step_ids
def _get_testcase_steps( yaml_data: YamlInputFile, test_suite: str, test_case: str) -> list: """ Get the list of steps within test_suite:test_case structure (yaml data) Args: yaml_data (YamlInputFile): YAML data structure test_suite (str): Name of test suite in data test_case (str): Name of test case in data Returns: (list) List of steps """ # Get the test case definition tc_data = ReferentialYAML.get_referenced_test_data( yaml_input=yaml_data, testsuite=test_suite, testcase=test_case) logging.debug(f"TEST CASE DATA:\n{pprint.pformat(tc_data)}") # Get the step definitions from the test case definition steps = tc_data.get(YAMLConsts.STEPS, {}) # Get the names of the steps (maintained in order) step_names = [list(x.keys())[0] for x in steps] return step_names
def __init__(self, input_file): super(StatePathsYaml, self).__init__(input_file) self.test_case = None # Check if YAML file is a referential file (points to another YAML # file. If so, build corresponding updated YAML structure. If it # is not a referential file, it will return the original YAML # structure built by the YamlInputFile instantiation. self.data = ReferentialYAML(self).evaluate_yaml_file()
def test_check_if_file_references_empty_string(self): data_file = get_data_file( test_dir_name=self.TESTS_SUBDIR, data_dir_name=self.DATA_SUBDIR, filename=self.SIMPLE_REF) yaml_obj = YamlInputFile(input_file=data_file) self._update_yaml(yaml=yaml_obj, test_suite=self.TEST_SUITE, test_case=self.TEST_CASE, element=SMConsts.REFERENCE, value='') ref_yaml_obj = ReferentialYAML(yaml_input=yaml_obj) # Verify the reference is found but fails due to being unable to parse ref_yaml_obj.check_if_file_references_another_file()
def _test_modify_steps( self, filename: str, testsuite: str, testcase: str, updated_step_data: list) -> typing.Tuple[dict, dict]: """ Get the ReferentialYAML file, and update the specified testsuite & testcase with the specified modifications Args: filename (str): Name of the YAML file to read testsuite (str): Test suite to update testcase (str): Test case to update updated_step_data (List): List of dictionaries of updated step data Returns: Tuple of 2 dictionaries (expected & actual test case definitions) """ element = YAMLConsts.MOD_STEPS ref_yaml_obj = self._read_and_update_source_yaml_file( filename=filename, testsuite=testsuite, testcase=testcase, element=element, value=updated_step_data) # EXPECTED: Get the original step definition, make a copy, and combine # the updated data for the step orig_tc_data = ReferentialYAML.get_referenced_test_data( yaml_input=ref_yaml_obj, testsuite=self.TEST_SUITE, testcase=self.TEST_CASE) orig_tc_data = copy.deepcopy(orig_tc_data) expected_tc = self._combine_steps( original=orig_tc_data, modifications=updated_step_data[0]) logging.info(f"Expected TC YAML:\n{pprint.pformat(expected_tc)}") # MODIFY the requested steps via the actual source code ref_yaml_obj.modify_steps() # ACTUAL: Get the actual testcase actual_tc = ReferentialYAML.get_referenced_test_data( yaml_input=ref_yaml_obj, testsuite=self.TEST_SUITE, testcase=self.TEST_CASE) logging.info(f"Actual TC YAML:\n{pprint.pformat(actual_tc)}") return expected_tc, actual_tc
def test_check_if_file_references_another_file_invalid_reference(self): data_file = get_data_file( test_dir_name=self.TESTS_SUBDIR, data_dir_name=self.DATA_SUBDIR, filename=self.SIMPLE_REF) yaml_obj = YamlInputFile(input_file=data_file) illegal_value = (f"{self.BASE_REF_FILE}{ReferentialYAML.DELIMITER}" f"{self.TEST_SUITE}{ReferentialYAML.DELIMITER}") self._update_yaml(yaml=yaml_obj, test_suite=self.TEST_SUITE, test_case=self.TEST_CASE, element=SMConsts.REFERENCE, value=illegal_value) ref_yaml_obj = ReferentialYAML(yaml_input=yaml_obj) # Verify the reference is found but fails due to being unable to parse ref_yaml_obj.check_if_file_references_another_file()
def test_check_if_file_references_another_valid_file_and_validate_ref(self): expected_ref_obj = ReferentialYAML.REFERENCE_DATA( target_test_suite=self.TEST_SUITE, target_test_case=self.TEST_CASE, reference_file=self.BASE_REF_FILE, reference_test_suite=self.TEST_SUITE, reference_test_case=self.TEST_CASE) data_file = get_data_file( test_dir_name=self.TESTS_SUBDIR, data_dir_name=self.DATA_SUBDIR, filename=self.SIMPLE_REF) yaml_obj = YamlInputFile(input_file=data_file) ref_yaml_obj = ReferentialYAML(yaml_input=yaml_obj) # Verify the reference is found assert_true(ref_yaml_obj.check_if_file_references_another_file()) # Verify the reference was parsed correctly for ref_obj in ref_yaml_obj.references: self._validate_reference_data(expected_ref_obj, ref_obj)
def test_evaluate_yaml_file(self): filename = self.SIMPLE_REF testsuite = self.TEST_SUITE testcase = self.TEST_CASE element = YAMLConsts.DEL_STEPS added_step_name_1 = 'STEP_1A' insert_before_id_1 = '2' added_test_case_id_1 = 'ADDED_1' added_step_name_2 = 'STEP_2A' insert_after_id_2 = 'ADDED_1' added_test_case_id_2 = 'ADDED_2' add_tc_data = [ { added_step_name_1: { YAMLConsts.BEFORE_ID: insert_before_id_1, YAMLConsts.ID: added_test_case_id_1, YAMLConsts.DATA: None, YAMLConsts.EXPECTATIONS: {'test_me': False} } }, { added_step_name_2: { YAMLConsts.AFTER_ID: insert_after_id_2, YAMLConsts.ID: added_test_case_id_2, YAMLConsts.DATA: None, YAMLConsts.EXPECTATIONS: {'test_me': False} } } ] modify_tc_data = [ { "STEP_1": { YAMLConsts.ID: 'DNE', YAMLConsts.DATA: {'params': 1, 'value': 7}, YAMLConsts.EXPECTATIONS: { "expectations_10": False, "expectations_20": True, } } } ] delete_step_ids = ['3', '4'] data_file = get_data_file( test_dir_name=self.TESTS_SUBDIR, data_dir_name=self.DATA_SUBDIR, filename=filename) yaml_obj = YamlInputFile(input_file=data_file) test_yaml_obj = ReferentialYAML(yaml_input=yaml_obj) test_yaml_obj.evaluate_yaml_file() ref_yaml_obj = self._read_and_update_source_yaml_file( filename=filename, testsuite=testsuite, testcase=testcase, element=YAMLConsts.ADD_STEPS, value=add_tc_data) ref_yaml_obj = self._read_and_update_source_yaml_file( filename=filename, testsuite=testsuite, testcase=testcase, element=YAMLConsts.MOD_STEPS, value=modify_tc_data, ref_yaml=ref_yaml_obj) ref_yaml_obj = self._read_and_update_source_yaml_file( filename=filename, testsuite=testsuite, testcase=testcase, element=YAMLConsts.DEL_STEPS, value=delete_step_ids, ref_yaml=ref_yaml_obj) ref_ts = list(ref_yaml_obj.data[0].values())[0] ref_tc = list(ref_ts.values())[0] del ref_tc[YAMLConsts.ADD_STEPS] del ref_tc[YAMLConsts.MOD_STEPS] del ref_tc[YAMLConsts.DEL_STEPS] assert_equals.__self__.maxDiff = None assert_equals(test_yaml_obj.data, ref_yaml_obj.data)