Skip to content

Commit

Permalink
tests are now passing
Browse files Browse the repository at this point in the history
  • Loading branch information
maxwellflitton committed Feb 26, 2024
1 parent 01b7506 commit fce6cb4
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 43 deletions.
6 changes: 2 additions & 4 deletions surrealdb/async_execution_mixins/set.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
26 changes: 25 additions & 1 deletion surrealdb/execution_mixins/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)

Expand Down
29 changes: 11 additions & 18 deletions tests/integration/async/test_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__":
Expand Down
33 changes: 13 additions & 20 deletions tests/integration/blocking/test_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__":
Expand Down

0 comments on commit fce6cb4

Please sign in to comment.