Skip to content

Commit

Permalink
fix: correct empty values going in assembled profile (#1491)
Browse files Browse the repository at this point in the history
* fix: correct empty values going in assembled profile

Signed-off-by: Alejandro Jose Leiva Palomo <alejandro.leiva.palomo@ibm.com>

* fix: add test case to check profile values replaced

Signed-off-by: Alejandro Jose Leiva Palomo <alejandro.leiva.palomo@ibm.com>

---------

Signed-off-by: Alejandro Jose Leiva Palomo <alejandro.leiva.palomo@ibm.com>
  • Loading branch information
AleJo2995 committed Jan 5, 2024
1 parent c112f9c commit 136b712
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 9 deletions.
60 changes: 52 additions & 8 deletions tests/trestle/core/commands/author/profile_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'
Expand All @@ -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)
Expand Down Expand Up @@ -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)

Expand All @@ -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
1 change: 0 additions & 1 deletion trestle/core/catalog/catalog_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 136b712

Please sign in to comment.