def test_rulegroup_append_replace(rule_group): rule_group.append(DayRule('mmm', 1, 1, multiply=Decimal('2'))) rule_group.append(DayRule('mmm', 1, 1, multiply=Decimal('3')), replace=True) assert len(rule_group._rules) == 1 assert rule_group._rules['mmm']._slug == 'mmm' assert rule_group._rules['mmm']._multiply == Decimal("3")
def test_rulegroup_extend(rule_group): rule_group.extend([ DayRule('mmm1', 1, 1, multiply=Decimal('2')), DayRule('mmm2', 1, 2, multiply=Decimal('2')), DayRule('mmm3', 1, 3, multiply=Decimal('2')), ]) assert len(rule_group._rules) == 3 assert rule_group._rules['mmm1']._slug == 'mmm1' assert rule_group._rules['mmm2']._slug == 'mmm2' assert rule_group._rules['mmm3']._slug == 'mmm3'
def test_rulegroup_iter(make_rule_group): mg = make_rule_group(rules=[ DayRule('mmm1', 1, 1, multiply=Decimal('2')), DayRule('mmm2', 2, 2, multiply=Decimal('2')), ]) i = iter(mg) assert next(i)._slug == 'mmm1' assert next(i)._slug == 'mmm2' with pytest.raises(StopIteration): next(i)
def __init__(self, **kwargs): rules = ( RuleGroup('GRP_DE_NIGHT', 'Nachtarbeit', ( R( 'DE_NIGHT', 'Nachtarbeit 20:00-06:00', lambda m: m.hour >= 20 or m.hour < 6, multiply=Decimal('0.25'), tests=( ('~19:59', False), ('~20:00', True), ('~21:00', True), ('~05:59', True), ('~06:00', False), ), ), R( 'DE_NIGHT_START_YESTERDAY', 'Nachtarbeit 00:00-04:00 (Folgetag)', lambda m, s: 0 <= m.hour < 4 and s.date() < m.date(), multiply=Decimal('0.4'), tests=( ('00:00~00:01', False), ('23:59~00:01', True), ('23:59~03:59', True), ('23:59~04:00', False), ) ), )), RuleGroup('GRP_HOLIDAYS', 'Sonntags und Feiertagsarbeit', ( R( 'DE_SUNDAY', 'Sonntagsarbeit', lambda m: m.weekday() == 6, multiply=Decimal('0.5'), tests=( ('~2018-09-15', False), ('~2018-09-16 00:00', True), ('~2018-09-16 23:59', True), ('2018-09-16 23:59~2018-09-17 00:00', False), ), ), R( 'DE_SUNDAY_NEXT_NIGHT', 'Sonntagsarbeit (Montag)', lambda m, s: s.weekday() == 6 and 0 <= m.hour < 4, multiply=Decimal('0.5'), tests=( # 2018-09-16 is a Sunday ('~2018-09-16 00:00', False), ('~2018-09-16 23:59', False), ('2018-09-16 23:59~2018-09-17 00:00', True), ('2018-09-16 23:59~2018-09-17 03:59', True), ('2018-09-16 23:59~2018-09-17 04:00', False), ('2018-09-17 23:59~2018-09-18 00:00', False), ), ), R( 'DE_HOLIDAY', 'Feiertagsarbeit', lambda m, s, holidays: m in holidays, multiply=Decimal('1.25'), tests=( # 2018-05-10 is Christi Himmelfahrt ('~2018-05-09 23:59', False), ('~2018-05-10 00:00', True), ('~2018-05-10 23:59', True), ('~2018-05-11 00:00', False), ('2018-05-10 23:59~2018-05-11 00:00', False), ), ), R( 'DE_HOLIDAY_NEXT_NIGHT', 'Feiertagsarbeit (Folgetag)', lambda m, s, holidays: s in holidays and 0 <= m.hour < 4, multiply=Decimal('1.25'), tests=( # 2018-05-10 is Christi Himmelfahrt ('~2018-05-10 00:00', False), ('~2018-05-10 23:59', False), ('2018-05-10 23:59~2018-05-11 00:00', True), ('2018-05-10 23:59~2018-05-11 03:59', True), ('2018-05-10 23:59~2018-05-11 04:00', False), ('2018-09-11 23:59~2018-05-12 00:00', False), ) ), DayTimeRule('DE_HEILIGABEND', 12, 24, 14, multiply=Decimal('1.25')), DayTimeRule('DE_SILVESTER', 12, 31, 14, multiply=Decimal('1.25')), DayRule('DE_WEIHNACHTSFEIERTAG_1', 12, 25, multiply=Decimal('1.5')), DayRule('DE_WEIHNACHTSFEIERTAG_2', 12, 26, multiply=Decimal('1.5')), DayRule('DE_TAGDERARBEIT', 5, 1, multiply=Decimal('1.5')), )), ) super().__init__('DE', rules, **kwargs)
def test_rulegroup_contains(make_rule_group): mg = make_rule_group(rules=[DayRule('mmm', 1, 1, multiply=Decimal('2'))]) assert 'mmm' in mg assert DayRule('mmm', 1, 1, multiply=Decimal('2')) in mg
def test_rulegroup_extend_duplicate(rule_group): rule_group.append(DayRule('mmm', 1, 1, multiply=Decimal('2'))) with pytest.raises(Exception): rule_group.extend([DayRule('mmm', 1, 1, multiply=Decimal('3'))]) assert len(rule_group._rules) == 1 assert rule_group._rules['mmm']._multiply == Decimal("2")
def test_rulegroup_append_wrong_type(rule_group): rule_group.append(DayRule('mmm1', 1, 1, multiply=Decimal('2'))) with pytest.raises(Exception): rule_group.append(DayRule('mmm2', 2, 2, add=Decimal('2')))
def test_rulegroup_append_duplicate(rule_group): rule_group.append(DayRule('mmm', 1, 1, multiply=Decimal('2'))) with pytest.raises(Exception): rule_group.append(DayRule('mmm', 1, 1, multiply=Decimal('2'))) assert len(rule_group._rules) == 1 assert rule_group._rules['mmm']._slug == 'mmm'
def test_rulegroup_append(rule_group): rule_group.append(DayRule('mmm', 1, 1, multiply=Decimal('2'))) assert len(rule_group._rules) == 1 assert rule_group._rules['mmm']._slug == 'mmm'
def test_rulegroup_duplicate(): with pytest.raises(Exception): RuleGroup('XXX', 'xxx', [ DayRule('mmm', 1, 1, multiply=Decimal('2')), DayRule('mmm', 1, 1, multiply=Decimal('2')), ])
def test_rulegroup_init(): mg = RuleGroup('XXX', 'xxx', [DayRule('mmm', 1, 1, multiply=Decimal('2'))]) assert mg._description == 'xxx' assert len(mg._rules) == 1 assert mg._rules['mmm']._slug == 'mmm'
def test_DayRule(dt, res): m = DayRule('XXX', 12, 26, multiply=Decimal('1.25')) assert m.match(dt, dt, None) == res