Update updates

This commit is contained in:
Andros Fenollosa 2022-12-05 14:52:09 +01:00
parent 7f00126742
commit 3d09cb6ebf
2 changed files with 56 additions and 8 deletions

View File

@ -132,7 +132,7 @@ def add(new_data: Type_Add_Data, table: str = "default") -> Type_Add_Return:
def update( def update(
id: int, new_data: dict, table: str = "", force: bool = False id: int, new_data: dict, table: str = "default", force: bool = False
) -> Type_Update_Return: ) -> Type_Update_Return:
"""Update data in the database """Update data in the database
Args: Args:
@ -141,9 +141,29 @@ def update(
table (str, optional): The table to update. Defaults to "". table (str, optional): The table to update. Defaults to "".
force (bool, optional): Force the update. Defaults to False. force (bool, optional): Force the update. Defaults to False.
Returns: Returns:
dict[int, int, dict]: The data updated dict[int, int, dict] or None: The data updated
""" """
print("Function not implemented yet") global database
# Get the key to update
def get_key(
current_key: Union[Tuple[int, int, Dict], None], key_and_row: TypeData
) -> Union[int, None]:
if (
current_key == None
and key_and_row["id"] == id
and key_and_row["table"] == table
):
return key_and_row[0]
return None
key = reduce(get_key, list(enumerate(database))[::-1], None)
if key:
if force:
database[key]["data"] = new_data
else:
database[key]["data"].update(new_data)
database[key]["rev"] += 1
return None
def delete(id: int, data: dict, table: str = "") -> Type_Delete_Return: def delete(id: int, data: dict, table: str = "") -> Type_Delete_Return:

View File

@ -209,7 +209,7 @@ def test_update_multiple_values_default():
"id": 1, "id": 1,
"rev": 1, "rev": 1,
"table": "default", "table": "default",
"data": {"name": "David", "age": 9, "height": 188}, "data": {"name": "Antony", "age": 77, "height": 188},
}, },
{ {
"id": 2, "id": 2,
@ -217,6 +217,12 @@ def test_update_multiple_values_default():
"table": "default", "table": "default",
"data": {"name": "Dolores", "age": 32}, "data": {"name": "Dolores", "age": 32},
}, },
{
"id": 1,
"rev": 2,
"table": "default",
"data": {"name": "David", "age": 9, "height": 188},
},
] ]
@ -231,7 +237,7 @@ def test_update_multiple_values_table():
"id": 1, "id": 1,
"rev": 1, "rev": 1,
"table": "users", "table": "users",
"data": {"name": "David", "age": 9, "height": 188}, "data": {"name": "Antony", "age": 77, "height": 188},
}, },
{ {
"id": 2, "id": 2,
@ -239,6 +245,12 @@ def test_update_multiple_values_table():
"table": "users", "table": "users",
"data": {"name": "Dolores", "age": 32}, "data": {"name": "Dolores", "age": 32},
}, },
{
"id": 1,
"rev": 2,
"table": "users",
"data": {"name": "David", "age": 9, "height": 188},
},
] ]
@ -257,8 +269,6 @@ def test_update_with_keys_not_exists():
"name": "Antony", "name": "Antony",
"age": 77, "age": 77,
"height": 188, "height": 188,
"is_active": True,
"eyes": "blue",
}, },
}, },
{ {
@ -267,6 +277,18 @@ def test_update_with_keys_not_exists():
"table": "default", "table": "default",
"data": {"name": "Dolores", "age": 32}, "data": {"name": "Dolores", "age": 32},
}, },
{
"id": 1,
"rev": 2,
"table": "default",
"data": {
"name": "Antony",
"age": 77,
"height": 188,
"is_active": True,
"eyes": "blue",
},
},
] ]
@ -281,7 +303,7 @@ def test_update_with_force():
"id": 1, "id": 1,
"rev": 1, "rev": 1,
"table": "default", "table": "default",
"data": {"name": "David", "age": 9}, "data": {"name": "Antony", "age": 77, "height": 188},
}, },
{ {
"id": 2, "id": 2,
@ -289,4 +311,10 @@ def test_update_with_force():
"table": "default", "table": "default",
"data": {"name": "Dolores", "age": 32}, "data": {"name": "Dolores", "age": 32},
}, },
{
"id": 1,
"rev": 2,
"table": "default",
"data": {"name": "David", "age": 9},
},
] ]