Skip to content

Commit c311a2c

Browse files
authored
4th round of migrating integration tests to TestKit (#575)
1 parent cad3718 commit c311a2c

File tree

4 files changed

+53
-197
lines changed

4 files changed

+53
-197
lines changed

tests/integration/io/__init__.py

Whitespace-only changes.

tests/integration/test_graph_types.py

Lines changed: 0 additions & 55 deletions
This file was deleted.

tests/integration/test_neo4j_driver.py

Lines changed: 0 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -45,126 +45,6 @@
4545
# python -m pytest tests/integration/test_neo4j_driver.py -s -v
4646

4747

48-
def test_neo4j_uri(neo4j_uri, auth, target):
49-
# python -m pytest tests/integration/test_neo4j_driver.py -s -v -k test_neo4j_uri
50-
try:
51-
with GraphDatabase.driver(neo4j_uri, auth=auth) as driver:
52-
with driver.session() as session:
53-
value = session.run("RETURN 1").single().value()
54-
assert value == 1
55-
except ServiceUnavailable as error:
56-
if error.args[0] == "Server does not support routing":
57-
# This is because a single instance Neo4j 3.5 does not have dbms.routing.cluster.getRoutingTable() call
58-
pytest.skip(error.args[0])
59-
elif isinstance(error.__cause__, BoltHandshakeError):
60-
pytest.skip(error.args[0])
61-
62-
63-
def test_supports_multi_db(neo4j_driver, requires_bolt_4x):
64-
# python -m pytest tests/integration/test_neo4j_driver.py -s -v -k test_supports_multi_db
65-
assert neo4j_driver.supports_multi_db()
66-
67-
68-
def test_test_multi_db_specify_database(neo4j_uri, auth, target):
69-
# python -m pytest tests/integration/test_neo4j_driver.py -s -v -k test_test_multi_db_specify_database
70-
try:
71-
with GraphDatabase.driver(neo4j_uri, auth=auth, database="test_database") as driver:
72-
with driver.session() as session:
73-
result = session.run("RETURN 1")
74-
assert next(result) == 1
75-
summary = result.consume()
76-
assert summary.database == "test_database"
77-
except ServiceUnavailable as error:
78-
if isinstance(error.__cause__, BoltHandshakeError):
79-
pytest.skip(error.args[0])
80-
except ConfigurationError as error:
81-
assert "Database name parameter for selecting database is not supported in Bolt Protocol Version(3, 0)." in error.args[0]
82-
except ClientError as error:
83-
# FAILURE {'code': 'Neo.ClientError.Database.DatabaseNotFound' - This message is sent from the server
84-
assert error.code == "Neo.ClientError.Database.DatabaseNotFound"
85-
assert "test_database" in error.message
86-
87-
88-
def test_neo4j_multi_database_support_create(neo4j_uri, auth, target, requires_bolt_4x):
89-
# python -m pytest tests/integration/test_neo4j_driver.py -s -v -k test_neo4j_multi_database_support_create
90-
with GraphDatabase.driver(neo4j_uri, auth=auth) as driver:
91-
with driver.session(database="system") as session:
92-
session.run("DROP DATABASE test IF EXISTS").consume()
93-
result = session.run("SHOW DATABASES")
94-
databases = set()
95-
for record in result:
96-
databases.add(record.get("name"))
97-
assert "system" in databases
98-
assert "neo4j" in databases
99-
100-
session.run("CREATE DATABASE test").consume()
101-
result = session.run("SHOW DATABASES")
102-
for record in result:
103-
databases.add(record.get("name"))
104-
assert "system" in databases
105-
assert "neo4j" in databases
106-
assert "test" in databases
107-
with driver.session(database="system") as session:
108-
session.run("DROP DATABASE test IF EXISTS").consume()
109-
110-
111-
def test_neo4j_multi_database_support_different(neo4j_uri, auth, target, requires_bolt_4x):
112-
# python -m pytest tests/integration/test_neo4j_driver.py -s -v -k test_neo4j_multi_database_support_different
113-
with GraphDatabase.driver(neo4j_uri, auth=auth) as driver:
114-
with driver.session() as session:
115-
# Test that default database is empty
116-
session.run("MATCH (n) DETACH DELETE n").consume()
117-
result = session.run("MATCH (p:Person) RETURN p")
118-
names = set()
119-
for ix in result:
120-
names.add(ix["p"].get("name"))
121-
assert names == set() # THIS FAILS?
122-
with driver.session(database="system") as session:
123-
session.run("DROP DATABASE testa IF EXISTS").consume()
124-
session.run("DROP DATABASE testb IF EXISTS").consume()
125-
with driver.session(database="system") as session:
126-
result = session.run("SHOW DATABASES")
127-
databases = set()
128-
for record in result:
129-
databases.add(record.get("name"))
130-
assert databases == {"system", "neo4j"}
131-
result = session.run("CREATE DATABASE testa")
132-
result.consume()
133-
result = session.run("CREATE DATABASE testb")
134-
result.consume()
135-
with driver.session(database="testa") as session:
136-
result = session.run('CREATE (p:Person {name: "ALICE"})')
137-
result.consume()
138-
with driver.session(database="testb") as session:
139-
result = session.run('CREATE (p:Person {name: "BOB"})')
140-
result.consume()
141-
with driver.session() as session:
142-
# Test that default database is still empty
143-
result = session.run("MATCH (p:Person) RETURN p")
144-
names = set()
145-
for ix in result:
146-
names.add(ix["p"].get("name"))
147-
assert names == set() # THIS FAILS?
148-
with driver.session(database="testa") as session:
149-
result = session.run("MATCH (p:Person) RETURN p")
150-
names = set()
151-
for ix in result:
152-
names.add(ix["p"].get("name"))
153-
assert names == {"ALICE", }
154-
with driver.session(database="testb") as session:
155-
result = session.run("MATCH (p:Person) RETURN p")
156-
names = set()
157-
for ix in result:
158-
names.add(ix["p"].get("name"))
159-
assert names == {"BOB", }
160-
with driver.session(database="system") as session:
161-
session.run("DROP DATABASE testa IF EXISTS").consume()
162-
with driver.session(database="system") as session:
163-
session.run("DROP DATABASE testb IF EXISTS").consume()
164-
with driver.session() as session:
165-
session.run("MATCH (n) DETACH DELETE n").consume()
166-
167-
16848
def test_neo4j_multi_database_test_routing_table_creates_new_if_deleted(neo4j_uri, auth, target, requires_bolt_4x):
16949
# python -m pytest tests/integration/test_neo4j_driver.py -s -v -k test_neo4j_multi_database_test_routing_table_creates_new_if_deleted
17050
with GraphDatabase.driver(neo4j_uri, auth=auth) as driver:

tests/unit/test_types.py

Lines changed: 53 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818
# See the License for the specific language governing permissions and
1919
# limitations under the License.
2020

21+
from itertools import product
2122

22-
from unittest import TestCase
23+
import pytest
2324

2425
from neo4j.data import DataHydrator
2526
from neo4j.graph import (
@@ -39,14 +40,20 @@
3940
def test_can_create_node():
4041
g = Graph()
4142
gh = Graph.Hydrator(g)
42-
alice = gh.hydrate_node(1, {"Person"}, {"name": "Alice", "age": 33})
43+
alice = gh.hydrate_node(123, {"Person"}, {"name": "Alice", "age": 33})
4344
assert isinstance(alice, Node)
45+
assert alice.id == 123
4446
assert alice.labels == {"Person"}
4547
assert set(alice.keys()) == {"name", "age"}
4648
assert set(alice.values()) == {"Alice", 33}
4749
assert set(alice.items()) == {("name", "Alice"), ("age", 33)}
50+
assert dict(alice) == {"name": "Alice", "age": 33}
4851
assert alice.get("name") == "Alice"
4952
assert alice.get("age") == 33
53+
assert alice.get("unknown") is None
54+
assert alice.get("unknown", None) is None
55+
assert alice.get("unknown", False) is False
56+
assert alice.get("unknown", "default") == "default"
5057
assert len(alice) == 2
5158
assert alice["name"] == "Alice"
5259
assert alice["age"] == 33
@@ -55,10 +62,11 @@ def test_can_create_node():
5562
assert set(iter(alice)) == {"name", "age"}
5663

5764

58-
def test_null_properties():
65+
def test_node_with_null_properties():
5966
g = Graph()
6067
gh = Graph.Hydrator(g)
61-
stuff = gh.hydrate_node(1, (), {"good": ["puppies", "kittens"], "bad": None})
68+
stuff = gh.hydrate_node(1, (), {"good": ["puppies", "kittens"],
69+
"bad": None})
6270
assert isinstance(stuff, Node)
6371
assert set(stuff.keys()) == {"good"}
6472
assert stuff.get("good") == ["puppies", "kittens"]
@@ -70,13 +78,26 @@ def test_null_properties():
7078
assert "bad" not in stuff
7179

7280

73-
def test_node_equality():
74-
g = Graph()
75-
node_1 = Node(g, 1234)
76-
node_2 = Node(g, 1234)
77-
node_3 = Node(g, 5678)
78-
assert node_1 == node_2
79-
assert node_1 != node_3
81+
@pytest.mark.parametrize(("g1", "id1", "props1", "g2", "id2", "props2"), (
82+
(*n1, *n2)
83+
for n1, n2 in product(
84+
(
85+
(g, id_, props)
86+
for g in (0, 1)
87+
for id_ in (1, 1234)
88+
for props in (None, {}, {"a": 1})
89+
),
90+
repeat=2
91+
)
92+
))
93+
def test_node_equality(g1, id1, props1, g2, id2, props2):
94+
graphs = (Graph(), Graph())
95+
node_1 = Node(graphs[g1], id1, props1)
96+
node_2 = Node(graphs[g2], id2, props2)
97+
if g1 == g2 and id1 == id2:
98+
assert node_1 == node_2
99+
else:
100+
assert node_1 != node_2
80101
assert node_1 != "this is not a node"
81102

82103

@@ -109,6 +130,7 @@ def test_can_create_relationship():
109130
assert alice_knows_bob.start_node == alice
110131
assert alice_knows_bob.type == "KNOWS"
111132
assert alice_knows_bob.end_node == bob
133+
assert dict(alice_knows_bob) == {"since": 1999}
112134
assert set(alice_knows_bob.keys()) == {"since"}
113135
assert set(alice_knows_bob.values()) == {1999}
114136
assert set(alice_knows_bob.items()) == {("since", 1999)}
@@ -133,32 +155,41 @@ def test_can_create_path():
133155
alice = gh.hydrate_node(1, {"Person"}, {"name": "Alice", "age": 33})
134156
bob = gh.hydrate_node(2, {"Person"}, {"name": "Bob", "age": 44})
135157
carol = gh.hydrate_node(3, {"Person"}, {"name": "Carol", "age": 55})
136-
alice_knows_bob = gh.hydrate_relationship(1, alice.id, bob.id, "KNOWS", {"since": 1999})
137-
carol_dislikes_bob = gh.hydrate_relationship(2, carol.id, bob.id, "DISLIKES", {})
158+
alice_knows_bob = gh.hydrate_relationship(1, alice.id, bob.id, "KNOWS",
159+
{"since": 1999})
160+
carol_dislikes_bob = gh.hydrate_relationship(2, carol.id, bob.id,
161+
"DISLIKES", {})
138162
path = Path(alice, alice_knows_bob, carol_dislikes_bob)
139163
assert isinstance(path, Path)
140-
assert path.start_node == alice
141-
assert path.end_node == carol
164+
assert path.start_node is alice
165+
assert path.end_node is carol
142166
assert path.nodes == (alice, bob, carol)
143167
assert path.relationships == (alice_knows_bob, carol_dislikes_bob)
144168
assert list(path) == [alice_knows_bob, carol_dislikes_bob]
145169

146170

147-
def test_can_hydrate_path():
171+
@pytest.mark.parametrize("cyclic", (True, False))
172+
def test_can_hydrate_path(cyclic):
148173
g = Graph()
149174
gh = Graph.Hydrator(g)
150175
alice = gh.hydrate_node(1, {"Person"}, {"name": "Alice", "age": 33})
151176
bob = gh.hydrate_node(2, {"Person"}, {"name": "Bob", "age": 44})
152-
carol = gh.hydrate_node(3, {"Person"}, {"name": "Carol", "age": 55})
177+
if cyclic:
178+
carol = alice
179+
else:
180+
carol = gh.hydrate_node(3, {"Person"}, {"name": "Carol", "age": 55})
153181
r = [gh.hydrate_unbound_relationship(1, "KNOWS", {"since": 1999}),
154182
gh.hydrate_unbound_relationship(2, "DISLIKES", {})]
155183
path = gh.hydrate_path([alice, bob, carol], r, [1, 1, -2, 2])
156-
assert path.start_node == alice
157-
assert path.end_node == carol
184+
assert path.start_node is alice
185+
assert path.end_node is carol
158186
assert path.nodes == (alice, bob, carol)
159-
expected_alice_knows_bob = gh.hydrate_relationship(1, alice.id, bob.id, "KNOWS", {"since": 1999})
160-
expected_carol_dislikes_bob = gh.hydrate_relationship(2, carol.id, bob.id, "DISLIKES", {})
161-
assert path.relationships == (expected_alice_knows_bob, expected_carol_dislikes_bob)
187+
expected_alice_knows_bob = gh.hydrate_relationship(1, alice.id, bob.id,
188+
"KNOWS", {"since": 1999})
189+
expected_carol_dislikes_bob = gh.hydrate_relationship(2, carol.id, bob.id,
190+
"DISLIKES", {})
191+
assert path.relationships == (expected_alice_knows_bob,
192+
expected_carol_dislikes_bob)
162193
assert list(path) == [expected_alice_knows_bob, expected_carol_dislikes_bob]
163194

164195

0 commit comments

Comments
 (0)