def test_event_delete(self): event = CalendarIdCreatedEvent(str(uuid.uuid4()), 1, 1) EventEvent.append_event(event) with session_scope(self.DBSession) as session: session.add(CalendarId( user_id=1, id=1)) now = datetime.datetime.now() post_data = json.dumps( dict(name='Pranzo Dalla Nonna', location=[44.6368, 10.5697], start_time=strftime(now), end_time=strftime(now + datetime.timedelta(hours=1)), recurrence_rule='DAILY', next_is_base=False, until=strftime(now + datetime.timedelta(days=3)), flex=True, flex_duration=1800)) self.client.post('/users/1/calendars/1/events', data=post_data, content_type='application/json') self.simulate_eventual_consistency() self.client.delete('/users/1/calendars/1/events/1') self.simulate_eventual_consistency() with session_scope(self.DBSession) as session: self.assertIsNone(session.query(Event).first())
def handle_get_event(user_id, calendar_id, id): """ This query endpoint returns an event resource. :return: a json object with the event info on a 200 response status code. """ cal_id = get_user_calendar_id(user_id, calendar_id) if not cal_id: return not_found(jsonify(dict(error='Calendar not found'))) event, event_recurrences = get_event(user_id, calendar_id, id) if not event: return not_found(jsonify(dict(error='Event not found'))) response = dict(name=event.name, location=event.location, start_time=strftime(event.start_time), end_time=strftime(event.end_time), next_is_base=event.next_is_base, recurrence_rule=event.recurrence_rule.value, until=strftime(event.until), flex=event.flex, flex_duration=event.flex_duration, recurrences=[]) for recurrence in event_recurrences: response['recurrences'].append( dict(recurrence_id=recurrence.id, start_time=strftime(recurrence.start_time), end_time=strftime(recurrence.end_time))) return ok(jsonify(response))
def test_event_get(self): event = CalendarIdCreatedEvent(str(uuid.uuid4()), 1, 1) EventEvent.append_event(event) with session_scope(self.DBSession) as session: session.add(CalendarId( user_id=1, id=1)) now = datetime.datetime.now() post_data = json.dumps( dict(name='Evento', location=[44.6368, 10.5697], start_time=strftime((now + datetime.timedelta(days=1))), end_time=strftime((now + datetime.timedelta(days=1, hours=1))), recurrence_rule='MONTHLY', next_is_base=False, until=strftime(datetime.datetime(year=2018, month=2, day=1)), flex=True, flex_duration=1800)) self.client.post('/users/1/calendars/1/events', data=post_data, content_type='application/json') self.simulate_eventual_consistency() response = self.client.get('/users/1/calendars/1/events/1') self.assertEqual(response.status_code, 200)
def handle_get_schedule(user_id): """ This query endpoint returns the user schedule. :return: a json object with the list of all the recurrences in the user schedule on a 200 response status code. """ recurrences_and_names = get_schedule(user_id) response = dict(recurrences=[]) for recurrence, name in recurrences_and_names: response['recurrences'].append( dict(user_id=user_id, calendar_id=recurrence.calendar_id, event_id=recurrence.event_id, id=recurrence.id, start_time=strftime(recurrence.start_time), end_time=strftime(recurrence.end_time), event_name=name)) return ok(jsonify(response))
def handle_get_all_calendar_events(user_id, calendar_id): """ This query endpoint returns all the events of a user calendar. :return: a json object listing all the events in the user calendar on a 200 response status code. """ cal_id = get_user_calendar_id(user_id, calendar_id) if not cal_id: return not_found(jsonify(dict(error='Calendar not found'))) events = get_all_calendar_events(user_id, calendar_id) response = {} for event, event_recurrences in events: response[str(event.id)] = dict( name=event.name, location=event.location, start_time=strftime(event.start_time), end_time=strftime(event.end_time), next_is_base=event.next_is_base, recurrence_rule=event.recurrence_rule.value, until=strftime(event.until), flex=event.flex, flex_duration=event.flex_duration, recurrences=dict() ) for recurrence in event_recurrences: response[str(event.id)]['recurrences'].update( { str(recurrence.id): dict( start_time=strftime(recurrence.start_time), end_time=strftime(recurrence.end_time) ) } ) return ok(jsonify(response))
def test_event_validations(self): now = datetime.datetime.now() event_from_client = dict( name='Pranzo Dalla Nonna', location=[44.6368, 10.5697], start_time=strftime(now), end_time=strftime(now + datetime.timedelta(hours=1)), recurrence_rule='NORMAL', next_is_base=False) self.assertTrue(validate_event(event_from_client)) event_from_client = dict( name='Pranzo Dalla Nonna', location=[44.6368, 10.5697], start_time=strftime(now), end_time=strftime(now - datetime.timedelta(hours=1)), recurrence_rule='NORMAL', next_is_base=False) self.assertFalse(validate_event(event_from_client)) event_from_client = dict( name='Pranzo Dalla Nonna', location=[204.6368, 10.5697], start_time=strftime(now), end_time=strftime(now + datetime.timedelta(hours=1)), recurrence_rule='NORMAL', next_is_base=False) self.assertFalse(validate_event(event_from_client)) event_from_client = dict( name='Pranzo Dalla Nonna', location=[44.6368, 10.5697], start_time=strftime(now), end_time=strftime(now + datetime.timedelta(hours=1)), recurrence_rule='DAILY', next_is_base=False, until=strftime(now + datetime.timedelta(days=3)), flex=None, flex_duration=None) self.assertTrue(validate_event(event_from_client)) event_from_client = dict( name='Pranzo Dalla Nonna', location=[44.6368, 10.5697], start_time=strftime(now), end_time=strftime(now + datetime.timedelta(days=2)), recurrence_rule='DAILY', next_is_base=False, until=strftime(now + datetime.timedelta(days=3)), flex=None, flex_duration=None) self.assertFalse(validate_event(event_from_client)) event_from_client = dict( name='Pranzo Dalla Nonna', location=[44.6368, 10.5697], start_time=strftime(now), end_time=strftime(now + datetime.timedelta(hours=1)), recurrence_rule='DAILY', next_is_base=False, until=strftime(now + datetime.timedelta(days=3)), flex=True, flex_duration=1800) self.assertTrue(validate_event(event_from_client)) event_from_client = dict( name='Pranzo Dalla Nonna', location=[44.6368, 10.5697], start_time=strftime(now), end_time=strftime(now + datetime.timedelta(hours=1)), recurrence_rule='DAILY', next_is_base=False, until=strftime(now + datetime.timedelta(days=3)), flex=True, flex_duration=3600) self.assertFalse(validate_event(event_from_client))
def test_event_create(self): event = CalendarIdCreatedEvent(str(uuid.uuid4()), 1, 1) EventEvent.append_event(event) with session_scope(self.DBSession) as session: session.add(CalendarId(user_id=1, id=1)) now = datetime.datetime.now() post_data = json.dumps( dict(name='Pranzo Dalla Nonna', location=[44.6368, 10.5697], start_time=strftime(now), end_time=strftime(now + datetime.timedelta(hours=1)), recurrence_rule='DAILY', next_is_base=False, until=strftime(now + datetime.timedelta(days=3)), flex=True, flex_duration=1800)) response = self.client.post('/users/1/calendars/1/events', data=post_data, content_type='application/json') self.simulate_eventual_consistency() self.assertEqual(response.status_code, 201) now = datetime.datetime.now() post_data = json.dumps( dict(name='Football Match', location=[44.6368, 10.5697], start_time=strftime(now + datetime.timedelta(days=3)), end_time=strftime(now + datetime.timedelta(days=4)), recurrence_rule='NORMAL', next_is_base=False, until=None, flex=None, flex_duration=None)) response = self.client.post('/users/1/calendars/1/events', data=post_data, content_type='application/json') self.simulate_eventual_consistency() self.assertEqual(response.status_code, 400) # the event overlaps now = datetime.datetime.now() post_data = json.dumps( dict(name='Football Match', location=[44.6368, 10.5697], start_time=strftime(now + datetime.timedelta(days=10)), end_time=strftime(now + datetime.timedelta(days=12)), recurrence_rule='NORMAL', next_is_base=False, until=None, flex=None, flex_duration=None)) response = self.client.post('/users/1/calendars/1/events', data=post_data, content_type='application/json') self.simulate_eventual_consistency() self.assertEqual(response.status_code, 201)
def test_aggregate_builder(self): event1 = CalendarIdCreatedEvent(uuid=str(uuid.uuid4()), user_id=1, id=1) event2 = CalendarIdCreatedEvent(uuid=str(uuid.uuid4()), user_id=1, id=2) now = datetime.datetime.now() event3 = EventCreatedEvent( user_id=1, calendar_id=1, id=1, name='Meeting', location=[44.6368, 10.5697], start_time=strftime(now), end_time=strftime(now + datetime.timedelta(hours=1)), recurrence_rule='NORMAL', next_is_base=False, until=None, flex=None, flex_duration=None ) now = datetime.datetime.now() event4 = EventCreatedEvent( user_id=1, calendar_id=1, id=2, name='Cena Natalizia', location=[44.6368, 10.5697], start_time=strftime(now), end_time=strftime(now + datetime.timedelta(hours=1)), next_is_base=False, recurrence_rule='DAILY', until=strftime(now + datetime.timedelta(days=3)), flex=True, flex_duration=3600 ) now = datetime.datetime.now() event5 = EventModifiedEvent( user_id=1, calendar_id=1, id=1, name='Lol finals', location=[44.6368, 10.5697], start_time=strftime(now), end_time=strftime(now + datetime.timedelta(hours=1)), next_is_base=False, recurrence_rule='DAILY', until=strftime(now + datetime.timedelta(days=10)), flex=True, flex_duration=3600 ) now = datetime.datetime.now() event6 = EventCreatedEvent( user_id=1, calendar_id=2, id=1, name='For lab', location=[44.6368, 10.5697], start_time=strftime(now), end_time=strftime(now + datetime.timedelta(hours=1)), recurrence_rule='NORMAL', next_is_base=False, until=None, flex=None, flex_duration=None ) event7 = CalendarIdCreatedEvent(uuid=str(uuid.uuid4()), user_id=2, id=1) now = datetime.datetime.now() event8 = EventCreatedEvent( user_id=2, calendar_id=1, id=1, name='Cinema', location=[44.6368, 10.5697], start_time=strftime(now), end_time=strftime(now + datetime.timedelta(hours=1)), recurrence_rule='NORMAL', next_is_base=False, until=None, flex=None, flex_duration=None ) event9 = EventDeletedEvent( user_id=1, calendar_id=1, id=2 ) EventEvent.append_event(event1) EventEvent.append_event(event2) EventEvent.append_event(event3) EventEvent.append_event(event4) EventEvent.append_event(event5) EventEvent.append_event(event6) EventEvent.append_event(event7) EventEvent.append_event(event8) EventEvent.append_event(event9) aggregate_status = build_event_aggregate() self.assertEqual(len(aggregate_status['1']['calendars'].keys()), 2) self.assertEqual(aggregate_status['1']['calendars']['1']['events']['1']['name'], 'Lol finals') self.assertEqual(len(aggregate_status['1']['calendars']['1']['events']['1']['recurrences'].keys()), 11) self.assertEqual(aggregate_status['2']['calendars']['1']['events']['1']['name'], 'Cinema') self.assertTrue('2' not in aggregate_status['1']['calendars']['1']['events']) event10 = CalendarIdDeletedEvent(uuid=str(uuid.uuid4()), user_id=1, id=1) EventEvent.append_event(event10) aggregate_status = build_event_aggregate() self.assertEqual(len(aggregate_status['1']['calendars'].keys()), 1)
def test_events(self): event = CalendarIdCreatedEvent(str(uuid.uuid4()), 1, 1) publish_event(self.app.sending_channel, event, CALENDAR_ID_CREATED) self.simulate_eventual_consistency() now = datetime.datetime.now() event = EventCreatedEvent( user_id=1, calendar_id=1, id=1, name='Pranzo Dalla Nonna', location=[44.6368, 10.5697], start_time=strftime(now), end_time=strftime(now + datetime.timedelta(hours=1)), next_is_base=False, recurrence_rule='DAILY', until=strftime(now + datetime.timedelta(days=3)), flex=True, flex_duration=3600 ) publish_event(self.app.sending_channel, event, EVENT_CREATED) self.simulate_eventual_consistency() with session_scope(self.DBSession) as session: self.assertEqual(len(session.query(Recurrence).all()), 4) event = EventModifiedEvent( user_id=1, calendar_id=1, id=1, name='Lol finals', location=[44.6368, 10.5697], start_time=strftime(now), end_time=strftime(now + datetime.timedelta(hours=1)), next_is_base=False, recurrence_rule='DAILY', until=strftime(now + datetime.timedelta(days=10)), flex=True, flex_duration=3600 ) publish_event(self.app.sending_channel, event, EVENT_MODIFIED) self.simulate_eventual_consistency() with session_scope(self.DBSession) as session: self.assertEqual(len(session.query(Event).all()), 1) self.assertEqual(session.query(Event).first().name, 'Lol finals') self.assertEqual(len(session.query(Recurrence).all()), 11) event = EventDeletedEvent( user_id=1, calendar_id=1, id=1 ) publish_event(self.app.sending_channel, event, EVENT_DELETED) self.simulate_eventual_consistency() with session_scope(self.DBSession) as session: self.assertIsNone(session.query(Event).first()) self.assertIsNone(session.query(Recurrence).first()) event = EventCreatedEvent( user_id=1, calendar_id=1, id=1, name='Pranzo Dalla Nonna', location=[44.6368, 10.5697], start_time=strftime(now), end_time=strftime(now + datetime.timedelta(hours=1)), next_is_base=False, recurrence_rule='NORMAL', until=None, flex=None, flex_duration=None ) publish_event(self.app.sending_channel, event, EVENT_CREATED) self.simulate_eventual_consistency() with session_scope(self.DBSession) as session: self.assertEqual(len(session.query(Recurrence).all()), 1) event = CalendarIdDeletedEvent(str(uuid.uuid4()), 1, 1) publish_event(self.app.sending_channel, event, CALENDAR_ID_DELETED) self.simulate_eventual_consistency() with session_scope(self.DBSession) as session: self.assertIsNone(session.query(Event).first()) self.assertIsNone(session.query(Recurrence).first())