From da0221de50dcb9ef1d5b19c4724bae15901e6670 Mon Sep 17 00:00:00 2001 From: "Audrey M. Roy Greenfeld" Date: Tue, 12 Nov 2024 16:57:20 +0000 Subject: [PATCH] Tests for updates where nothing is updated Co-authored-by: Daniel Roy Greenfeld --- tests/test_query.py | 10 ++++++++++ tests/test_update.py | 15 +++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/tests/test_query.py b/tests/test_query.py index fe79cc0..9d87460 100644 --- a/tests/test_query.py +++ b/tests/test_query.py @@ -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") @@ -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"}] diff --git a/tests/test_update.py b/tests/test_update.py index aba0079..76b43e6 100644 --- a/tests/test_update.py +++ b/tests/test_update.py @@ -6,7 +6,21 @@ 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"}) @@ -14,6 +28,7 @@ def test_update_rowid_table(fresh_db): 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