示例#1
0
def test_get_close_db(app):
    with app.app_context():
        db = get_db()
        assert db is get_db()

    with pytest.raises(sqlite3.ProgrammingError) as e:
        db.execute('SELECT 1')

    assert 'closed' in str(e.value)
示例#2
0
def find_all_labyaks() -> List[LabYak]:
    db = get_db()
    labyaks_from_db = db.execute('SELECT name, age, sex '
                                 'FROM labyak '
                                 'ORDER BY name').fetchall()

    return [labyak_to_model(record) for record in labyaks_from_db]
示例#3
0
def app():
    db_fd, db_path = tempfile.mkstemp()

    app = create_app({
        'TESTING': True,
        'DATABASE': db_path,
    })

    with app.app_context():
        init_db()
        get_db().executescript(_data_sql)

    yield app

    os.close(db_fd)
    os.unlink(db_path)
示例#4
0
def find_orders_by_day(day: int) -> List[Order]:
    db = get_db()
    records = db.execute(
        'SELECT customer_id, day, milk_requested, skins_requested, milk_allocated, skins_allocated, status '
        'FROM orders '
        'WHERE day = ?', (day, )).fetchall()

    return [order_to_model(record) for record in records]
示例#5
0
def stock_allocated(day: int) -> StockReport:
    db = get_db()
    results = db.execute(
        'SELECT coalesce(sum(milk_allocated), 0) AS milk, '
        '       coalesce(sum(skins_allocated), 0) AS skins '
        'FROM orders '
        'WHERE day <= ?', (day, )).fetchone()
    return StockReport(day=day,
                       milk_liters=results['milk'],
                       skins=results['skins'])
示例#6
0
def save_order(order: Order):
    db = get_db()
    db.execute(
        'INSERT INTO orders ('
        '    customer_id,'
        '    day,'
        '    milk_requested,'
        '    skins_requested,'
        '    milk_allocated,'
        '    skins_allocated,'
        '    status'
        ') '
        'VALUES (?, ?, ?, ?, ?, ?, ?)', order_to_record(order))
    db.commit()