From 3d09cb6ebfdb3e3f657fc5642d3449aed6cacc25 Mon Sep 17 00:00:00 2001 From: Andros Fenollosa Date: Mon, 5 Dec 2022 14:52:09 +0100 Subject: [PATCH] Update updates --- fiable_db.py | 26 +++++++++++++++++++++++--- test/test_update.py | 38 +++++++++++++++++++++++++++++++++----- 2 files changed, 56 insertions(+), 8 deletions(-) diff --git a/fiable_db.py b/fiable_db.py index cad944e..c5820fb 100644 --- a/fiable_db.py +++ b/fiable_db.py @@ -132,7 +132,7 @@ def add(new_data: Type_Add_Data, table: str = "default") -> Type_Add_Return: 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: """Update data in the database Args: @@ -141,9 +141,29 @@ def update( table (str, optional): The table to update. Defaults to "". force (bool, optional): Force the update. Defaults to False. 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: diff --git a/test/test_update.py b/test/test_update.py index 3b936ed..28d7b2e 100644 --- a/test/test_update.py +++ b/test/test_update.py @@ -209,7 +209,7 @@ def test_update_multiple_values_default(): "id": 1, "rev": 1, "table": "default", - "data": {"name": "David", "age": 9, "height": 188}, + "data": {"name": "Antony", "age": 77, "height": 188}, }, { "id": 2, @@ -217,6 +217,12 @@ def test_update_multiple_values_default(): "table": "default", "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, "rev": 1, "table": "users", - "data": {"name": "David", "age": 9, "height": 188}, + "data": {"name": "Antony", "age": 77, "height": 188}, }, { "id": 2, @@ -239,6 +245,12 @@ def test_update_multiple_values_table(): "table": "users", "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", "age": 77, "height": 188, - "is_active": True, - "eyes": "blue", }, }, { @@ -267,6 +277,18 @@ def test_update_with_keys_not_exists(): "table": "default", "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, "rev": 1, "table": "default", - "data": {"name": "David", "age": 9}, + "data": {"name": "Antony", "age": 77, "height": 188}, }, { "id": 2, @@ -289,4 +311,10 @@ def test_update_with_force(): "table": "default", "data": {"name": "Dolores", "age": 32}, }, + { + "id": 1, + "rev": 2, + "table": "default", + "data": {"name": "David", "age": 9}, + }, ]