Skip to content

Commit ed6a8ab

Browse files
author
Zhen Li
committed
Merge pull request #63 from neo4j/1.0-doc-example
Updated doc example to use king-Arthur story
2 parents 4464036 + b997619 commit ed6a8ab

File tree

1 file changed

+137
-71
lines changed

1 file changed

+137
-71
lines changed

test/v1/examples.test.js

Lines changed: 137 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ describe('examples', function() {
3333

3434
beforeEach(function(done) {
3535
var neo4j = neo4jv1;
36-
// tag::construct-driver[]
37-
driverGlobal = neo4j.driver("bolt://localhost", neo4jv1.auth.basic("neo4j", "neo4j"));
36+
//tag::construct-driver[]
37+
driverGlobal = neo4j.driver("bolt://localhost", neo4j.auth.basic("neo4j", "neo4j"));
3838
//end::construct-driver[]
3939
sessionGlobal = driverGlobal.session();
4040

@@ -61,25 +61,28 @@ describe('examples', function() {
6161
// tag::minimal-example[]
6262
var driver = neo4j.driver("bolt://localhost", neo4j.auth.basic("neo4j", "neo4j"));
6363
var session = driver.session();
64-
session.run( "CREATE (neo:Person {name:'Neo', age:23})" );
6564
session
66-
.run( "MATCH (p:Person) WHERE p.name = 'Neo' RETURN p.age" )
65+
.run( "CREATE (a:Person {name:'Arthur', title:'King'})" )
66+
.then( function()
67+
{
68+
return session.run( "MATCH (a:Person) WHERE a.name = 'Arthur' RETURN a.name AS name, a.title AS title" )
69+
})
6770
.then( function( result ) {
68-
console.log( "Neo is " + result.records[0].get("p.age").toInt() + " years old." );
71+
console.log( result.records[0].get("title") + " " + result.records[0].get("name") );
6972
session.close();
7073
driver.close();
71-
});
74+
})
7275
// end::minimal-example[]
73-
setTimeout(function() {
74-
expect(out[0]).toBe("Neo is 23 years old.");
75-
done();
76-
}, 500);
76+
.then(function() {
77+
expect(out[0]).toBe("King Arthur");
78+
done();
79+
});
7780
});
7881

7982
it('should be able to configure session pool size', function (done) {
8083
var neo4j = neo4jv1;
8184
// tag::configuration[]
82-
var driver = neo4j.driver("bolt://localhost", neo4jv1.auth.basic("neo4j", "neo4j"), {connectionPoolSize: 50});
85+
var driver = neo4j.driver("bolt://localhost", neo4j.auth.basic("neo4j", "neo4j"), {connectionPoolSize: 50});
8386
//end::configuration[]
8487

8588
var s = driver.session();
@@ -89,123 +92,175 @@ describe('examples', function() {
8992
console.log(theOnesCreated);
9093
s.close();
9194
driver.close();
92-
});
93-
94-
setTimeout(function() {
95-
expect(out[0]).toBe(1);
96-
done();
97-
}, 500);
95+
})
96+
.then(function() {
97+
expect(out[0]).toBe(1);
98+
done();
99+
});
98100
});
99101

100102
it('should document a statement', function(done) {
101103
var session = sessionGlobal;
102-
var resultPromise =
103104
// tag::statement[]
104105
session
105-
.run( "CREATE (p:Person { name: {name} })", {name: "The One"} )
106+
.run( "CREATE (person:Person {name: {name}})", {name: "Arthur"} )
107+
// end::statement[]
106108
.then( function(result) {
107109
var theOnesCreated = result.summary.updateStatistics.nodesCreated();
108110
console.log("There were " + theOnesCreated + " the ones created.")
111+
})
112+
.then(function() {
113+
expect(out[0]).toBe("There were 1 the ones created.");
114+
done();
109115
});
110-
// end::statement[]
111-
112-
// Then
113-
resultPromise.then(function() {
114-
expect(out[0]).toBe("There were 1 the ones created.");
115-
done();
116-
});
117116
});
118117

119118
it('should document a statement without parameters', function(done) {
120119
var session = sessionGlobal;
121-
var resultPromise =
122120
// tag::statement-without-parameters[]
123121
session
124-
.run( "CREATE (p:Person { name: 'The One' })" )
125-
122+
.run( "CREATE (p:Person { name: 'Arthur' })" )
123+
// end::statement-without-parameters[]
126124
.then( function(result) {
127125
var theOnesCreated = result.summary.updateStatistics.nodesCreated();
128126
console.log("There were " + theOnesCreated + " the ones created.");
129127
});
130-
// end::statement-without-parameters[]
131128

132129
// Then
133-
resultPromise.then(function() {
130+
setTimeout(function() {
134131
expect(out[0]).toBe("There were 1 the ones created.");
135132
done();
136-
})
133+
}, 500)
137134
});
138135

139136
it('should be able to iterate results', function(done) {
140137
var session = sessionGlobal;
141-
// tag::retain-result-query[]
138+
session
139+
.run( "CREATE (weapon:Weapon { name: 'Sword in the stone' })" )
140+
.then(function() {
141+
// tag::result-traversal[]
142+
var searchTerm = "Sword";
142143
session
143-
.run( "MATCH (p:Person { name: {name} }) RETURN p.age", {name : "The One"} )
144+
.run( "MATCH (weapon:Weapon) WHERE weapon.name CONTAINS {term} RETURN weapon.name", {term : searchTerm} )
144145
.subscribe({
145146
onNext: function(record) {
146-
console.log(record);
147+
console.log("" + record.get("weapon.name"));
147148
},
148149
onCompleted: function() {
149-
// Completed!
150150
session.close();
151151
},
152152
onError: function(error) {
153153
console.log(error);
154154
}
155155
});
156-
// end::retain-result-query[]
156+
// end::result-traversal[]
157+
});
157158
// Then
158-
done();
159+
setTimeout(function() {
160+
expect(out[0]).toBe("Sword in the stone");
161+
done();
162+
}, 500);
159163
});
160164

161-
it('should be able to do nested queries', function(done) {
165+
it('should be able to access records', function(done) {
162166
var session = sessionGlobal;
163-
164-
session.run("CREATE (:Person {name:'The One'})").then(function() {
165-
// tag::result-cursor[]
167+
session
168+
.run( "CREATE (weapon:Weapon { name: 'Sword in the stone', owner: 'Arthur', material: 'Stone', size: 'Huge' })" )
169+
.then(function() {
170+
// tag::access-record[]
171+
var searchTerm = "Arthur";
166172
session
167-
.run("MATCH (p:Person { name: {name} }) RETURN id(p)", {name: "The One"})
168-
.then(function (result) {
169-
var id = result.records[0].get('id(p)');
170-
session.run( "MATCH (p) WHERE id(p) = {id} CREATE (p)-[:HAS_TRAIT]->(:Trait {type:'Immortal'})", {id: id })
171-
.then(function (neoRecord) {
172-
var immortalsCreated = neoRecord.summary.updateStatistics.nodesCreated();
173-
var relationshipCreated = neoRecord.summary.updateStatistics.relationshipsCreated();
174-
console.log("There were " + immortalsCreated + " immortal and " + relationshipCreated +
175-
" relationships created");
173+
.run( "MATCH (weapon:Weapon) WHERE weapon.owner CONTAINS {term} RETURN weapon.name, weapon.material, weapon.size", {term : searchTerm} )
174+
.subscribe({
175+
onNext: function(record) {
176+
var sword = [];
177+
record.forEach(function(value, key)
178+
{
179+
sword.push(key + ": " + value);
176180
});
181+
console.log(sword);
182+
},
183+
onCompleted: function() {
184+
session.close();
185+
},
186+
onError: function(error) {
187+
console.log(error);
188+
}
177189
});
178-
// tag::result-cursor[]
179-
});
190+
// end::access-record[]
191+
});
180192

181-
//await the result
193+
// Then
182194
setTimeout(function() {
183-
expect(out[0]).toBe("There were 1 immortal and 1 relationships created");
195+
expect(out[0].length).toBe(3);
184196
done();
185-
}, 500);
197+
}, 500)
186198
});
187199

188200
it('should be able to retain for later processing', function(done) {
189201
var session = sessionGlobal;
190202

191-
session.run("CREATE (:Person {name:'The One', age: 23})").then(function() {
192-
// tag::retain-result-process[]
203+
session
204+
.run("CREATE (knight:Person:Knight { name: 'Lancelot', castle: 'Camelot' })")
205+
.then(function() {
206+
// tag::retain-result[]
193207
session
194-
.run("MATCH (p:Person { name: {name} }) RETURN p.age", {name: "The One"})
208+
.run("MATCH (knight:Person:Knight) WHERE knight.castle = {castle} RETURN knight.name AS name", {castle: "Camelot"})
195209
.then(function (result) {
210+
var records = [];
196211
for (i = 0; i < result.records.length; i++) {
197-
result.records[i].forEach(function (value, key, record) {
198-
console.log("Value for key " + key + " has value " + value);
199-
});
212+
records.push(result.records[i]);
213+
}
214+
return records;
215+
})
216+
.then(function (records) {
217+
for(i = 0; i < records.length; i ++)
218+
{
219+
console.log(records[i].get("name") + " is a knight of Camelot");
200220
}
201-
202221
});
203-
// end::retain-result-process[]
222+
// end::retain-result[]
204223
});
205224

206225
//await the result
207226
setTimeout(function() {
208-
expect(out[0]).toBe("Value for key p.age has value 23");
227+
expect(out[0]).toBe("Lancelot is a knight of Camelot");
228+
done();
229+
}, 500);
230+
});
231+
232+
it('should be able to do nested queries', function(done) {
233+
var session = sessionGlobal;
234+
session
235+
.run( "CREATE (knight:Person:Knight { name: 'Lancelot', castle: 'Camelot' })" +
236+
"CREATE (king:Person { name: 'Arthur', title: 'King' })" )
237+
.then(function() {
238+
// tag::nested-statements[]
239+
session
240+
.run("MATCH (knight:Person:Knight) WHERE knight.castle = {castle} RETURN id(knight) AS knight_id", {"castle": "Camelot"})
241+
.subscribe({
242+
onNext: function(record) {
243+
session
244+
.run("MATCH (knight) WHERE id(knight) = {id} MATCH (king:Person) WHERE king.name = {king} CREATE (knight)-[:DEFENDS]->(king)",
245+
{"id": record.get("knight_id"), "king": "Arthur"});
246+
},
247+
onCompleted: function() {
248+
session
249+
.run("MATCH (:Knight)-[:DEFENDS]->() RETURN count(*)")
250+
.then(function (result) {
251+
console.log("Count is " + result.records[0].get(0).toInt());
252+
});
253+
},
254+
onError: function(error) {
255+
console.log(error);
256+
}
257+
});
258+
// tag::nested-statements[]
259+
});
260+
261+
//await the result
262+
setTimeout(function() {
263+
expect(out[0]).toBe("Count is 1");
209264
done();
210265
}, 500);
211266
});
@@ -226,10 +281,10 @@ describe('examples', function() {
226281
it('should be able to profile', function(done) {
227282
var session = sessionGlobal;
228283

229-
session.run("CREATE (:Person {name:'The One', age: 23})").then(function() {
284+
session.run("CREATE (:Person {name:'Arthur'})").then(function() {
230285
// tag::result-summary-query-profile[]
231286
session
232-
.run("PROFILE MATCH (p:Person { name: {name} }) RETURN id(p)", {name: "The One"})
287+
.run("PROFILE MATCH (p:Person {name: {name}}) RETURN id(p)", {name: "Arthur"})
233288
.then(function (result) {
234289
console.log(result.summary.profile);
235290
});
@@ -248,7 +303,7 @@ describe('examples', function() {
248303

249304
// tag::result-summary-notifications[]
250305
session
251-
.run("EXPLAIN MATCH (a), (b) RETURN a,b")
306+
.run("EXPLAIN MATCH (king), (queen) RETURN king, queen")
252307
.then(function (result) {
253308
var notifications = result.summary.notifications, i;
254309
for (i = 0; i < notifications.length; i++) {
@@ -268,7 +323,7 @@ describe('examples', function() {
268323

269324
// tag::transaction-commit[]
270325
var tx = session.beginTransaction();
271-
tx.run( "CREATE (p:Person { name: 'The One' })" );
326+
tx.run( "CREATE (:Person {name: 'Guinevere'})" );
272327
tx.commit();
273328
// end::transaction-commit[]
274329
});
@@ -278,7 +333,7 @@ describe('examples', function() {
278333

279334
// tag::transaction-rollback[]
280335
var tx = session.beginTransaction();
281-
tx.run( "CREATE (p:Person { name: 'The One' })" );
336+
tx.run( "CREATE (:Person {name: 'Merlin'})" );
282337
tx.rollback();
283338
// end::transaction-rollback[]
284339
});
@@ -308,7 +363,7 @@ describe('examples', function() {
308363
});
309364
// end::tls-trust-on-first-use[]
310365
driver.close();
311-
});
366+
});
312367

313368
it('should document how to configure a trusted signing certificate', function() {
314369
var neo4j = neo4jv1;
@@ -325,4 +380,15 @@ describe('examples', function() {
325380
driver.close();
326381
});
327382

383+
it('should document how to disable auth', function() {
384+
var neo4j = neo4jv1;
385+
// tag::connect-with-auth-disabled[]
386+
var driver = neo4j.driver("bolt://localhost", {
387+
// In NodeJS, encryption is on by default. In the web bundle, it is off.
388+
encrypted:true
389+
});
390+
// end::connect-with-auth-disabled[]
391+
driver.close();
392+
});
393+
328394
});

0 commit comments

Comments
 (0)