Use db.table() and db.view() in tests, closes #838

This commit is contained in:
Simon Willison 2026-08-12 13:40:55 -07:00
commit 38fe466700
43 changed files with 1321 additions and 1254 deletions

View file

@ -32,15 +32,15 @@ def test_extracts(fresh_db, kwargs, expected_table, use_table_factory):
assert {expected_table, "Trees"} == set(fresh_db.table_names())
assert (
f'CREATE TABLE "{expected_table}" (\n "id" INTEGER PRIMARY KEY,\n "value" TEXT\n)'
== fresh_db[expected_table].schema
== fresh_db.table(expected_table).schema
)
assert (
f'CREATE TABLE "Trees" (\n "id" INTEGER,\n "species_id" INTEGER REFERENCES "{expected_table}"("id")\n)'
== fresh_db["Trees"].schema
== fresh_db.table("Trees").schema
)
# Should have a foreign key reference
assert len(fresh_db["Trees"].foreign_keys) == 1
fk = fresh_db["Trees"].foreign_keys[0]
assert len(fresh_db.table("Trees").foreign_keys) == 1
fk = fresh_db.table("Trees").foreign_keys[0]
assert fk.table == "Trees"
assert fk.column == "species_id"
@ -54,22 +54,22 @@ def test_extracts(fresh_db, kwargs, expected_table, use_table_factory):
partial=0,
columns=["value"],
)
] == fresh_db[expected_table].indexes
] == fresh_db.table(expected_table).indexes
# Finally, check the rows
assert [{"id": 1, "value": "Oak"}, {"id": 2, "value": "Palm"}] == list(
fresh_db[expected_table].rows
fresh_db.table(expected_table).rows
)
assert [
{"id": 1, "species_id": 1},
{"id": 2, "species_id": 1},
{"id": 3, "species_id": 2},
] == list(fresh_db["Trees"].rows)
] == list(fresh_db.table("Trees").rows)
def test_extracts_null_values(fresh_db):
# https://github.com/simonw/sqlite-utils/issues/186
# Null values should stay null, not be extracted into the lookup table
fresh_db["Trees"].insert_all(
fresh_db.table("Trees").insert_all(
[
{"id": 1, "species_id": "Oak"},
{"id": 2, "species_id": None},
@ -78,11 +78,11 @@ def test_extracts_null_values(fresh_db):
],
extracts={"species_id": "Species"},
)
assert list(fresh_db["Species"].rows) == [
assert list(fresh_db.table("Species").rows) == [
{"id": 1, "value": "Oak"},
{"id": 2, "value": "Palm"},
]
assert list(fresh_db["Trees"].rows) == [
assert list(fresh_db.table("Trees").rows) == [
{"id": 1, "species_id": 1},
{"id": 2, "species_id": None},
{"id": 3, "species_id": 2},
@ -92,7 +92,7 @@ def test_extracts_null_values(fresh_db):
def test_extracts_null_values_list_mode(fresh_db):
# Same as test_extracts_null_values but for list-based records
fresh_db["Trees"].insert_all(
fresh_db.table("Trees").insert_all(
[
["id", "species_id"],
[1, "Oak"],
@ -102,11 +102,11 @@ def test_extracts_null_values_list_mode(fresh_db):
],
extracts={"species_id": "Species"},
)
assert list(fresh_db["Species"].rows) == [
assert list(fresh_db.table("Species").rows) == [
{"id": 1, "value": "Oak"},
{"id": 2, "value": "Palm"},
]
assert list(fresh_db["Trees"].rows) == [
assert list(fresh_db.table("Trees").rows) == [
{"id": 1, "species_id": 1},
{"id": 2, "species_id": None},
{"id": 3, "species_id": 2},