1919package org .neo4j .driver .internal ;
2020
2121
22+ import java .util .ArrayList ;
23+ import java .util .LinkedList ;
24+ import java .util .List ;
25+
2226import org .junit .Rule ;
2327import org .junit .Test ;
2428import org .junit .rules .ExpectedException ;
2529import org .mockito .invocation .InvocationOnMock ;
2630import org .mockito .stubbing .Answer ;
2731
28- import java .util .ArrayList ;
29- import java .util .Arrays ;
30- import java .util .LinkedList ;
31- import java .util .List ;
32-
3332import org .neo4j .driver .internal .spi .Connection ;
3433import org .neo4j .driver .internal .value .NullValue ;
3534import org .neo4j .driver .v1 .Record ;
36- import org .neo4j .driver .v1 .Records ;
3735import org .neo4j .driver .v1 .Statement ;
3836import org .neo4j .driver .v1 .StatementResult ;
3937import org .neo4j .driver .v1 .Value ;
4038import org .neo4j .driver .v1 .exceptions .NoSuchRecordException ;
4139import org .neo4j .driver .v1 .util .Pair ;
4240
41+ import static java .util .Arrays .asList ;
42+
4343import static org .hamcrest .CoreMatchers .equalTo ;
4444import static org .hamcrest .collection .IsCollectionWithSize .hasSize ;
4545import static org .junit .Assert .assertFalse ;
4646import static org .junit .Assert .assertNotNull ;
4747import static org .junit .Assert .assertNull ;
4848import static org .junit .Assert .assertThat ;
4949import static org .junit .Assert .assertTrue ;
50+ import static org .junit .Assert .fail ;
5051import static org .mockito .Mockito .doAnswer ;
5152import static org .mockito .Mockito .mock ;
53+
54+ import static org .neo4j .driver .v1 .Records .column ;
55+ import static org .neo4j .driver .v1 .Values .ofString ;
5256import static org .neo4j .driver .v1 .Values .value ;
5357
5458public class InternalStatementResultTest
@@ -64,16 +68,20 @@ public void iterationShouldWorksAsExpected()
6468
6569 // WHEN
6670 assertTrue ( result .hasNext () );
67- assertThat ( values ( result .next () ), equalTo (Arrays . asList (value ("v1-1" ), value ( "v2-1" ))));
71+ assertThat ( values ( result .next () ), equalTo ( asList (value ("v1-1" ), value ( "v2-1" ))));
6872
6973 assertTrue ( result .hasNext () );
70- assertThat ( values ( result .next () ), equalTo (Arrays . asList (value ("v1-2" ), value ( "v2-2" ))));
74+ assertThat ( values ( result .next () ), equalTo ( asList (value ("v1-2" ), value ( "v2-2" ))));
7175
7276 assertTrue ( result .hasNext () ); //1 -> 2
7377
7478 // THEN
75- assertThat ( values ( result .next () ), equalTo (Arrays . asList (value ("v1-3" ), value ( "v2-3" ))));
79+ assertThat ( values ( result .next () ), equalTo ( asList (value ("v1-3" ), value ( "v2-3" ))));
7680 assertFalse ( result .hasNext () );
81+
82+ expectedException .expect ( NoSuchRecordException .class );
83+
84+ // WHEN
7785 assertNull ( result .next () );
7886 }
7987
@@ -100,11 +108,16 @@ public void firstOfFieldIndexShouldWorkAsExpected()
100108 }
101109
102110 @ Test
103- public void singleAfterNextShouldWork ()
111+ public void singlePastFirstShouldFail ()
104112 {
105113 // GIVEN
106114 StatementResult result = createResult ( 2 );
107115 result .next ();
116+ result .next ();
117+
118+
119+ // THEN
120+ expectedException .expect ( NoSuchRecordException .class );
108121
109122 // THEN
110123 result .single ();
@@ -186,6 +199,84 @@ public void singleShouldThrowOnEmptyResult()
186199 createResult ( 0 ).single ();
187200 }
188201
202+ @ Test
203+ public void singleShouldThrowOnConsumedResult ()
204+ {
205+ // Expect
206+ expectedException .expect ( NoSuchRecordException .class );
207+
208+ // When
209+ StatementResult result = createResult ( 2 );
210+ result .consume ();
211+ result .single ();
212+ }
213+
214+ @ Test
215+ public void shouldConsumeTwice ()
216+ {
217+ // GIVEN
218+ StatementResult result = createResult ( 2 );
219+ result .consume ();
220+
221+ // WHEN
222+ result .consume ();
223+
224+ // THEN
225+ assertFalse ( result .hasNext () );
226+ }
227+
228+ @ Test
229+ public void shouldList ()
230+ {
231+ // GIVEN
232+ StatementResult result = createResult ( 2 );
233+ List <String > records = result .list ( column ( "k1" , ofString () ) );
234+
235+ // THEN
236+ assertThat ( records , equalTo ( asList ( "v1-1" , "v1-2" ) ) );
237+ }
238+
239+ @ Test
240+ public void shouldListTwice ()
241+ {
242+ // GIVEN
243+ StatementResult result = createResult ( 2 );
244+ List <Record > firstList = result .list ();
245+ assertThat ( firstList .size (), equalTo ( 2 ) );
246+
247+ // THEN
248+ List <Record > secondList = result .list ();
249+ assertThat ( secondList .size (), equalTo ( 0 ) );
250+ }
251+
252+ @ Test
253+ public void singleShouldNotThrowOnPartiallyConsumedResult ()
254+ {
255+ // Given
256+ StatementResult result = createResult ( 2 );
257+ result .next ();
258+
259+ // When + Then
260+ assertNotNull ( result .single () );
261+ }
262+
263+ @ Test
264+ public void singleShouldConsumeIfFailing ()
265+ {
266+ // Given
267+ StatementResult result = createResult ( 2 );
268+
269+ try
270+ {
271+ result .single ();
272+ fail ( "Exception expected" );
273+ }
274+ catch ( NoSuchRecordException e )
275+ {
276+ assertFalse ( result .hasNext () );
277+ }
278+ }
279+
189280 @ Test
190281 public void retainShouldWorkAsExpected ()
191282 {
@@ -207,7 +298,7 @@ public void retainAndMapByKeyShouldWorkAsExpected()
207298 StatementResult result = createResult ( 3 );
208299
209300 // WHEN
210- List <Value > records = result .list ( Records . column ( "k1" ) );
301+ List <Value > records = result .list ( column ( "k1" ) );
211302
212303 // THEN
213304 assertFalse (result .hasNext ());
@@ -221,7 +312,7 @@ public void retainAndMapByIndexShouldWorkAsExpected()
221312 StatementResult result = createResult ( 3 );
222313
223314 // WHEN
224- List <Value > records = result .list ( Records . column ( 0 ) );
315+ List <Value > records = result .list ( column ( 0 ) );
225316
226317 // THEN
227318 assertFalse (result .hasNext ());
@@ -254,7 +345,7 @@ public void accessingKeysWithoutCallingNextShouldNotFail()
254345 // not calling next or single
255346
256347 // THEN
257- assertThat ( result .keys (), equalTo ( Arrays . asList ( "k1" , "k2" ) ) );
348+ assertThat ( result .keys (), equalTo ( asList ( "k1" , "k2" ) ) );
258349 }
259350
260351 @ Test
@@ -276,18 +367,23 @@ public void shouldPeekIntoTheFuture()
276367 result .next ();
277368
278369 // THEN
279- assertNull ( result .peek () );
370+ expectedException .expect ( NoSuchRecordException .class );
371+
372+ // WHEN
373+ result .peek ();
280374 }
281375
282376 @ Test
283377 public void shouldNotPeekIntoTheFutureWhenResultIsEmpty ()
284378 {
285379 // GIVEN
286380 StatementResult result = createResult ( 0 );
287- Record future = result .peek ();
381+
382+ // THEN
383+ expectedException .expect ( NoSuchRecordException .class );
288384
289385 // WHEN
290- assertNull ( future );
386+ Record future = result . peek ( );
291387 }
292388
293389 private StatementResult createResult ( int numberOfRecords )
0 commit comments