def sample_catalog_rich_controls(): """Return a catalog with controls in groups and in the catalog itself.""" catalog_obj = gens.generate_sample_model(cat.Catalog) param_0 = common.Parameter( id='param_0', values=[common.ParameterValue(__root__='param_0_val')]) param_1 = common.Parameter( id='param_1', values=[common.ParameterValue(__root__='param_1_val')]) control_a = cat.Control(id='control_a', title='this is control a', params=[param_0, param_1]) control_b = cat.Control(id='control_b', title='this is control b') group = cat.Group(id='xy', title='The xy control group', controls=[control_a, control_b]) catalog_obj.groups = [group] part = common.Part(id='cpart', name='name.c.part') control_c = cat.Control(id='control_c', title='this is control c', parts=[part]) control_d = cat.Control(id='control_d', title='this is control d') control_d1 = cat.Control(id='control_d1', title='this is control d1') control_d.controls = [control_d1] catalog_obj.controls = [control_c, control_d] return catalog_obj
def test_profile_resolver_param_sub() -> None: """Test profile resolver param sub via regex.""" id_1 = 'ac-2_smt.1' param_1 = com.Parameter(id=id_1, values=[com.ParameterValue(__root__='the cat')]) id_10 = 'ac-2_smt.10' param_10 = com.Parameter(id=id_10, values=[com.ParameterValue(__root__='well fed')]) param_text = 'Make sure that {{insert: param, ac-2_smt.1}} is very {{ac-2_smt.10}} today. Very {{ac-2_smt.10}}!' param_dict = {id_1: param_1, id_10: param_10} new_text = Modify._replace_params(param_text, param_dict) assert new_text == 'Make sure that the cat is very well fed today. Very well fed!'
def generate_complex_catalog(stem: str = '') -> cat.Catalog: """Generate a complex and deep catalog for testing.""" group_a = generators.generate_sample_model(cat.Group, True) group_a.id = f'{stem}a' group_a.controls = generate_control_list(group_a.id, 4) group_b = generators.generate_sample_model(cat.Group, True) group_b.id = f'{stem}b' group_b.controls = generate_control_list(group_b.id, 3) group_b.controls[2].controls = generate_control_list(f'{group_b.id}-2', 3) group_ba = generators.generate_sample_model(cat.Group, True) group_ba.id = f'{stem}ba' group_ba.controls = generate_control_list(group_ba.id, 2) group_b.groups = [group_ba] catalog = generators.generate_sample_model(cat.Catalog, True) catalog.controls = generate_control_list(f'{stem}cat', 3) catalog.params = generate_param_list(f'{stem}parm', 3) test_control = generators.generate_sample_model(cat.Control, False) test_control.id = f'{stem}test-1' test_control.params = [common.Parameter(id=f'{test_control.id}_prm_1', values=['Default', 'Values'])] test_control.parts = [ common.Part( id=f'{test_control.id}_smt', name='statement', prose='Statement with no parts. Prose with param value {{ insert: param, test-1_prm_1 }}' ) ] catalog.controls.append(test_control) catalog.groups = [group_a, group_b] return catalog
def setparam_to_param(param_id: str, set_param: prof.SetParameter) -> common.Parameter: """ Convert setparameter to parameter. Args: param_id: the id of the parameter set_param: the set_parameter from a profile Returns: a Parameter with param_id and content from the SetParameter """ return common.Parameter(id=param_id, values=set_param.values, select=set_param.select, label=set_param.label)
def test_parameter_to_dict() -> None: """Test parameter to dict conversion.""" test1 = common.Test(expression='not too big', remarks='test for 1') test2 = common.Test(expression='keep it small', remarks='test for 2') constraints = [ common.ParameterConstraint(description='my constraints', tests=[test1, test2]) ] sel = common.ParameterSelection(how_many=common.HowMany.one_or_more, choice=['one', 'two', 'three']) values = [ common.ParameterValue(__root__='one'), common.ParameterValue(__root__='two') ] prop1 = common.Property(name='prop1', value='value1') prop2 = common.Property(name='prop2', value='value2', remarks='remark2') param = common.Parameter(id='param1', label='label1', values=values, props=[prop1, prop2], select=sel, remarks='remarks1', constraints=constraints) param_dict = ModelUtils.parameter_to_dict(param, False) dict_copy = copy.deepcopy(param_dict) new_param = ModelUtils.dict_to_parameter(dict_copy) assert param == new_param # confirm it strips items properly to partial form partial_dict = ModelUtils.parameter_to_dict(param, True) new_partial_param = ModelUtils.dict_to_parameter(partial_dict) partial_param = common.Parameter(id='param1', label='label1', values=values, select=sel) assert new_partial_param == partial_param # confirm that disallowed attributes raise exception dict_copy = copy.deepcopy(param_dict) dict_copy['foo'] = 'bar' with pytest.raises(ValidationError): _ = ModelUtils.dict_to_parameter(dict_copy) # confirm that bad string for how-many raises exception dict_copy = copy.deepcopy(param_dict) dict_copy['select']['how_many'] = 'seven' with pytest.raises(err.TrestleError): _ = ModelUtils.dict_to_parameter(dict_copy) # confirm values must be among allowed choices or raise exception sel = common.ParameterSelection(how_many=common.HowMany.one_or_more, choice=['one', 'two', 'three']) param = common.Parameter(id='param1', label='label1', select=sel, values=['two', 'five']) param_dict = ModelUtils.parameter_to_dict(param, False) with pytest.raises(err.TrestleError): _ = ModelUtils.dict_to_parameter(param_dict) # confirm only one item if HowMany is one or raise exception sel = common.ParameterSelection(how_many=common.HowMany.one, choice=['one', 'two', 'three']) param = common.Parameter(id='param1', label='label1', select=sel, values=['two', 'three']) param_dict = ModelUtils.parameter_to_dict(param, False) with pytest.raises(err.TrestleError): _ = ModelUtils.dict_to_parameter(param_dict) # confirm special handling for one value works sel = common.ParameterSelection(how_many=common.HowMany.one, choice=['one', 'two', 'three']) param = common.Parameter(id='param1', label='label1', select=sel, values=['two']) param_dict = ModelUtils.parameter_to_dict(param, False) assert param == ModelUtils.dict_to_parameter(param_dict)
def test_replace_params(param_id, param_text, prose, result) -> None: """Test cases of replacing param in string.""" param = com.Parameter(id=param_id, values=[com.ParameterValue(__root__=param_text)]) param_10 = com.Parameter(id='ac-2_smt.10', values=[com.ParameterValue(__root__='my 10 str')]) param_dict = {param_id: param, 'ac-1_smt.10': param_10} assert Modify._replace_ids_with_text(prose, ParameterRep.VALUE_OR_STRING_NONE, param_dict) == result