Skip to content

Commit

Permalink
8th and final round of migrating integration tests to TestKit (#873)
Browse files Browse the repository at this point in the history
* TestKit backend: except txMeta as Cypher types
  • Loading branch information
robsdedude authored Dec 15, 2022
1 parent af3e930 commit 4611b00
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 169 deletions.
13 changes: 9 additions & 4 deletions testkitbackend/fromtestkit.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,22 @@ def to_cypher_and_params(data):
if params is None:
return data["cypher"], None
# Transform the params to Python native
params_dict = {p: to_param(params[p]) for p in params}
params_dict = {k: to_param(v) for k, v in params.items()}
if isinstance(params, Request):
params.mark_all_as_read()
return data["cypher"], params_dict


def to_tx_kwargs(data):
from .backend import Request
kwargs = {}
if "txMeta" in data:
kwargs["metadata"] = data["txMeta"]
if isinstance(kwargs["metadata"], Request):
kwargs["metadata"].mark_all_as_read()
metadata = data["txMeta"]
kwargs["metadata"] = metadata
if metadata is not None:
kwargs["metadata"] = {k: to_param(v) for k, v in metadata.items()}
if isinstance(metadata, Request):
metadata.mark_all_as_read()
if "timeout" in data:
kwargs["timeout"] = data["timeout"]
if kwargs["timeout"] is not None:
Expand Down
8 changes: 6 additions & 2 deletions tests/integration/test_readme.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
# python -m pytest tests/integration/test_readme.py -s -v


def _work(tx, query, **params):
tx.run(query, **params).consume()


def test_should_run_readme(uri, auth):
names = set()
print = names.add
Expand Down Expand Up @@ -49,14 +53,14 @@ def print_friends(tx, name):

with driver.session(database="neo4j") as session:
# === END: README ===
session.run("MATCH (a) DETACH DELETE a")
session.execute_write(_work, "MATCH (a) DETACH DELETE a")
# === START: README ===
session.execute_write(add_friend, "Arthur", "Guinevere")
session.execute_write(add_friend, "Arthur", "Lancelot")
session.execute_write(add_friend, "Arthur", "Merlin")
session.execute_read(print_friends, "Arthur")
# === END: README ===
session.run("MATCH (a) DETACH DELETE a")
session.execute_write(_work, "MATCH (a) DETACH DELETE a")
# === START: README ===

driver.close()
Expand Down
159 changes: 0 additions & 159 deletions tests/integration/test_tx_functions.py

This file was deleted.

13 changes: 11 additions & 2 deletions tests/unit/async_/work/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@
@contextmanager
def assert_warns_tx_func_deprecation(tx_func_name):
if tx_func_name.endswith("_transaction"):
with pytest.warns(DeprecationWarning,
match=f"{tx_func_name}.*execute_"):
mode = tx_func_name.split("_")[0]
with pytest.warns(
DeprecationWarning,
match=f"^{mode}_transaction has been renamed to execute_{mode}$"
):
yield
else:
yield
Expand Down Expand Up @@ -289,6 +292,12 @@ async def work(tx):
with assert_warns_tx_func_deprecation(tx_type):
await getattr(session, tx_type)(work)
assert called
assert len(fake_pool.acquired_connection_mocks) == 1
cx = fake_pool.acquired_connection_mocks[0]
cx.begin.assert_called_once()
for key in ("timeout", "metadata"):
value = decorator_kwargs.get(key)
assert cx.begin.call_args[1][key] == value


@mark_async_test
Expand Down
29 changes: 29 additions & 0 deletions tests/unit/async_/work/test_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,35 @@ async def test_transaction_run_takes_no_query_object(async_fake_connection):
await tx.run(Query("RETURN 1"))


@mark_async_test
@pytest.mark.parametrize("params", (
{"x": 1},
{"x": "1"},
{"x": "1", "y": 2},
{"parameters": {"nested": "parameters"}},
))
@pytest.mark.parametrize("as_kwargs", (True, False))
async def test_transaction_run_parameters(
async_fake_connection, params, as_kwargs
):
on_closed = MagicMock()
on_error = MagicMock()
on_cancel = MagicMock()
tx = AsyncTransaction(async_fake_connection, 2, on_closed, on_error,
on_cancel)
if not as_kwargs:
params = {"parameters": params}
await tx.run("RETURN $x", **params)
calls = [call for call in async_fake_connection.method_calls
if call[0] in ("run", "send_all", "fetch_message")]
assert [call[0] for call in calls] == ["run", "send_all", "fetch_message"]
run = calls[0]
assert run[1][0] == "RETURN $x"
if "parameters" in params:
params = params["parameters"]
assert run[2]["parameters"] == params


@mark_async_test
async def test_transaction_rollbacks_on_open_connections(
async_fake_connection
Expand Down
13 changes: 11 additions & 2 deletions tests/unit/sync/work/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@
@contextmanager
def assert_warns_tx_func_deprecation(tx_func_name):
if tx_func_name.endswith("_transaction"):
with pytest.warns(DeprecationWarning,
match=f"{tx_func_name}.*execute_"):
mode = tx_func_name.split("_")[0]
with pytest.warns(
DeprecationWarning,
match=f"^{mode}_transaction has been renamed to execute_{mode}$"
):
yield
else:
yield
Expand Down Expand Up @@ -289,6 +292,12 @@ def work(tx):
with assert_warns_tx_func_deprecation(tx_type):
getattr(session, tx_type)(work)
assert called
assert len(fake_pool.acquired_connection_mocks) == 1
cx = fake_pool.acquired_connection_mocks[0]
cx.begin.assert_called_once()
for key in ("timeout", "metadata"):
value = decorator_kwargs.get(key)
assert cx.begin.call_args[1][key] == value


@mark_sync_test
Expand Down
29 changes: 29 additions & 0 deletions tests/unit/sync/work/test_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,35 @@ def test_transaction_run_takes_no_query_object(fake_connection):
tx.run(Query("RETURN 1"))


@mark_sync_test
@pytest.mark.parametrize("params", (
{"x": 1},
{"x": "1"},
{"x": "1", "y": 2},
{"parameters": {"nested": "parameters"}},
))
@pytest.mark.parametrize("as_kwargs", (True, False))
def test_transaction_run_parameters(
fake_connection, params, as_kwargs
):
on_closed = MagicMock()
on_error = MagicMock()
on_cancel = MagicMock()
tx = Transaction(fake_connection, 2, on_closed, on_error,
on_cancel)
if not as_kwargs:
params = {"parameters": params}
tx.run("RETURN $x", **params)
calls = [call for call in fake_connection.method_calls
if call[0] in ("run", "send_all", "fetch_message")]
assert [call[0] for call in calls] == ["run", "send_all", "fetch_message"]
run = calls[0]
assert run[1][0] == "RETURN $x"
if "parameters" in params:
params = params["parameters"]
assert run[2]["parameters"] == params


@mark_sync_test
def test_transaction_rollbacks_on_open_connections(
fake_connection
Expand Down

0 comments on commit 4611b00

Please sign in to comment.