コード例 #1
0
def add_basic_room_item_and_borrowhistory(session):
    r = Room('basic')
    session.add(r)
    session.commit()
    i = Item(r, 'toy trucks', 'hudson')
    session.add(i)
    session.commit()
    bh = BorrowHistory(i, 'Luca')
    session.add(bh)
    session.commit()
    return (r, i, bh)
def test_create_Borrow_History(session):
    room, items = add_default_room_and_two_items(session)
    bh = BorrowHistory(items[0], 'Luca')
    session.add(bh)
    session.commit()

    assert BorrowHistory.query.count() == 1
    assert len(Item.query.first().history) == 1
    assert Item.query.first().history[0].who_borrowed == 'Luca'
    # assert bh.date_borrowed != None

    bh.returned = True
    bh.date_returned = datetime.datetime.utcnow()

    bh2 = BorrowHistory(items[0], 'Frank')
    session.add(bh2)
    session.commit()

    assert BorrowHistory.query.count() == 2
    assert len(Item.query.first().history) == 2
    assert items[0].history[0].returned == True
    assert items[0].history[0].date_returned != None
def test_get_info_methods(session):
    room, items = add_default_room_and_two_items(session)
    bh = BorrowHistory(items[0], 'Luca')
    session.add(bh)
    session.commit()

    item_info = room.get_items()
    assert item_info['id'] == room.id
    assert item_info['room_name'] == room.room_name
    assert 'encoded_room_name' not in item_info
    assert len(item_info['items']) == 2
    assert item_info['items'][0]['name'] == items[0].name
    assert len(item_info['items'][0]['history']) == 1
    assert item_info['items'][0]['history'][0]['who_borrowed'] == 'Luca'
コード例 #4
0
def test_delete_cascade_doesnt_delete_parents(session):
    room = Room('basic room')
    session.add(room)
    session.commit()

    item = Item(room, 'toys', 'hudson')
    session.add(item)
    session.commit()

    h = BorrowHistory(item, 'Luca')
    session.add(h)
    session.commit()

    Item.query.filter_by(id=item.id).delete()
    session.commit()

    assert Room.query.count() == 1
    assert Item.query.count() == 0
    assert BorrowHistory.query.count() == 0
コード例 #5
0
def test_delete_cascade(session):
    room = Room('basic room')
    session.add(room)
    session.commit()

    item = Item(room, 'toys', 'hudson')
    session.add(item)
    session.commit()

    h = BorrowHistory(item, 'Luca')
    session.add(h)
    session.commit()

    assert Room.query.first() == room
    assert Item.query.first() == item
    assert BorrowHistory.query.first() == h

    Room.query.filter_by(room_name=room.room_name).delete()
    session.commit()

    assert Room.query.count() == 0
    assert Item.query.count() == 0
    assert BorrowHistory.query.count() == 0
コード例 #6
0
def test_update_borrow_history(session, client):
    r = Room('basic')
    session.add(r)
    session.commit()
    i = Item(r, 'toy trucks', 'hudson')
    session.add(i)
    session.commit()
    bh = BorrowHistory(i, 'Luca')
    session.add(bh)
    session.commit()

    data = {
        'requestedRoom': r.encoded_room_name,
        'password': '',
        'action': {
            'type': 'update',
            'target': 'borrowHistory',
            'targetId': bh.id,
            'dataToUpdate': {
                'date_borrowed': datetime.date(2019, 11, 14).__str__(),
                'due_back': datetime.date(2019, 11, 18).__str__(),
                'notes': 'yuck, it smelled funny'
            }
        }
    }

    response = client.post(url_for('server.update_record'),
                           data=json.dumps(data),
                           headers={'Content-Type': 'application/json'})

    assert response.status_code == 200
    assert response.json['status'] == 'success'
    assert response.json['dbresponse'] == 1
    bhist = BorrowHistory.query.first()
    assert bhist.date_borrowed == datetime.datetime(2019, 11, 14, 0, 0)
    assert bhist.notes == 'yuck, it smelled funny'
    assert bhist.due_back == datetime.datetime(2019, 11, 18, 0, 0)
コード例 #7
0
def add_history_to_item(session, item, who_borrowed='Luca'):
    new_history = BorrowHistory(item, who_borrowed)
    session.add(new_history)
    session.commit()
    return new_history