Skip to content

Commit

Permalink
8th and final round of migrating integration tests from Python (#538)
Browse files Browse the repository at this point in the history
* Rename session.read/write_transaction -> execute_read/write
* 8th and final round of migrating integration tests from Python
* Protocol: wrap tx metadata in Cypher types
  • Loading branch information
robsdedude authored Dec 15, 2022
1 parent 8c237fd commit 9e357ef
Show file tree
Hide file tree
Showing 29 changed files with 220 additions and 153 deletions.
8 changes: 4 additions & 4 deletions nutkit/frontend/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def close(self, hooks=None):
if not isinstance(res, protocol.Session):
raise Exception("Should be session but was: %s" % res)

def run(self, cypher, params=None, tx_meta=None, hooks=None, **kwargs):
def run(self, cypher, *, params=None, tx_meta=None, hooks=None, **kwargs):
req = protocol.SessionRun(self._session.id, cypher, params,
txMeta=tx_meta, **kwargs)
res = self._driver.send_and_receive(req, hooks=hooks,
Expand Down Expand Up @@ -80,21 +80,21 @@ def process_transaction(self, req, fn, config=None, hooks=None):
"Should be RetryableTry or RetryableDone but was: %s" % res
)

def read_transaction(self, fn, tx_meta=None, hooks=None, **kwargs):
def execute_read(self, fn, *, tx_meta=None, hooks=None, **kwargs):
# Send request to enter transactional read function
req = protocol.SessionReadTransaction(
self._session.id, txMeta=tx_meta, **kwargs
)
return self.process_transaction(req, fn, hooks=hooks)

def write_transaction(self, fn, tx_meta=None, hooks=None, **kwargs):
def execute_write(self, fn, *, tx_meta=None, hooks=None, **kwargs):
# Send request to enter transactional read function
req = protocol.SessionWriteTransaction(
self._session.id, txMeta=tx_meta, **kwargs
)
return self.process_transaction(req, fn, hooks=hooks)

def begin_transaction(self, tx_meta=None, hooks=None, **kwargs):
def begin_transaction(self, *, tx_meta=None, hooks=None, **kwargs):
req = protocol.SessionBeginTransaction(
self._session.id, txMeta=tx_meta, **kwargs
)
Expand Down
6 changes: 3 additions & 3 deletions tests/neo4j/datatypes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def work(tx):
assert isinstance(result.next(), types.NullRecord)
return record_

record = self._session.read_transaction(work)
record = self._session.execute_read(work)
self.assertEqual(record, types.Record(values=[val]))

def _read_query_values(self, query, params=None):
Expand All @@ -45,7 +45,7 @@ def work(tx):
assert isinstance(result.next(), types.NullRecord)
return record_.values

return self._session.read_transaction(work)
return self._session.execute_read(work)

def _write_query_values(self, query, params=None):
values = []
Expand All @@ -60,5 +60,5 @@ def work(tx):
raise ApplicationCodeError

with self.assertRaises(types.FrontendError):
self._session.write_transaction(work)
self._session.execute_write(work)
return values
6 changes: 3 additions & 3 deletions tests/neo4j/datatypes/test_datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def work(tx):

self._create_driver_and_session()

record = self._session.write_transaction(work)
record = self._session.execute_write(work)
self.assertIsInstance(record, types.Record)

node = record.values[0]
Expand All @@ -173,7 +173,7 @@ def work(tx):

self._create_driver_and_session()

record = self._session.write_transaction(work)
record = self._session.execute_write(work)
self.assertIsInstance(record, types.Record)
values = record.values
self.assertEqual(len(values), 3)
Expand Down Expand Up @@ -205,7 +205,7 @@ def work(tx):

self._create_driver_and_session()

record = self._session.write_transaction(work)
record = self._session.execute_write(work)
self.assertIsInstance(record, types.Record)
values = record.values
self.assertEqual(len(values), 6)
Expand Down
4 changes: 2 additions & 2 deletions tests/neo4j/datatypes/test_temporal_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def work(tx):

assert self._driver and self._session
try:
echoed_tz_id = self._session.read_transaction(work)
echoed_tz_id = self._session.execute_read(work)
return echoed_tz_id == tz_id
except types.DriverError as e:
assert tz_id in e.msg
Expand Down Expand Up @@ -208,7 +208,7 @@ def work(tx):
continue
with self.subTest(tz_id=tz_id):
dt, y, mo, d, h, m, s, ns, offset, tz = \
self._session.read_transaction(work)
self._session.execute_read(work)
cypher_dt = types.CypherDateTime(
y, mo, d, h, m, s, ns, offset, tz_id
)
Expand Down
2 changes: 1 addition & 1 deletion tests/neo4j/test_authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def dummy_query(tx_or_session):

self.create_driver_and_session(auth_token)
if use_tx:
result = self._session.read_transaction(dummy_query)
result = self._session.execute_read(dummy_query)
else:
result = self._session.run("RETURN 2 as Number")
self.assertEqual(
Expand Down
6 changes: 3 additions & 3 deletions tests/neo4j/test_bookmarks.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def work(tx):
result.next()

with self.assertRaises(types.DriverError) as exc:
self._session.read_transaction(work)
self._session.execute_read(work)
if get_driver_name() in ["java"]:
self.assertEqual(
"org.neo4j.driver.exceptions.ClientException",
Expand Down Expand Up @@ -151,7 +151,7 @@ def create_node(tx):

for _ in range(expected_node_count):
self._session = self._driver.session("w")
self._session.write_transaction(create_node)
self._session.execute_write(create_node)
bookmarks.append(self._session.last_bookmarks())
self._session.close()

Expand All @@ -167,7 +167,7 @@ def get_node_count(tx):
record = result.next()
return record.values[0]

count = self._session.read_transaction(get_node_count)
count = self._session.execute_read(get_node_count)
self.assertEqual(types.CypherInt(expected_node_count), count)

@cluster_unsafe_test
Expand Down
2 changes: 1 addition & 1 deletion tests/neo4j/test_direct_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def work(tx):

self._driver = get_driver(self._backend)
self._session = self._driver.session("w")
summary = self._session.read_transaction(work)
summary = self._session.execute_read(work)
result = self._driver.supports_multi_db()

self.assertTrue(result)
Expand Down
4 changes: 2 additions & 2 deletions tests/neo4j/test_session_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def test_autocommit_transactions_should_support_metadata(self):
self._session1 = self._driver.session("r")
result = self._session1.run(
"CALL tx.getMetaData",
tx_meta={k: v.value for k, v in metadata.items()}
tx_meta=metadata
)
record = result.next()
self.assertIsInstance(record, types.Record)
Expand Down Expand Up @@ -294,7 +294,7 @@ def test_iteration_nested(self):
def run(i, n):
return self._session1.run(
"UNWIND RANGE ($i, $n) AS x RETURN x",
{"i": types.CypherInt(i), "n": types.CypherInt(n)}
params={"i": types.CypherInt(i), "n": types.CypherInt(n)}
)
i0 = 0
n0 = 6
Expand Down
2 changes: 1 addition & 1 deletion tests/neo4j/test_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def work(tx):
return summary
params = {} if params is None else params
self._session = self._driver.session("w")
return self._session.write_transaction(work)
return self._session.execute_write(work)

def test_can_obtain_summary_after_consuming_result(self):
summary = self.get_summary("CREATE (n) RETURN n")
Expand Down
57 changes: 38 additions & 19 deletions tests/neo4j/test_tx_func_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,38 @@ def _test():
self._driver.close()
self._driver = get_driver(self._backend, user_agent="test")
self._session1 = self._driver.session("r", fetch_size=2)
self._session1.read_transaction(work)
self._session1.execute_read(work)
self._session1.close()
self._session1 = None

for consume in (True, False):
with self.subTest(consume=consume):
_test()

def test_parameter(self):
def work(tx):
result = tx.run("RETURN $x", {"x": types.CypherInt(1)})
self.assertEqual(list(result),
[types.Record([types.CypherInt(1)])])

self._session1 = self._driver.session("r")
self._session1.execute_read(work)

def test_meta_data(self):
metadata = {"foo": types.CypherFloat(1.5),
"bar": types.CypherString("baz")}

def work(tx):
result = tx.run("CALL tx.getMetaData")
record = result.next()
self.assertIsInstance(record, types.Record)
self.assertEqual(record.values, [types.CypherMap(metadata)])

self._session1 = self._driver.session("r")
self._session1.execute_read(
work, tx_meta=metadata,
)

def test_iteration_nested(self):
# Verifies that it is possible to nest results with small fetch sizes
# within a transaction function.
Expand All @@ -57,8 +81,10 @@ def test_iteration_nested(self):
self.skipTest("Fails for some reason")

def run(tx, i, n):
return tx.run("UNWIND RANGE ($i, $n) AS x RETURN x",
{"i": types.CypherInt(i), "n": types.CypherInt(n)})
return tx.run(
"UNWIND RANGE ($i, $n) AS x RETURN x",
params={"i": types.CypherInt(i), "n": types.CypherInt(n)}
)

self._session1 = self._driver.session("r", fetch_size=2)

Expand Down Expand Up @@ -100,7 +126,7 @@ def nested(tx):
self.assertEqual(res0.next(), types.NullRecord())
return "done"

x = self._session1.read_transaction(nested)
x = self._session1.execute_read(nested)
self.assertEqual(lasts, {0: 6, 1: 11, 2: 1001})
self.assertEqual(x, "done")

Expand All @@ -112,7 +138,7 @@ def run(tx):
tx.run("CREATE (n:SessionNode) RETURN n")

self._session1 = self._driver.session("w")
self._session1.write_transaction(run)
self._session1.execute_write(run)
bookmarks = self._session1.last_bookmarks()
self.assertEqual(len(bookmarks), 1)
self.assertGreater(len(bookmarks[0]), 3)
Expand All @@ -126,7 +152,7 @@ def run(tx):

self._session1 = self._driver.session("w")
with self.assertRaises(types.FrontendError):
self._session1.write_transaction(run)
self._session1.execute_write(run)
bookmarks = self._session1.last_bookmarks()
self.assertEqual(len(bookmarks), 0)

Expand All @@ -151,15 +177,11 @@ def assertion_query(tx):
self._session1 = self._driver.session("w")
expected_exc = types.FrontendError
with self.assertRaises(expected_exc):
self._session1.write_transaction(run)
self._session1.execute_write(run)

self._session1.read_transaction(assertion_query)
self._session1.execute_read(assertion_query)

def test_tx_func_configuration(self):
# TODO: remove this block once all languages work
if get_driver_name() in ["java"]:
self.skipTest("Does not send metadata")

def run(tx):
values = []
result = tx.run("UNWIND [1,2,3,4] AS x RETURN x")
Expand All @@ -177,10 +199,7 @@ def run(tx):
metadata = {"foo": types.CypherFloat(1.5),
"bar": types.CypherString("baz")}
self._session1 = self._driver.session("w")
res = self._session1.read_transaction(
run, timeout=3000,
tx_meta={k: v.value for k, v in metadata.items()}
)
res = self._session1.execute_read(run, timeout=3000, tx_meta=metadata)
self.assertEqual(res, list(map(types.CypherInt, range(1, 5))))

def test_tx_timeout(self):
Expand All @@ -198,7 +217,7 @@ def update1(tx):
tx.run("MATCH (a:Node) SET a.property = 1").consume()

with self.assertRaises(types.FrontendError):
self._session2.write_transaction(update2, timeout=250)
self._session2.execute_write(update2, timeout=250)

def update2(tx):
nonlocal exc
Expand All @@ -215,11 +234,11 @@ def update2(tx):
exc = None

self._session1 = self._driver.session("w")
db = self._session1.write_transaction(create)
db = self._session1.execute_write(create)
self._session2 = self._driver.session(
"w", bookmarks=self._session1.last_bookmarks(), database=db
)
self._session1.write_transaction(update1)
self._session1.execute_write(update1)
self.assertIsInstance(exc, types.DriverError)

self.assertEqual(exc.code,
Expand Down
10 changes: 5 additions & 5 deletions tests/neo4j/test_tx_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ def test_can_commit_transaction(self):
tx.commit()

# Check the property value
result = self._session1.run("MATCH (a) WHERE id(a) = $n "
"RETURN a.foo", {"n": node_id})
result = self._session1.run("MATCH (a) WHERE id(a) = $n RETURN a.foo",
params={"n": node_id})
record = result.next()
self.assertIsInstance(record, types.Record)
self.assertEqual(len(record.values), 1)
Expand Down Expand Up @@ -109,8 +109,8 @@ def test_can_rollback_transaction(self):
tx.rollback()

# Check the property value
result = self._session1.run("MATCH (a) WHERE id(a) = $n "
"RETURN a.foo", {"n": node_id})
result = self._session1.run("MATCH (a) WHERE id(a) = $n RETURN a.foo",
params={"n": node_id})
record = result.next()
self.assertIsInstance(record, types.NullRecord)

Expand Down Expand Up @@ -290,7 +290,7 @@ def test_tx_configuration(self):
"bar": types.CypherString("baz")}
self._session1 = self._driver.session("r")
tx = self._session1.begin_transaction(
tx_meta={k: v.value for k, v in metadata.items()}, timeout=3000
tx_meta=metadata, timeout=3000
)
result = tx.run("UNWIND [1,2,3,4] AS x RETURN x")
values = []
Expand Down
Loading

0 comments on commit 9e357ef

Please sign in to comment.