def holidays_test(): def parse_datetime(d): return datetime.strptime(d, '%Y-%m-%d %H:%M') HOLIDAYS = [ # Friday is closed '2014-03-28 14:00', '2014-03-25 21:15', # part day '2014-03-27 20:00', # full day # multiple full days '2014-03-30 19:00', '2014-03-31 18:00', ] WORK_DAYS = [ # Part day '2014-03-25 15:00', # Normal days '2014-03-26 15:30', '2014-04-01 16:00', ] sa = timezone('Australia/Adelaide') # setup some configuration for Treacle to use treacle = Treacle(config=dict( # default attributes. DEFAULT=dict( holidays=join(TEST_PATH, 'partdays.ics'), tz='Australia/Adelaide', hours='Mon,Tue,Wed,Thursday,Sat,Sun@12:00-23:00' ), adel=dict( ), ), config_as_dict=True) # now use some days and see if we should work on them for d in HOLIDAYS: dt = sa.localize(parse_datetime(d)) assert not treacle.in_hours(when=dt), ('holiday %r was not marked as a holiday' % (dt)) for d in WORK_DAYS: dt = sa.localize(parse_datetime(d)) assert treacle.in_hours(when=dt), ('work day %r was marked as a holiday' % (dt))
def holidays_test(): def parse_date(d): return datetime.strptime(d, '%Y-%m-%d').date() def parse_time(t): return datetime.strptime(t, '%H:%M').time() HOLIDAYS = [ # anzac day '2011-04-25', # australia day '2011-01-26', # xmas '2011-12-25', # weekends '2012-06-02', '2012-06-30', '2012-07-07', ] WORK_DAYS = [ '2013-03-05', '2012-10-08', ] VIC_HOLIDAYS = [ # melbourne cup '2012-11-06', ] SA_HOLIDAYS = [ # new years eve '2012-12-31', ] ALL_HOURS = [ '09:00', '10:00', '11:15', '14:00', '17:00' ] NOT_ADEL_HOURS = [ '08:15', ] NOT_MELB_HOURS = [ # expressed in adelaide time, -0.5hr '17:45', ] NEVER_HOURS = [ '00:00', '03:00', '19:00', '21:00' ] sa = timezone('Australia/Adelaide') # setup some configuration for Treacle to use treacle = Treacle(config=dict( # default attributes. DEFAULT=dict( holidays=join(TEST_PATH, 'au_holidays_test.ics'), tz='Australia/Adelaide', hours='Mon,Tue,Wed,Thursday,Fri@08:00-18:00' ), melb=dict( holidays_vic=join(TEST_PATH, 'vic_holidays_test.ics'), # Melbourne has a different timezone to default tz='Australia/Melbourne', # Use mix of short and long days for testing hours='Mon,Tuesday,Wed,Thu,Friday@08:30-18:00' ), adel=dict( holidays_sa=join(TEST_PATH, 'sa_holidays_test.ics'), hours='Monday,Tue,Wed,Thu,Fri@08:30-18:00', ), nuri=dict( holidays_sa=join(TEST_PATH, 'sa_holidays_test.ics') ) ), config_as_dict=True) # now use some days and see if we should work on them for d in HOLIDAYS: d = parse_date(d) for t in ALL_HOURS + NEVER_HOURS: dt = sa.localize(datetime.combine(d, parse_time(t))) assert not treacle.in_hours(when=dt), ('holiday %r was not marked as a global holiday' % (dt)) for d in VIC_HOLIDAYS: d = parse_date(d) for t in ALL_HOURS: dt = sa.localize(datetime.combine(d, parse_time(t))) assert not treacle.in_hours(office='melb', when=dt), ('%r should not be work time' % dt) assert treacle.in_hours(office='adel', when=dt), ('%r should be work time' % dt) for t in NEVER_HOURS + NOT_MELB_HOURS: dt = sa.localize(datetime.combine(d, parse_time(t))) assert not treacle.in_hours(office='melb', when=dt), ('%r should not be work time' % dt) for d in WORK_DAYS: d = parse_date(d) for t in ALL_HOURS: dt = sa.localize(datetime.combine(d, parse_time(t))) for o in ('adel', 'nuri', 'melb'): assert treacle.in_hours(office=o, when=dt), ('%r should be work time in %r' % (dt, o)) for t in NEVER_HOURS: dt = sa.localize(datetime.combine(d, parse_time(t))) for o in ('adel', 'nuri', 'melb'): assert not treacle.in_hours(office=o, when=dt), ('%r should not be work time in %r' % (dt, o)) for t in NOT_ADEL_HOURS: dt = sa.localize(datetime.combine(d, parse_time(t))) assert not treacle.in_hours(office='adel', when=dt), ('%r should not be work time in adel' % dt) assert treacle.in_hours(office='melb', when=dt), ('%r should be work time in melb' % dt) assert treacle.in_hours(office='nuri', when=dt), ('%r should be work time in nuri' % dt) for t in NOT_MELB_HOURS: dt = sa.localize(datetime.combine(d, parse_time(t))) assert treacle.in_hours(office='adel', when=dt), ('%r should be work time in adel' % dt) assert not treacle.in_hours(office='melb', when=dt), ('%r should not be work time in melb' % dt) assert treacle.in_hours(office='nuri', when=dt), ('%r should be work time in nuri' % dt)