Skip to content

Commit 5b82e47

Browse files
authored
2nd round of migrating integration tests to testkit (#564)
1 parent f138da2 commit 5b82e47

File tree

5 files changed

+183
-257
lines changed

5 files changed

+183
-257
lines changed

tests/integration/test_bolt_driver.py

Lines changed: 2 additions & 204 deletions
Original file line numberDiff line numberDiff line change
@@ -60,198 +60,6 @@ def test_encrypted_set_to_false_by_default(bolt_driver):
6060
assert bolt_driver.encrypted is False
6161

6262

63-
def test_bolt_driver_fetch_size_config_case_on_close_result_consume(bolt_uri, auth):
64-
# python -m pytest tests/integration/test_bolt_driver.py -s -v -k test_bolt_driver_fetch_size_config_case_on_close_result_consume
65-
try:
66-
with GraphDatabase.driver(bolt_uri, auth=auth, user_agent="test") as driver:
67-
assert isinstance(driver, BoltDriver)
68-
with driver.session(fetch_size=2, default_access_mode=READ_ACCESS) as session:
69-
result = session.run("UNWIND [1,2,3,4] AS x RETURN x")
70-
# Check the expected result with logging manually
71-
except ServiceUnavailable as error:
72-
if isinstance(error.__cause__, BoltHandshakeError):
73-
pytest.skip(error.args[0])
74-
75-
76-
def test_bolt_driver_fetch_size_config_case_normal(bolt_uri, auth):
77-
# python -m pytest tests/integration/test_bolt_driver.py -s -v -k test_bolt_driver_fetch_size_config_case_normal
78-
try:
79-
with GraphDatabase.driver(bolt_uri, auth=auth, user_agent="test") as driver:
80-
assert isinstance(driver, BoltDriver)
81-
with driver.session(fetch_size=2, default_access_mode=READ_ACCESS) as session:
82-
expected = []
83-
result = session.run("UNWIND [1,2,3,4] AS x RETURN x")
84-
for record in result:
85-
expected.append(record["x"])
86-
87-
assert expected == [1, 2, 3, 4]
88-
except ServiceUnavailable as error:
89-
if isinstance(error.__cause__, BoltHandshakeError):
90-
pytest.skip(error.args[0])
91-
92-
93-
def test_bolt_driver_fetch_size_config_run_consume_run(bolt_uri, auth):
94-
# python -m pytest tests/integration/test_bolt_driver.py -s -v -k test_bolt_driver_fetch_size_config_run_consume_run
95-
try:
96-
with GraphDatabase.driver(bolt_uri, auth=auth, user_agent="test") as driver:
97-
assert isinstance(driver, BoltDriver)
98-
with driver.session(fetch_size=2, default_access_mode=READ_ACCESS) as session:
99-
expected = []
100-
result1 = session.run("UNWIND [1,2,3,4] AS x RETURN x")
101-
result1.consume()
102-
result2 = session.run("UNWIND [5,6,7,8] AS x RETURN x")
103-
104-
for record in result2:
105-
expected.append(record["x"])
106-
107-
result_summary = result2.consume()
108-
assert isinstance(result_summary, ResultSummary)
109-
110-
assert expected == [5, 6, 7, 8]
111-
except ServiceUnavailable as error:
112-
if isinstance(error.__cause__, BoltHandshakeError):
113-
pytest.skip(error.args[0])
114-
115-
116-
def test_bolt_driver_fetch_size_config_run_run(bolt_uri, auth):
117-
# python -m pytest tests/integration/test_bolt_driver.py -s -v -k test_bolt_driver_fetch_size_config_run_run
118-
try:
119-
with GraphDatabase.driver(bolt_uri, auth=auth, user_agent="test") as driver:
120-
assert isinstance(driver, BoltDriver)
121-
with driver.session(fetch_size=2, default_access_mode=READ_ACCESS) as session:
122-
expected = []
123-
result1 = session.run("UNWIND [1,2,3,4] AS x RETURN x")
124-
result2 = session.run("UNWIND [5,6,7,8] AS x RETURN x")
125-
126-
for record in result2:
127-
expected.append(record["x"])
128-
129-
result_summary = result2.consume()
130-
assert isinstance(result_summary, ResultSummary)
131-
132-
assert expected == [5, 6, 7, 8]
133-
except ServiceUnavailable as error:
134-
if isinstance(error.__cause__, BoltHandshakeError):
135-
pytest.skip(error.args[0])
136-
137-
138-
def test_bolt_driver_read_transaction_fetch_size_config_normal_case(bolt_uri, auth):
139-
# python -m pytest tests/integration/test_bolt_driver.py -s -v -k test_bolt_driver_read_transaction_fetch_size_config_normal_case
140-
@unit_of_work(timeout=3, metadata={"foo": "bar"})
141-
def unwind(transaction):
142-
assert isinstance(transaction, Transaction)
143-
values = []
144-
result = transaction.run("UNWIND [1,2,3,4] AS x RETURN x")
145-
assert isinstance(result, Result)
146-
for record in result:
147-
values.append(record["x"])
148-
return values
149-
150-
try:
151-
with GraphDatabase.driver(bolt_uri, auth=auth, user_agent="test") as driver:
152-
assert isinstance(driver, BoltDriver)
153-
with driver.session(fetch_size=2, default_access_mode=READ_ACCESS) as session:
154-
expected = session.read_transaction(unwind)
155-
156-
assert expected == [1, 2, 3, 4]
157-
except ServiceUnavailable as error:
158-
if isinstance(error.__cause__, BoltHandshakeError):
159-
pytest.skip(error.args[0])
160-
161-
162-
def test_bolt_driver_multiple_results_case_1(bolt_uri, auth):
163-
# python -m pytest tests/integration/test_bolt_driver.py -s -v -k test_bolt_driver_multiple_results_case_1
164-
try:
165-
with GraphDatabase.driver(bolt_uri, auth=auth, user_agent="test") as driver:
166-
assert isinstance(driver, BoltDriver)
167-
with driver.session(fetch_size=2, default_access_mode=READ_ACCESS) as session:
168-
transaction = session.begin_transaction(timeout=3, metadata={"foo": "bar"})
169-
result1 = transaction.run("UNWIND [1,2,3,4] AS x RETURN x")
170-
values1 = []
171-
for ix in result1:
172-
values1.append(ix["x"])
173-
transaction.commit()
174-
assert values1 == [1, 2, 3, 4]
175-
176-
except ServiceUnavailable as error:
177-
if isinstance(error.__cause__, BoltHandshakeError):
178-
pytest.skip(error.args[0])
179-
180-
181-
def test_bolt_driver_multiple_results_case_2(bolt_uri, auth):
182-
# python -m pytest tests/integration/test_bolt_driver.py -s -v -k test_bolt_driver_multiple_results_case_2
183-
try:
184-
with GraphDatabase.driver(bolt_uri, auth=auth, user_agent="test") as driver:
185-
assert isinstance(driver, BoltDriver)
186-
with driver.session(fetch_size=2, default_access_mode=READ_ACCESS) as session:
187-
transaction = session.begin_transaction(timeout=3, metadata={"foo": "bar"})
188-
result1 = transaction.run("UNWIND [1,2,3,4] AS x RETURN x")
189-
result2 = transaction.run("UNWIND [5,6,7,8] AS x RETURN x")
190-
values1 = []
191-
values2 = []
192-
for ix in result2:
193-
values2.append(ix["x"])
194-
for ix in result1:
195-
values1.append(ix["x"])
196-
transaction.commit()
197-
assert values2 == [5, 6, 7, 8]
198-
assert values1 == [1, 2, 3, 4]
199-
200-
except ServiceUnavailable as error:
201-
if isinstance(error.__cause__, BoltHandshakeError):
202-
pytest.skip(error.args[0])
203-
204-
205-
def test_bolt_driver_multiple_results_case_3(bolt_uri, auth):
206-
# python -m pytest tests/integration/test_bolt_driver.py -s -v -k test_bolt_driver_multiple_results_case_3
207-
try:
208-
with GraphDatabase.driver(bolt_uri, auth=auth, user_agent="test") as driver:
209-
assert isinstance(driver, BoltDriver)
210-
with driver.session(fetch_size=2, default_access_mode=READ_ACCESS) as session:
211-
transaction = session.begin_transaction(timeout=3, metadata={"foo": "bar"})
212-
values1 = []
213-
values2 = []
214-
result1 = transaction.run("UNWIND [1,2,3,4] AS x RETURN x")
215-
result1_iter = iter(result1)
216-
values1.append(next(result1_iter)["x"])
217-
result2 = transaction.run("UNWIND [5,6,7,8] AS x RETURN x")
218-
for ix in result2:
219-
values2.append(ix["x"])
220-
transaction.commit() # Should discard the rest of records in result1 and result2 -> then set mode discard.
221-
assert values2 == [5, 6, 7, 8]
222-
assert result2._closed is True
223-
assert values1 == [1, ]
224-
225-
try:
226-
values1.append(next(result1_iter)["x"])
227-
except StopIteration as e:
228-
# Bolt 4.0
229-
assert values1 == [1, ]
230-
assert result1._closed is True
231-
else:
232-
# Bolt 3 only have PULL ALL and no qid so it will behave like autocommit r1=session.run rs2=session.run
233-
values1.append(next(result1_iter)["x"])
234-
assert values1 == [1, 2, 3]
235-
assert result1._closed is False
236-
237-
assert result1._closed is True
238-
239-
except ServiceUnavailable as error:
240-
if isinstance(error.__cause__, BoltHandshakeError):
241-
pytest.skip(error.args[0])
242-
243-
244-
def test_bolt_driver_case_pull_no_records(driver):
245-
# python -m pytest tests/integration/test_bolt_driver.py -s -v -k test_bolt_driver_case_pull_no_records
246-
try:
247-
with driver.session(default_access_mode=WRITE_ACCESS) as session:
248-
with session.begin_transaction() as tx:
249-
result = tx.run("CREATE (a:Thing {uuid:$uuid})", uuid=123)
250-
except ServiceUnavailable as error:
251-
if isinstance(error.__cause__, BoltHandshakeError):
252-
pytest.skip(error.args[0])
253-
254-
25563
@fixture
25664
def server_info(driver):
25765
""" Simple fixture to provide quick and easy access to a
@@ -262,18 +70,8 @@ def server_info(driver):
26270
yield summary.server
26371

26472

265-
def test_server_address(server_info):
266-
assert server_info.address == ("127.0.0.1", 7687)
267-
268-
269-
def test_server_protocol_version(server_info):
270-
assert server_info.protocol_version >= (3, 0)
271-
272-
273-
def test_server_agent(server_info):
274-
assert server_info.agent.startswith("Neo4j/")
275-
276-
73+
# TODO: this test will stay asy python is currently the only driver exposing the
74+
# connection id. So this might change in the future.
27775
def test_server_connection_id(server_info):
27876
cid = server_info.connection_id
27977
assert cid.startswith("bolt-") and cid[5:].isdigit()
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#!/usr/bin/env python
2+
# -*- encoding: utf-8 -*-
3+
4+
# Copyright (c) "Neo4j"
5+
# Neo4j Sweden AB [http://neo4j.com]
6+
#
7+
# This file is part of Neo4j.
8+
#
9+
# Licensed under the Apache License, Version 2.0 (the "License");
10+
# you may not use this file except in compliance with the License.
11+
# You may obtain a copy of the License at
12+
#
13+
# http://www.apache.org/licenses/LICENSE-2.0
14+
#
15+
# Unless required by applicable law or agreed to in writing, software
16+
# distributed under the License is distributed on an "AS IS" BASIS,
17+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
# See the License for the specific language governing permissions and
19+
# limitations under the License.
20+
21+
22+
import inspect
23+
from unittest.mock import NonCallableMagicMock
24+
25+
import pytest
26+
27+
from neo4j import ServerInfo
28+
29+
30+
class FakeConnection(NonCallableMagicMock):
31+
callbacks = []
32+
server_info = ServerInfo("127.0.0.1", (4, 3))
33+
34+
def fetch_message(self, *args, **kwargs):
35+
if self.callbacks:
36+
cb = self.callbacks.pop(0)
37+
cb()
38+
return super().__getattr__("fetch_message")(*args, **kwargs)
39+
40+
def fetch_all(self, *args, **kwargs):
41+
while self.callbacks:
42+
cb = self.callbacks.pop(0)
43+
cb()
44+
return super().__getattr__("fetch_all")(*args, **kwargs)
45+
46+
def __getattr__(self, name):
47+
parent = super()
48+
49+
def build_message_handler(name):
50+
def func(*args, **kwargs):
51+
def callback():
52+
for cb_name, param_count in (
53+
("on_success", 1),
54+
("on_summary", 0)
55+
):
56+
cb = kwargs.get(cb_name, None)
57+
if callable(cb):
58+
try:
59+
param_count = \
60+
len(inspect.signature(cb).parameters)
61+
except ValueError:
62+
# e.g. built-in method as cb
63+
pass
64+
if param_count == 1:
65+
cb({})
66+
else:
67+
cb()
68+
self.callbacks.append(callback)
69+
return parent.__getattr__(name)(*args, **kwargs)
70+
71+
return func
72+
73+
if name in ("run", "commit", "pull", "rollback", "discard"):
74+
return build_message_handler(name)
75+
return parent.__getattr__(name)
76+
77+
def defunct(self):
78+
return False
79+
80+
81+
@pytest.fixture
82+
def fake_connection():
83+
return FakeConnection()

tests/unit/work/test_result.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ def _fetch_and_compare_all_records(result, key, expected_records, method,
189189
received_records.append([record.data().get(key, None)])
190190
if limit is not None and len(received_records) == limit:
191191
break
192+
if limit is None:
193+
assert result._closed
192194
elif method == "next":
193195
iter_ = iter(result)
194196
n = len(expected_records) if limit is None else limit
@@ -197,13 +199,15 @@ def _fetch_and_compare_all_records(result, key, expected_records, method,
197199
if limit is None:
198200
with pytest.raises(StopIteration):
199201
received_records.append([next(iter_).get(key, None)])
202+
assert result._closed
200203
elif method == "new iter":
201204
n = len(expected_records) if limit is None else limit
202205
for _ in range(n):
203206
received_records.append([next(iter(result)).get(key, None)])
204207
if limit is None:
205208
with pytest.raises(StopIteration):
206209
received_records.append([next(iter(result)).get(key, None)])
210+
assert result._closed
207211
else:
208212
raise ValueError()
209213
assert received_records == expected_records

0 commit comments

Comments
 (0)