Renamed test_database.py to test_internals_database.py

Also added a db fixture to remove some boilerplate.
This commit is contained in:
Simon Willison 2020-05-30 07:33:02 -07:00
commit ca56c226a9
2 changed files with 190 additions and 201 deletions

View file

@ -9,17 +9,20 @@ import time
import uuid import uuid
@pytest.fixture
def db(app_client):
return app_client.ds.get_database("fixtures")
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_execute(app_client): async def test_execute(db):
db = app_client.ds.databases["fixtures"]
results = await db.execute("select * from facetable") results = await db.execute("select * from facetable")
assert isinstance(results, Results) assert isinstance(results, Results)
assert 15 == len(results) assert 15 == len(results)
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_results_first(app_client): async def test_results_first(db):
db = app_client.ds.databases["fixtures"]
assert None is (await db.execute("select * from facetable where pk > 100")).first() assert None is (await db.execute("select * from facetable where pk > 100")).first()
results = await db.execute("select * from facetable") results = await db.execute("select * from facetable")
row = results.first() row = results.first()
@ -35,8 +38,7 @@ async def test_results_first(app_client):
], ],
) )
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_results_single_value(app_client, query, expected): async def test_results_single_value(db, query, expected):
db = app_client.ds.databases["fixtures"]
results = await db.execute(query) results = await db.execute(query)
if expected: if expected:
assert expected == results.single_value() assert expected == results.single_value()
@ -46,9 +48,7 @@ async def test_results_single_value(app_client, query, expected):
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_execute_fn(app_client): async def test_execute_fn(db):
db = app_client.ds.databases["fixtures"]
def get_1_plus_1(conn): def get_1_plus_1(conn):
return conn.execute("select 1 + 1").fetchall()[0][0] return conn.execute("select 1 + 1").fetchall()[0][0]
@ -63,16 +63,14 @@ async def test_execute_fn(app_client):
), ),
) )
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_table_exists(app_client, tables, exists): async def test_table_exists(db, tables, exists):
db = app_client.ds.databases["fixtures"]
for table in tables: for table in tables:
actual = await db.table_exists(table) actual = await db.table_exists(table)
assert exists == actual assert exists == actual
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_get_all_foreign_keys(app_client): async def test_get_all_foreign_keys(db):
db = app_client.ds.databases["fixtures"]
all_foreign_keys = await db.get_all_foreign_keys() all_foreign_keys = await db.get_all_foreign_keys()
assert { assert {
"incoming": [], "incoming": [],
@ -102,8 +100,7 @@ async def test_get_all_foreign_keys(app_client):
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_table_names(app_client): async def test_table_names(db):
db = app_client.ds.databases["fixtures"]
table_names = await db.table_names() table_names = await db.table_names()
assert [ assert [
"simple_primary_key", "simple_primary_key",
@ -139,8 +136,7 @@ async def test_table_names(app_client):
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_execute_write_block_true(app_client): async def test_execute_write_block_true(db):
db = app_client.ds.databases["fixtures"]
await db.execute_write( await db.execute_write(
"update roadside_attractions set name = ? where pk = ?", "update roadside_attractions set name = ? where pk = ?",
["Mystery!", 1], ["Mystery!", 1],
@ -151,8 +147,7 @@ async def test_execute_write_block_true(app_client):
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_execute_write_block_false(app_client): async def test_execute_write_block_false(db):
db = app_client.ds.databases["fixtures"]
await db.execute_write( await db.execute_write(
"update roadside_attractions set name = ? where pk = ?", ["Mystery!", 1], "update roadside_attractions set name = ? where pk = ?", ["Mystery!", 1],
) )
@ -162,9 +157,7 @@ async def test_execute_write_block_false(app_client):
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_execute_write_fn_block_false(app_client): async def test_execute_write_fn_block_false(db):
db = app_client.ds.databases["fixtures"]
def write_fn(conn): def write_fn(conn):
with conn: with conn:
conn.execute("delete from roadside_attractions where pk = 1;") conn.execute("delete from roadside_attractions where pk = 1;")
@ -177,9 +170,7 @@ async def test_execute_write_fn_block_false(app_client):
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_execute_write_fn_block_true(app_client): async def test_execute_write_fn_block_true(db):
db = app_client.ds.databases["fixtures"]
def write_fn(conn): def write_fn(conn):
with conn: with conn:
conn.execute("delete from roadside_attractions where pk = 1;") conn.execute("delete from roadside_attractions where pk = 1;")
@ -191,9 +182,7 @@ async def test_execute_write_fn_block_true(app_client):
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_execute_write_fn_exception(app_client): async def test_execute_write_fn_exception(db):
db = app_client.ds.databases["fixtures"]
def write_fn(conn): def write_fn(conn):
assert False assert False