def testRecurrenceEventsWhereExDateIsNotNow(self): """ Recurrence rules that have exceptions. """ # create an event starting 61 minutes ago, lasting 60 minutes, and # repeating every hour for five times; # but with an exception for the first instances now = _now() now = now.replace( microsecond=0) # recurring adjustments lose precision start = now - timedelta(minutes=61) end = now - timedelta(minutes=1) exdates = [start] rrules = [ "FREQ=HOURLY;INTERVAL=1;COUNT=5", ] calendar = Calendar() calendar.addEvent( Event('uid', start, end, 'foo', rrules=rrules, exdates=exdates)) eis = calendar.getActiveEventInstances() self.failUnless(eis) # the first event after the exception happens to start when the # exception would have ended self.assertEquals(eis[0].start, end)
def testAddEventsWithDifferentUID(self): # test that internally they end up in different event sets calendar = Calendar() start1 = datetime.now(UTC) end1 = start1 + timedelta(hours=1) exdates1 = [start1 + timedelta(hours=2)] start2 = start1 end2 = start2 + timedelta(hours=1) exdates2 = [start2 + timedelta(hours=2)] e1 = Event('uid1', start1, end1, 'content1', exdates=exdates1) e2 = Event('uid2', start2, end2, 'content2', exdates=exdates2) calendar.addEvent(e1) calendar.addEvent(e2) event1 = calendar._eventSets['uid1']._events[0] event2 = calendar._eventSets['uid2']._events[0] self.assertEquals(len(calendar._eventSets['uid1']._events), 1) self.assertEquals(len(calendar._eventSets['uid2']._events), 1) self.assertEventAttribsEqual(event1, start1, end1, 'content1', None, exdates1) self.assertEventAttribsEqual(event2, start2, end2, 'content2', None, exdates2)
def testAddEventsWithSameUID(self): # test that internally they end up in the same event set calendar = Calendar() start1 = datetime.now(UTC) end1 = start1 + timedelta(hours=1) content1 = "content1" exdates1 = [start1 + timedelta(hours=2)] start2 = start1 end2 = start2 + timedelta(hours=1) content2 = "content2" exdates2 = [start2 + timedelta(hours=2)] e1 = Event('uid', start1, end1, content1, rrules=None, exdates=exdates1) e2 = Event('uid', start2, end2, content2, rrules=None, exdates=exdates2) calendar.addEvent(e1) calendar.addEvent(e2) # verify internals of calendar event1 = calendar._eventSets['uid']._events[0] event2 = calendar._eventSets['uid']._events[1] self.assertEquals(len(calendar._eventSets['uid']._events), 2) self.assertEventAttribsEqual(event1, start1, end1, content1, None, exdates1) self.assertEventAttribsEqual(event2, start2, end2, content2, None, exdates2)
def testScheduledNotACallbackWhenCancelled(self): now = _now() start = now end = now + timedelta(hours=1) calendar = eventcalendar.Calendar() calendar.addEvent(eventcalendar.Event('uid', start, end, 'content')) self._scheduler.setCalendar(calendar)
def testGetActiveSimple(self): now = _now() start = now - datetime.timedelta(hours=1) end = now + datetime.timedelta(minutes=1) calendar = eventcalendar.Calendar() calendar.addEvent(eventcalendar.Event("uid", start, end, "foo")) ei = calendar.getActiveEventInstances()[0] self.assertEventAttribsEqual(ei.event, start, end, "foo", None, None)
def testGetActiveSimple(self): now = _now() start = now - timedelta(hours=1) end = now + timedelta(minutes=1) calendar = Calendar() calendar.addEvent(Event('uid', start, end, 'foo')) ei = calendar.getActiveEventInstances()[0] self.assertEventAttribsEqual(ei.event, start, end, 'foo', None, None)
def testAddEvent(self): now = _now() start = now end = start + datetime.timedelta(hours=1) exdates = [start + datetime.timedelta(hours=2)] calendar = eventcalendar.Calendar() calendar.addEvent(eventcalendar.Event("uid", start, end, "foo", exdates=exdates)) event = calendar._eventSets["uid"]._events[0] self.assertEventAttribsEqual(event, start, end, "foo", None, exdates)
def testAddEvent(self): now = _now() start = now end = start + timedelta(hours=1) exdates = [start + timedelta(hours=2)] calendar = Calendar() calendar.addEvent(Event('uid', start, end, 'foo', exdates=exdates)) event = calendar._eventSets['uid']._events[0] self.assertEventAttribsEqual(event, start, end, 'foo', None, exdates)
def testDefaultScheduledCallbackWhenBeforeAndAfterTheWindowSize(self): now = _now() start = now - timedelta(days=1) end = now + timedelta(days=2) calendar = eventcalendar.Calendar() calendar.addEvent(eventcalendar.Event('uid', start, end, 'content')) self._scheduler.setCalendar(calendar) resultSeconds = self._scheduler._nextStart expectedSeconds = _toSeconds(self._scheduler.windowSize) / 2 self.assertEquals(round(resultSeconds / 10.0), round(expectedSeconds / 10.0))
def testScheduledEndCallbackWhenEndInWindowSize(self): now = _now() start = now - timedelta(hours=1) end = now + timedelta(hours=1) calendar = eventcalendar.Calendar() calendar.addEvent(eventcalendar.Event('uid', start, end, 'content')) self._scheduler.setCalendar(calendar) resultSeconds = self._scheduler._nextStart expectedSeconds = _toSeconds(end - now) self.assertEquals(round(resultSeconds / 10.0), round(expectedSeconds / 10.0))
def testScheduledEndCallbackWhenEndInWindowSize(self): now = _now() start = now - timedelta(hours=1) end = now + timedelta(hours=1) calendar = eventcalendar.Calendar() calendar.addEvent(eventcalendar.Event('uid', start, end, 'content')) self._scheduler.setCalendar(calendar) resultSeconds = self._scheduler._nextStart expectedSeconds = _toSeconds(end-now) self.assertEquals(round(resultSeconds / 10.0), round(expectedSeconds / 10.0))
def testRecurrenceEventsWhereExDateIsStartDate(self): """ Recurrence rules that have exceptions. """ now = _now() now = now.replace(microsecond=0) # recurring adjustments lose precision start = now - datetime.timedelta(minutes=61) end = now - datetime.timedelta(minutes=1) exdates = [start] rrules = ["FREQ=HOURLY;INTERVAL=1;COUNT=5"] calendar = eventcalendar.Calendar() calendar.addEvent(eventcalendar.Event("uid", start, end, "foo", rrules=rrules, exdates=exdates)) eis = calendar.getActiveEventInstances(start) self.failIf(eis)
def testRecurrenceEventsWhereExDateIsStartDate(self): """ Recurrence rules that have exceptions. """ now = _now() now = now.replace( microsecond=0) # recurring adjustments lose precision start = now - timedelta(minutes=61) end = now - timedelta(minutes=1) exdates = [start] rrules = [ "FREQ=HOURLY;INTERVAL=1;COUNT=5", ] calendar = Calendar() calendar.addEvent( Event('uid', start, end, 'foo', rrules=rrules, exdates=exdates)) eis = calendar.getActiveEventInstances(start) self.failIf(eis)
def testRecurrenceEventsWhereExDateIsNotNow(self): """ Recurrence rules that have exceptions. """ # create an event starting 61 minutes ago, lasting 60 minutes, and # repeating every hour for five times; # but with an exception for the first instances now = _now() now = now.replace(microsecond=0) # recurring adjustments lose precision start = now - datetime.timedelta(minutes=61) end = now - datetime.timedelta(minutes=1) exdates = [start] rrules = ["FREQ=HOURLY;INTERVAL=1;COUNT=5"] calendar = eventcalendar.Calendar() calendar.addEvent(eventcalendar.Event("uid", start, end, "foo", rrules=rrules, exdates=exdates)) eis = calendar.getActiveEventInstances() self.failUnless(eis) # the first event after the exception happens to start when the # exception would have ended self.assertEquals(eis[0].start, end)
def testUnsubscribe(self): # create a list to store subscription call results calls = [] started = lambda c: calls.append(('started', c.content)) ended = lambda c: calls.append(('ended', c.content)) sid = self._scheduler.subscribe(started, ended) self._scheduler.unsubscribe(sid) now = _now() start = now - timedelta(hours=1) end = now + timedelta(minutes=1) calendar = eventcalendar.Calendar() self.assertEquals(calls, []) calendar.addEvent(eventcalendar.Event('uid', start, end, 'content')) self._scheduler.setCalendar(calendar) self.assertEquals(calls, []) eis = self._scheduler.getCalendar().getActiveEventInstances() self.failUnless(eis) self.assertEquals(eis[0].event.content, 'content')
def testAddEventsWithDifferentUID(self): # test that internally they end up in different event sets calendar = eventcalendar.Calendar() start1 = datetime.datetime.now(eventcalendar.UTC) end1 = start1 + datetime.timedelta(hours=1) exdates1 = [start1 + datetime.timedelta(hours=2)] start2 = start1 end2 = start2 + datetime.timedelta(hours=1) exdates2 = [start2 + datetime.timedelta(hours=2)] e1 = eventcalendar.Event("uid1", start1, end1, "content1", exdates=exdates1) e2 = eventcalendar.Event("uid2", start2, end2, "content2", exdates=exdates2) calendar.addEvent(e1) calendar.addEvent(e2) event1 = calendar._eventSets["uid1"]._events[0] event2 = calendar._eventSets["uid2"]._events[0] self.assertEquals(len(calendar._eventSets["uid1"]._events), 1) self.assertEquals(len(calendar._eventSets["uid2"]._events), 1) self.assertEventAttribsEqual(event1, start1, end1, "content1", None, exdates1) self.assertEventAttribsEqual(event2, start2, end2, "content2", None, exdates2)
def testSubscribe(self): # create a list to store subscription call results calls = [] started = lambda ei: calls.append(('started', ei.event.content)) ended = lambda ei: calls.append(('ended', ei.event.content)) sid = self._scheduler.subscribe(started, ended) self.assertEquals(calls, []) # create a calendar and put a currently active event on it calendar = eventcalendar.Calendar() now = _now() start = now - timedelta(hours=1) end = now + timedelta(minutes=1) calendar.addEvent(eventcalendar.Event('uid', start, end, 'foo')) self._scheduler.setCalendar(calendar) # verify that we gat a start call for the active event instance self.assertEquals(calls, [('started', 'foo')]) eis = self._scheduler.getCalendar().getActiveEventInstances() self.assertEventAttribsEqual(eis[0].event, start, end, 'foo', None, None) # now set a new empty calendar on the scheduler calendar = eventcalendar.Calendar() self._scheduler.setCalendar(calendar) # make sure we got an end call for the previously active event instance self.assertEquals(calls, [('started', 'foo'), ('ended', 'foo')]) eis = self._scheduler.getCalendar().getActiveEventInstances() self.failIf(eis, 'Empty calendar so should not have active event instances') self._scheduler.unsubscribe(sid)