From 136b712cfaf0392ae5673a6103700014d27b2866 Mon Sep 17 00:00:00 2001 From: AleJo2995 Date: Fri, 5 Jan 2024 08:04:31 -0600 Subject: [PATCH] fix: correct empty values going in assembled profile (#1491) * fix: correct empty values going in assembled profile Signed-off-by: Alejandro Jose Leiva Palomo * fix: add test case to check profile values replaced Signed-off-by: Alejandro Jose Leiva Palomo --------- Signed-off-by: Alejandro Jose Leiva Palomo --- .../core/commands/author/profile_test.py | 60 ++++++++++++++++--- trestle/core/catalog/catalog_reader.py | 1 - 2 files changed, 52 insertions(+), 9 deletions(-) diff --git a/tests/trestle/core/commands/author/profile_test.py b/tests/trestle/core/commands/author/profile_test.py index 32edf0c8c..dc0c63ecb 100644 --- a/tests/trestle/core/commands/author/profile_test.py +++ b/tests/trestle/core/commands/author/profile_test.py @@ -299,7 +299,7 @@ def test_profile_generate_assemble( if set_parameters_flag: assert set_params[2].values[0] == 'new value' assert set_params[1].props[0].ns == const.TRESTLE_GENERIC_NS - assert len(set_params) == 18 + assert len(set_params) == 14 else: # the original profile did not have ns set for this display name # confirm the namespace is not defined unless set_parameters_flag is True @@ -405,7 +405,7 @@ def test_profile_ohv(required_sections: Optional[str], success: bool, ohv: bool, ) set_params = profile.modify.set_parameters - assert len(set_params) == 18 + assert len(set_params) == 14 assert set_params[0].values[0] == 'all personnel' # the label is present in the header so it ends up in the set_parameter assert set_params[0].label == 'label from edit' @@ -415,11 +415,11 @@ def test_profile_ohv(required_sections: Optional[str], success: bool, ohv: bool, assert set_params[2].values[0] == 'new value' assert profile.metadata.version == new_version if ohv: - assert set_params[4].values[0] == 'no meetings from cli yaml' - assert set_params[4].label == 'meetings cancelled from cli yaml' + assert set_params[3].values[0] == 'no meetings from cli yaml' + assert set_params[3].label == 'meetings cancelled from cli yaml' else: - assert set_params[4].values[0] == 'all meetings' - assert set_params[4].label is None + assert set_params[3].values[0] == 'all meetings' + assert set_params[3].label is None catalog = ProfileResolver.get_resolved_profile_catalog(tmp_trestle_dir, assembled_prof_dir / 'profile.json') catalog_interface = CatalogInterface(catalog) @@ -1425,9 +1425,9 @@ def test_param_value_origin_from_inherited_profile(tmp_trestle_dir: pathlib.Path prof.Profile, FileContentType.JSON) # grabs parameter ac-3.3_prm_1 and verify if it was changed correctly - assert profile.modify.set_parameters[18].props[0].value == 'Needed to change param value origin' + assert profile.modify.set_parameters[16].props[0].value == 'Needed to change param value origin' # now change the value in the json file to verify if in the markdown is changed - profile.modify.set_parameters[18].props[0].value = 'this is a change test' + profile.modify.set_parameters[16].props[0].value = 'this is a change test' ModelUtils.save_top_level_model(profile, tmp_trestle_dir, 'my_assembled_prof', FileContentType.JSON) @@ -1448,3 +1448,47 @@ def test_param_value_origin_from_inherited_profile(tmp_trestle_dir: pathlib.Path assert header # verify the change done in json is reflected correctly in the profile-param-value-origin assert header[const.SET_PARAMS_TAG]['ac-3.3_prm_1'][const.PROFILE_PARAM_VALUE_ORIGIN] == 'this is a change test' + + +def test_profile_values_included_if_replaced(tmp_trestle_dir: pathlib.Path, monkeypatch: MonkeyPatch) -> None: + """Test the profile markdown generator.""" + _, assembled_prof_dir, _, markdown_path = setup_profile_generate(tmp_trestle_dir, 'simple_test_profile.json') + yaml_header_path = test_utils.YAML_TEST_DATA_PATH / 'good_simple.yaml' + + # convert resolved profile catalog to markdown then assemble it after adding an item to a control + # generate, edit, assemble + test_args = f'trestle author profile-generate -n {prof_name} -o {md_name} -rs NeededExtra'.split( # noqa E501 + ) + test_args.extend(['-y', str(yaml_header_path)]) + test_args.extend(['-s', all_sections_str]) + monkeypatch.setattr(sys, 'argv', test_args) + + assert Trestle().run() == 0 + + md_path = markdown_path / 'ac' / 'ac-1.md' + assert md_path.exists() + md_api = MarkdownAPI() + header, tree = md_api.processor.process_markdown(md_path) + + assert header + profile_values_for_param = header[const.SET_PARAMS_TAG]['ac-1_prm_7'][const.PROFILE_VALUES] + assert const.REPLACE_ME_PLACEHOLDER in profile_values_for_param + profile_values_for_param = [a for a in profile_values_for_param if a != const.REPLACE_ME_PLACEHOLDER] + profile_values_for_param.append('Test value') + header[const.SET_PARAMS_TAG]['ac-1_prm_7'][const.PROFILE_VALUES] = profile_values_for_param + md_api.write_markdown_with_header(md_path, header, tree.content.raw_text) + # verify if value was replaced correctly + assert 'Test value' in header[const.SET_PARAMS_TAG]['ac-1_prm_7'][const.PROFILE_VALUES] + + # assemble based on set_parameters_flag + test_args = f'trestle author profile-assemble -n {prof_name} -m {md_name} -o {assembled_prof_name}'.split() + test_args.append('-sp') + assembled_prof_dir.mkdir() + monkeypatch.setattr(sys, 'argv', test_args) + assert Trestle().run() == 0 + + profile, _ = ModelUtils.load_model_for_class(tmp_trestle_dir, 'my_assembled_prof', + prof.Profile, FileContentType.JSON) + + # grabs 6 parameter in line and test out the value is in there + assert 'Test value' in profile.modify.set_parameters[6].values diff --git a/trestle/core/catalog/catalog_reader.py b/trestle/core/catalog/catalog_reader.py index 09d8e36e1..ed118d84d 100644 --- a/trestle/core/catalog/catalog_reader.py +++ b/trestle/core/catalog/catalog_reader.py @@ -71,7 +71,6 @@ def read_additional_content( ) alters_map[sort_id] = control_alters for param_id, param_dict in control_param_dict.items(): - param_dict[const.VALUES] = param_dict[const.VALUES] if const.VALUES in param_dict else [] # if profile_values are present, overwrite values with them if const.PROFILE_VALUES in param_dict: if param_dict[const.PROFILE_VALUES] != [] and param_dict[const.PROFILE_VALUES] is not None: