示例#1
0
    def test_calendar_delete(self):
        default_vehicles = ['bus', 'subway', 'train', 'tram', 'car', 'walking', 'bike', 'taxi', 'enjoy', 'mobike']
        default_preferences = dict(vehicles=default_vehicles, personal_vehicles=[])

        with session_scope(self.DBSession) as session:
            session.add(GlobalPreferences(
                user_id=1,
                preferences=default_preferences))

        CalendarEvent.append_event(GlobalPreferencesCreatedEvent(str(uuid.uuid4()), 1, default_preferences))

        post_data = json.dumps(dict(name='Home', description='Home sweet home', base=[50, 50],
                                    color=[243, 250, 152],
                                    active=True, carbon=True,
                                    preferences=[dict(
                                        name='bus',
                                        time=['19:00', '20:30'],
                                        mileage=10
                                    )]))
        self.client.post('/users/1/calendars', data=post_data, content_type='application/json')
        self.simulate_eventual_consistency()

        self.client.delete('/users/1/calendars/1')
        self.simulate_eventual_consistency()

        with session_scope(self.DBSession) as session:
            self.assertIsNone(session.query(Calendar).filter(Calendar.user_id == 1, Calendar.id == 1).first())
    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())
示例#3
0
    def test_global_preferences_modify(self):
        default_vehicles = ['bus', 'subway', 'train', 'tram', 'car', 'walking', 'bike', 'taxi', 'enjoy', 'mobike']
        default_preferences = dict(vehicles=default_vehicles, personal_vehicles=[])

        with session_scope(self.DBSession) as session:
            session.add(GlobalPreferences(
                user_id=1,
                preferences=default_preferences))

        CalendarEvent.append_event(GlobalPreferencesCreatedEvent(str(uuid.uuid4()), 1, default_preferences))

        new_preferences = json.dumps(dict(
            vehicles=['bus', 'subway'],
            personal_vehicles=[
                dict(
                    name="tesla",
                    type='car',
                    location=(13, 14),
                    active=True
                )
            ])
        )
        response = self.client.put('/users/1/preferences', data=new_preferences, content_type='application/json')
        self.assertTrue(response.status_code, 200)
        self.assertEqual(json.loads(response.data)['id'], 1)
        self.simulate_eventual_consistency()

        with session_scope(self.DBSession) as session:
            self.assertEqual(
                session.query(GlobalPreferences).filter(GlobalPreferences.user_id == 1).first().preferences,
                json.loads(new_preferences))

        response = self.client.put('/users/10/preferences', data=new_preferences, content_type='application/json')
        self.assertTrue(response.status_code, 404)
示例#4
0
    def test_user_create(self):
        post_data = json.dumps(
            dict(email='*****@*****.**',
                 password='******',
                 first_name='test',
                 last_name='test'))
        response = self.client.post('/users',
                                    data=post_data,
                                    content_type='application/json')
        self.assertEqual(response.status_code, 201)
        self.assertEqual(json.loads(response.data)['id'], 1)
        self.simulate_eventual_consistency()

        with session_scope(self.DBSession) as session:
            self.assertEqual(
                session.query(User).filter(User.id == 1).first().email,
                '*****@*****.**')

        post_data = json.dumps(
            dict(email='*****@*****.**',
                 password='******',
                 first_name='prova',
                 last_name='prova'))
        response = self.client.post('/users',
                                    data=post_data,
                                    content_type='application/json')
        self.assertEqual(json.loads(response.data)['id'], 2)
        self.simulate_eventual_consistency()

        with session_scope(self.DBSession) as session:
            self.assertEqual(
                session.query(User).filter(User.id == 2).first().email,
                '*****@*****.**')
    def test_external_events(self):
        user_created_event = dict(
            uuid=str(uuid.uuid4()),
            type=USER_CREATED_EVENT,
            event_info=dict(
                id='234',
                email='*****@*****.**',
                password='******',
                first_name='prova',
                last_name='prova'
            )
        )
        self.publish_from_external(json.dumps(user_created_event), USER_CREATED)
        self.simulate_eventual_consistency()

        with session_scope(self.DBSession) as session:
            self.assertIsNotNone(session.query(GlobalPreferences).filter(GlobalPreferences.user_id == 234).first())

        user_deleted_event = dict(
            uuid=str(uuid.uuid4()),
            type=USER_DELETED_EVENT,
            event_info=dict(
                id='234'
            )
        )
        self.publish_from_external(json.dumps(user_deleted_event), USER_DELETED)
        self.simulate_eventual_consistency()

        with session_scope(self.DBSession) as session:
            self.assertIsNone(session.query(GlobalPreferences).filter(GlobalPreferences.user_id == 234).first())
示例#6
0
    def test_update_interface(self):
        add_calendar_id(CalendarId(user_id=1, id=1))
        with session_scope(self.DBSession) as session:
            self.assertIsNotNone(session.query(CalendarId).filter(CalendarId.user_id == 1, CalendarId.id == 1).first())

        delete_calendar_id(CalendarId(user_id=1, id=1))
        with session_scope(self.DBSession) as session:
            self.assertIsNone(session.query(CalendarId).filter(CalendarId.user_id == 1, CalendarId.id == 1).first())
示例#7
0
    def test_update_interface(self):
        user = User(id=2, email='*****@*****.**', password=hash_pass('prova'), first_name='prova', last_name='prova')
        add_user(user)
        modify_user(User(id=2, email='*****@*****.**', password=hash_pass('prova'), first_name='test', last_name='prova'))

        with session_scope(self.DBSession) as session:
            self.assertEqual(session.query(User).filter(User.email == '*****@*****.**').first().first_name, 'test')

        delete_user(User(id=2))

        with session_scope(self.DBSession) as session:
            self.assertIsNone(session.query(User).filter(User.id == 2).first())
示例#8
0
    def test_calendar_db(self):
        with session_scope(self.DBSession) as session:
            session.add(
                GlobalPreferences(user_id=1,
                                  preferences=json.dumps(
                                      dict(vehicles=['bus'],
                                           personal_vehicles=[
                                               dict(name='tesla',
                                                    type='car',
                                                    location=(44.700546,
                                                              8.035837),
                                                    active=True)
                                           ]))))

        def add_invalid_calendar():
            with session_scope(self.DBSession) as session:
                session.add(
                    Calendar(
                        user_id=2,
                        id=1,
                        name='Lavoro',
                        description='Full Stack',
                        base=[50.700546, 10.035837],
                        color=[255, 255, 255],
                        active=True,
                        carbon=False,
                        preferences=[dict(name='bus', time=None, mileage=10)]))

        # there is no user_id 2
        self.assertRaises(SQLAlchemyError, add_invalid_calendar)

        with session_scope(self.DBSession) as session:
            session.add(
                Calendar(user_id=1,
                         id=1,
                         name='Lavoro',
                         description='Full Stack',
                         base=[50.700546, 10.035837],
                         color=[255, 255, 255],
                         active=True,
                         carbon=False,
                         preferences=[dict(name='bus', time=None,
                                           mileage=10)]))

        with session_scope(self.DBSession) as session:
            self.assertIsNotNone(
                session.query(GlobalPreferences).filter(
                    GlobalPreferences.user_id == 1).first())
            self.assertIsNotNone(
                session.query(Calendar).filter(
                    Calendar.name == 'Lavoro').first())
示例#9
0
    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)
示例#10
0
    def test_gp_modify(self):
        event = GlobalPreferencesCreatedEvent(
            str(uuid.uuid4()), 1,
            dict(vehicles=[
                'bus', 'subway', 'train', 'tram', 'car', 'walking', 'bike',
                'taxi', 'enjoy', 'mobike'
            ]))
        publish_event(self.app.sending_channel, event, PREFERENCES_CREATED)
        self.simulate_eventual_consistency()

        event = CalendarCreatedEvent(
            user_id=1,
            id=1,
            name='Home',
            description='',
            base=[35.1324, 36.1234],
            color=[243, 250, 152],
            active=True,
            carbon=True,
            preferences=[dict(name='bus', time=None, mileage=10)])
        publish_event(self.app.sending_channel, event, CALENDAR_CREATED)
        self.simulate_eventual_consistency()

        event = GlobalPreferencesModifiedEvent(1, dict(vehicles=['mobike']))
        publish_event(self.app.sending_channel, event, PREFERENCES_MODIFIED)
        self.simulate_eventual_consistency()

        with session_scope(self.DBSession) as session:
            self.assertEqual(
                session.query(Calendar).filter(
                    Calendar.user_id == 1,
                    Calendar.id == 1).first().preferences, [])
示例#11
0
    def test_on_delete_cascade(self):
        with session_scope(self.DBSession) as session:
            session.add(
                GlobalPreferences(user_id=1,
                                  preferences=json.dumps(
                                      dict(vehicles=['bus'],
                                           personal_vehicles=[
                                               dict(name='tesla',
                                                    type='car',
                                                    location=(44.700546,
                                                              8.035837),
                                                    active=True)
                                           ]))))
            session.flush()
            for i in range(1, 10):
                session.add(
                    Calendar(
                        user_id=1,
                        id=i,
                        name=f'Lavoro{i}',
                        description='Full Stack',
                        base=[50.700546, 10.035837],
                        color=[255, 255, 255],
                        active=True,
                        carbon=False,
                        preferences=[dict(name='bus', time=None, mileage=10)]))
            session.flush()

            self.assertEqual(len(session.query(Calendar).all()), 9)
            session.delete(
                session.query(GlobalPreferences).filter(
                    GlobalPreferences.user_id == 1).first())
            self.assertIsNone(session.query(Calendar).first())
示例#12
0
def add_calendar_id(calendar_id):
    """
    Adds a new calendarid in the database.
    :param calendar_id: the calendarid to add.
    """
    with session_scope(DBSession) as session:
        session.add(calendar_id)
示例#13
0
def add_user(user):
    """
    Adds a user to the database.
    :param user: the user to add.
    """
    with session_scope(DBSession) as session:
        session.add(user)
示例#14
0
def modify_event(event):
    """
    Modifies an event in the database.
    :param event: the event data needed for the update.
    """
    with session_scope(DBSession) as session:
        for recurrence in session.query(Recurrence).filter(
                Recurrence.user_id == event.user_id,
                Recurrence.calendar_id == event.calendar_id,
                Recurrence.event_id == event.id):
            session.delete(recurrence)

        session.flush()

        event_to_update = session.query(Event).filter(
            Event.user_id == event.user_id,
            Event.calendar_id == event.calendar_id,
            Event.id == event.id).first()

        event_to_update.name = event.name
        event_to_update.location = event.location
        event_to_update.start_time = event.start_time
        event_to_update.end_time = event.end_time
        event_to_update.recurrence_rule = event.recurrence_rule
        event_to_update.until = event.until
        event_to_update.next_is_base = event.next_is_base
        event_to_update.flex = event.flex
        event_to_update.flex_duration = event.flex_duration
        session.flush()

        add_recurrences(session, event)
示例#15
0
def delete_user(user):
    """
    Deletes a user in the database
    :param user: the user to delete.
    """
    with session_scope(DBSession) as session:
        user_to_delete = session.query(User).filter(User.id == user.id).first()
        session.delete(user_to_delete)
示例#16
0
 def add_invalid_event():
     with session_scope(self.DBSession) as session:
         session.add(Event(user_id=1, calendar_id=2, id=1, name='Ultima Cena', location=[50, 40],
                           start_time=datetime.datetime.now(),
                           end_time=datetime.datetime.now() + datetime.timedelta(hours=3),
                           recurrence_rule=Rrule.NORMAL,
                           next_is_base=False,
                           ))
示例#17
0
def get_user_global_preferences(user_id):
    """
    Retrieves a user global preferences.
    :param user_id: the user id.
    :return: the global preferences of the user.
    """
    with session_scope(DBSession) as session:
        global_preferences = session.query(GlobalPreferences).filter(GlobalPreferences.user_id == user_id).first()
        return copy.deepcopy(global_preferences)
 def test_add_event(self):
     with session_scope(self.EventStoreSession) as session:
         event = UserCreatedEvent(1, '*****@*****.**', hash_pass('test_password'), 'test_first_name',
                                  'test_last_name')
         j_event = event.toJSON()
         session.add(UserEvent(uuid=event.uuid, event=j_event))
         q = session.query(UserEvent).first()
         self.assertEqual(q.event, j_event)
         self.assertEqual(json.loads(q.event)['type'], USER_CREATED_EVENT)
示例#19
0
def get_user_by_id(id):
    """
    Retrieves a user from the database given the id.
    :param id: the user id.
    :return: the user id.
    """
    with session_scope(DBSession) as session:
        user = session.query(User).filter(User.id == id).first()
        return copy.deepcopy(user)
示例#20
0
def get_all_user_calendars(user_id):
    """
    Retrieves all the calendars of a given user.
    :param user_id: the user id.
    :return: the list of the calendars of the user.
    """
    with session_scope(DBSession) as session:
        calendars = session.query(Calendar).filter(Calendar.user_id == user_id).all()
        return list(copy.deepcopy(calendar) for calendar in calendars)
 def test_add_event(self):
     with session_scope(self.EventStoreSession) as session:
         event = CalendarIdCreatedEvent(str(uuid.uuid4()), 1, 1)
         j_event = event.toJSON()
         EventEvent.append_event(event)
         q = session.query(EventEvent).first()
         self.assertEqual(q.event, j_event)
         self.assertEqual(
             json.loads(q.event)['type'], CALENDAR_ID_CREATED_EVENT)
示例#22
0
def add_event(event):
    # add event + add all the to be generated recurrences
    """
    Adds a new event in the database with its recurrences.
    :param event: the event to be added.
    """
    with session_scope(DBSession) as session:
        session.add(event)
        session.flush()
        add_recurrences(session, event)
示例#23
0
def delete_calendar_id(calendar_id):
    """
    Deletes a calendarid in the database.
    :param calendar_id: the calendarid to delete.
    """
    with session_scope(DBSession) as session:
        calendar_id_to_delete = session.query(CalendarId).filter(
            CalendarId.user_id == calendar_id.user_id,
            CalendarId.id == calendar_id.id).first()
        session.delete(calendar_id_to_delete)
示例#24
0
def get_calendar(user_id, id):
    """
    Retrieves a user calendar.
    :param user_id: the user id.
    :param id: the calendar id.
    :return: the calendar.
    """
    with session_scope(DBSession) as session:
        calendar = session.query(Calendar).filter(Calendar.user_id == user_id, Calendar.id == id).first()
        return copy.deepcopy(calendar)
示例#25
0
def modify_user(user):
    """
    Modifies a user in the database.
    :param user: the user data to insert.
    """
    with session_scope(DBSession) as session:
        user_to_update = session.query(User).filter(User.id == user.id).first()
        user_to_update.email = user.email
        user_to_update.password = user.password
        user_to_update.first_name = user.first_name
        user_to_update.last_name = user.last_name
示例#26
0
def get_all_user_events(user_id):
    """
    Retrieves all the events of a user.
    :param user_id: the user id.
    :return: the list of tuples where each tuple contain an event and its recurrences.
    """
    with session_scope(DBSession) as session:
        events = session.query(Event).filter(Event.user_id == user_id).all()
        return list(
            (copy.deepcopy(event), get_event_recurrences(session, event))
            for event in events)
示例#27
0
def delete_event(event):
    """
    Deletes an event in the database.
    :param event: the event to delete.
    """
    with session_scope(DBSession) as session:
        event_to_delete = session.query(Event).filter(
            Event.user_id == event.user_id,
            Event.calendar_id == event.calendar_id,
            Event.id == event.id).first()
        session.delete(event_to_delete)
    def test_all_calendars_get(self):
        default_vehicles = [
            'bus', 'subway', 'train', 'tram', 'car', 'walking', 'bike', 'taxi',
            'enjoy', 'mobike'
        ]
        default_preferences = dict(vehicles=default_vehicles,
                                   personal_vehicles=[])

        with session_scope(self.DBSession) as session:
            session.add(
                GlobalPreferences(user_id=1, preferences=default_preferences))

        CalendarEvent.append_event(
            GlobalPreferencesCreatedEvent(str(uuid.uuid4()), 1,
                                          default_preferences))

        post_data = json.dumps(
            dict(name='Home',
                 description='Home sweet home',
                 base=[50, 50],
                 color=[243, 250, 152],
                 active=True,
                 carbon=True,
                 preferences=[
                     dict(name='bus', time=['19:00', '20:30'], mileage=10)
                 ]))
        self.client.post('/users/1/calendars',
                         data=post_data,
                         content_type='application/json')
        self.simulate_eventual_consistency()

        post_data = json.dumps(
            dict(name='Job',
                 description='',
                 base=[50, 50],
                 color=[243, 250, 152],
                 active=True,
                 carbon=True,
                 preferences=[
                     dict(name='bus', time=['19:00', '20:30'], mileage=10)
                 ]))
        self.client.post('/users/1/calendars',
                         data=post_data,
                         content_type='application/json')
        self.simulate_eventual_consistency()

        response = self.client.get('/users/1/calendars')
        self.assertEqual(
            json.loads(response.data)['calendars'][0]['name'], 'Home')
        self.assertEqual(
            json.loads(response.data)['calendars'][1]['name'], 'Job')

        response = self.client.get('/users/128/calendars')
        self.assertEqual(response.status_code, 404)
示例#29
0
def delete_user_calendars(user_id):
    """
    Deletes all the user's calendarids in the database.
    :param user_id: the user id.
    """
    with session_scope(DBSession) as session:
        calendar_ids_to_delete = session.query(CalendarId).filter(
            CalendarId.user_id == user_id).all()

        if calendar_ids_to_delete:  # user has atleast one calendar
            for calendar_id in calendar_ids_to_delete:
                session.delete(calendar_id)
示例#30
0
def delete_recurrence(recurrence):
    """
    Deletes a recurrence in the database.
    :param recurrence: the recurrence to delete.
    """
    with session_scope(DBSession) as session:
        recurrence_to_delete = session.query(Recurrence).filter(
            Recurrence.user_id == recurrence.user_id,
            Recurrence.calendar_id == recurrence.calendar_id,
            Recurrence.event_id == recurrence.event_id,
            Recurrence.id == recurrence.id).first()
        session.delete(recurrence_to_delete)