Skip to content

Commit 7a42da7

Browse files
authored
Merge pull request #37 from AnswerDotAI/update-tests
Tests for updates where nothing is updated
2 parents 89483c0 + da0221d commit 7a42da7

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

tests/test_query.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import types
22

33

4+
# Basic query tests
5+
46
def test_query(fresh_db):
57
fresh_db["dogs"].insert_all([{"name": "Cleo"}, {"name": "Pancakes"}])
68
results = fresh_db.query("select * from dogs order by name desc")
@@ -15,3 +17,11 @@ def test_execute_returning_dicts(fresh_db):
1517
assert fresh_db.execute_returning_dicts("select * from test") == [
1618
{"id": 1, "bar": 2}
1719
]
20+
21+
def test_query_no_update(fresh_db):
22+
"""Test that a query that doesn't update the database:
23+
1) Returns an empty list and 2) Doesn't change the database"""
24+
fresh_db["message"].insert({"msg_type": "greeting", "content": "hello"})
25+
results = fresh_db.query("update message set msg_type='note' where msg_type='md'")
26+
assert list(results) == []
27+
assert list(fresh_db["message"].rows) == [{"msg_type": "greeting", "content": "hello"}]

tests/test_update.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,29 @@
66
from sqlite_minutils.db import NotFoundError
77

88

9+
## Updates where nothing changes
10+
11+
def test_update_no_change(fresh_db):
12+
"Test updating a row with the same values it already has"
13+
table = fresh_db["table"]
14+
table.insert({"foo": "bar"})
15+
table.update(1, {"foo": "bar"})
16+
assert [{"foo": "bar"}] == list(table.rows)
17+
table.update(1, {})
18+
assert [{"foo": "bar"}] == list(table.rows)
19+
20+
## Updates where something changes
21+
922
def test_update_rowid_table(fresh_db):
23+
"Test updating a row that just got inserted, using the inserted row's last_pk as rowid"
1024
table = fresh_db["table"]
1125
rowid = table.insert({"foo": "bar"}).last_pk
1226
table.update(rowid, {"foo": "baz"})
1327
assert [{"foo": "baz"}] == list(table.rows)
1428

1529

1630
def test_update_pk_table(fresh_db):
31+
"Like test_update_rowid_table, but with a user-specified primary key"
1732
table = fresh_db["table"]
1833
pk = table.insert({"foo": "bar", "id": 5}, pk="id").last_pk
1934
assert 5 == pk

0 commit comments

Comments
 (0)