@@ -28,13 +28,13 @@ describe('session', () => {
28
28
29
29
let driver ;
30
30
let session ;
31
- let server ;
31
+ let serverMetadata ;
32
32
let originalTimeout ;
33
33
34
34
beforeEach ( done => {
35
35
driver = neo4j . driver ( 'bolt://localhost' , neo4j . auth . basic ( 'neo4j' , 'neo4j' ) ) ;
36
36
driver . onCompleted = meta => {
37
- server = meta [ 'server' ] ;
37
+ serverMetadata = meta [ 'server' ] ;
38
38
} ;
39
39
session = driver . session ( ) ;
40
40
originalTimeout = jasmine . DEFAULT_TIMEOUT_INTERVAL ;
@@ -228,11 +228,7 @@ describe('session', () => {
228
228
} ) ;
229
229
230
230
it ( 'should expose server info on successful query' , done => {
231
- //lazy way of checking the version number
232
- //if server has been set we know it is at least
233
- //3.1 (todo actually parse the version string)
234
- if ( ! server ) {
235
- done ( ) ;
231
+ if ( ! serverIs31OrLater ( done ) ) {
236
232
return ;
237
233
}
238
234
@@ -251,14 +247,10 @@ describe('session', () => {
251
247
} ) ;
252
248
253
249
it ( 'should expose execution time information when using 3.1 and onwards' , done => {
254
-
255
- //lazy way of checking the version number
256
- //if server has been set we know it is at least
257
- //3.1 (todo actually parse the version string)
258
- if ( ! server ) {
259
- done ( ) ;
250
+ if ( ! serverIs31OrLater ( done ) ) {
260
251
return ;
261
252
}
253
+
262
254
// Given
263
255
const statement = 'UNWIND range(1,10000) AS n RETURN n AS number' ;
264
256
// When & Then
@@ -632,6 +624,184 @@ describe('session', () => {
632
624
} ) ;
633
625
} ) ;
634
626
627
+ it ( 'should commit read transaction' , done => {
628
+ if ( ! serverIs31OrLater ( done ) ) {
629
+ return ;
630
+ }
631
+
632
+ const bookmarkBefore = session . lastBookmark ( ) ;
633
+ const resultPromise = session . readTransaction ( tx => tx . run ( 'RETURN 42 AS answer' ) ) ;
634
+
635
+ resultPromise . then ( result => {
636
+ expect ( result . records . length ) . toEqual ( 1 ) ;
637
+ expect ( result . records [ 0 ] . get ( 'answer' ) . toNumber ( ) ) . toEqual ( 42 ) ;
638
+
639
+ const bookmarkAfter = session . lastBookmark ( ) ;
640
+ verifyBookmark ( bookmarkAfter ) ;
641
+ expect ( bookmarkAfter ) . not . toEqual ( bookmarkBefore ) ;
642
+
643
+ done ( ) ;
644
+ } ) ;
645
+ } ) ;
646
+
647
+ it ( 'should commit write transaction' , done => {
648
+ if ( ! serverIs31OrLater ( done ) ) {
649
+ return ;
650
+ }
651
+
652
+ const bookmarkBefore = session . lastBookmark ( ) ;
653
+ const resultPromise = session . writeTransaction ( tx => tx . run ( 'CREATE (n:Node {id: 42}) RETURN n.id AS answer' ) ) ;
654
+
655
+ resultPromise . then ( result => {
656
+ expect ( result . records . length ) . toEqual ( 1 ) ;
657
+ expect ( result . records [ 0 ] . get ( 'answer' ) . toNumber ( ) ) . toEqual ( 42 ) ;
658
+ expect ( result . summary . counters . nodesCreated ( ) ) . toEqual ( 1 ) ;
659
+
660
+ const bookmarkAfter = session . lastBookmark ( ) ;
661
+ verifyBookmark ( bookmarkAfter ) ;
662
+ expect ( bookmarkAfter ) . not . toEqual ( bookmarkBefore ) ;
663
+
664
+ countNodes ( 'Node' , 'id' , 42 ) . then ( count => {
665
+ expect ( count ) . toEqual ( 1 ) ;
666
+ done ( ) ;
667
+ } ) ;
668
+ } ) ;
669
+ } ) ;
670
+
671
+ it ( 'should not commit already committed read transaction' , done => {
672
+ if ( ! serverIs31OrLater ( done ) ) {
673
+ return ;
674
+ }
675
+
676
+ const resultPromise = session . readTransaction ( tx => {
677
+ return new Promise ( ( resolve , reject ) => {
678
+ tx . run ( 'RETURN 42 AS answer' ) . then ( result => {
679
+ tx . commit ( ) . then ( ( ) => {
680
+ resolve ( { result : result , bookmark : session . lastBookmark ( ) } ) ;
681
+ } ) . catch ( error => reject ( error ) ) ;
682
+ } ) . catch ( error => reject ( error ) ) ;
683
+ } ) ;
684
+ } ) ;
685
+
686
+ resultPromise . then ( outcome => {
687
+ const bookmark = outcome . bookmark ;
688
+ const result = outcome . result ;
689
+
690
+ verifyBookmark ( bookmark ) ;
691
+ expect ( session . lastBookmark ( ) ) . toEqual ( bookmark ) ; // expect bookmark to not change
692
+
693
+ expect ( result . records . length ) . toEqual ( 1 ) ;
694
+ expect ( result . records [ 0 ] . get ( 'answer' ) . toNumber ( ) ) . toEqual ( 42 ) ;
695
+
696
+ done ( ) ;
697
+ } ) ;
698
+ } ) ;
699
+
700
+ it ( 'should not commit already committed write transaction' , done => {
701
+ if ( ! serverIs31OrLater ( done ) ) {
702
+ return ;
703
+ }
704
+
705
+ const resultPromise = session . readTransaction ( tx => {
706
+ return new Promise ( ( resolve , reject ) => {
707
+ tx . run ( 'CREATE (n:Node {id: 42}) RETURN n.id AS answer' ) . then ( result => {
708
+ tx . commit ( ) . then ( ( ) => {
709
+ resolve ( { result : result , bookmark : session . lastBookmark ( ) } ) ;
710
+ } ) . catch ( error => reject ( error ) ) ;
711
+ } ) . catch ( error => reject ( error ) ) ;
712
+ } ) ;
713
+ } ) ;
714
+
715
+ resultPromise . then ( outcome => {
716
+ const bookmark = outcome . bookmark ;
717
+ const result = outcome . result ;
718
+
719
+ verifyBookmark ( bookmark ) ;
720
+ expect ( session . lastBookmark ( ) ) . toEqual ( bookmark ) ; // expect bookmark to not change
721
+
722
+ expect ( result . records . length ) . toEqual ( 1 ) ;
723
+ expect ( result . records [ 0 ] . get ( 'answer' ) . toNumber ( ) ) . toEqual ( 42 ) ;
724
+ expect ( result . summary . counters . nodesCreated ( ) ) . toEqual ( 1 ) ;
725
+
726
+ countNodes ( 'Node' , 'id' , 42 ) . then ( count => {
727
+ expect ( count ) . toEqual ( 1 ) ;
728
+ done ( ) ;
729
+ } ) ;
730
+ } ) ;
731
+ } ) ;
732
+
733
+ it ( 'should not commit rolled back read transaction' , done => {
734
+ if ( ! serverIs31OrLater ( done ) ) {
735
+ return ;
736
+ }
737
+
738
+ const bookmarkBefore = session . lastBookmark ( ) ;
739
+ const resultPromise = session . readTransaction ( tx => {
740
+ return new Promise ( ( resolve , reject ) => {
741
+ tx . run ( 'RETURN 42 AS answer' ) . then ( result => {
742
+ tx . rollback ( ) . then ( ( ) => {
743
+ resolve ( result ) ;
744
+ } ) . catch ( error => reject ( error ) ) ;
745
+ } ) . catch ( error => reject ( error ) ) ;
746
+ } ) ;
747
+ } ) ;
748
+
749
+ resultPromise . then ( result => {
750
+ expect ( result . records . length ) . toEqual ( 1 ) ;
751
+ expect ( result . records [ 0 ] . get ( 'answer' ) . toNumber ( ) ) . toEqual ( 42 ) ;
752
+ expect ( session . lastBookmark ( ) ) . toBe ( bookmarkBefore ) ; // expect bookmark to not change
753
+
754
+ done ( ) ;
755
+ } ) ;
756
+ } ) ;
757
+
758
+ it ( 'should not commit rolled back write transaction' , done => {
759
+ if ( ! serverIs31OrLater ( done ) ) {
760
+ return ;
761
+ }
762
+
763
+ const bookmarkBefore = session . lastBookmark ( ) ;
764
+ const resultPromise = session . readTransaction ( tx => {
765
+ return new Promise ( ( resolve , reject ) => {
766
+ tx . run ( 'CREATE (n:Node {id: 42}) RETURN n.id AS answer' ) . then ( result => {
767
+ tx . rollback ( ) . then ( ( ) => {
768
+ resolve ( result ) ;
769
+ } ) . catch ( error => reject ( error ) ) ;
770
+ } ) . catch ( error => reject ( error ) ) ;
771
+ } ) ;
772
+ } ) ;
773
+
774
+ resultPromise . then ( result => {
775
+ expect ( result . records . length ) . toEqual ( 1 ) ;
776
+ expect ( result . records [ 0 ] . get ( 'answer' ) . toNumber ( ) ) . toEqual ( 42 ) ;
777
+ expect ( result . summary . counters . nodesCreated ( ) ) . toEqual ( 1 ) ;
778
+ expect ( session . lastBookmark ( ) ) . toBe ( bookmarkBefore ) ; // expect bookmark to not change
779
+
780
+ countNodes ( 'Node' , 'id' , 42 ) . then ( count => {
781
+ expect ( count ) . toEqual ( 0 ) ;
782
+ done ( ) ;
783
+ } ) ;
784
+ } ) ;
785
+ } ) ;
786
+
787
+ function serverIs31OrLater ( done ) {
788
+ // lazy way of checking the version number
789
+ // if server has been set we know it is at least 3.1
790
+ if ( ! serverMetadata ) {
791
+ done ( ) ;
792
+ return false ;
793
+ }
794
+ return true ;
795
+ }
796
+
797
+ function countNodes ( label , propertyKey , propertyValue ) {
798
+ return new Promise ( ( resolve , reject ) => {
799
+ session . run ( `MATCH (n: ${ label } {${ propertyKey } : ${ propertyValue } }) RETURN count(n) AS count` ) . then ( result => {
800
+ resolve ( result . records [ 0 ] . get ( 'count' ) . toNumber ( ) ) ;
801
+ } ) . catch ( error => reject ( error ) ) ;
802
+ } ) ;
803
+ }
804
+
635
805
function withQueryInTmpSession ( driver , callback ) {
636
806
const tmpSession = driver . session ( ) ;
637
807
return tmpSession . run ( 'RETURN 1' ) . then ( ( ) => {
@@ -653,4 +823,9 @@ describe('session', () => {
653
823
const idleConnections = connectionPool . _pools [ address ] ;
654
824
return idleConnections . length ;
655
825
}
826
+
827
+ function verifyBookmark ( bookmark ) {
828
+ expect ( bookmark ) . toBeDefined ( ) ;
829
+ expect ( bookmark ) . not . toBeNull ( ) ;
830
+ }
656
831
} ) ;
0 commit comments