Beispiel #1
0
    def post(self, schedule_id):
        data = request.get_json()
        if data is not None:
            try:
                name = request.json['name']
                value = float(request.json['value'])
            except KeyError:
                return DiaryBuilder.create_error_response(
                    400, 'Invalid json payload')
            except ValueError:
                return DiaryBuilder.create_error_response(
                    400, 'Value must be number')
        else:
            return DiaryBuilder.create_error_response(415, 'please json')
        item = Item(name=name, value=value)

        s_item = ScheduleItem(schedule_id=schedule_id, item=item)
        try:
            db.session.add(s_item)
            db.session.commit()
        except IntegrityError:
            return DiaryBuilder.create_error_response(409,
                                                      'Item 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_delete_schedule_item(db_handle):
    '''
    Tests deleting item and schedule relationships
    '''
    schedule_item = ScheduleItem(item=_get_item(), schedule=_get_schedule())
    db_handle.session.add(schedule_item)
    db_handle.session.commit()

    assert ScheduleItem.query.count() == 1

    db_handle.session.delete(schedule_item)
    db_handle.session.commit()

    assert ScheduleItem.query.count() == 0
Beispiel #4
0
def test_add_item_schedule(db_handle):
    '''
    Tests that creating the relationship table between items and schedules work
    '''
    item = _get_item()
    schedule = _get_schedule()

    schedule_item = ScheduleItem(schedule=schedule, item=item)

    db_handle.session.add(schedule_item)
    db_handle.session.commit()

    assert Schedule.query.count() == 1
    assert Item.query.count() == 1
    assert ScheduleItem.query.count() == 1
Beispiel #5
0
def test_query_schedule_item(db_handle):
    '''
    Tests the success and failure of querying RoutineItems by their ids
    '''
    item = _get_item()
    schedule = _get_schedule()

    schedule_item = ScheduleItem(schedule=schedule, item=item)

    db_handle.session.add(schedule_item)
    db_handle.session.commit()

    assert len(ScheduleItem.query.filter_by(item_id=item.id).all()) == 1
    assert len(
        ScheduleItem.query.filter_by(schedule_id=schedule.id).all()) == 1

    assert len(ScheduleItem.query.filter_by(item_id=item.id + 1).all()) == 0
    assert len(
        ScheduleItem.query.filter_by(schedule_id=schedule.id + 1).all()) == 0
Beispiel #6
0
def test_update_schedule_item(db_handle):
    '''
    Tests updating the items and schedules of ScheduleItems
    '''
    item1 = Item(name="item1", value=1.0)
    item2 = Item(name="item2", value=2.0)
    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 = ScheduleItem(item=item1, schedule=schedule1)
    db_handle.session.add(schedule_event)
    db_handle.session.commit()
    si = ScheduleItem.query.filter_by(item_id=item1.id).first()
    assert si is not None
    si.item = item2
    si.schedule = schedule2
    db_handle.session.commit()

    assert len(ScheduleItem.query.filter_by(item_id=item2.id).all()) == 1
    assert len(
        ScheduleItem.query.filter_by(schedule_id=schedule2.id).all()) == 1