Only set last_pk on singular .insert()/.update(), refs #98

This commit is contained in:
Simon Willison 2020-04-12 20:22:32 -07:00
commit 635c91475a
3 changed files with 41 additions and 14 deletions

View file

@ -135,7 +135,12 @@ def test_create_table_with_not_null(fresh_db):
),
)
def test_create_table_from_example(fresh_db, example, expected_columns):
fresh_db["people"].insert(example)
people_table = fresh_db["people"]
assert None == people_table.last_rowid
assert None == people_table.last_pk
people_table.insert(example)
assert 1 == people_table.last_rowid
assert 1 == people_table.last_pk
assert ["people"] == fresh_db.table_names()
assert expected_columns == [
{"name": col.name, "type": col.type} for col in fresh_db["people"].columns

View file

@ -7,6 +7,7 @@ def test_upsert(fresh_db):
table.insert({"id": 1, "name": "Cleo"}, pk="id")
table.upsert({"id": 1, "age": 5}, pk="id", alter=True)
assert [{"id": 1, "name": "Cleo", "age": 5}] == list(table.rows)
assert 1 == table.last_pk
def test_upsert_all(fresh_db):
@ -17,7 +18,7 @@ def test_upsert_all(fresh_db):
{"id": 1, "name": "Cleo", "age": 5},
{"id": 2, "name": "Nixie", "age": 5},
] == list(table.rows)
assert 2 == table.last_pk
assert table.last_pk is None
def test_upsert_error_if_no_pk(fresh_db):
@ -34,6 +35,7 @@ def test_upsert_with_hash_id(fresh_db):
assert [{"pk": "a5e744d0164540d33b1d7ea616c28f2fa97e754a", "foo": "bar"}] == list(
table.rows
)
assert "a5e744d0164540d33b1d7ea616c28f2fa97e754a" == table.last_pk
def test_upsert_compound_primary_key(fresh_db):
@ -45,8 +47,13 @@ def test_upsert_compound_primary_key(fresh_db):
],
pk=("species", "id"),
)
table.upsert_all([{"species": "dog", "id": 1, "age": 5}], pk=("species", "id"))
assert None == table.last_pk
table.upsert({"species": "dog", "id": 1, "age": 5}, pk=("species", "id"))
assert ("dog", 1) == table.last_pk
assert [
{"species": "dog", "id": 1, "name": "Cleo", "age": 5},
{"species": "cat", "id": 1, "name": "Catbag", "age": None},
] == list(table.rows)
# .upsert_all() with a single item should set .last_pk
table.upsert_all([{"species": "cat", "id": 1, "age": 5}], pk=("species", "id"))
assert ("cat", 1) == table.last_pk