Exemple #1
0
def test_get_close_db(app):
    """Test the opening and closing of the database."""
    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)
Exemple #2
0
def app():
    """Create flask app for testing with a temporary db."""
    db_fd, db_path = tempfile.mkstemp()
    app = create_app({"TESTING": True, "DATABASE": db_path})

    with app.app_context():
        init_db()

    # create the database and load test data
    with app.app_context():
        init_db()
        get_db().executescript(_data_sql)

    yield app

    # cleanup temporary files
    os.close(db_fd)
    os.unlink(db_path)
Exemple #3
0
def get_names(table):
    """Get the names from a given table as dict {id: name}."""
    # TODO: test function
    db = get_db()
    if check_table_name(table):
        # TODO: Check if the table has name column
        names = db.execute("SELECT id, name FROM " + table).fetchall()
    else:
        raise ValueError("The table of name {} does not exits".format(table))
    return list_to_dict(names)
Exemple #4
0
def test_add_names_new_name(client, app, table, new_name):
    """Test for only adding new names."""
    response = client.post("/add_data/" + table, data={"name": new_name})
    with app.app_context():
        # somehow name_in_table does not work
        db = get_db()
        entry = db.execute("SELECT id FROM " + table + " WHERE name = ?",
                           (new_name, )).fetchone()
        assert entry is not None
        # redirect to index after successful entry
        assert b"<a href=\"/\">" in response.data
Exemple #5
0
def write_transaction(date, amount, contact_id, payment_method_id,
                      category_id):
    """Write a new entry into the money_transfer table."""
    db = get_db()
    # date is saved as a timestamp from 01.01.1970
    date = int(date.timestamp())
    db.execute(
        "INSERT INTO money_transfer " +
        "(transaction_date, amount, contact_id, payment_method_id, " +
        " category_id) VALUES (?, ?, ?, ?, ?)",
        (date, amount, contact_id, payment_method_id, category_id))
    db.commit()
Exemple #6
0
def add_name_to_table(name, table):
    """Add the name into the table.

    Returns:
        bool: If the entry was succesfully inserted.

    """
    db = get_db()
    if check_table_name(table):
        db.execute("INSERT INTO " + table + " (name) VALUES (?)", (name, ))
        db.commit()
        return True
    return False
Exemple #7
0
def name_in_table(name, table):
    """Check if the given name already exists in the given table.

    Returns:
        bool: If the entry is in the given table.

    """
    db = get_db()
    if check_table_name(table):
        entry = db.execute("SELECT id FROM " + table + " WHERE name = ?",
                           (name, )).fetchone()
    else:
        raise ValueError("The table of name {} does not exits".format(table))
    return entry is None