Skip to content

Commit aa1a57e

Browse files
committed
Fixed connector
1 parent 8bac8d5 commit aa1a57e

File tree

6 files changed

+88
-7
lines changed

6 files changed

+88
-7
lines changed

neo4j/__init__.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"SessionError",
4141
"SessionExpired",
4242
"TransactionError",
43+
"unit_of_work",
4344
"basic_auth",
4445
"custom_auth",
4546
"kerberos_auth",
@@ -574,7 +575,7 @@ def begin_transaction(self, bookmark=None):
574575
self._open_transaction()
575576
return self._transaction
576577

577-
def _open_transaction(self, access_mode=None):
578+
def _open_transaction(self, access_mode=None, metadata=None):
578579
self._transaction = Transaction(self, on_close=self._close_transaction)
579580
self._connect(access_mode)
580581
self._connection.begin(self._bookmarks_in)
@@ -619,6 +620,9 @@ def _run_transaction(self, access_mode, unit_of_work, *args, **kwargs):
619620

620621
if not callable(unit_of_work):
621622
raise TypeError("Unit of work is not callable")
623+
624+
metadata = getattr(unit_of_work, "metadata", None)
625+
print(metadata) # TODO
622626
retry_delay = retry_delay_generator(INITIAL_RETRY_DELAY,
623627
RETRY_DELAY_MULTIPLIER,
624628
RETRY_DELAY_JITTER_FACTOR)
@@ -1341,6 +1345,21 @@ def __init__(self, transaction, *args, **kwargs):
13411345
self.transaction = transaction
13421346

13431347

1348+
def unit_of_work(**metadata):
1349+
""" Decorator for transaction functions.
1350+
"""
1351+
1352+
def wrapper(f):
1353+
1354+
def wrapped(*args, **kwargs):
1355+
return f(*args, **kwargs)
1356+
1357+
wrapped.metadata = metadata
1358+
return wrapped
1359+
1360+
return wrapper
1361+
1362+
13441363
def basic_auth(user, password, realm=None):
13451364
""" Generate a basic auth token for a given user and password.
13461365

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
neobolt==1.7.0b2
1+
neobolt==1.7.0rc2
22
neotime

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from neo4j.meta import package, version
3131

3232
install_requires = [
33-
"neobolt==1.7.0b2",
33+
"neobolt==1.7.0rc2",
3434
"neotime",
3535
]
3636
classifiers = [

test/integration/test_session.py

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424

2525
from neo4j import \
2626
READ_ACCESS, WRITE_ACCESS, \
27-
CypherError, SessionError, TransactionError
28-
from neo4j.v1.types.graph import Node, Relationship, Path
27+
CypherError, SessionError, TransactionError, unit_of_work
28+
from neo4j.types.graph import Node, Relationship, Path
2929
from neo4j.exceptions import CypherSyntaxError
3030

3131
from test.integration.tools import DirectIntegrationTestCase
@@ -471,6 +471,7 @@ def test_large_values(self):
471471
session.run("RETURN '{}'".format("A" * 2 ** 20))
472472
session.close()
473473

474+
474475
class TransactionCommittedTestCase(DirectIntegrationTestCase):
475476

476477
def setUp(self):
@@ -483,3 +484,62 @@ def setUp(self):
483484
def test_errors_on_run(self):
484485
with self.assertRaises(TransactionError):
485486
self.transaction.run("RETURN 1")
487+
488+
489+
class TransactionFunctionTestCase(DirectIntegrationTestCase):
490+
491+
def test_simple_read(self):
492+
493+
def work(tx):
494+
return tx.run("RETURN 1").single().value()
495+
496+
with self.driver.session() as session:
497+
value = session.read_transaction(work)
498+
self.assertEqual(value, 1)
499+
500+
def test_read_with_arg(self):
501+
502+
def work(tx, x):
503+
return tx.run("RETURN $x", x=x).single().value()
504+
505+
with self.driver.session() as session:
506+
value = session.read_transaction(work, x=1)
507+
self.assertEqual(value, 1)
508+
509+
def test_read_with_arg_and_metadata(self):
510+
511+
@unit_of_work(timeout=25, metadata={"foo": "bar"})
512+
def work(tx, x):
513+
return tx.run("RETURN $x", x=x).single().value()
514+
515+
with self.driver.session() as session:
516+
value = session.read_transaction(work, x=1)
517+
self.assertEqual(value, 1)
518+
519+
def test_simple_write(self):
520+
521+
def work(tx):
522+
return tx.run("CREATE (a {x: 1}) RETURN a.x").single().value()
523+
524+
with self.driver.session() as session:
525+
value = session.write_transaction(work)
526+
self.assertEqual(value, 1)
527+
528+
def test_write_with_arg(self):
529+
530+
def work(tx, x):
531+
return tx.run("CREATE (a {x: $x}) RETURN a.x", x=x).single().value()
532+
533+
with self.driver.session() as session:
534+
value = session.write_transaction(work, x=1)
535+
self.assertEqual(value, 1)
536+
537+
def test_write_with_arg_and_metadata(self):
538+
539+
@unit_of_work(timeout=25, metadata={"foo": "bar"})
540+
def work(tx, x):
541+
return tx.run("CREATE (a {x: $x}) RETURN a.x", x=x).single().value()
542+
543+
with self.driver.session() as session:
544+
value = session.write_transaction(work, x=1)
545+
self.assertEqual(value, 1)

test/integration/tools.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
except ImportError:
3131
from urllib import urlretrieve
3232

33+
from boltkit.config import update as update_config
3334
from boltkit.controller import _install, WindowsController, UnixController
3435

3536
from neo4j import GraphDatabase
@@ -156,10 +157,11 @@ def _unpack(cls, package):
156157
def _start_server(cls, home):
157158
controller_class = WindowsController if platform.system() == "Windows" else UnixController
158159
cls.controller = controller_class(home, 1)
160+
update_config(cls.controller.home, {"dbms.connectors.default_listen_address": "::"})
159161
if NEO4J_USER is None:
160162
cls.controller.create_user(cls.user, cls.password)
161163
cls.controller.set_user_role(cls.user, "admin")
162-
cls.controller.start()
164+
cls.controller.start(timeout=90)
163165

164166
@classmethod
165167
def _stop_server(cls):

test/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
boltkit>=1.0.34
1+
boltkit>=1.0.39
22
coverage
33
mock
44
pytest

0 commit comments

Comments
 (0)