Skip to content

Commit 4611b00

Browse files
authored
8th and final round of migrating integration tests to TestKit (#873)
* TestKit backend: except txMeta as Cypher types
1 parent af3e930 commit 4611b00

File tree

7 files changed

+95
-169
lines changed

7 files changed

+95
-169
lines changed

testkitbackend/fromtestkit.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,22 @@ def to_cypher_and_params(data):
4040
if params is None:
4141
return data["cypher"], None
4242
# Transform the params to Python native
43-
params_dict = {p: to_param(params[p]) for p in params}
43+
params_dict = {k: to_param(v) for k, v in params.items()}
44+
if isinstance(params, Request):
45+
params.mark_all_as_read()
4446
return data["cypher"], params_dict
4547

4648

4749
def to_tx_kwargs(data):
4850
from .backend import Request
4951
kwargs = {}
5052
if "txMeta" in data:
51-
kwargs["metadata"] = data["txMeta"]
52-
if isinstance(kwargs["metadata"], Request):
53-
kwargs["metadata"].mark_all_as_read()
53+
metadata = data["txMeta"]
54+
kwargs["metadata"] = metadata
55+
if metadata is not None:
56+
kwargs["metadata"] = {k: to_param(v) for k, v in metadata.items()}
57+
if isinstance(metadata, Request):
58+
metadata.mark_all_as_read()
5459
if "timeout" in data:
5560
kwargs["timeout"] = data["timeout"]
5661
if kwargs["timeout"] is not None:

tests/integration/test_readme.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
# python -m pytest tests/integration/test_readme.py -s -v
2323

2424

25+
def _work(tx, query, **params):
26+
tx.run(query, **params).consume()
27+
28+
2529
def test_should_run_readme(uri, auth):
2630
names = set()
2731
print = names.add
@@ -49,14 +53,14 @@ def print_friends(tx, name):
4953

5054
with driver.session(database="neo4j") as session:
5155
# === END: README ===
52-
session.run("MATCH (a) DETACH DELETE a")
56+
session.execute_write(_work, "MATCH (a) DETACH DELETE a")
5357
# === START: README ===
5458
session.execute_write(add_friend, "Arthur", "Guinevere")
5559
session.execute_write(add_friend, "Arthur", "Lancelot")
5660
session.execute_write(add_friend, "Arthur", "Merlin")
5761
session.execute_read(print_friends, "Arthur")
5862
# === END: README ===
59-
session.run("MATCH (a) DETACH DELETE a")
63+
session.execute_write(_work, "MATCH (a) DETACH DELETE a")
6064
# === START: README ===
6165

6266
driver.close()

tests/integration/test_tx_functions.py

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

tests/unit/async_/work/test_session.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,11 @@
4040
@contextmanager
4141
def assert_warns_tx_func_deprecation(tx_func_name):
4242
if tx_func_name.endswith("_transaction"):
43-
with pytest.warns(DeprecationWarning,
44-
match=f"{tx_func_name}.*execute_"):
43+
mode = tx_func_name.split("_")[0]
44+
with pytest.warns(
45+
DeprecationWarning,
46+
match=f"^{mode}_transaction has been renamed to execute_{mode}$"
47+
):
4548
yield
4649
else:
4750
yield
@@ -289,6 +292,12 @@ async def work(tx):
289292
with assert_warns_tx_func_deprecation(tx_type):
290293
await getattr(session, tx_type)(work)
291294
assert called
295+
assert len(fake_pool.acquired_connection_mocks) == 1
296+
cx = fake_pool.acquired_connection_mocks[0]
297+
cx.begin.assert_called_once()
298+
for key in ("timeout", "metadata"):
299+
value = decorator_kwargs.get(key)
300+
assert cx.begin.call_args[1][key] == value
292301

293302

294303
@mark_async_test

tests/unit/async_/work/test_transaction.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,35 @@ async def test_transaction_run_takes_no_query_object(async_fake_connection):
130130
await tx.run(Query("RETURN 1"))
131131

132132

133+
@mark_async_test
134+
@pytest.mark.parametrize("params", (
135+
{"x": 1},
136+
{"x": "1"},
137+
{"x": "1", "y": 2},
138+
{"parameters": {"nested": "parameters"}},
139+
))
140+
@pytest.mark.parametrize("as_kwargs", (True, False))
141+
async def test_transaction_run_parameters(
142+
async_fake_connection, params, as_kwargs
143+
):
144+
on_closed = MagicMock()
145+
on_error = MagicMock()
146+
on_cancel = MagicMock()
147+
tx = AsyncTransaction(async_fake_connection, 2, on_closed, on_error,
148+
on_cancel)
149+
if not as_kwargs:
150+
params = {"parameters": params}
151+
await tx.run("RETURN $x", **params)
152+
calls = [call for call in async_fake_connection.method_calls
153+
if call[0] in ("run", "send_all", "fetch_message")]
154+
assert [call[0] for call in calls] == ["run", "send_all", "fetch_message"]
155+
run = calls[0]
156+
assert run[1][0] == "RETURN $x"
157+
if "parameters" in params:
158+
params = params["parameters"]
159+
assert run[2]["parameters"] == params
160+
161+
133162
@mark_async_test
134163
async def test_transaction_rollbacks_on_open_connections(
135164
async_fake_connection

tests/unit/sync/work/test_session.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,11 @@
4040
@contextmanager
4141
def assert_warns_tx_func_deprecation(tx_func_name):
4242
if tx_func_name.endswith("_transaction"):
43-
with pytest.warns(DeprecationWarning,
44-
match=f"{tx_func_name}.*execute_"):
43+
mode = tx_func_name.split("_")[0]
44+
with pytest.warns(
45+
DeprecationWarning,
46+
match=f"^{mode}_transaction has been renamed to execute_{mode}$"
47+
):
4548
yield
4649
else:
4750
yield
@@ -289,6 +292,12 @@ def work(tx):
289292
with assert_warns_tx_func_deprecation(tx_type):
290293
getattr(session, tx_type)(work)
291294
assert called
295+
assert len(fake_pool.acquired_connection_mocks) == 1
296+
cx = fake_pool.acquired_connection_mocks[0]
297+
cx.begin.assert_called_once()
298+
for key in ("timeout", "metadata"):
299+
value = decorator_kwargs.get(key)
300+
assert cx.begin.call_args[1][key] == value
292301

293302

294303
@mark_sync_test

tests/unit/sync/work/test_transaction.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,35 @@ def test_transaction_run_takes_no_query_object(fake_connection):
130130
tx.run(Query("RETURN 1"))
131131

132132

133+
@mark_sync_test
134+
@pytest.mark.parametrize("params", (
135+
{"x": 1},
136+
{"x": "1"},
137+
{"x": "1", "y": 2},
138+
{"parameters": {"nested": "parameters"}},
139+
))
140+
@pytest.mark.parametrize("as_kwargs", (True, False))
141+
def test_transaction_run_parameters(
142+
fake_connection, params, as_kwargs
143+
):
144+
on_closed = MagicMock()
145+
on_error = MagicMock()
146+
on_cancel = MagicMock()
147+
tx = Transaction(fake_connection, 2, on_closed, on_error,
148+
on_cancel)
149+
if not as_kwargs:
150+
params = {"parameters": params}
151+
tx.run("RETURN $x", **params)
152+
calls = [call for call in fake_connection.method_calls
153+
if call[0] in ("run", "send_all", "fetch_message")]
154+
assert [call[0] for call in calls] == ["run", "send_all", "fetch_message"]
155+
run = calls[0]
156+
assert run[1][0] == "RETURN $x"
157+
if "parameters" in params:
158+
params = params["parameters"]
159+
assert run[2]["parameters"] == params
160+
161+
133162
@mark_sync_test
134163
def test_transaction_rollbacks_on_open_connections(
135164
fake_connection

0 commit comments

Comments
 (0)