Skip to content

Commit e3538cc

Browse files
author
Zhen Li
committed
Merge pull request #103 from jakewins/1.0-more-examples
Expand the number of documented examples.
2 parents 5ff14c2 + c57af0c commit e3538cc

File tree

1 file changed

+179
-0
lines changed

1 file changed

+179
-0
lines changed

driver/src/test/java/org/neo4j/driver/v1/integration/ExamplesIT.java

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,36 @@
2222
import org.junit.Test;
2323

2424
import java.io.File;
25+
import java.util.List;
2526

27+
import org.neo4j.driver.internal.spi.Logger;
28+
import org.neo4j.driver.internal.spi.Logging;
2629
import org.neo4j.driver.v1.Config;
2730
import org.neo4j.driver.v1.Driver;
2831
import org.neo4j.driver.v1.GraphDatabase;
32+
import org.neo4j.driver.v1.Notification;
33+
import org.neo4j.driver.v1.Pair;
34+
import org.neo4j.driver.v1.Record;
2935
import org.neo4j.driver.v1.ResultCursor;
36+
import org.neo4j.driver.v1.ResultSummary;
3037
import org.neo4j.driver.v1.Session;
3138
import org.neo4j.driver.v1.Transaction;
39+
import org.neo4j.driver.v1.Value;
3240
import org.neo4j.driver.v1.Values;
3341
import org.neo4j.driver.v1.util.StdIOCapture;
3442
import org.neo4j.driver.v1.util.TestNeo4j;
3543

3644
import static java.util.Arrays.asList;
3745
import static org.hamcrest.Matchers.equalTo;
46+
import static org.junit.Assert.assertNotNull;
3847
import static org.junit.Assert.assertThat;
3948
import static org.neo4j.driver.v1.Config.TlsAuthenticationConfig.usingKnownCerts;
4049
import static org.neo4j.driver.v1.Config.TlsAuthenticationConfig.usingTrustedCert;
4150

51+
// NOTE: Be careful about auto-formatting here. The below segment should contain GraphDatabase, Driver and Session imports
52+
// tag::include-driver[]
53+
// end::include-driver[]
54+
4255
/**
4356
* The tests below are examples that get pulled into the Driver Manual using the tags inside the tests.
4457
*
@@ -77,6 +90,31 @@ public void minimumViableSnippet() throws Throwable
7790
assertThat( stdIO.stdout(), equalTo( asList("Neo is 23 years old.") ) );
7891
}
7992

93+
@Test
94+
public void constructDriver() throws Throwable
95+
{
96+
// tag::construct-driver[]
97+
Driver driver = GraphDatabase.driver( "bolt://localhost" );
98+
// end::construct-driver[]
99+
100+
// Then
101+
assertNotNull( driver );
102+
}
103+
104+
@Test
105+
public void configuration() throws Throwable
106+
{
107+
// tag::configuration[]
108+
Driver driver = GraphDatabase.driver( "bolt://localhost",
109+
Config.build()
110+
.withLogging( new MyLogging() )
111+
.toConfig());
112+
// end::configuration[]
113+
114+
// Then
115+
assertNotNull( driver );
116+
}
117+
80118
@Test
81119
public void statement() throws Throwable
82120
{
@@ -117,6 +155,99 @@ public void statementWithoutParameters() throws Throwable
117155
assertThat( stdIO.stdout(), equalTo( asList("There were 1 the ones created.") ) );
118156
}
119157

158+
@Test
159+
public void resultCursor() throws Throwable
160+
{
161+
StdIOCapture stdIO = new StdIOCapture();
162+
try( AutoCloseable captured = stdIO.capture();
163+
Driver driver = GraphDatabase.driver( "bolt://localhost" );
164+
Session session = driver.session() )
165+
{
166+
session.run( "MATCH (n) DETACH DELETE n" );
167+
session.run( "CREATE (p:Person { name: 'The One', age:23 })" );
168+
169+
// tag::result-cursor[]
170+
ResultCursor result = session.run( "MATCH (p:Person { name: {name} }) RETURN p.age",
171+
Values.parameters( "name", "The One" ) );
172+
173+
while( result.next() )
174+
{
175+
System.out.println("Record: " + result.position() );
176+
for ( Pair<String,Value> fieldInRecord : result.fields() )
177+
{
178+
System.out.println( " " + fieldInRecord.key() + " = " + fieldInRecord.value() );
179+
}
180+
}
181+
// end::result-cursor[]
182+
}
183+
184+
// Then
185+
assertThat( stdIO.stdout(), equalTo( asList("Record: 0"," p.age = 23 :: INTEGER") ) );
186+
}
187+
188+
@Test
189+
public void retainResultsForLaterProcessing() throws Throwable
190+
{
191+
StdIOCapture stdIO = new StdIOCapture();
192+
try( AutoCloseable captured = stdIO.capture();
193+
Driver driver = GraphDatabase.driver( "bolt://localhost" );
194+
Session session = driver.session() )
195+
{
196+
session.run( "MATCH (n) DETACH DELETE n" );
197+
session.run( "CREATE (p:Person { name: 'The One', age:23 })" );
198+
199+
// tag::retain-result-query[]
200+
ResultCursor result = session.run( "MATCH (p:Person { name: {name} }) RETURN id(p)",
201+
Values.parameters( "name", "The One" ) );
202+
203+
for ( Record record : result.list() )
204+
{
205+
session.run( "MATCH (p) WHERE id(p) = {id} " +
206+
"CREATE (p)-[:HAS_TRAIT]->(:Trait {type:'Immortal'})",
207+
Values.parameters( "id", record.value( "id(p)" ) ) );
208+
}
209+
// end::retain-result-query[]
210+
}
211+
}
212+
213+
@Test
214+
public void retainResultsForNestedQuerying() throws Throwable
215+
{
216+
StdIOCapture stdIO = new StdIOCapture();
217+
try( AutoCloseable captured = stdIO.capture();
218+
Driver driver = GraphDatabase.driver( "bolt://localhost" ); )
219+
{
220+
try( Session setup = driver.session() )
221+
{
222+
setup.run( "MATCH (n) DETACH DELETE n" );
223+
setup.run( "CREATE (p:Person { name: 'The One', age:23 })" );
224+
}
225+
226+
// tag::retain-result-process[]
227+
Session session = driver.session();
228+
229+
ResultCursor result = session.run( "MATCH (p:Person { name: {name} }) RETURN p.age",
230+
Values.parameters( "name", "The One" ) );
231+
232+
List<Record> records = result.list();
233+
234+
session.close();
235+
236+
for ( Record record : records )
237+
{
238+
for ( Pair<String,Value> fieldInRecord : record.fields() )
239+
{
240+
System.out.println( fieldInRecord.key() + " = " + fieldInRecord.value() );
241+
}
242+
}
243+
// end::retain-result-process[]
244+
}
245+
246+
// Then
247+
assertThat( stdIO.stdout(), equalTo( asList("p.age = 23 :: INTEGER") ) );
248+
}
249+
250+
120251
@Test
121252
public void transactionCommit() throws Throwable
122253
{
@@ -159,6 +290,45 @@ public void transactionRollback() throws Throwable
159290
}
160291
}
161292

293+
@Test
294+
public void resultSummary() throws Throwable
295+
{
296+
StdIOCapture stdIO = new StdIOCapture();
297+
try( AutoCloseable captured = stdIO.capture();
298+
Driver driver = GraphDatabase.driver( "bolt://localhost" );
299+
Session session = driver.session() )
300+
{
301+
// tag::result-summary-query-profile[]
302+
ResultCursor result = session.run( "PROFILE MATCH (p:Person { name: {name} }) RETURN id(p)",
303+
Values.parameters( "name", "The One" ) );
304+
305+
ResultSummary summary = result.summarize();
306+
307+
System.out.println( summary.statementType() );
308+
System.out.println( summary.profile() );
309+
// end::result-summary-query-profile[]
310+
}
311+
}
312+
313+
@Test
314+
public void notifications() throws Throwable
315+
{
316+
StdIOCapture stdIO = new StdIOCapture();
317+
try( AutoCloseable captured = stdIO.capture();
318+
Driver driver = GraphDatabase.driver( "bolt://localhost" );
319+
Session session = driver.session() )
320+
{
321+
// tag::result-summary-notifications[]
322+
ResultSummary summary = session.run( "EXPLAIN MATCH (a), (b) RETURN a,b" ).summarize();
323+
324+
for ( Notification notification : summary.notifications() )
325+
{
326+
System.out.println(notification);
327+
}
328+
// end::result-summary-notifications[]
329+
}
330+
}
331+
162332
@Test
163333
public void requireEncryption() throws Throwable
164334
{
@@ -193,4 +363,13 @@ public void trustSignedCertificates() throws Throwable
193363
// end::tls-signed[]
194364
driver.close();
195365
}
366+
367+
private class MyLogging implements Logging
368+
{
369+
@Override
370+
public Logger getLog( String name )
371+
{
372+
return null;
373+
}
374+
}
196375
}

0 commit comments

Comments
 (0)