Skip to content

Commit 486d4ec

Browse files
authored
Merge pull request #510 from zhenlineo/4.0-driver-doc
Adding missing examples for driver docs
2 parents e959d0f + eef5a73 commit 486d4ec

File tree

1 file changed

+201
-54
lines changed

1 file changed

+201
-54
lines changed

test/examples.test.js

Lines changed: 201 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import neo4j from '../src'
2121
import sharedNeo4j from './internal/shared-neo4j'
2222
import { ServerVersion, VERSION_4_0_0 } from '../src/internal/server-version'
23+
import { map, materialize, toArray } from 'rxjs/operators'
24+
import { Notification } from 'rxjs'
2325

2426
/**
2527
* 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', () => {
6567
await driverGlobal.close()
6668
})
6769

68-
it('autocommit transaction example', async () => {
70+
// tag::autocommit-transaction[]
71+
// Not supported
72+
// end::autocommit-transaction[]
73+
it('async autocommit transaction example', async () => {
6974
const driver = driverGlobal
7075

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 () {
7388
const session = driver.session()
7489
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
78103
} finally {
79104
await session.close()
80105
}
81106
}
82107

83-
// end::autocommit-transaction[]
108+
// end::async-autocommit-transaction[]
84109

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+
}
86119

120+
const driver = driverGlobal
87121
const session = driver.session()
88122
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+
})
96127
} finally {
97128
await session.close()
98129
}
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+
])
99153
})
100154

101155
it('basic auth example', async () => {
@@ -379,46 +433,87 @@ describe('#integration examples', () => {
379433
expect(await consoleLoggedMsg).toContain('Matched created node with id')
380434
})
381435

382-
it('result consume example', async () => {
436+
// tag::result-consume[]
437+
// Not supported
438+
// end::result-consume[]
439+
it('async result consume example', async () => {
383440
const console = consoleOverride
384441
const consoleLoggedMsg = consoleOverridePromise
385442
const driver = driverGlobal
386443
const names = { nameA: 'Alice', nameB: 'Bob' }
387-
const tmpSession = driver.session()
444+
const tempSession = driver.session()
388445
try {
389-
await tmpSession.run(
446+
await tempSession.run(
390447
'CREATE (a:Person {name: $nameA}), (b:Person {name: $nameB})',
391448
names
392449
)
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[]
415450
} finally {
416-
await tmpSession.close()
451+
await tempSession.close()
417452
}
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[]
418473

419474
expect(await consoleLoggedMsg).toEqual('Names: Alice, Bob')
420475
})
421476

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[]
422517
it('result retain example', async () => {
423518
const console = consoleOverride
424519
const consoleLoggedMsg = consoleOverridePromise
@@ -433,7 +528,7 @@ describe('#integration examples', () => {
433528
personNames
434529
)
435530

436-
// tag::result-retain[]
531+
// tag::async-result-retain[]
437532
const session = driver.session()
438533
try {
439534
const result = await session.readTransaction(tx =>
@@ -458,7 +553,7 @@ describe('#integration examples', () => {
458553
} finally {
459554
await session.close()
460555
}
461-
// end::result-retain[]
556+
// end::async-result-retain[]
462557
} finally {
463558
await tmpSession.close()
464559
}
@@ -519,28 +614,80 @@ describe('#integration examples', () => {
519614
expect(await consoleLoggedMsg).toBe('Person created, session closed')
520615
})
521616

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 () => {
525621
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+
}
527631

528-
// tag::transaction-function[]
632+
// tag::async-transaction-function[]
529633
const session = driver.session()
634+
const titles = []
530635
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 })
533638
)
534639

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)
537644
}
538645
} finally {
539646
await session.close()
540647
}
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+
}
542659

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+
])
544691
})
545692

546693
it('pass bookmarks example', done => {

0 commit comments

Comments
 (0)