Refine search
@Test @SuppressWarnings( "unchecked" ) public void shouldNotIncludePlanUnlessAskedFor() throws Exception { // Given Result result = mock( Result.class ); when( result.hasNext() ).thenReturn( false ); when( result.columns() ).thenReturn( new ArrayList<>() ); // When Map<String, Object> serialized = serializeToStringThenParseAsToMap( new CypherResultRepresentation( result, /*includeStats=*/false, false ) ); // Then assertFalse( "Didn't expect to see a plan here", serialized.containsKey( "plan" ) ); }
@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!" ) );
static void assertQueryFindsIds( GraphDatabaseService db, boolean queryNodes, String index, String query, long... ids ) { try ( Transaction tx = db.beginTx() ) { String queryCall = queryNodes ? QUERY_NODES : QUERY_RELS; Result result = db.execute( format( queryCall, index, query ) ); int num = 0; Double score = Double.MAX_VALUE; while ( result.hasNext() ) { Map entry = result.next(); Long nextId = ((Entity) entry.get( queryNodes ? NODE : RELATIONSHIP )).getId(); Double nextScore = (Double) entry.get( SCORE ); assertThat( nextScore, lessThanOrEqualTo( score ) ); score = nextScore; if ( num < ids.length ) { assertEquals( format( "Result returned id %d, expected %d", nextId, ids[num] ), ids[num], nextId.longValue() ); } else { fail( format( "Result returned id %d, which is beyond the number of ids (%d) that were expected.", nextId, ids.length ) ); } num++; } assertEquals( "Number of results differ from expected", ids.length, num ); tx.success(); } }
@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 );
try ( Transaction tx = db.beginTx() ) Result result = db.execute( format( queryCall, index, query ) ); Double score = Double.MAX_VALUE; while ( result.hasNext() ) Map entry = result.next(); long nextId = ((Entity) entry.get( queryNodes ? NODE : RELATIONSHIP )).getId(); Double nextScore = (Double) entry.get( SCORE );
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(); } } }
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; }
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 ) ); }