diff --git a/tests/unit/test_conf.py b/tests/unit/test_conf.py index 7c6c49b4..b644dadf 100644 --- a/tests/unit/test_conf.py +++ b/tests/unit/test_conf.py @@ -222,4 +222,4 @@ def test_init_session_config_with_not_valid_key(): with pytest.raises(ConfigurationError): _ = SessionConfig.consume(test_config_b) - assert session_config.connection_acquisition_timeout == 333 \ No newline at end of file + assert session_config.connection_acquisition_timeout == 333 diff --git a/tests/unit/test_data.py b/tests/unit/test_data.py new file mode 100644 index 00000000..124404ef --- /dev/null +++ b/tests/unit/test_data.py @@ -0,0 +1,80 @@ +#!/usr/bin/env python +# -*- encoding: utf-8 -*- + +# Copyright (c) 2002-2020 "Neo4j," +# Neo4j Sweden AB [http://neo4j.com] +# +# This file is part of Neo4j. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +import pytest + +from neo4j.data import DataHydrator +from neo4j.packstream import Structure + +# python -m pytest -s -v tests/unit/test_data.py + + +def test_can_hydrate_node_structure(): + hydrant = DataHydrator() + + struct = Structure(b'N', 123, ["Person"], {"name": "Alice"}) + alice, = hydrant.hydrate([struct]) + + assert alice.id == 123 + assert alice.labels == {"Person"} + assert set(alice.keys()) == {"name"} + assert alice.get("name") == "Alice" + + +def test_hydrating_unknown_structure_returns_same(): + hydrant = DataHydrator() + + struct = Structure(b'?', "foo") + mystery, = hydrant.hydrate([struct]) + + assert mystery == struct + + +def test_can_hydrate_in_list(): + hydrant = DataHydrator() + + struct = Structure(b'N', 123, ["Person"], {"name": "Alice"}) + alice_in_list, = hydrant.hydrate([[struct]]) + + assert isinstance(alice_in_list, list) + + alice, = alice_in_list + + assert alice.id == 123 + assert alice.labels == {"Person"} + assert set(alice.keys()) == {"name"} + assert alice.get("name") == "Alice" + + +def test_can_hydrate_in_dict(): + hydrant = DataHydrator() + + struct = Structure(b'N', 123, ["Person"], {"name": "Alice"}) + alice_in_dict, = hydrant.hydrate([{"foo": struct}]) + + assert isinstance(alice_in_dict, dict) + + alice = alice_in_dict["foo"] + + assert alice.id == 123 + assert alice.labels == {"Person"} + assert set(alice.keys()) == {"name"} + assert alice.get("name") == "Alice" diff --git a/tests/unit/test_exceptions.py b/tests/unit/test_exceptions.py index 4e9ac529..a4fc5f26 100644 --- a/tests/unit/test_exceptions.py +++ b/tests/unit/test_exceptions.py @@ -217,4 +217,3 @@ def test_neo4jerror_hydrate_with_message_and_code_client(): assert error.metadata == {} assert error.message == "Test error message" assert error.code == "Neo.{}.General.TestError".format(CLASSIFICATION_CLIENT) - diff --git a/tests/unit/test_import_neo4j.py b/tests/unit/test_import_neo4j.py index 6b8bb89a..d1b04668 100644 --- a/tests/unit/test_import_neo4j.py +++ b/tests/unit/test_import_neo4j.py @@ -144,8 +144,8 @@ def test_import_graph_node(): from neo4j.graph import Node -def test_import_graph_Path(): - # python -m pytest tests/unit/test_import_neo4j.py -s -v -k test_import_graph_Path +def test_import_graph_path(): + # python -m pytest tests/unit/test_import_neo4j.py -s -v -k test_import_graph_path from neo4j.graph import Path diff --git a/tests/unit/test_record.py b/tests/unit/test_record.py index e198a113..10fb5c3a 100644 --- a/tests/unit/test_record.py +++ b/tests/unit/test_record.py @@ -19,146 +19,164 @@ # limitations under the License. -from unittest import TestCase +import pytest from neo4j.data import Record +# python -m pytest -s -v tests/unit/test_record.py -class RecordTestCase(TestCase): - - def test_record_equality(self): - record1 = Record(zip(["name", "empire"], ["Nigel", "The British Empire"])) - record2 = Record(zip(["name", "empire"], ["Nigel", "The British Empire"])) - record3 = Record(zip(["name", "empire"], ["Stefan", "Das Deutschland"])) - assert record1 == record2 - assert record1 != record3 - assert record2 != record3 - - def test_record_hashing(self): - record1 = Record(zip(["name", "empire"], ["Nigel", "The British Empire"])) - record2 = Record(zip(["name", "empire"], ["Nigel", "The British Empire"])) - record3 = Record(zip(["name", "empire"], ["Stefan", "Das Deutschland"])) - assert hash(record1) == hash(record2) - assert hash(record1) != hash(record3) - assert hash(record2) != hash(record3) - - def test_record_iter(self): - a_record = Record(zip(["name", "empire"], ["Nigel", "The British Empire"])) - assert list(a_record.__iter__()) == ["Nigel", "The British Empire"] - - def test_record_as_dict(self): - a_record = Record(zip(["name", "empire"], ["Nigel", "The British Empire"])) - assert dict(a_record) == {"name": "Nigel", "empire": "The British Empire"} - - def test_record_as_list(self): - a_record = Record(zip(["name", "empire"], ["Nigel", "The British Empire"])) - assert list(a_record) == ["Nigel", "The British Empire"] - - def test_record_len(self): - a_record = Record(zip(["name", "empire"], ["Nigel", "The British Empire"])) - assert len(a_record) == 2 - - def test_record_repr(self): - a_record = Record(zip(["name", "empire"], ["Nigel", "The British Empire"])) - assert repr(a_record) == "" - - def test_record_data(self): - r = Record(zip(["name", "age", "married"], ["Alice", 33, True])) - self.assertEqual(r.data(), {"name": "Alice", "age": 33, "married": True}) - self.assertEqual(r.data("name"), {"name": "Alice"}) - self.assertEqual(r.data("age", "name"), {"age": 33, "name": "Alice"}) - self.assertEqual(r.data("age", "name", "shoe size"), {"age": 33, "name": "Alice", "shoe size": None}) - self.assertEqual(r.data(0, "name"), {"name": "Alice"}) - self.assertEqual(r.data(0), {"name": "Alice"}) - self.assertEqual(r.data(1, 0), {"age": 33, "name": "Alice"}) - with self.assertRaises(IndexError): - _ = r.data(1, 0, 999) - - def test_record_keys(self): - r = Record(zip(["name", "age", "married"], ["Alice", 33, True])) - self.assertEqual(r.keys(), ["name", "age", "married"]) - - def test_record_values(self): - r = Record(zip(["name", "age", "married"], ["Alice", 33, True])) - self.assertEqual(r.values(), ["Alice", 33, True]) - self.assertEqual(r.values("name"), ["Alice"]) - self.assertEqual(r.values("age", "name"), [33, "Alice"]) - self.assertEqual(r.values("age", "name", "shoe size"), [33, "Alice", None]) - self.assertEqual(r.values(0, "name"), ["Alice", "Alice"]) - self.assertEqual(r.values(0), ["Alice"]) - self.assertEqual(r.values(1, 0), [33, "Alice"]) - with self.assertRaises(IndexError): - _ = r.values(1, 0, 999) - - def test_record_items(self): - r = Record(zip(["name", "age", "married"], ["Alice", 33, True])) - self.assertEqual(r.items(), [("name", "Alice"), ("age", 33), ("married", True)]) - self.assertEqual(r.items("name"), [("name", "Alice")]) - self.assertEqual(r.items("age", "name"), [("age", 33), ("name", "Alice")]) - self.assertEqual(r.items("age", "name", "shoe size"), [("age", 33), ("name", "Alice"), ("shoe size", None)]) - self.assertEqual(r.items(0, "name"), [("name", "Alice"), ("name", "Alice")]) - self.assertEqual(r.items(0), [("name", "Alice")]) - self.assertEqual(r.items(1, 0), [("age", 33), ("name", "Alice")]) - with self.assertRaises(IndexError): - _ = r.items(1, 0, 999) - - def test_record_index(self): - r = Record(zip(["name", "age", "married"], ["Alice", 33, True])) - self.assertEqual(r.index("name"), 0) - self.assertEqual(r.index("age"), 1) - self.assertEqual(r.index("married"), 2) - with self.assertRaises(KeyError): - _ = r.index("shoe size") - self.assertEqual(r.index(0), 0) - self.assertEqual(r.index(1), 1) - self.assertEqual(r.index(2), 2) - with self.assertRaises(IndexError): - _ = r.index(3) - with self.assertRaises(TypeError): - _ = r.index(None) - - def test_record_value(self): - r = Record(zip(["name", "age", "married"], ["Alice", 33, True])) - self.assertEqual(r.value(), "Alice") - self.assertEqual(r.value("name"), "Alice") - self.assertEqual(r.value("age"), 33) - self.assertEqual(r.value("married"), True) - self.assertEqual(r.value("shoe size"), None) - self.assertEqual(r.value("shoe size", 6), 6) - self.assertEqual(r.value(0), "Alice") - self.assertEqual(r.value(1), 33) - self.assertEqual(r.value(2), True) - self.assertEqual(r.value(3), None) - self.assertEqual(r.value(3, 6), 6) - with self.assertRaises(TypeError): - _ = r.value(None) - - def test_record_contains(self): - r = Record(zip(["name", "age", "married"], ["Alice", 33, True])) - self.assertTrue("Alice" in r) - self.assertTrue(33 in r) - self.assertTrue(True in r) - self.assertFalse(7.5 in r) - with self.assertRaises(TypeError): - _ = r.index(None) - - def test_record_from_dict(self): - r = Record({"name": "Alice", "age": 33}) - self.assertEqual("Alice", r["name"]) - self.assertEqual(33, r["age"]) - - def test_record_get_slice(self): - r = Record(zip(["name", "age", "married"], ["Alice", 33, True])) - self.assertEqual(Record(zip(["name", "age"], ["Alice", 33])), r[0:2]) - - def test_record_get_by_index(self): - r = Record(zip(["name", "age", "married"], ["Alice", 33, True])) - self.assertEqual("Alice", r[0]) - - def test_record_get_by_name(self): - r = Record(zip(["name", "age", "married"], ["Alice", 33, True])) - self.assertEqual("Alice", r["name"]) - - def test_record_get_by_out_of_bounds_index(self): - r = Record(zip(["name", "age", "married"], ["Alice", 33, True])) - self.assertIsNone(r[9]) + +def test_record_equality(): + record1 = Record(zip(["name", "empire"], ["Nigel", "The British Empire"])) + record2 = Record(zip(["name", "empire"], ["Nigel", "The British Empire"])) + record3 = Record(zip(["name", "empire"], ["Stefan", "Das Deutschland"])) + assert record1 == record2 + assert record1 != record3 + assert record2 != record3 + + +def test_record_hashing(): + record1 = Record(zip(["name", "empire"], ["Nigel", "The British Empire"])) + record2 = Record(zip(["name", "empire"], ["Nigel", "The British Empire"])) + record3 = Record(zip(["name", "empire"], ["Stefan", "Das Deutschland"])) + assert hash(record1) == hash(record2) + assert hash(record1) != hash(record3) + assert hash(record2) != hash(record3) + + +def test_record_iter(): + a_record = Record(zip(["name", "empire"], ["Nigel", "The British Empire"])) + assert list(a_record.__iter__()) == ["Nigel", "The British Empire"] + + +def test_record_as_dict(): + a_record = Record(zip(["name", "empire"], ["Nigel", "The British Empire"])) + assert dict(a_record) == {"name": "Nigel", "empire": "The British Empire"} + + +def test_record_as_list(): + a_record = Record(zip(["name", "empire"], ["Nigel", "The British Empire"])) + assert list(a_record) == ["Nigel", "The British Empire"] + + +def test_record_len(): + a_record = Record(zip(["name", "empire"], ["Nigel", "The British Empire"])) + assert len(a_record) == 2 + + +def test_record_repr(): + a_record = Record(zip(["name", "empire"], ["Nigel", "The British Empire"])) + assert repr(a_record) == "" + + +def test_record_data(): + r = Record(zip(["name", "age", "married"], ["Alice", 33, True])) + assert r.data() == {"name": "Alice", "age": 33, "married": True} + assert r.data("name") == {"name": "Alice"} + assert r.data("age", "name") == {"age": 33, "name": "Alice"} + assert r.data("age", "name", "shoe size") == {"age": 33, "name": "Alice", "shoe size": None} + assert r.data(0, "name") == {"name": "Alice"} + assert r.data(0) == {"name": "Alice"} + assert r.data(1, 0) == {"age": 33, "name": "Alice"} + with pytest.raises(IndexError): + _ = r.data(1, 0, 999) + + +def test_record_keys(): + r = Record(zip(["name", "age", "married"], ["Alice", 33, True])) + assert r.keys() == ["name", "age", "married"] + + +def test_record_values(): + r = Record(zip(["name", "age", "married"], ["Alice", 33, True])) + assert r.values() == ["Alice", 33, True] + assert r.values("name") == ["Alice"] + assert r.values("age", "name") == [33, "Alice"] + assert r.values("age", "name", "shoe size") == [33, "Alice", None] + assert r.values(0, "name") == ["Alice", "Alice"] + assert r.values(0) == ["Alice"] + assert r.values(1, 0) == [33, "Alice"] + with pytest.raises(IndexError): + _ = r.values(1, 0, 999) + + +def test_record_items(): + r = Record(zip(["name", "age", "married"], ["Alice", 33, True])) + assert r.items() == [("name", "Alice"), ("age", 33), ("married", True)] + assert r.items("name") == [("name", "Alice")] + assert r.items("age", "name") == [("age", 33), ("name", "Alice")] + assert r.items("age", "name", "shoe size") == [("age", 33), ("name", "Alice"), ("shoe size", None)] + assert r.items(0, "name") == [("name", "Alice"), ("name", "Alice")] + assert r.items(0) == [("name", "Alice")] + assert r.items(1, 0) == [("age", 33), ("name", "Alice")] + with pytest.raises(IndexError): + _ = r.items(1, 0, 999) + + +def test_record_index(): + r = Record(zip(["name", "age", "married"], ["Alice", 33, True])) + assert r.index("name") == 0 + assert r.index("age") == 1 + assert r.index("married") == 2 + with pytest.raises(KeyError): + _ = r.index("shoe size") + assert r.index(0) == 0 + assert r.index(1) == 1 + assert r.index(2) == 2 + with pytest.raises(IndexError): + _ = r.index(3) + with pytest.raises(TypeError): + _ = r.index(None) + + +def test_record_value(): + r = Record(zip(["name", "age", "married"], ["Alice", 33, True])) + assert r.value() == "Alice" + assert r.value("name") == "Alice" + assert r.value("age") == 33 + assert r.value("married") is True + assert r.value("shoe size") is None + assert r.value("shoe size", 6) == 6 + assert r.value(0) == "Alice" + assert r.value(1) == 33 + assert r.value(2) is True + assert r.value(3) is None + assert r.value(3, 6) == 6 + with pytest.raises(TypeError): + _ = r.value(None) + + +def test_record_contains(): + r = Record(zip(["name", "age", "married"], ["Alice", 33, True])) + assert "Alice" in r + assert 33 in r + assert True in r + assert 7.5 not in r + with pytest.raises(TypeError): + _ = r.index(None) + + +def test_record_from_dict(): + r = Record({"name": "Alice", "age": 33}) + assert r["name"] == "Alice" + assert r["age"] == 33 + + +def test_record_get_slice(): + r = Record(zip(["name", "age", "married"], ["Alice", 33, True])) + assert Record(zip(["name", "age"], ["Alice", 33])) == r[0:2] + + +def test_record_get_by_index(): + r = Record(zip(["name", "age", "married"], ["Alice", 33, True])) + assert r[0] == "Alice" + + +def test_record_get_by_name(): + r = Record(zip(["name", "age", "married"], ["Alice", 33, True])) + assert r["name"] == "Alice" + + +def test_record_get_by_out_of_bounds_index(): + r = Record(zip(["name", "age", "married"], ["Alice", 33, True])) + assert r[9] is None diff --git a/tests/unit/test_security.py b/tests/unit/test_security.py index 570b6bde..8d957566 100644 --- a/tests/unit/test_security.py +++ b/tests/unit/test_security.py @@ -19,40 +19,46 @@ # limitations under the License. -from unittest import TestCase -from neo4j import kerberos_auth, basic_auth, custom_auth - - -class AuthTokenTestCase(TestCase): - - def test_should_generate_kerberos_auth_token_correctly(self): - auth = kerberos_auth("I am a base64 service ticket") - assert auth.scheme == "kerberos" - assert auth.principal == "" - assert auth.credentials == "I am a base64 service ticket" - assert not auth.realm - assert not hasattr(auth, "parameters") - - def test_should_generate_basic_auth_without_realm_correctly(self): - auth = basic_auth("molly", "meoooow") - assert auth.scheme == "basic" - assert auth.principal == "molly" - assert auth.credentials == "meoooow" - assert not auth.realm - assert not hasattr(auth, "parameters") - - def test_should_generate_base_auth_with_realm_correctly(self): - auth = basic_auth("molly", "meoooow", "cat_cafe") - assert auth.scheme == "basic" - assert auth.principal == "molly" - assert auth.credentials == "meoooow" - assert auth.realm == "cat_cafe" - assert not hasattr(auth, "parameters") - - def test_should_generate_custom_auth_correctly(self): - auth = custom_auth("molly", "meoooow", "cat_cafe", "cat", age="1", color="white") - assert auth.scheme == "cat" - assert auth.principal == "molly" - assert auth.credentials == "meoooow" - assert auth.realm == "cat_cafe" - assert auth.parameters == {"age": "1", "color": "white"} +from neo4j.api import ( + kerberos_auth, + basic_auth, + custom_auth, +) + +# python -m pytest -s -v tests/unit/test_security.py + + +def test_should_generate_kerberos_auth_token_correctly(): + auth = kerberos_auth("I am a base64 service ticket") + assert auth.scheme == "kerberos" + assert auth.principal == "" + assert auth.credentials == "I am a base64 service ticket" + assert not auth.realm + assert not hasattr(auth, "parameters") + + +def test_should_generate_basic_auth_without_realm_correctly(): + auth = basic_auth("molly", "meoooow") + assert auth.scheme == "basic" + assert auth.principal == "molly" + assert auth.credentials == "meoooow" + assert not auth.realm + assert not hasattr(auth, "parameters") + + +def test_should_generate_base_auth_with_realm_correctly(): + auth = basic_auth("molly", "meoooow", "cat_cafe") + assert auth.scheme == "basic" + assert auth.principal == "molly" + assert auth.credentials == "meoooow" + assert auth.realm == "cat_cafe" + assert not hasattr(auth, "parameters") + + +def test_should_generate_custom_auth_correctly(): + auth = custom_auth("molly", "meoooow", "cat_cafe", "cat", age="1", color="white") + assert auth.scheme == "cat" + assert auth.principal == "molly" + assert auth.credentials == "meoooow" + assert auth.realm == "cat_cafe" + assert auth.parameters == {"age": "1", "color": "white"} diff --git a/tests/unit/test_types.py b/tests/unit/test_types.py index c301dbfa..697fceb6 100644 --- a/tests/unit/test_types.py +++ b/tests/unit/test_types.py @@ -22,175 +22,151 @@ from unittest import TestCase from neo4j.data import DataHydrator -from neo4j.graph import Node, Path, Graph +from neo4j.graph import ( + Node, + Path, + Graph, +) from neo4j.packstream import Structure - -class NodeTestCase(TestCase): - - def test_can_create_node(self): - g = Graph() - gh = Graph.Hydrator(g) - alice = gh.hydrate_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_null_properties(self): - g = Graph() - gh = Graph.Hydrator(g) - stuff = gh.hydrate_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_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_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)) - - -class RelationshipTestCase(TestCase): - - def test_can_create_relationship(self): - g = Graph() - gh = Graph.Hydrator(g) - 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}) - 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)) - - -class PathTestCase(TestCase): - - def test_can_create_path(self): - g = Graph() - gh = Graph.Hydrator(g) - alice = gh.hydrate_node(1, {"Person"}, {"name": "Alice", "age": 33}) - bob = gh.hydrate_node(2, {"Person"}, {"name": "Bob", "age": 44}) - carol = gh.hydrate_node(3, {"Person"}, {"name": "Carol", "age": 55}) - 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) - 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_hydrate_path(self): - g = Graph() - gh = Graph.Hydrator(g) - alice = gh.hydrate_node(1, {"Person"}, {"name": "Alice", "age": 33}) - bob = gh.hydrate_node(2, {"Person"}, {"name": "Bob", "age": 44}) - carol = gh.hydrate_node(3, {"Person"}, {"name": "Carol", "age": 55}) - r = [gh.hydrate_unbound_relationship(1, "KNOWS", {"since": 1999}), - gh.hydrate_unbound_relationship(2, "DISLIKES", {})] - path = gh.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 = gh.hydrate_relationship(1, alice.id, bob.id, "KNOWS", {"since": 1999}) - expected_carol_dislikes_bob = gh.hydrate_relationship(2, carol.id, bob.id, "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_equality(self): - g = Graph() - gh = Graph.Hydrator(g) - alice = gh.hydrate_node(1, {"Person"}, {"name": "Alice", "age": 33}) - bob = gh.hydrate_node(2, {"Person"}, {"name": "Bob", "age": 44}) - carol = gh.hydrate_node(3, {"Person"}, {"name": "Carol", "age": 55}) - 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_1 = Path(alice, alice_knows_bob, carol_dislikes_bob) - path_2 = Path(alice, alice_knows_bob, carol_dislikes_bob) - self.assertEqual(path_1, path_2) - self.assertNotEqual(path_1, "this is not a path") - - def test_path_hashing(self): - g = Graph() - gh = Graph.Hydrator(g) - alice = gh.hydrate_node(1, {"Person"}, {"name": "Alice", "age": 33}) - bob = gh.hydrate_node(2, {"Person"}, {"name": "Bob", "age": 44}) - carol = gh.hydrate_node(3, {"Person"}, {"name": "Carol", "age": 55}) - 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_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)) - - -class HydrationTestCase(TestCase): - - def setUp(self): - self.hydrant = DataHydrator() - - def test_can_hydrate_node_structure(self): - struct = Structure(b'N', 123, ["Person"], {"name": "Alice"}) - alice, = self.hydrant.hydrate([struct]) - self.assertEqual(alice.id, 123) - self.assertEqual(alice.labels, {"Person"}) - self.assertEqual(set(alice.keys()), {"name"}) - self.assertEqual(alice.get("name"), "Alice") - - def test_hydrating_unknown_structure_returns_same(self): - struct = Structure(b'?', "foo") - mystery, = self.hydrant.hydrate([struct]) - self.assertEqual(mystery, struct) - - def test_can_hydrate_in_list(self): - struct = Structure(b'N', 123, ["Person"], {"name": "Alice"}) - alice_in_list, = self.hydrant.hydrate([[struct]]) - self.assertIsInstance(alice_in_list, list) - alice, = alice_in_list - self.assertEqual(alice.id, 123) - self.assertEqual(alice.labels, {"Person"}) - self.assertEqual(set(alice.keys()), {"name"}) - self.assertEqual(alice.get("name"), "Alice") - - def test_can_hydrate_in_dict(self): - struct = Structure(b'N', 123, ["Person"], {"name": "Alice"}) - alice_in_dict, = self.hydrant.hydrate([{"foo": struct}]) - self.assertIsInstance(alice_in_dict, dict) - alice = alice_in_dict["foo"] - self.assertEqual(alice.id, 123) - self.assertEqual(alice.labels, {"Person"}) - self.assertEqual(set(alice.keys()), {"name"}) - self.assertEqual(alice.get("name"), "Alice") +# python -m pytest -s -v tests/unit/test_types.py + + +# Node + + +def test_can_create_node(): + g = Graph() + gh = Graph.Hydrator(g) + alice = gh.hydrate_node(1, {"Person"}, {"name": "Alice", "age": 33}) + 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 + assert "name" in alice + assert "age" in alice + assert set(iter(alice)) == {"name", "age"} + + +def test_null_properties(): + g = Graph() + gh = Graph.Hydrator(g) + stuff = gh.hydrate_node(1, (), {"good": ["puppies", "kittens"], "bad": None}) + assert set(stuff.keys()) == {"good"} + assert stuff.get("good") == ["puppies", "kittens"] + assert stuff.get("bad") is None + assert len(stuff) == 1 + assert stuff["good"] == ["puppies", "kittens"] + assert stuff["bad"] is None + assert "good" in stuff + assert "bad" not in stuff + + +def test_node_equality(): + g = Graph() + node_1 = Node(g, 1234) + node_2 = Node(g, 1234) + node_3 = Node(g, 5678) + assert node_1 == node_2 + assert node_1 != node_3 + assert node_1 != "this is not a node" + + +def test_node_hashing(): + g = Graph() + node_1 = Node(g, 1234) + node_2 = Node(g, 1234) + node_3 = Node(g, 5678) + assert hash(node_1) == hash(node_2) + assert hash(node_1) != hash(node_3) + + +# Relationship + + +def test_can_create_relationship(): + g = Graph() + gh = Graph.Hydrator(g) + 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 alice_knows_bob.start_node == alice + assert alice_knows_bob.type == "KNOWS" + assert alice_knows_bob.end_node == bob + assert set(alice_knows_bob.keys()) == {"since"} + 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}>" + + +# Path + + +def test_can_create_path(): + g = Graph() + gh = Graph.Hydrator(g) + alice = gh.hydrate_node(1, {"Person"}, {"name": "Alice", "age": 33}) + bob = gh.hydrate_node(2, {"Person"}, {"name": "Bob", "age": 44}) + carol = gh.hydrate_node(3, {"Person"}, {"name": "Carol", "age": 55}) + 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 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(): + g = Graph() + gh = Graph.Hydrator(g) + alice = gh.hydrate_node(1, {"Person"}, {"name": "Alice", "age": 33}) + bob = gh.hydrate_node(2, {"Person"}, {"name": "Bob", "age": 44}) + carol = gh.hydrate_node(3, {"Person"}, {"name": "Carol", "age": 55}) + r = [gh.hydrate_unbound_relationship(1, "KNOWS", {"since": 1999}), + gh.hydrate_unbound_relationship(2, "DISLIKES", {})] + path = gh.hydrate_path([alice, bob, carol], r, [1, 1, -2, 2]) + assert path.start_node == alice + assert path.end_node == carol + assert path.nodes == (alice, bob, carol) + expected_alice_knows_bob = gh.hydrate_relationship(1, alice.id, bob.id, "KNOWS", {"since": 1999}) + 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(): + g = Graph() + gh = Graph.Hydrator(g) + alice = gh.hydrate_node(1, {"Person"}, {"name": "Alice", "age": 33}) + bob = gh.hydrate_node(2, {"Person"}, {"name": "Bob", "age": 44}) + carol = gh.hydrate_node(3, {"Person"}, {"name": "Carol", "age": 55}) + 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_1 = Path(alice, alice_knows_bob, carol_dislikes_bob) + path_2 = Path(alice, alice_knows_bob, carol_dislikes_bob) + assert path_1 == path_2 + assert path_1 != "this is not a path" + + +def test_path_hashing(): + g = Graph() + gh = Graph.Hydrator(g) + alice = gh.hydrate_node(1, {"Person"}, {"name": "Alice", "age": 33}) + bob = gh.hydrate_node(2, {"Person"}, {"name": "Bob", "age": 44}) + carol = gh.hydrate_node(3, {"Person"}, {"name": "Carol", "age": 55}) + 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_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)