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_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_node_hashing(self): g = Graph() node_1 = Node(g, 1234) node_2 = Node(g, 1234) node_3 = Node(g, 5678) self.assertEqual(hash(node_1), hash(node_2)) self.assertNotEqual(hash(node_1), hash(node_3))
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_node_equality(self): g = Graph() node_1 = Node(g, 1234) node_2 = Node(g, 1234) node_3 = Node(g, 5678) self.assertEqual(node_1, node_2) self.assertNotEqual(node_1, node_3) self.assertNotEqual(node_1, "this is not a node")
def test_can_hydrate_path(self): from neo4j.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_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_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))