From fef79f4cf3c1d4dcb899b6d47942e9f0939b6f42 Mon Sep 17 00:00:00 2001 From: martin bendsoe Date: Wed, 25 Mar 2020 19:34:01 +0100 Subject: [PATCH] fixed the repr unit test case for types --- neo4j/graph/__init__.py | 2 +- tests/unit/test_types.py | 37 +++++++++++++++++++++++++++++++++---- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/neo4j/graph/__init__.py b/neo4j/graph/__init__.py index e6d43731..2f898ca3 100644 --- a/neo4j/graph/__init__.py +++ b/neo4j/graph/__init__.py @@ -221,7 +221,7 @@ def __init__(self, graph, n_id, n_labels=None, properties=None): self._labels = frozenset(n_labels or ()) def __repr__(self): - return "" % (self._id, "', '".join(sorted(self._labels)), {key: val for key, val in sorted(self._properties.items())}) + return "" % (self._id, self._labels, self._properties) @property def labels(self): diff --git a/tests/unit/test_types.py b/tests/unit/test_types.py index 957b1029..221e90bf 100644 --- a/tests/unit/test_types.py +++ b/tests/unit/test_types.py @@ -26,6 +26,7 @@ Node, Path, Graph, + Relationship, ) from neo4j.packstream import Structure @@ -39,13 +40,13 @@ def test_can_create_node(): g = Graph() gh = Graph.Hydrator(g) alice = gh.hydrate_node(1, {"Person"}, {"name": "Alice", "age": 33}) + assert isinstance(alice, Node) assert alice.labels == {"Person"} assert set(alice.keys()) == {"name", "age"} assert set(alice.values()) == {"Alice", 33} assert set(alice.items()) == {("name", "Alice"), ("age", 33)} assert alice.get("name") == "Alice" assert alice.get("age") == 33 - assert repr(alice) == "" assert len(alice) == 2 assert alice["name"] == "Alice" assert alice["age"] == 33 @@ -58,6 +59,7 @@ def test_null_properties(): g = Graph() gh = Graph.Hydrator(g) stuff = gh.hydrate_node(1, (), {"good": ["puppies", "kittens"], "bad": None}) + assert isinstance(stuff, Node) assert set(stuff.keys()) == {"good"} assert stuff.get("good") == ["puppies", "kittens"] assert stuff.get("bad") is None @@ -87,6 +89,13 @@ def test_node_hashing(): assert hash(node_1) != hash(node_3) +def test_node_repr(): + g = Graph() + gh = Graph.Hydrator(g) + alice = gh.hydrate_node(1, {"Person"}, {"name": "Alice"}) + assert repr(alice) == "" + + # Relationship @@ -96,6 +105,7 @@ def test_can_create_relationship(): alice = gh.hydrate_node(1, {"Person"}, {"name": "Alice", "age": 33}) bob = gh.hydrate_node(2, {"Person"}, {"name": "Bob", "age": 44}) alice_knows_bob = gh.hydrate_relationship(1, alice.id, bob.id, "KNOWS", {"since": 1999}) + assert isinstance(alice_knows_bob, Relationship) assert alice_knows_bob.start_node == alice assert alice_knows_bob.type == "KNOWS" assert alice_knows_bob.end_node == bob @@ -103,7 +113,15 @@ def test_can_create_relationship(): assert set(alice_knows_bob.values()) == {1999} assert set(alice_knows_bob.items()) == {("since", 1999)} assert alice_knows_bob.get("since") == 1999 - assert repr(alice_knows_bob) == ", ) type='KNOWS' properties={'since': 1999}>" + + +def test_relationship_repr(): + g = Graph() + gh = Graph.Hydrator(g) + alice = gh.hydrate_node(1, {"Person"}, {"name": "Alice"}) + bob = gh.hydrate_node(2, {"Person"}, {"name": "Bob"}) + alice_knows_bob = gh.hydrate_relationship(1, alice.id, bob.id, "KNOWS", {"since": 1999}) + assert repr(alice_knows_bob) == ", ) type='KNOWS' properties={'since': 1999}>" # Path @@ -118,12 +136,12 @@ def test_can_create_path(): alice_knows_bob = gh.hydrate_relationship(1, alice.id, bob.id, "KNOWS", {"since": 1999}) carol_dislikes_bob = gh.hydrate_relationship(2, carol.id, bob.id, "DISLIKES", {}) path = Path(alice, alice_knows_bob, carol_dislikes_bob) + assert isinstance(path, Path) assert path.start_node == alice assert path.end_node == carol assert path.nodes == (alice, bob, carol) assert path.relationships == (alice_knows_bob, carol_dislikes_bob) assert list(path) == [alice_knows_bob, carol_dislikes_bob] - assert repr(path) == " end= size=2>" def test_can_hydrate_path(): @@ -142,7 +160,6 @@ def test_can_hydrate_path(): expected_carol_dislikes_bob = gh.hydrate_relationship(2, carol.id, bob.id, "DISLIKES", {}) assert path.relationships == (expected_alice_knows_bob, expected_carol_dislikes_bob) assert list(path) == [expected_alice_knows_bob, expected_carol_dislikes_bob] - assert repr(path) == " end= size=2>" def test_path_equality(): @@ -170,3 +187,15 @@ def test_path_hashing(): path_1 = Path(alice, alice_knows_bob, carol_dislikes_bob) path_2 = Path(alice, alice_knows_bob, carol_dislikes_bob) assert hash(path_1) == hash(path_2) + + +def test_path_repr(): + g = Graph() + gh = Graph.Hydrator(g) + alice = gh.hydrate_node(1, {"Person"}, {"name": "Alice"}) + bob = gh.hydrate_node(2, {"Person"}, {"name": "Bob"}) + carol = gh.hydrate_node(3, {"Person"}, {"name": "Carol"}) + alice_knows_bob = gh.hydrate_relationship(1, alice.id, bob.id, "KNOWS", {"since": 1999}) + carol_dislikes_bob = gh.hydrate_relationship(2, carol.id, bob.id, "DISLIKES", {}) + path = Path(alice, alice_knows_bob, carol_dislikes_bob) + assert repr(path) == " end= size=2>"