@Test public void collections_in_collections_look_alright() { Result result = db.execute( "CREATE (n:TheNode) RETURN [[ [1,2],[3,4] ],[[5,6]]] as x" ); Map<String, Object> next = result.next(); @SuppressWarnings( "unchecked" ) //We know it's a collection. List<List<Object>> x = (List<List<Object>>)next.get( "x" ); Iterable objects = x.get( 0 ); assertThat(objects, isA(Iterable.class)); } }
@SuppressWarnings( "unchecked" ) @Test public void shouldNotReturnInternalPointWhenInArray() { // when Result execute = graphDb.execute( "RETURN [point({longitude: 144.317718, latitude: -37.031738})] AS ps" ); // then List<Point> points = (List<Point>)execute.next().get( "ps" ); assertThat( points.get(0), Matchers.instanceOf(Point.class)); }
private void verifyResult( Result result ) { Map firstRowValue = (Map) result.next().values().iterator().next(); assertThat( firstRowValue.get( "key" ), is( "Value" ) ); List theList = (List) firstRowValue.get( "collectionKey" ); assertThat( ((Map) theList.get( 0 )).get( "inner" ), is( "Map1" ) ); assertThat( ((Map) theList.get( 1 )).get( "inner" ), is( "Map2" ) ); }
@Test public void shouldProduceWellFormedJsonEvenIfResultIteratorThrowsExceptionOnNext() throws Exception mockAccept( executionResult ); when( executionResult.columns() ).thenReturn( new ArrayList<>( data.keySet() ) ); when( executionResult.hasNext() ).thenReturn( true, true, false ); when( executionResult.next() ).thenReturn( data ).thenThrow( new RuntimeException( "Stuff went wrong!" ) );
@Test public void shouldThrowAppropriateException() { try { db.execute( "RETURN rand()/0" ).next(); } catch ( QueryExecutionException ex ) { assertThat( ex.getCause(), instanceOf( QueryExecutionKernelException.class ) ); assertThat( ex.getCause().getCause(), instanceOf( ArithmeticException.class ) ); } }
private long executeNumericResultStatement(@Name("statement") String statement, @Name("params") Map<String, Object> parameters) { long sum = 0; try (Result result = db.execute(statement, parameters)) { while (result.hasNext()) { Collection<Object> row = result.next().values(); for (Object value : row) { if (value instanceof Number) { sum += ((Number)value).longValue(); } } } } return sum; }
@SuppressWarnings( "unchecked" ) @Test public void shouldNotReturnInternalPointWhenInMap() { // when Result execute = graphDb.execute( "RETURN {p: point({longitude: 144.317718, latitude: -37.031738})} AS m" ); // then Map<String,Object> points = (Map<String, Object>)execute.next().get( "m" ); assertThat( points.get("p"), Matchers.instanceOf(Point.class)); }
@Test public void shouldProduceWellFormedJsonEvenIfResultIteratorThrowsExceptionOnHasNext() throws Exception mockAccept( executionResult ); when( executionResult.columns() ).thenReturn( new ArrayList<>( data.keySet() ) ); when( executionResult.hasNext() ).thenReturn( true ).thenThrow( new RuntimeException( "Stuff went wrong!" ) ); when( executionResult.next() ).thenReturn( data );
@Test public void shouldCloseTransactionsWhenIteratingResults() { // Given an execution result that has been started but not exhausted createNode(); createNode(); Result executionResult = db.execute( "CYPHER runtime=interpreted MATCH (n) RETURN n" ); executionResult.next(); assertThat( activeTransaction(), is( notNullValue() ) ); // When executionResult.close(); // Then assertThat( activeTransaction(), is( nullValue() ) ); }
private Object executeStatement(BlockingQueue<RowResult> queue, String stmt, Map<String, Object> params, boolean addStatistics, long timeout) throws InterruptedException { try (Result result = db.execute(stmt,params)) { long time = System.currentTimeMillis(); int row = 0; while (result.hasNext()) { terminationGuard.check(); queue.put(new RowResult(row++, result.next())); } if (addStatistics) { queue.offer(new RowResult(-1, toMap(result.getQueryStatistics(), System.currentTimeMillis() - time, row)), timeout,TimeUnit.SECONDS); } return row; } }
private void executeDistantFriendsCountQuery( int userId ) { Map<String,Object> params = singletonMap( "userId", (long) randomInt( userId ) ); try ( Result result = db.execute( "MATCH (user:User { userId: {userId} } ) -[:FRIEND]- () -[:FRIEND]- (distantFriend) " + "RETURN COUNT(distinct distantFriend)", params ) ) { while ( result.hasNext() ) { result.next(); } } }
@Test public void shouldHandleMapWithPointsAsInput() { // Given Point point1 = (Point) db.execute( "RETURN point({latitude: 12.78, longitude: 56.7}) as point" ).next().get( "point" ); Point point2 = (Point) db.execute( "RETURN point({latitude: 12.18, longitude: 56.2}) as point" ).next().get( "point" ); // When double distance = (double) db.execute( "RETURN distance({points}['p1'], {points}['p2']) as dist", map( "points", map("p1", point1, "p2", point2) ) ).next().get( "dist" ); // Then assertThat(Math.round( distance ), equalTo(86107L)); }
@Test public void fulltextIndexMustIgnoreNonStringPropertiesForPopulation() if ( nodes.hasNext() ) fail( "did not expect to find any nodes, but found at least: " + nodes.next() ); if ( relationships.hasNext() ) fail( "did not expect to find any relationships, but found at least: " + relationships.next() );
private void assertResultsAreEqual( String query1, String query2 ) { try ( Transaction ignore = db.beginTx() ) { Result result1 = db.execute( query1 ); Result result2 = db.execute( query2 ); while ( result1.hasNext() ) { assertTrue( result2.hasNext() ); assertThat( result1.next(), equalTo( result2.next() ) ); } assertFalse( result2.hasNext() ); } }
private static void mockAccept( Result mock ) { doAnswer( invocation -> { Result result = (Result) invocation.getMock(); Result.ResultVisitor visitor = invocation.getArgument( 0 ); while ( result.hasNext() ) { visitor.visit( new MapRow( result.next() ) ); } return null; } ).when( mock ) .accept( (Result.ResultVisitor<RuntimeException>) any( Result.ResultVisitor.class ) ); }