def test_is_satisfied(): param_1 = Parameter('regime', ParameterType.REGIME) param_2 = Parameter('date', ParameterType.DATE) condition_1 = Equal(param_1, Regime.A) condition_2 = Equal(param_1, Regime.E) condition_3 = Littler(param_2, 1, True) assert not AndCondition(frozenset([condition_1])).is_satisfied({}) assert AndCondition(frozenset([condition_1 ])).is_satisfied({param_1: Regime.A}) assert OrCondition(frozenset([condition_1, condition_2 ])).is_satisfied({param_1: Regime.A}) assert OrCondition(frozenset([condition_1, condition_3 ])).is_satisfied({param_1: Regime.A}) assert not AndCondition(frozenset([condition_1, condition_2 ])).is_satisfied({param_1: Regime.A}) assert AndCondition(frozenset([condition_1, condition_3])).is_satisfied({ param_1: Regime.A, param_2: 0.5 }) assert OrCondition(frozenset([condition_2, condition_3])).is_satisfied({ param_1: Regime.E, param_2: 0.5 }) assert not OrCondition(frozenset([condition_1, condition_3])).is_satisfied( { param_1: Regime.E, param_2: 5 })
def test_generate_options_dict(): parameter = Parameter('test', ParameterType.BOOLEAN) conditions: List[Condition] = [ Equal(parameter, True), Equal(parameter, True) ] res = _generate_options_dict(conditions) assert res == [('test == True', True), ('test != True', False)]
def test_extract_sorted_interval_sides_targets(): parameter = Parameter('test', ParameterType.DATE) conditions = [ Range(parameter, datetime(2020, 1, 1), datetime(2021, 1, 1), False, True), Littler(parameter, datetime(2020, 1, 1), True), Greater(parameter, datetime(2021, 1, 1), False), ] assert extract_sorted_interval_sides_targets( conditions, True) == [datetime(2020, 1, 1), datetime(2021, 1, 1)]
def test_generate_equal_option_dicts_2(): parameter = Parameter('regime', ParameterType.REGIME) conditions: List[Condition] = [ Equal(parameter, Regime.A), Equal(parameter, Regime.NC) ] res = _generate_equal_option_dicts(conditions) expected = [ ('regime == A', Regime.A), ('regime == E', Regime.E), ('regime == D', Regime.D), ('regime == NC', Regime.NC), ] assert expected == res
def test_generate_options_dict_2(): parameter = Parameter('test', ParameterType.DATE) conditions = [ Range(parameter, date(2020, 1, 1), date(2021, 1, 1), False, True), Littler(parameter, date(2020, 1, 1), True), Greater(parameter, date(2021, 1, 1), False), ] res = _generate_options_dict(conditions) str_dt_20 = '2020-01-01' str_dt_21 = '2021-01-01' expected = [ (f'test < {str_dt_20}', date(2019, 12, 31)), (f'{str_dt_20} <= test < {str_dt_21}', date(2020, 7, 2)), (f'test >= {str_dt_21}', date(2021, 1, 2)), ] assert res == expected
def test_generate_combinations(): parameter_1 = Parameter('test_1', ParameterType.DATE) parameter_2 = Parameter('test_2', ParameterType.BOOLEAN) options_1 = (parameter_1, [('test_1 < a', datetime(2021, 1, 1)), ('test_1 >= a', datetime(2022, 1, 1))]) options_2 = (parameter_2, [('test_2 == True', True), ('test_2 != True', False)]) res = _generate_combinations([options_1, options_2], False) expected: Combinations = { ('test_1 < a', 'test_2 == True'): { parameter_1: datetime(2021, 1, 1), parameter_2: True }, ('test_1 < a', 'test_2 != True'): { parameter_1: datetime(2021, 1, 1), parameter_2: False }, ('test_1 >= a', 'test_2 == True'): { parameter_1: datetime(2022, 1, 1), parameter_2: True }, ('test_1 >= a', 'test_2 != True'): { parameter_1: datetime(2022, 1, 1), parameter_2: False }, } assert expected == res parameter_1 = Parameter('test_1', ParameterType.DATE) parameter_2 = Parameter('test_2', ParameterType.BOOLEAN) options_1 = (parameter_1, [('test_1 < a', datetime(2021, 1, 1)), ('test_1 >= a', datetime(2022, 1, 1))]) options_2 = (parameter_2, [('test_2 == True', True), ('test_2 != True', False)]) res = _generate_combinations([options_1, options_2], True) expected: Combinations = { (): {}, ('test_2 == True', ): { parameter_2: True }, ('test_2 != True', ): { parameter_2: False }, ('test_1 < a', ): { parameter_1: datetime(2021, 1, 1) }, ('test_1 < a', 'test_2 == True'): { parameter_1: datetime(2021, 1, 1), parameter_2: True }, ('test_1 < a', 'test_2 != True'): { parameter_1: datetime(2021, 1, 1), parameter_2: False }, ('test_1 >= a', ): { parameter_1: datetime(2022, 1, 1) }, ('test_1 >= a', 'test_2 == True'): { parameter_1: datetime(2022, 1, 1), parameter_2: True }, ('test_1 >= a', 'test_2 != True'): { parameter_1: datetime(2022, 1, 1), parameter_2: False }, } assert expected == res
from datetime import date from envinorma.models.condition import AndCondition, Littler, Range, extract_leaf_conditions from envinorma.models.parameter import Parameter, ParameterType from envinorma.models.structured_text import StructuredText from envinorma.models.text_elements import EnrichedString from envinorma.parametrization.combinations import _generate_options_dicts, generate_exhaustive_combinations from envinorma.parametrization.models.parametrization import ( AlternativeSection, AMWarning, InapplicableSection, Parametrization, extract_conditions_from_parametrization, ) _DATE = Parameter(id='date-d-installation', type=ParameterType.DATE) _NEW_TEXT = StructuredText( title=EnrichedString(text='Article 2.1'), outer_alineas=[EnrichedString(text='Contenu nouveau')], sections=[], applicability=None, reference=None, annotations=None, id='d16d0fE7C7fc', ) _PARAMETRIZATION = Parametrization( inapplicable_sections=[ InapplicableSection( section_id='abcdef', alineas=None, condition=AndCondition(