def test_extract_date_range(): date_ = ParameterEnum.DATE_INSTALLATION.value dt_1 = datetime.now() dt_2 = dt_1 + timedelta(days=1) assert _extract_date_range(Range(date_, dt_1, dt_2)) == (dt_1, dt_2) assert _extract_date_range(Equal(date_, dt_1)) == (dt_1, dt_1) assert _extract_date_range(Littler(date_, dt_1)) == (None, dt_1) assert _extract_date_range(Greater(date_, dt_1)) == (dt_1, None)
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_extract_conditions(): date_ = ParameterEnum.DATE_INSTALLATION.value dt_1 = datetime.now() dt_2 = dt_1 + timedelta(days=1) cd_1 = Range(date_, dt_1, dt_2) cd_2 = Greater(date_, dt_2) parametrization = Parametrization([], [], []) assert parametrization.extract_conditions() == [] parametrization = Parametrization([_simple_nac(cd_1)], [], []) assert parametrization.extract_conditions() == [cd_1] parametrization = Parametrization( [_simple_nac(cd_1), _simple_nac(cd_2)], [], []) assert parametrization.extract_conditions() == [cd_1, cd_2]
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_check_date_conditions_not_compatible(): date_ = ParameterEnum.DATE_INSTALLATION.value dt_1 = datetime.now() dt_2 = dt_1 + timedelta(days=1) cd_1 = Range(date_, dt_1, dt_2) cd_2 = Equal(date_, dt_1) cd_3 = Littler(date_, dt_1) cd_4 = Greater(date_, dt_2) _check_date_conditions_not_compatible([cd_1], date_) _check_date_conditions_not_compatible([cd_1, cd_2], date_) _check_date_conditions_not_compatible([cd_1, cd_3, cd_4], date_) _check_date_conditions_not_compatible([cd_2], date_) _check_date_conditions_not_compatible([cd_3], date_) _check_date_conditions_not_compatible([cd_4], date_) cd_5 = Littler(date_, dt_2) cd_6 = Greater(date_, dt_1) with pytest.raises(ParametrizationError): _check_date_conditions_not_compatible([cd_5, cd_6], date_) with pytest.raises(ParametrizationError): _check_date_conditions_not_compatible([cd_1, cd_6], date_) with pytest.raises(ParametrizationError): _check_date_conditions_not_compatible([cd_5, cd_1], date_)
def test_check_parametrization_consistency(): date_ = ParameterEnum.DATE_INSTALLATION.value dt_1 = datetime.now() dt_2 = dt_1 + timedelta(days=1) cd_1 = Range(date_, dt_1, dt_2) cd_3 = Littler(date_, dt_1) cd_4 = Greater(date_, dt_2) cd_5 = Littler(date_, dt_2) cd_6 = Greater(date_, dt_1) new_text = StructuredText(_str('Art. 2'), [_str('version modifiée')], [], None) Parametrization( [_NAC('', None, cd_1), _NAC('', None, cd_3)], [AlternativeSection('', new_text, cd_4)], []).check_consistency() Parametrization([_NAC('', None, cd_3)], [], []).check_consistency() with pytest.raises(ParametrizationError): Parametrization( [_NAC('', None, cd_1), _NAC('', None, cd_5)], [AlternativeSection('', new_text, cd_6)], []).check_consistency() with pytest.raises(ParametrizationError): Parametrization( [_NAC('', None, cd_1), _NAC('', None, cd_1)], [], []).check_consistency()
def test_extract_conditions_from_parametrization(): res = extract_conditions_from_parametrization(_DATE, _PARAMETRIZATION) assert res == [ Littler(parameter=_DATE, target=date(2021, 1, 1), strict=True), Range(parameter=_DATE, left=date(2020, 1, 1), right=date(2021, 1, 1)), ]
_PARAMETRIZATION = Parametrization( inapplicable_sections=[ InapplicableSection( section_id='abcdef', alineas=None, condition=AndCondition( conditions=frozenset([Littler(parameter=_DATE, target=date(2021, 1, 1), strict=True)]) ), ) ], alternative_sections=[ AlternativeSection( section_id='123456', new_text=_NEW_TEXT, condition=AndCondition( conditions=frozenset([Range(parameter=_DATE, left=date(2020, 1, 1), right=date(2021, 1, 1))]) ), ) ], warnings=[AMWarning('ABCDEF', 'AM warning')], ) def test_generate_exhaustive_combinations(): res = generate_exhaustive_combinations(_PARAMETRIZATION) assert len(res) == 4 def test_generate_options_dicts(): res = _generate_options_dicts(_PARAMETRIZATION) expected = [