예제 #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 new(data):
    db = get_db()
    if data is None:
        data = {}
    data = clean_and_validate(data)
    db[data['id']] = data
    return data
예제 #3
0
def app():
    """Create and configure a new app instance for each test."""
    # create a temporary file to isolate the database for each test
    db_fd, db_path = tempfile.mkstemp()
    # create the app with common test config
    app = create_app({"TESTING": True, "DATABASE": db_path})

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

    yield app

    # close and remove the temporary database
    os.close(db_fd)
    os.unlink(db_path)
예제 #4
0
 def update(self, task_id, data):
     task = data
     task['id'] = task_id
     db = get_db()
     db.execute("UPDATE todo SET task = ? WHERE id = ?",
                (data['task'], task_id))
     db.commit()
     return task
예제 #5
0
def test_get(client, app):
    client.get('/todos/1')
    with app.app_context():
        db = get_db()
        post = db.execute("SELECT * FROM todo WHERE id = 1").fetchone()
        assert post["task"] == "Complete Service Template for Python"

    assert client.get("/todos/2").status_code == 404
예제 #6
0
 def get(self, task_id):
     task = (get_db().execute(
         "SELECT id, task"
         " FROM todo"
         " WHERE id = ?",
         (task_id, ),
     ).fetchone())
     if task is None:
         ns.abort(404, "Todo {} doesn't exist".format(task_id))
     return task
예제 #7
0
    def create(self, data):
        task = data
        task['id'] = self.counter = self.counter + 1

        db = get_db()
        db.execute("INSERT INTO todo (id, task) VALUES (?,?)", (
            task['id'],
            task['task'],
        ))
        db.commit()

        return task
예제 #8
0
def edit(data):
    db = get_db()
    if 'id' not in data or data.get('id', -1) not in db:
        raise Exception('thermometer with id {} not found.  '
                        'update failed.'.format(data.get('id', -1)))
    else:
        # get a copy of the record from the db and apply the updates
        thermostat = db[data['id']].copy()
        thermostat.update(data)

        # validate the data after the updates on the copy
        data = clean_and_validate(thermostat)

        # if valid, replace the record with the updates
        db[data['id']] = data
    return data
예제 #9
0
def get_all():
    db = get_db()
    return db.copy()
예제 #10
0
def delete(therm_id):
    db = get_db()
    del db[int(therm_id)]
    return {}
예제 #11
0
def view(therm_id):
    db = get_db()
    data = db.get(int(therm_id))
    if data is None:
        raise Exception('Thermometer with id {} not found.'.format(therm_id))
    return data
예제 #12
0
 def list(self):
     db = get_db()
     todos = db.execute("SELECT id, task" " FROM todo").fetchall()
     return todos
예제 #13
0
 def delete(self, task_id):
     db = get_db()
     db.execute("DELETE FROM todo WHERE id = ?", (task_id, ))
     db.commit()
예제 #14
0
def test_create(client, app):
    client.post('/todos/', json={"task": "Task1"})
    with app.app_context():
        db = get_db()
        count = db.execute('SELECT COUNT(id) FROM todo').fetchone()[0]
        assert count == 2
예제 #15
0
def test_delete(client, app):
    client.delete('/todos/1')
    with app.app_context():
        db = get_db()
        count = db.execute("SELECT COUNT(id) FROM todo").fetchone()[0]
        assert count == 0
예제 #16
0
def test_update(client, app):
    client.put('/todos/1', json={"task": "Task1"})
    with app.app_context():
        db = get_db()
        post = db.execute("SELECT * FROM todo WHERE id = 1").fetchone()
        assert post["task"] == "Task1"