20
20
import neo4j from '../src'
21
21
import sharedNeo4j from './internal/shared-neo4j'
22
22
import { ServerVersion , VERSION_4_0_0 } from '../src/internal/server-version'
23
+ import { map , materialize , toArray } from 'rxjs/operators'
24
+ import { Notification } from 'rxjs'
23
25
24
26
/**
25
27
* The tests below are examples that get pulled into the Driver Manual using the tags inside the tests.
@@ -65,37 +67,89 @@ describe('#integration examples', () => {
65
67
await driverGlobal . close ( )
66
68
} )
67
69
68
- it ( 'autocommit transaction example' , async ( ) => {
70
+ // tag::autocommit-transaction[]
71
+ // Not supported
72
+ // end::autocommit-transaction[]
73
+ it ( 'async autocommit transaction example' , async ( ) => {
69
74
const driver = driverGlobal
70
75
71
- // tag::autocommit-transaction[]
72
- async function addPerson ( name ) {
76
+ const session = driver . session ( )
77
+ try {
78
+ await session . run ( 'CREATE (p:Product) SET p.id = $id, p.title = $title' , {
79
+ id : 0 ,
80
+ title : 'Product-0'
81
+ } )
82
+ } finally {
83
+ await session . close ( )
84
+ }
85
+
86
+ // tag::async-autocommit-transaction[]
87
+ async function readProductTitles ( ) {
73
88
const session = driver . session ( )
74
89
try {
75
- return await session . run ( 'CREATE (a:Person {name: $name})' , {
76
- name : name
77
- } )
90
+ const result = await session . run (
91
+ 'MATCH (p:Product) WHERE p.id = $id RETURN p.title' ,
92
+ {
93
+ id : 0
94
+ }
95
+ )
96
+ const records = result . records
97
+ const titles = [ ]
98
+ for ( let i = 0 ; i < records . length ; i ++ ) {
99
+ const title = records [ i ] . get ( 0 )
100
+ titles . push ( title )
101
+ }
102
+ return titles
78
103
} finally {
79
104
await session . close ( )
80
105
}
81
106
}
82
107
83
- // end::autocommit-transaction[]
108
+ // end::async- autocommit-transaction[]
84
109
85
- await addPerson ( 'Alice' )
110
+ const titles = await readProductTitles ( )
111
+ expect ( titles . length ) . toEqual ( 1 )
112
+ expect ( titles [ 0 ] ) . toEqual ( 'Product-0' )
113
+ } )
114
+
115
+ it ( 'rx autocommit transaction example' , async ( ) => {
116
+ if ( version . compareTo ( VERSION_4_0_0 ) < 0 ) {
117
+ return
118
+ }
86
119
120
+ const driver = driverGlobal
87
121
const session = driver . session ( )
88
122
try {
89
- const result = await session . run (
90
- 'MATCH (a:Person {name: $name}) RETURN count(a) AS result' ,
91
- {
92
- name : 'Alice'
93
- }
94
- )
95
- expect ( result . records [ 0 ] . get ( 'result' ) . toInt ( ) ) . toEqual ( 1 )
123
+ await session . run ( 'CREATE (p:Product) SET p.id = $id, p.title = $title' , {
124
+ id : 0 ,
125
+ title : 'Product-0'
126
+ } )
96
127
} finally {
97
128
await session . close ( )
98
129
}
130
+
131
+ // tag::rx-autocommit-transaction[]
132
+ function readProductTitles ( ) {
133
+ const session = driver . rxSession ( )
134
+ return session
135
+ . run ( 'MATCH (p:Product) WHERE p.id = $id RETURN p.title' , {
136
+ id : 0
137
+ } )
138
+ . records ( )
139
+ . pipe (
140
+ map ( r => r . get ( 0 ) ) ,
141
+ materialize ( ) ,
142
+ toArray ( )
143
+ )
144
+ }
145
+
146
+ // end::rx-autocommit-transaction[]
147
+ const result = await readProductTitles ( ) . toPromise ( )
148
+
149
+ expect ( result ) . toEqual ( [
150
+ Notification . createNext ( 'Product-0' ) ,
151
+ Notification . createComplete ( )
152
+ ] )
99
153
} )
100
154
101
155
it ( 'basic auth example' , async ( ) => {
@@ -379,46 +433,87 @@ describe('#integration examples', () => {
379
433
expect ( await consoleLoggedMsg ) . toContain ( 'Matched created node with id' )
380
434
} )
381
435
382
- it ( 'result consume example' , async ( ) => {
436
+ // tag::result-consume[]
437
+ // Not supported
438
+ // end::result-consume[]
439
+ it ( 'async result consume example' , async ( ) => {
383
440
const console = consoleOverride
384
441
const consoleLoggedMsg = consoleOverridePromise
385
442
const driver = driverGlobal
386
443
const names = { nameA : 'Alice' , nameB : 'Bob' }
387
- const tmpSession = driver . session ( )
444
+ const tempSession = driver . session ( )
388
445
try {
389
- await tmpSession . run (
446
+ await tempSession . run (
390
447
'CREATE (a:Person {name: $nameA}), (b:Person {name: $nameB})' ,
391
448
names
392
449
)
393
- // tag::result-consume[]
394
- const session = driver . session ( )
395
- const result = session . run (
396
- 'MATCH (a:Person) RETURN a.name ORDER BY a.name'
397
- )
398
- const collectedNames = [ ]
399
-
400
- result . subscribe ( {
401
- onNext : record => {
402
- const name = record . get ( 0 )
403
- collectedNames . push ( name )
404
- } ,
405
- onCompleted : ( ) => {
406
- session . close ( ) . then ( ( ) => {
407
- console . log ( 'Names: ' + collectedNames . join ( ', ' ) )
408
- } )
409
- } ,
410
- onError : error => {
411
- console . log ( error )
412
- }
413
- } )
414
- // end::result-consume[]
415
450
} finally {
416
- await tmpSession . close ( )
451
+ await tempSession . close ( )
417
452
}
453
+ // tag::async-result-consume[]
454
+ const session = driver . session ( )
455
+ const result = session . run ( 'MATCH (a:Person) RETURN a.name ORDER BY a.name' )
456
+ const collectedNames = [ ]
457
+
458
+ result . subscribe ( {
459
+ onNext : record => {
460
+ const name = record . get ( 0 )
461
+ collectedNames . push ( name )
462
+ } ,
463
+ onCompleted : ( ) => {
464
+ session . close ( ) . then ( ( ) => {
465
+ console . log ( 'Names: ' + collectedNames . join ( ', ' ) )
466
+ } )
467
+ } ,
468
+ onError : error => {
469
+ console . log ( error )
470
+ }
471
+ } )
472
+ // end::async-result-consume[]
418
473
419
474
expect ( await consoleLoggedMsg ) . toEqual ( 'Names: Alice, Bob' )
420
475
} )
421
476
477
+ it ( 'rx result consume example' , async ( ) => {
478
+ if ( version . compareTo ( VERSION_4_0_0 ) < 0 ) {
479
+ return
480
+ }
481
+
482
+ const driver = driverGlobal
483
+ const names = { nameA : 'Alice' , nameB : 'Bob' }
484
+ const tempSession = driver . session ( )
485
+ try {
486
+ await tempSession . run (
487
+ 'CREATE (a:Person {name: $nameA}), (b:Person {name: $nameB})' ,
488
+ names
489
+ )
490
+ } finally {
491
+ await tempSession . close ( )
492
+ }
493
+
494
+ // tag::rx-result-consume[]
495
+ const session = driver . rxSession ( )
496
+ const result = session
497
+ . run ( 'MATCH (a:Person) RETURN a.name ORDER BY a.name' )
498
+ . records ( )
499
+ . pipe (
500
+ map ( r => r . get ( 0 ) ) ,
501
+ materialize ( ) ,
502
+ toArray ( )
503
+ )
504
+ // end::rx-result-consume[]
505
+
506
+ const people = await result . toPromise ( )
507
+ expect ( people ) . toEqual ( [
508
+ Notification . createNext ( 'Alice' ) ,
509
+ Notification . createNext ( 'Bob' ) ,
510
+ Notification . createComplete ( )
511
+ ] )
512
+ } )
513
+
514
+ // tag::result-retain[]
515
+ // Not supported
516
+ // end::result-retain[]
422
517
it ( 'result retain example' , async ( ) => {
423
518
const console = consoleOverride
424
519
const consoleLoggedMsg = consoleOverridePromise
@@ -433,7 +528,7 @@ describe('#integration examples', () => {
433
528
personNames
434
529
)
435
530
436
- // tag::result-retain[]
531
+ // tag::async- result-retain[]
437
532
const session = driver . session ( )
438
533
try {
439
534
const result = await session . readTransaction ( tx =>
@@ -458,7 +553,7 @@ describe('#integration examples', () => {
458
553
} finally {
459
554
await session . close ( )
460
555
}
461
- // end::result-retain[]
556
+ // end::async- result-retain[]
462
557
} finally {
463
558
await tmpSession . close ( )
464
559
}
@@ -519,28 +614,80 @@ describe('#integration examples', () => {
519
614
expect ( await consoleLoggedMsg ) . toBe ( 'Person created, session closed' )
520
615
} )
521
616
522
- it ( 'transaction function example' , async ( ) => {
523
- const console = consoleOverride
524
- const consoleLoggedMsg = consoleOverridePromise
617
+ // tag::transaction-function[]
618
+ // Not supported
619
+ // end::transaction-function[]
620
+ it ( 'async transaction function example' , async ( ) => {
525
621
const driver = driverGlobal
526
- const personName = 'Alice'
622
+ const tempSession = driver . session ( )
623
+ try {
624
+ await tempSession . run (
625
+ "UNWIND ['Infinity Gauntlet', 'Mjölnir'] AS item " +
626
+ 'CREATE (:Product {id: 0, title: item})'
627
+ )
628
+ } finally {
629
+ await tempSession . close ( )
630
+ }
527
631
528
- // tag::transaction-function[]
632
+ // tag::async- transaction-function[]
529
633
const session = driver . session ( )
634
+ const titles = [ ]
530
635
try {
531
- const result = await session . writeTransaction ( tx =>
532
- tx . run ( 'CREATE (a:Person {name: $name}) ' , { name : personName } )
636
+ const result = await session . readTransaction ( tx =>
637
+ tx . run ( 'MATCH (p:Product) WHERE p.id = $id RETURN p.title ' , { id : 0 } )
533
638
)
534
639
535
- if ( result ) {
536
- console . log ( 'Person created' )
640
+ const records = result . records
641
+ for ( let i = 0 ; i < records . length ; i ++ ) {
642
+ const title = records [ i ] . get ( 0 )
643
+ titles . push ( title )
537
644
}
538
645
} finally {
539
646
await session . close ( )
540
647
}
541
- // end::transaction-function[]
648
+ // end::async-transaction-function[]
649
+
650
+ expect ( titles . length ) . toEqual ( 2 )
651
+ expect ( titles [ 0 ] ) . toEqual ( 'Infinity Gauntlet' )
652
+ expect ( titles [ 1 ] ) . toEqual ( 'Mjölnir' )
653
+ } )
654
+
655
+ it ( 'rx transaction function example' , async ( ) => {
656
+ if ( version . compareTo ( VERSION_4_0_0 ) < 0 ) {
657
+ return
658
+ }
542
659
543
- expect ( await consoleLoggedMsg ) . toBe ( 'Person created' )
660
+ const driver = driverGlobal
661
+ const tempSession = driver . session ( )
662
+ try {
663
+ await tempSession . run (
664
+ "UNWIND ['Infinity Gauntlet', 'Mjölnir'] AS item " +
665
+ 'CREATE (:Product {id: 0, title: item})'
666
+ )
667
+ } finally {
668
+ await tempSession . close ( )
669
+ }
670
+
671
+ // tag::rx-transaction-function[]
672
+ const session = driver . rxSession ( )
673
+ const result = session . readTransaction ( tx =>
674
+ tx
675
+ . run ( 'MATCH (p:Product) WHERE p.id = $id RETURN p.title' , { id : 0 } )
676
+ . records ( )
677
+ . pipe (
678
+ map ( r => r . get ( 0 ) ) ,
679
+ materialize ( ) ,
680
+ toArray ( )
681
+ )
682
+ )
683
+ // end::rx-transaction-function[]
684
+
685
+ const people = await result . toPromise ( )
686
+ expect ( people ) . toEqual ( [
687
+ Notification . createNext ( 'Infinity Gauntlet' ) ,
688
+ Notification . createNext ( 'Mjölnir' ) ,
689
+ Notification . createComplete ( )
690
+ ] )
544
691
} )
545
692
546
693
it ( 'pass bookmarks example' , done => {
0 commit comments