Beispiel #1
0
    def post(self, schedule_id):
        if request.json is not None:
            try:
                name = request.json['name']
                duration = int(request.json['duration'])
                note = request.json.get('note', None)
            except KeyError:
                return DiaryBuilder.create_error_response(
                    400, 'Missing keys in payload')
            except ValueError:
                return DiaryBuilder.create_error_response(
                    400, 'Duration must be integer')
        else:
            return DiaryBuilder.create_error_response(415, 'please json')

        event = Event(name=name, duration=duration, note=note)
        s_event = ScheduleEvent(schedule_id=schedule_id, event=event)
        try:
            db.session.add(s_event)
            db.session.commit()
        except IntegrityError:
            return DiaryBuilder.create_error_response(409,
                                                      'Event already exists')
        else:
            return Response(status=201)
Beispiel #2
0
def test_cascade_schedule_tables_2(db_handle):
    '''
    Tests that corresponding rows of ScheduleEvent/-Task/-Item tables are deleted when the schedule is deleted
    '''
    event = _get_event()
    task = _get_task()
    item = _get_item()
    schedule = _get_schedule()

    schedule_event = ScheduleEvent(event=event, schedule=schedule)
    schedule_task = ScheduleTask(task=task, schedule=schedule)
    schedule_item = ScheduleItem(item=item, schedule=schedule)

    db_handle.session.add(event)
    db_handle.session.add(task)
    db_handle.session.add(item)
    db_handle.session.add(schedule)
    db_handle.session.add(schedule_event)
    db_handle.session.add(schedule_task)
    db_handle.session.add(schedule_item)
    db_handle.session.commit()

    db_handle.session.delete(schedule)

    assert ScheduleEvent.query.count() == 0
    assert ScheduleTask.query.count() == 0
    assert ScheduleItem.query.count() == 0
Beispiel #3
0
def test_add_event_schedule(db_handle):
    '''
    Tests that creating the relationship table between events and schedules work
    '''
    event = _get_event()
    schedule = _get_schedule()

    schedule_event = ScheduleEvent(schedule=schedule, event=event)
    db_handle.session.add(schedule_event)
    db_handle.session.commit()
    assert Schedule.query.count() == 1
    assert Event.query.count() == 1
    assert ScheduleEvent.query.count() == 1
Beispiel #4
0
def test_delete_schedule_event(db_handle):
    '''
    Tests deleting event and schedule relationships
    '''
    schedule_event = ScheduleEvent(event=_get_event(),
                                   schedule=_get_schedule())
    db_handle.session.add(schedule_event)
    db_handle.session.commit()

    assert ScheduleEvent.query.count() == 1

    db_handle.session.delete(schedule_event)
    db_handle.session.commit()

    assert ScheduleEvent.query.count() == 0
Beispiel #5
0
def test_query_schedule_event(db_handle):
    '''
    Tests the success and failure of querying ScheduleEvents by their ids
    '''
    event = _get_event()
    schedule = _get_schedule()

    schedule_event = ScheduleEvent(schedule=schedule, event=event)
    db_handle.session.add(schedule_event)
    db_handle.session.commit()

    assert len(ScheduleEvent.query.filter_by(event_id=event.id).all()) == 1
    assert len(
        ScheduleEvent.query.filter_by(schedule_id=schedule.id).all()) == 1

    assert len(ScheduleEvent.query.filter_by(event_id=event.id + 1).all()) == 0
    assert len(
        ScheduleEvent.query.filter_by(schedule_id=schedule.id + 1).all()) == 0
Beispiel #6
0
def test_update_schedule_event(db_handle):
    '''
    Tests updating the events and schedules of ScheduleEvents
    '''
    event1 = Event(name="event1", duration=1, note="")
    event2 = Event(name="event2", duration=2, note="")
    schedule1 = Schedule(name="schedule1",
                         start_time=datetime(2019, 1, 1, 12, 0, 0, 0),
                         end_time=datetime(2019, 1, 2, 12, 0, 0, 0))
    schedule2 = Schedule(name="schedule2",
                         start_time=datetime(2019, 2, 1, 12, 0, 0, 0),
                         end_time=datetime(2019, 2, 2, 12, 0, 0, 0))
    schedule_event = ScheduleEvent(event=event1, schedule=schedule1)
    db_handle.session.add(schedule_event)
    db_handle.session.commit()
    se = ScheduleEvent.query.filter_by(event_id=event1.id).first()
    assert se is not None
    se.event = event2
    se.schedule = schedule2
    db_handle.session.commit()

    assert len(ScheduleEvent.query.filter_by(event_id=event2.id).all()) == 1
    assert len(
        ScheduleEvent.query.filter_by(schedule_id=schedule2.id).all()) == 1