Skip to content

Commit

Permalink
Merge pull request #37 from AnswerDotAI/update-tests
Browse files Browse the repository at this point in the history
Tests for updates where nothing is updated
  • Loading branch information
jph00 authored Nov 12, 2024
2 parents 89483c0 + da0221d commit 7a42da7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
10 changes: 10 additions & 0 deletions tests/test_query.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import types


# Basic query tests

def test_query(fresh_db):
fresh_db["dogs"].insert_all([{"name": "Cleo"}, {"name": "Pancakes"}])
results = fresh_db.query("select * from dogs order by name desc")
Expand All @@ -15,3 +17,11 @@ def test_execute_returning_dicts(fresh_db):
assert fresh_db.execute_returning_dicts("select * from test") == [
{"id": 1, "bar": 2}
]

def test_query_no_update(fresh_db):
"""Test that a query that doesn't update the database:
1) Returns an empty list and 2) Doesn't change the database"""
fresh_db["message"].insert({"msg_type": "greeting", "content": "hello"})
results = fresh_db.query("update message set msg_type='note' where msg_type='md'")
assert list(results) == []
assert list(fresh_db["message"].rows) == [{"msg_type": "greeting", "content": "hello"}]
15 changes: 15 additions & 0 deletions tests/test_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,29 @@
from sqlite_minutils.db import NotFoundError


## Updates where nothing changes

def test_update_no_change(fresh_db):
"Test updating a row with the same values it already has"
table = fresh_db["table"]
table.insert({"foo": "bar"})
table.update(1, {"foo": "bar"})
assert [{"foo": "bar"}] == list(table.rows)
table.update(1, {})
assert [{"foo": "bar"}] == list(table.rows)

## Updates where something changes

def test_update_rowid_table(fresh_db):
"Test updating a row that just got inserted, using the inserted row's last_pk as rowid"
table = fresh_db["table"]
rowid = table.insert({"foo": "bar"}).last_pk
table.update(rowid, {"foo": "baz"})
assert [{"foo": "baz"}] == list(table.rows)


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

0 comments on commit 7a42da7

Please sign in to comment.