def test_to_json_attendees(self): e = Event('Good day', start=(1 / Jul / 2020)[11:22:33], timezone=TEST_TIMEZONE, attendees=[ Attendee(email='*****@*****.**', response_status=ResponseStatus.NEEDS_ACTION), Attendee(email='*****@*****.**', response_status=ResponseStatus.ACCEPTED), ]) event_json = { 'summary': 'Good day', 'start': { 'dateTime': '2020-07-01T11:22:33+12:00', 'timeZone': TEST_TIMEZONE }, 'end': { 'dateTime': '2020-07-01T12:22:33+12:00', 'timeZone': TEST_TIMEZONE }, 'recurrence': [], 'visibility': 'default', 'attendees': [ { 'email': '*****@*****.**', 'responseStatus': ResponseStatus.NEEDS_ACTION }, { 'email': '*****@*****.**', 'responseStatus': ResponseStatus.ACCEPTED }, ], 'reminders': { 'useDefault': False }, 'attachments': [] } self.assertDictEqual(EventSerializer.to_json(e), event_json) e = Event('Good day2', start=20 / Jul / 2020, default_reminders=True) event_json = { 'summary': 'Good day2', 'start': { 'date': '2020-07-20' }, 'end': { 'date': '2020-07-21' }, 'recurrence': [], 'visibility': 'default', 'attendees': [], 'reminders': { 'useDefault': True }, 'attachments': [] } self.assertDictEqual(EventSerializer.to_json(e), event_json)
def test_repr_str(self): attendee = Attendee(email='*****@*****.**', display_name='Guest', comment='I do not know him', optional=True, additional_guests=2, _response_status=ResponseStatus.NEEDS_ACTION) self.assertEqual( attendee.__repr__(), "<Attendee '*****@*****.**' - response: 'needsAction'>") self.assertEqual(attendee.__str__(), "'*****@*****.**' - response: 'needsAction'")
def test_add_attendees(self): e = Event('Good day', start=(17 / Jul / 2020), timezone=TEST_TIMEZONE, attendees=[ Attendee(email="*****@*****.**"), "*****@*****.**", ]) self.assertEqual(len(e.attendees), 2) e.add_attendee(Attendee("*****@*****.**")) e.add_attendee(Attendee(email="*****@*****.**")) self.assertEqual(len(e.attendees), 4) self.assertEqual(e.attendees[0].email, "*****@*****.**") self.assertEqual(e.attendees[1].email, "*****@*****.**") self.assertEqual(e.attendees[2].email, "*****@*****.**") self.assertEqual(e.attendees[3].email, "*****@*****.**")
def _to_object(json_attendee): return Attendee( email=json_attendee['email'], display_name=json_attendee.get('displayName', None), comment=json_attendee.get('comment', None), optional=json_attendee.get('optional', None), is_resource=json_attendee.get('resource', None), additional_guests=json_attendee.get('additionalGuests', None), _response_status=json_attendee.get('responseStatus', None))
def test_to_json(self): attendee = Attendee(email='*****@*****.**', display_name='Guest', comment='I do not know him', optional=True, additional_guests=2, _response_status=ResponseStatus.NEEDS_ACTION) attendee_json = AttendeeSerializer.to_json(attendee) self.assertEqual(attendee.email, attendee_json['email']) self.assertEqual(attendee.display_name, attendee_json['displayName']) self.assertEqual(attendee.comment, attendee_json['comment']) self.assertEqual(attendee.optional, attendee_json['optional']) self.assertNotIn('resource', attendee_json) self.assertEqual(attendee.additional_guests, attendee_json['additionalGuests']) self.assertEqual(attendee.response_status, attendee_json['responseStatus'])
def list(self, **kwargs): """Emulates GoogleCalendar.service.events().list().execute()""" time_min = dateutil.parser.parse(kwargs['timeMin']) time_max = dateutil.parser.parse(kwargs['timeMax']) order_by = kwargs['orderBy'] single_events = kwargs['singleEvents'] page_token = kwargs['pageToken'] or 0 # page number in this case q = kwargs['q'] test_events = [ Event('test{}'.format(i), start=insure_localisation(D.today()[:] + i * days + i * hours), event_id='1', _updated=insure_localisation(D.today()[:] + (i + 1) * days + i * hours), attendees=[ Attendee(email='{}@gmail.com'.format( attendee_name.lower()), display_name=attendee_name) ] if attendee_name else None) for i, attendee_name in zip(range(1, 10), ['John', 'Josh'] + [''] * 8) ] recurring_event = Event( 'Recurring event', start=insure_localisation(D.today()[:] + 2 * days), event_id='recurring_id', _updated=insure_localisation(D.today()[:] + 3 * days)) recurring_instances = [ Event( recurring_event.summary, start=recurring_event.start + i * days, event_id=recurring_event.id + '_' + (recurring_event.start + i * days).isoformat() + 'Z', _updated=recurring_event.updated, _recurring_event_id=recurring_event.id, ) for i in range(10) ] if single_events: test_events.extend(recurring_instances) else: test_events.append(recurring_event) event_in_a_year = Event( 'test42', start=insure_localisation(D.today()[:] + 1 * years + 2 * days), event_id='42', _updated=insure_localisation(D.today()[:] + 1 * years + 3 * days), attendees=[ Attendee(email='*****@*****.**', display_name='Frank') ]) test_events.append(event_in_a_year) def _filter(e): return ((time_min <= e.start and e.end < time_max) and (not q or q in e.summary or (e.description and q in e.description) or (e.attendees and any( (a.display_name and q in a.display_name) for a in e.attendees)))) def _sort_key(e): if order_by is None: return e.id if order_by == 'startTime': return e.start if order_by == 'updated': return e.updated filtered_events = list(filter(_filter, test_events)) ordered_events = sorted(filtered_events, key=_sort_key) serialized_events = list(map(self._serialize_event, ordered_events)) current_page_events = ordered_events[page_token * self.EVENTS_PER_PAGE:(page_token + 1) * self.EVENTS_PER_PAGE] return { 'items': current_page_events, 'nextPageToken': page_token + 1 if (page_token + 1) * 3 < len(serialized_events) else None }