From fce6cb408a11da15db72b38f81418d8eddbb3b6d Mon Sep 17 00:00:00 2001 From: maxwellflitton Date: Mon, 26 Feb 2024 16:03:18 +0000 Subject: [PATCH] tests are now passing --- surrealdb/async_execution_mixins/set.py | 6 ++--- surrealdb/execution_mixins/query.py | 26 ++++++++++++++++++- tests/integration/async/test_set.py | 29 +++++++++------------- tests/integration/blocking/test_set.py | 33 ++++++++++--------------- 4 files changed, 51 insertions(+), 43 deletions(-) diff --git a/surrealdb/async_execution_mixins/set.py b/surrealdb/async_execution_mixins/set.py index 2086c663..6b263a2e 100644 --- a/surrealdb/async_execution_mixins/set.py +++ b/surrealdb/async_execution_mixins/set.py @@ -12,7 +12,7 @@ class AsyncSetMixin: """ This class is responsible for the interface between python and the Rust SurrealDB library for creating a document. """ - async def set(self: "SurrealDB", key: str, value: dict) -> dict: + async def set(self: "SurrealDB", key: str, value: dict) -> None: """ Creates a new document in the database. @@ -29,8 +29,6 @@ async def set(self: "SurrealDB", key: str, value: dict) -> dict: raise SurrealDbError(e) if json_str is not None: try: - response = await rust_set_future(self._connection, key, json.dumps(value)) - print(f"\n\n\nresponse: {response}\n\n\n") - return json.loads(response) + _ = await rust_set_future(self._connection, key, json.dumps(value)) except Exception as e: raise SurrealDbError(e) diff --git a/surrealdb/execution_mixins/query.py b/surrealdb/execution_mixins/query.py index 32f104f4..2ea36445 100644 --- a/surrealdb/execution_mixins/query.py +++ b/surrealdb/execution_mixins/query.py @@ -17,6 +17,28 @@ class QueryMixin: """ This class is responsible for the interface between python and the Rust SurrealDB library for creating a document. """ + + @staticmethod + def convert_nested_json_strings(data): + """ + For some reason, if there is a dict in the query result, the value is a string. This does not happen with the + raw async method but the implementation is the same. For now this method will be used to convert the string + values to JSON in the query result to valid JSON but we should look into why this is happening. + + :param data: The query result data to be checked + :return: The processed query data + """ + for item in data: + for key, value in item.items(): + if isinstance(value, str): # Check if the value is a string + try: + # Attempt to load the string as JSON + item[key] = json.loads(value) + except json.JSONDecodeError: + # If it's not a valid JSON string, do nothing + pass + return data + def query(self: "SurrealDB", query: str) -> List[dict]: """ queries the database. @@ -30,7 +52,9 @@ async def _query(connection, query): try: loop_manager = AsyncioRuntime() - return json.loads(loop_manager.loop.run_until_complete(_query(self._connection, query)))[0] + return self.convert_nested_json_strings( + json.loads(loop_manager.loop.run_until_complete(_query(self._connection, query)))[0] + ) except Exception as e: raise SurrealDbError(e) diff --git a/tests/integration/async/test_set.py b/tests/integration/async/test_set.py index a3c8e5d6..7b988013 100644 --- a/tests/integration/async/test_set.py +++ b/tests/integration/async/test_set.py @@ -54,30 +54,23 @@ async def set(): def test_set(self): self.queries = ["DELETE person;"] - query = "CREATE person:100;" + query = "CREATE person:100 SET name = $name;" async def set(): - _ = await self.connection.query(query) - outcome = await self.connection.set( - "person:`100`", + _ = await self.connection.set( + "name", { "name": "Tobie", - "company": "SurrealDB", - "skills": ["Rust", "Go", "JavaScript"] + "last": "Morgan Hitchcock", } ) - # self.assertEqual( - # [ - # { - # 'id': 'person:100', - # 'name': 'Tobie', - # 'company': 'SurrealDB', - # 'skills': ['Rust', 'Go', 'JavaScript'] - # } - # ], - # outcome - # ) - # asyncio.run(set()) + _ = await self.connection.query(query) + outcome = await self.connection.query("SELECT * FROM person;") + self.assertEqual( + [{'id': 'person:100', 'name': {'last': 'Morgan Hitchcock', 'name': 'Tobie'}}], + outcome + ) + asyncio.run(set()) if __name__ == "__main__": diff --git a/tests/integration/blocking/test_set.py b/tests/integration/blocking/test_set.py index 83e2d19f..6353b72a 100644 --- a/tests/integration/blocking/test_set.py +++ b/tests/integration/blocking/test_set.py @@ -43,28 +43,21 @@ def test_set_ql(self): def test_set(self): self.queries = ["DELETE person;"] - query = "CREATE person:100;" + query = "CREATE person:100 SET name = $name;" + self.connection.set( + "name", + { + "name": "Tobie", + "last": "Morgan Hitchcock", + } + ) _ = self.connection.query(query) - # _ = self.connection.set( - # "person:`100`", - # { - # "name": "Tobie", - # "company": "SurrealDB", - # "skills": ["Rust", "Go", "JavaScript"] - # } - # ) - # self.assertEqual( - # [ - # { - # 'id': 'person:100', - # 'name': 'Tobie', - # 'company': 'SurrealDB', - # 'skills': ['Rust', 'Go', 'JavaScript'] - # } - # ], - # outcome - # ) + outcome = self.connection.query("SELECT * FROM person;") + self.assertEqual( + [{'id': 'person:100', 'name': {'last': 'Morgan Hitchcock', 'name': 'Tobie'}}], + outcome + ) if __name__ == "__main__":