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)
def new(data): db = get_db() if data is None: data = {} data = clean_and_validate(data) db[data['id']] = data return data
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)
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
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
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
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
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
def get_all(): db = get_db() return db.copy()
def delete(therm_id): db = get_db() del db[int(therm_id)] return {}
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
def list(self): db = get_db() todos = db.execute("SELECT id, task" " FROM todo").fetchall() return todos
def delete(self, task_id): db = get_db() db.execute("DELETE FROM todo WHERE id = ?", (task_id, )) db.commit()
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
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
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"