From 52afdbd08f875f9aa38637afc295ba73c84cd107 Mon Sep 17 00:00:00 2001 From: Andros Fenollosa Date: Wed, 30 Nov 2022 09:52:21 +0100 Subject: [PATCH] Update add with table --- fiable_db.py | 4 +-- test/test_add.py | 58 ++++++++++++++++++++++++-------------- test/test_save_and_load.py | 12 +++++--- todo.org | 7 +++-- 4 files changed, 52 insertions(+), 29 deletions(-) diff --git a/fiable_db.py b/fiable_db.py index 6b09282..3d23434 100644 --- a/fiable_db.py +++ b/fiable_db.py @@ -109,12 +109,12 @@ def add(new_data: Type_Add_Data, table: str = "default") -> Type_Add_Return: """ global database if isinstance(new_data, dict): - new_row = {"id": get_next_id(table), "rev": 1, "data": new_data} + new_row = {"id": get_next_id(table), "rev": 1, "table": table, "data": new_data} database.append(new_row) return new_row elif isinstance(new_data, list): for row in new_data: - new_row = {"id": get_next_id(table), "rev": 1, "data": row} + new_row = {"id": get_next_id(table), "rev": 1, "table": table, "data": row} database.append(new_row) else: raise TypeError("new_data must be a dict or list") diff --git a/test/test_add.py b/test/test_add.py index c9acab3..9f5b260 100644 --- a/test/test_add.py +++ b/test/test_add.py @@ -4,7 +4,9 @@ from fiable_db import add, get_database def test_add_one(): """Add one item to the database.""" add({"name": "John", "age": 42}) - assert get_database() == [{"id": 1, "rev": 1, "data": {"name": "John", "age": 42}}] + assert get_database() == [ + {"id": 1, "rev": 1, "table": "default", "data": {"name": "John", "age": 42}} + ] def test_add_two(): @@ -12,9 +14,9 @@ def test_add_two(): add({"name": "John", "age": 12}) add({"name": "Jane", "age": 34}) assert get_database() == [ - {"id": 1, "rev": 1, "data": {"name": "John", "age": 42}}, - {"id": 2, "rev": 1, "data": {"name": "John", "age": 12}}, - {"id": 3, "rev": 1, "data": {"name": "Jane", "age": 34}}, + {"id": 1, "rev": 1, "table": "default", "data": {"name": "John", "age": 42}}, + {"id": 2, "rev": 1, "table": "default", "data": {"name": "John", "age": 12}}, + {"id": 3, "rev": 1, "table": "default", "data": {"name": "Jane", "age": 34}}, ] @@ -27,11 +29,11 @@ def test_add_list(): ] ) assert get_database() == [ - {"id": 1, "rev": 1, "data": {"name": "John", "age": 42}}, - {"id": 2, "rev": 1, "data": {"name": "John", "age": 12}}, - {"id": 3, "rev": 1, "data": {"name": "Jane", "age": 34}}, - {"id": 4, "rev": 1, "data": {"name": "John", "age": 12}}, - {"id": 5, "rev": 1, "data": {"name": "Jane", "age": 34}}, + {"id": 1, "rev": 1, "table": "default", "data": {"name": "John", "age": 42}}, + {"id": 2, "rev": 1, "table": "default", "data": {"name": "John", "age": 12}}, + {"id": 3, "rev": 1, "table": "default", "data": {"name": "Jane", "age": 34}}, + {"id": 4, "rev": 1, "table": "default", "data": {"name": "John", "age": 12}}, + {"id": 5, "rev": 1, "table": "default", "data": {"name": "Jane", "age": 34}}, ] @@ -39,12 +41,12 @@ def test_add_list_with_one(): """Add a list with one item to the database.""" add([{"name": "John", "age": 42}]) assert get_database() == [ - {"id": 1, "rev": 1, "data": {"name": "John", "age": 42}}, - {"id": 2, "rev": 1, "data": {"name": "John", "age": 12}}, - {"id": 3, "rev": 1, "data": {"name": "Jane", "age": 34}}, - {"id": 4, "rev": 1, "data": {"name": "John", "age": 12}}, - {"id": 5, "rev": 1, "data": {"name": "Jane", "age": 34}}, - {"id": 6, "rev": 1, "data": {"name": "John", "age": 42}}, + {"id": 1, "rev": 1, "table": "default", "data": {"name": "John", "age": 42}}, + {"id": 2, "rev": 1, "table": "default", "data": {"name": "John", "age": 12}}, + {"id": 3, "rev": 1, "table": "default", "data": {"name": "Jane", "age": 34}}, + {"id": 4, "rev": 1, "table": "default", "data": {"name": "John", "age": 12}}, + {"id": 5, "rev": 1, "table": "default", "data": {"name": "Jane", "age": 34}}, + {"id": 6, "rev": 1, "table": "default", "data": {"name": "John", "age": 42}}, ] @@ -52,10 +54,24 @@ def test_add_empty(): """Add an empty list to the database.""" add([]) assert get_database() == [ - {"id": 1, "rev": 1, "data": {"name": "John", "age": 42}}, - {"id": 2, "rev": 1, "data": {"name": "John", "age": 12}}, - {"id": 3, "rev": 1, "data": {"name": "Jane", "age": 34}}, - {"id": 4, "rev": 1, "data": {"name": "John", "age": 12}}, - {"id": 5, "rev": 1, "data": {"name": "Jane", "age": 34}}, - {"id": 6, "rev": 1, "data": {"name": "John", "age": 42}}, + {"id": 1, "rev": 1, "table": "default", "data": {"name": "John", "age": 42}}, + {"id": 2, "rev": 1, "table": "default", "data": {"name": "John", "age": 12}}, + {"id": 3, "rev": 1, "table": "default", "data": {"name": "Jane", "age": 34}}, + {"id": 4, "rev": 1, "table": "default", "data": {"name": "John", "age": 12}}, + {"id": 5, "rev": 1, "table": "default", "data": {"name": "Jane", "age": 34}}, + {"id": 6, "rev": 1, "table": "default", "data": {"name": "John", "age": 42}}, + ] + + +def test_add_in_table_foo(): + """Add an item to the database in the table foo.""" + add({"name": "John", "age": 42}, table="foo") + assert get_database() == [ + {"id": 1, "rev": 1, "table": "default", "data": {"name": "John", "age": 42}}, + {"id": 2, "rev": 1, "table": "default", "data": {"name": "John", "age": 12}}, + {"id": 3, "rev": 1, "table": "default", "data": {"name": "Jane", "age": 34}}, + {"id": 4, "rev": 1, "table": "default", "data": {"name": "John", "age": 12}}, + {"id": 5, "rev": 1, "table": "default", "data": {"name": "Jane", "age": 34}}, + {"id": 6, "rev": 1, "table": "default", "data": {"name": "John", "age": 42}}, + {"id": 7, "rev": 1, "table": "foo", "data": {"name": "John", "age": 42}}, ] diff --git a/test/test_save_and_load.py b/test/test_save_and_load.py index ae961c4..1264624 100644 --- a/test/test_save_and_load.py +++ b/test/test_save_and_load.py @@ -3,11 +3,13 @@ from fiable_db import start, add, save, get_database, load filename = "fiabledb.json" + def delete_file(): """Delete the database file.""" if os.path.exists(filename): os.remove(filename) - + + def test_empty(): """Test that save() works when the database is empty.""" delete_file() @@ -24,7 +26,9 @@ def test_one(): add({"name": "John", "age": 30}) save() load() - assert get_database() == [{"id": 1, "rev": 1, "data": {"name": "John", "age": 30}}] + assert get_database() == [ + {"id": 1, "rev": 1, "table": "default", "data": {"name": "John", "age": 30}} + ] def test_two(): @@ -36,6 +40,6 @@ def test_two(): save() load() assert get_database() == [ - {"id": 1, "rev": 1, "data": {"name": "John", "age": 30}}, - {"id": 2, "rev": 1, "data": {"name": "Jane", "age": 28}}, + {"id": 1, "rev": 1, "table": "default", "data": {"name": "John", "age": 30}}, + {"id": 2, "rev": 1, "table": "default", "data": {"name": "Jane", "age": 28}}, ] diff --git a/todo.org b/todo.org index d95d34f..4bb6150 100644 --- a/todo.org +++ b/todo.org @@ -1,2 +1,5 @@ -[] Add table in add testing -[] Test Update +* Tasks + +** DONE Add table in add testing +** IN PROGRESS Test update +** TODO Function update