Exemplo n.º 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)
Exemplo n.º 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]
Exemplo n.º 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)
Exemplo n.º 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]
Exemplo n.º 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'])
Exemplo n.º 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()