Skip to content

Commit

Permalink
Remove extra positional argument for escape_value (#198)
Browse files Browse the repository at this point in the history
* Remove extra positional argument for function

`escape_value` doesn't accept two positional arguments, this change removes the extra boolean value.

* Add basic tests for list & dict

* Remove extra blank line after running Black

* Fix dict property bug

Before this change, a Python dict was being converted as `value:value` instead of `key:value`. Special thanks to katarina.supe for providing the fix.

* Remove neo4j parameterised test for dict property

Speaking to katarina.supe and we determined this is necessary as Neo4J doesn't support dict.
  • Loading branch information
insectatorious authored Apr 18, 2023
1 parent d0f5e4d commit cde5a5c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
4 changes: 2 additions & 2 deletions gqlalchemy/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,9 +374,9 @@ def escape_value(
elif isinstance(value, str):
return repr(value) if value.isprintable() else rf"'{value}'"
elif isinstance(value, list):
return "[" + ", ".join(self.escape_value(val, True) for val in value) + "]"
return "[" + ", ".join(self.escape_value(val) for val in value) + "]"
elif value_type == dict:
return "{" + ", ".join(f"{val}: {self.escape_value(val, True)}" for key, val in value.items()) + "}"
return "{" + ", ".join(f"{key}: {self.escape_value(val)}" for key, val in value.items()) + "}"
if isinstance(value, (timedelta, time, datetime, date)):
return f"{datetimeKwMapping[value_type]}('{_format_timedelta(value) if isinstance(value, timedelta) else value.isoformat()}')"
else:
Expand Down
29 changes: 29 additions & 0 deletions tests/ogm/test_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,32 @@ class Test(Node):
assert loaded_user4.last_name == "doe"
assert loaded_user5.name == "jack"
assert loaded_user5.last_name == "\u0013"


@pytest.mark.parametrize("database", ["neo4j", "memgraph"], indirect=True)
def test_list_property(database):
class User(Node):
my_list: list

user = User(my_list=[1, 2, 3]).save(database)

loaded_user = database.load_node(user)

assert type(loaded_user) is User
assert "my_list" in User.__fields__
assert loaded_user.my_list == [1, 2, 3]


@pytest.mark.parametrize("database", ["memgraph"], indirect=True)
def test_dict_property(database):
class User(Node):
my_dict: dict

expected_dict = dict(x=1, y=-2, z=3.1, d=20, testPoint=True, label="testPoint")
user = User(my_dict=expected_dict).save(database)

loaded_user = database.load_node(user)

assert type(loaded_user) is User
assert "my_dict" in User.__fields__
assert loaded_user.my_dict == expected_dict

0 comments on commit cde5a5c

Please sign in to comment.