-
-
Notifications
You must be signed in to change notification settings - Fork 642
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Missing tests for insert row API, refs #1851
- Loading branch information
Showing
1 changed file
with
38 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from datasette.app import Datasette | ||
from datasette.utils import sqlite3 | ||
import pytest | ||
import time | ||
|
||
|
||
@pytest.fixture | ||
def ds_write(tmp_path_factory): | ||
db_directory = tmp_path_factory.mktemp("dbs") | ||
db_path = str(db_directory / "data.db") | ||
db = sqlite3.connect(str(db_path)) | ||
db.execute("vacuum") | ||
db.execute("create table docs (id integer primary key, title text, score float)") | ||
ds = Datasette([db_path]) | ||
yield ds | ||
db.close() | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_write_row(ds_write): | ||
token = "dstok_{}".format( | ||
ds_write.sign( | ||
{"a": "root", "token": "dstok", "t": int(time.time())}, namespace="token" | ||
) | ||
) | ||
response = await ds_write.client.post( | ||
"/data/docs", | ||
json={"insert": {"title": "Test", "score": 1.0}}, | ||
headers={ | ||
"Authorization": "Bearer {}".format(token), | ||
"Content-Type": "application/json", | ||
}, | ||
) | ||
expected_row = {"id": 1, "title": "Test", "score": 1.0} | ||
assert response.status_code == 201 | ||
assert response.json()["inserted_row"] == expected_row | ||
rows = (await ds_write.get_database("data").execute("select * from docs")).rows | ||
assert dict(rows[0]) == expected_row |