Update add with table

This commit is contained in:
Andros Fenollosa 2022-11-30 09:52:21 +01:00
parent cd3f1eec65
commit 52afdbd08f
4 changed files with 52 additions and 29 deletions

View File

@ -109,12 +109,12 @@ def add(new_data: Type_Add_Data, table: str = "default") -> Type_Add_Return:
""" """
global database global database
if isinstance(new_data, dict): 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) database.append(new_row)
return new_row return new_row
elif isinstance(new_data, list): elif isinstance(new_data, list):
for row in new_data: 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) database.append(new_row)
else: else:
raise TypeError("new_data must be a dict or list") raise TypeError("new_data must be a dict or list")

View File

@ -4,7 +4,9 @@ from fiable_db import add, get_database
def test_add_one(): def test_add_one():
"""Add one item to the database.""" """Add one item to the database."""
add({"name": "John", "age": 42}) 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(): def test_add_two():
@ -12,9 +14,9 @@ def test_add_two():
add({"name": "John", "age": 12}) add({"name": "John", "age": 12})
add({"name": "Jane", "age": 34}) add({"name": "Jane", "age": 34})
assert get_database() == [ assert get_database() == [
{"id": 1, "rev": 1, "data": {"name": "John", "age": 42}}, {"id": 1, "rev": 1, "table": "default", "data": {"name": "John", "age": 42}},
{"id": 2, "rev": 1, "data": {"name": "John", "age": 12}}, {"id": 2, "rev": 1, "table": "default", "data": {"name": "John", "age": 12}},
{"id": 3, "rev": 1, "data": {"name": "Jane", "age": 34}}, {"id": 3, "rev": 1, "table": "default", "data": {"name": "Jane", "age": 34}},
] ]
@ -27,11 +29,11 @@ def test_add_list():
] ]
) )
assert get_database() == [ assert get_database() == [
{"id": 1, "rev": 1, "data": {"name": "John", "age": 42}}, {"id": 1, "rev": 1, "table": "default", "data": {"name": "John", "age": 42}},
{"id": 2, "rev": 1, "data": {"name": "John", "age": 12}}, {"id": 2, "rev": 1, "table": "default", "data": {"name": "John", "age": 12}},
{"id": 3, "rev": 1, "data": {"name": "Jane", "age": 34}}, {"id": 3, "rev": 1, "table": "default", "data": {"name": "Jane", "age": 34}},
{"id": 4, "rev": 1, "data": {"name": "John", "age": 12}}, {"id": 4, "rev": 1, "table": "default", "data": {"name": "John", "age": 12}},
{"id": 5, "rev": 1, "data": {"name": "Jane", "age": 34}}, {"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 a list with one item to the database."""
add([{"name": "John", "age": 42}]) add([{"name": "John", "age": 42}])
assert get_database() == [ assert get_database() == [
{"id": 1, "rev": 1, "data": {"name": "John", "age": 42}}, {"id": 1, "rev": 1, "table": "default", "data": {"name": "John", "age": 42}},
{"id": 2, "rev": 1, "data": {"name": "John", "age": 12}}, {"id": 2, "rev": 1, "table": "default", "data": {"name": "John", "age": 12}},
{"id": 3, "rev": 1, "data": {"name": "Jane", "age": 34}}, {"id": 3, "rev": 1, "table": "default", "data": {"name": "Jane", "age": 34}},
{"id": 4, "rev": 1, "data": {"name": "John", "age": 12}}, {"id": 4, "rev": 1, "table": "default", "data": {"name": "John", "age": 12}},
{"id": 5, "rev": 1, "data": {"name": "Jane", "age": 34}}, {"id": 5, "rev": 1, "table": "default", "data": {"name": "Jane", "age": 34}},
{"id": 6, "rev": 1, "data": {"name": "John", "age": 42}}, {"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 an empty list to the database."""
add([]) add([])
assert get_database() == [ assert get_database() == [
{"id": 1, "rev": 1, "data": {"name": "John", "age": 42}}, {"id": 1, "rev": 1, "table": "default", "data": {"name": "John", "age": 42}},
{"id": 2, "rev": 1, "data": {"name": "John", "age": 12}}, {"id": 2, "rev": 1, "table": "default", "data": {"name": "John", "age": 12}},
{"id": 3, "rev": 1, "data": {"name": "Jane", "age": 34}}, {"id": 3, "rev": 1, "table": "default", "data": {"name": "Jane", "age": 34}},
{"id": 4, "rev": 1, "data": {"name": "John", "age": 12}}, {"id": 4, "rev": 1, "table": "default", "data": {"name": "John", "age": 12}},
{"id": 5, "rev": 1, "data": {"name": "Jane", "age": 34}}, {"id": 5, "rev": 1, "table": "default", "data": {"name": "Jane", "age": 34}},
{"id": 6, "rev": 1, "data": {"name": "John", "age": 42}}, {"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}},
] ]

View File

@ -3,11 +3,13 @@ from fiable_db import start, add, save, get_database, load
filename = "fiabledb.json" filename = "fiabledb.json"
def delete_file(): def delete_file():
"""Delete the database file.""" """Delete the database file."""
if os.path.exists(filename): if os.path.exists(filename):
os.remove(filename) os.remove(filename)
def test_empty(): def test_empty():
"""Test that save() works when the database is empty.""" """Test that save() works when the database is empty."""
delete_file() delete_file()
@ -24,7 +26,9 @@ def test_one():
add({"name": "John", "age": 30}) add({"name": "John", "age": 30})
save() save()
load() 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(): def test_two():
@ -36,6 +40,6 @@ def test_two():
save() save()
load() load()
assert get_database() == [ assert get_database() == [
{"id": 1, "rev": 1, "data": {"name": "John", "age": 30}}, {"id": 1, "rev": 1, "table": "default", "data": {"name": "John", "age": 30}},
{"id": 2, "rev": 1, "data": {"name": "Jane", "age": 28}}, {"id": 2, "rev": 1, "table": "default", "data": {"name": "Jane", "age": 28}},
] ]

View File

@ -1,2 +1,5 @@
[] Add table in add testing * Tasks
[] Test Update
** DONE Add table in add testing
** IN PROGRESS Test update
** TODO Function update