def test_alternate_related_node_template(self): g = Graph() a = g.put_node(1, name="Alice") b = g.put_node(2, name="Bob") ab = g.put_relationship(9, a, b, "KNOWS", since=1999) r = cypher_repr(ab, related_node_template="{property.name}") self.assertEqual(u'(Alice)-[:KNOWS {since: 1999}]->(Bob)', r)
def test_can_hydrate_path(self): from neo4j.v1.types.graph import hydrate_path, _put_unbound_relationship g = Graph() alice = g.put_node(1, {"Person"}, {"name": "Alice", "age": 33}) bob = g.put_node(2, {"Person"}, {"name": "Bob", "age": 44}) carol = g.put_node(3, {"Person"}, {"name": "Carol", "age": 55}) r = [ _put_unbound_relationship(g, 1, "KNOWS", {"since": 1999}), _put_unbound_relationship(g, 2, "DISLIKES") ] path = hydrate_path([alice, bob, carol], r, [1, 1, -2, 2]) self.assertEqual(path.start_node, alice) self.assertEqual(path.end_node, carol) self.assertEqual(path.nodes, (alice, bob, carol)) expected_alice_knows_bob = g.put_relationship(1, alice, bob, "KNOWS", {"since": 1999}) expected_carol_dislikes_bob = g.put_relationship( 2, carol, bob, "DISLIKES") self.assertEqual( path.relationships, (expected_alice_knows_bob, expected_carol_dislikes_bob)) self.assertEqual( list(path), [expected_alice_knows_bob, expected_carol_dislikes_bob]) self.assertTrue(repr(path))
def test_basic_graph(self): g = Graph() a = g.put_node(1) b = g.put_node(2) c = g.put_node(3) d = g.put_node(4) ab = g.put_relationship(7, a, b, "KNOWS") cb = g.put_relationship(8, c, b, "KNOWS") cd = g.put_relationship(9, c, d, "KNOWS") r = cypher_repr(g) self.assertEqual(u"<Graph order=4 size=3>", r)
def test_path_hashing(self): g = Graph() alice = g.put_node(1, {"Person"}, {"name": "Alice", "age": 33}) bob = g.put_node(2, {"Person"}, {"name": "Bob", "age": 44}) carol = g.put_node(3, {"Person"}, {"name": "Carol", "age": 55}) alice_knows_bob = g.put_relationship(1, alice, bob, "KNOWS", {"since": 1999}) carol_dislikes_bob = g.put_relationship(2, carol, bob, "DISLIKES") path_1 = Path(alice, alice_knows_bob, carol_dislikes_bob) path_2 = Path(alice, alice_knows_bob, carol_dislikes_bob) self.assertEqual(hash(path_1), hash(path_2))
def test_basic_path(self): g = Graph() a = g.put_node(1) b = g.put_node(2) c = g.put_node(3) d = g.put_node(4) ab = g.put_relationship(7, a, b, "KNOWS") cb = g.put_relationship(8, c, b, "KNOWS") cd = g.put_relationship(9, c, d, "KNOWS") p = Path(a, ab, cb, cd) r = cypher_repr(p) self.assertEqual( u"(_1)-[:KNOWS {}]->(_2)<-[:KNOWS {}]-(_3)-[:KNOWS {}]->(_4)", r)
def test_basic_relationship(self): g = Graph() a = g.put_node(1) b = g.put_node(2) ab = g.put_relationship(9, a, b, "KNOWS", since=1999) r = cypher_repr(ab) self.assertEqual(u'(_1)-[:KNOWS {since: 1999}]->(_2)', r)
def test_can_create_path(self): g = Graph() alice = g.put_node(1, {"Person"}, {"name": "Alice", "age": 33}) bob = g.put_node(2, {"Person"}, {"name": "Bob", "age": 44}) carol = g.put_node(3, {"Person"}, {"name": "Carol", "age": 55}) alice_knows_bob = g.put_relationship(1, alice, bob, "KNOWS", {"since": 1999}) carol_dislikes_bob = g.put_relationship(2, carol, bob, "DISLIKES") path = Path(alice, alice_knows_bob, carol_dislikes_bob) self.assertEqual(path.start_node, alice) self.assertEqual(path.end_node, carol) self.assertEqual(path.nodes, (alice, bob, carol)) self.assertEqual(path.relationships, (alice_knows_bob, carol_dislikes_bob)) self.assertEqual(list(path), [alice_knows_bob, carol_dislikes_bob]) self.assertTrue(repr(path))
def test_can_create_relationship(self): g = Graph() alice = g.put_node(1, {"Person"}, name="Alice", age=33) bob = g.put_node(2, {"Person"}, name="Bob", age=44) alice_knows_bob = g.put_relationship(1, alice, bob, "KNOWS", since=1999) self.assertEqual(alice_knows_bob.start_node, alice) self.assertEqual(alice_knows_bob.type, "KNOWS") self.assertEqual(alice_knows_bob.end_node, bob) self.assertEqual(set(alice_knows_bob.keys()), {"since"}) self.assertEqual(set(alice_knows_bob.values()), {1999}) self.assertEqual(set(alice_knows_bob.items()), {("since", 1999)}) self.assertEqual(alice_knows_bob.get("since"), 1999) self.assertTrue(repr(alice_knows_bob))
def test_null_properties(self): g = Graph() stuff = g.put_node(1, (), good=["puppies", "kittens"], bad=None) self.assertEqual(set(stuff.keys()), {"good"}) self.assertEqual(stuff.get("good"), ["puppies", "kittens"]) self.assertIsNone(stuff.get("bad")) self.assertEqual(len(stuff), 1) self.assertEqual(stuff["good"], ["puppies", "kittens"]) self.assertIsNone(stuff["bad"]) self.assertIn("good", stuff) self.assertNotIn("bad", stuff)
def test_can_create_node(self): g = Graph() alice = g.put_node(1, {"Person"}, {"name": "Alice", "age": 33}) self.assertEqual(alice.labels, {"Person"}) self.assertEqual(set(alice.keys()), {"name", "age"}) self.assertEqual(set(alice.values()), {"Alice", 33}) self.assertEqual(set(alice.items()), {("name", "Alice"), ("age", 33)}) self.assertEqual(alice.get("name"), "Alice") self.assertEqual(alice.get("age"), 33) self.assertTrue(repr(alice)) self.assertEqual(len(alice), 2) self.assertEqual(alice["name"], "Alice") self.assertEqual(alice["age"], 33) self.assertIn("name", alice) self.assertIn("age", alice) self.assertEqual(set(iter(alice)), {"name", "age"})
def test_failed_selection(self): g = Graph() a = g.put_node(1, {"Person", "Employee"}, name="Alice", age=33) r = cypher_repr(a, node_template="{labels.Cat} {properties.paws}") self.assertEqual(u"({})", r)
def test_selection_by_property_value_only(self): g = Graph() a = g.put_node(1, {"Person", "Employee"}, name="Alice", age=33) r = cypher_repr(a, node_template="{property.name}") self.assertEqual(u"(Alice)", r)
def test_selection(self): g = Graph() a = g.put_node(1, {"Person", "Employee"}, name="Alice", age=33) r = cypher_repr(a, node_template="{labels.Person} {properties.name}") self.assertEqual(u"(:Person {name: 'Alice'})", r)
def test_node_with_only_properties(self): g = Graph() a = g.put_node(1, name="Alice", age=33) r = cypher_repr(a, node_template="{labels} {properties}") self.assertEqual(u"({age: 33, name: 'Alice'})", r)
def test_node_with_labels_and_properties(self): g = Graph() a = g.put_node(1, {"Person"}, name="Alice") r = cypher_repr(a, node_template="{labels} {properties}") self.assertEqual(u"(:Person {name: 'Alice'})", r)
def test_node_with_multiple_labels(self): g = Graph() a = g.put_node(1, {"Person", "Employee"}) r = cypher_repr(a, node_template="{labels} {properties}") self.assertEqual(u"(:Employee:Person {})", r)