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
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")

View File

@ -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}},
]

View File

@ -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}},
]

View File

@ -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