2424
2525from 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
2929from neo4j .exceptions import CypherSyntaxError
3030
3131from 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+
474475class 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 )
0 commit comments