Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updating tests according to the server-side changes
Browse files Browse the repository at this point in the history
injectives committed Feb 26, 2021
1 parent f844d94 commit db59604
Showing 4 changed files with 95 additions and 90 deletions.
Original file line number Diff line number Diff line change
@@ -154,7 +154,7 @@ void shouldFailForIncorrectQuery()
ResultCursor cursor = await( session.runAsync( "RETURN" ) );

Exception e = assertThrows( Exception.class, () -> await( cursor.nextAsync() ) );
assertThat( e, is( syntaxError( "Unexpected end of input" ) ) );
assertThat( e, is( syntaxError() ) );
}

@Test
@@ -446,9 +446,9 @@ void shouldFailForEachWhenActionFails()

IOException e = assertThrows( IOException.class, () ->
await( cursor.forEachAsync( record ->
{
throw new CompletionException( error );
} ) ) );
{
throw new CompletionException( error );
} ) ) );
assertEquals( error, e );
}

@@ -462,7 +462,7 @@ void shouldConvertToListWithEmptyCursor()
void shouldConvertToListWithNonEmptyCursor()
{
testList( "UNWIND range(1, 100, 10) AS x RETURN x",
Arrays.asList( 1L, 11L, 21L, 31L, 41L, 51L, 61L, 71L, 81L, 91L ) );
Arrays.asList( 1L, 11L, 21L, 31L, 41L, 51L, 61L, 71L, 81L, 91L ) );
}

@Test
@@ -489,9 +489,9 @@ void shouldFailWhenListTransformationFunctionFails()

RuntimeException e = assertThrows( RuntimeException.class, () ->
await( cursor.listAsync( record ->
{
throw error;
} ) ) );
{
throw error;
} ) ) );
assertEquals( error, e );
}

@@ -604,8 +604,8 @@ public CompletionStage<Integer> execute( AsyncTransaction tx )
throw new SessionExpiredException( "Oh!" );
}
return tx.runAsync( "UNWIND range(1, 10) AS x RETURN count(x)" )
.thenCompose( ResultCursor::singleAsync )
.thenApply( record -> record.get( 0 ).asInt() );
.thenCompose( ResultCursor::singleAsync )
.thenApply( record -> record.get( 0 ).asInt() );
}
} );

@@ -629,8 +629,8 @@ public CompletionStage<Integer> execute( AsyncTransaction tx )
throw new ServiceUnavailableException( "Oh!" );
}
return tx.runAsync( "CREATE (n1:TestNode), (n2:TestNode) RETURN 2" )
.thenCompose( ResultCursor::singleAsync )
.thenApply( record -> record.get( 0 ).asInt() );
.thenCompose( ResultCursor::singleAsync )
.thenApply( record -> record.get( 0 ).asInt() );
}
} );

@@ -651,16 +651,16 @@ void shouldExecuteReadTransactionUntilSuccessWhenWorkFails()
public CompletionStage<Integer> execute( AsyncTransaction tx )
{
return tx.runAsync( "RETURN 42" )
.thenCompose( ResultCursor::singleAsync )
.thenApply( record -> record.get( 0 ).asInt() )
.thenCompose( result ->
{
if ( failures.getAndIncrement() < maxFailures )
{
return failedFuture( new TransientException( "A", "B" ) );
}
return completedFuture( result );
} );
.thenCompose( ResultCursor::singleAsync )
.thenApply( record -> record.get( 0 ).asInt() )
.thenCompose( result ->
{
if ( failures.getAndIncrement() < maxFailures )
{
return failedFuture( new TransientException( "A", "B" ) );
}
return completedFuture( result );
} );
}
} );

@@ -680,16 +680,16 @@ void shouldExecuteWriteTransactionUntilSuccessWhenWorkFails()
public CompletionStage<String> execute( AsyncTransaction tx )
{
return tx.runAsync( "CREATE (:MyNode) RETURN 'Hello'" )
.thenCompose( ResultCursor::singleAsync )
.thenApply( record -> record.get( 0 ).asString() )
.thenCompose( result ->
{
if ( failures.getAndIncrement() < maxFailures )
{
return failedFuture( new ServiceUnavailableException( "Hi" ) );
}
return completedFuture( result );
} );
.thenCompose( ResultCursor::singleAsync )
.thenApply( record -> record.get( 0 ).asString() )
.thenCompose( result ->
{
if ( failures.getAndIncrement() < maxFailures )
{
return failedFuture( new ServiceUnavailableException( "Hi" ) );
}
return completedFuture( result );
} );
}
} );

@@ -779,7 +779,7 @@ void shouldPropagateFailureInCloseFromPreviousRun()
void shouldCloseCleanlyAfterFailure()
{
CompletionStage<ResultCursor> runWithOpenTx = session.beginTransactionAsync()
.thenCompose( tx -> session.runAsync( "RETURN 1" ) );
.thenCompose( tx -> session.runAsync( "RETURN 1" ) );

ClientException e = assertThrows( ClientException.class, () -> await( runWithOpenTx ) );
assertThat( e.getMessage(), startsWith( "Queries cannot be run directly on a session with an open transaction" ) );
@@ -791,9 +791,9 @@ void shouldCloseCleanlyAfterFailure()
void shouldPropagateFailureFromFirstIllegalQuery()
{
CompletionStage<ResultCursor> allQueries = session.runAsync( "CREATE (:Node1)" )
.thenCompose( ignore -> session.runAsync( "CREATE (:Node2)" ) )
.thenCompose( ignore -> session.runAsync( "RETURN invalid" ) )
.thenCompose( ignore -> session.runAsync( "CREATE (:Node3)" ) );
.thenCompose( ignore -> session.runAsync( "CREATE (:Node2)" ) )
.thenCompose( ignore -> session.runAsync( "RETURN invalid" ) )
.thenCompose( ignore -> session.runAsync( "CREATE (:Node3)" ) );

ClientException e = assertThrows( ClientException.class, () -> await( allQueries ) );
assertThat( e, is( syntaxError( "Variable `invalid` not defined" ) ) );
@@ -820,59 +820,59 @@ private Future<List<CompletionStage<Record>>> runNestedQueries( ResultCursor inp
return resultFuture;
}

private void runNestedQueries(ResultCursor inputCursor, List<CompletionStage<Record>> stages,
CompletableFuture<List<CompletionStage<Record>>> resultFuture )
private void runNestedQueries( ResultCursor inputCursor, List<CompletionStage<Record>> stages,
CompletableFuture<List<CompletionStage<Record>>> resultFuture )
{
final CompletionStage<Record> recordResponse = inputCursor.nextAsync();
stages.add( recordResponse );

recordResponse.whenComplete( ( record, error ) ->
{
if ( error != null )
{
resultFuture.completeExceptionally( error );
}
else if ( record != null )
{
runNestedQuery( inputCursor, record, stages, resultFuture );
}
else
{
resultFuture.complete( stages );
}
} );
}

private void runNestedQuery(ResultCursor inputCursor, Record record,
List<CompletionStage<Record>> stages, CompletableFuture<List<CompletionStage<Record>>> resultFuture )
{
if ( error != null )
{
resultFuture.completeExceptionally( error );
}
else if ( record != null )
{
runNestedQuery( inputCursor, record, stages, resultFuture );
}
else
{
resultFuture.complete( stages );
}
} );
}

private void runNestedQuery( ResultCursor inputCursor, Record record,
List<CompletionStage<Record>> stages, CompletableFuture<List<CompletionStage<Record>>> resultFuture )
{
Node node = record.get( 0 ).asNode();
long id = node.get( "id" ).asLong();
long age = id * 10;

CompletionStage<ResultCursor> response =
session.runAsync( "MATCH (p:Person {id: $id}) SET p.age = $age RETURN p",
parameters( "id", id, "age", age ) );
parameters( "id", id, "age", age ) );

response.whenComplete( ( cursor, error ) ->
{
if ( error != null )
{
resultFuture.completeExceptionally( Futures.completionExceptionCause( error ) );
}
else
{
stages.add( cursor.nextAsync() );
runNestedQueries( inputCursor, stages, resultFuture );
}
} );
{
if ( error != null )
{
resultFuture.completeExceptionally( Futures.completionExceptionCause( error ) );
}
else
{
stages.add( cursor.nextAsync() );
runNestedQueries( inputCursor, stages, resultFuture );
}
} );
}

private long countNodesByLabel( String label )
{
CompletionStage<Long> countStage = session.runAsync( "MATCH (n:" + label + ") RETURN count(n)" )
.thenCompose( ResultCursor::singleAsync )
.thenApply( record -> record.get( 0 ).asLong() );
.thenCompose( ResultCursor::singleAsync )
.thenApply( record -> record.get( 0 ).asLong() );

return await( countStage );
}
@@ -960,13 +960,13 @@ public CompletionStage<Record> execute( AsyncTransaction tx )
CompletableFuture<Record> resultFuture = new CompletableFuture<>();

tx.runAsync( query ).whenComplete( ( cursor, error ) ->
processQueryResult( cursor, Futures.completionExceptionCause( error ), resultFuture ) );
processQueryResult( cursor, Futures.completionExceptionCause( error ), resultFuture ) );

return resultFuture;
}

private void processQueryResult(ResultCursor cursor, Throwable error,
CompletableFuture<Record> resultFuture )
private void processQueryResult( ResultCursor cursor, Throwable error,
CompletableFuture<Record> resultFuture )
{
if ( error != null )
{
@@ -975,7 +975,7 @@ private void processQueryResult(ResultCursor cursor, Throwable error,
}

cursor.nextAsync().whenComplete( ( record, fetchError ) ->
processFetchResult( record, Futures.completionExceptionCause( fetchError ), resultFuture ) );
processFetchResult( record, Futures.completionExceptionCause( fetchError ), resultFuture ) );
}

private void processFetchResult( Record record, Throwable error, CompletableFuture<Record> resultFuture )
Original file line number Diff line number Diff line change
@@ -33,6 +33,7 @@
import java.util.concurrent.CompletionStage;
import java.util.concurrent.atomic.AtomicInteger;

import org.neo4j.driver.Bookmark;
import org.neo4j.driver.Query;
import org.neo4j.driver.Record;
import org.neo4j.driver.Value;
@@ -41,11 +42,10 @@
import org.neo4j.driver.async.ResultCursor;
import org.neo4j.driver.exceptions.ClientException;
import org.neo4j.driver.exceptions.NoSuchRecordException;
import org.neo4j.driver.exceptions.ServiceUnavailableException;
import org.neo4j.driver.Bookmark;
import org.neo4j.driver.exceptions.ResultConsumedException;
import org.neo4j.driver.summary.ResultSummary;
import org.neo4j.driver.exceptions.ServiceUnavailableException;
import org.neo4j.driver.summary.QueryType;
import org.neo4j.driver.summary.ResultSummary;
import org.neo4j.driver.types.Node;
import org.neo4j.driver.util.DatabaseExtension;
import org.neo4j.driver.util.ParallelizableIT;
@@ -63,8 +63,8 @@
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.neo4j.driver.Values.parameters;
import static org.neo4j.driver.SessionConfig.builder;
import static org.neo4j.driver.Values.parameters;
import static org.neo4j.driver.internal.InternalBookmark.parse;
import static org.neo4j.driver.internal.util.Iterables.single;
import static org.neo4j.driver.internal.util.Matchers.containsResultAvailableAfterAndResultConsumedAfter;
@@ -222,7 +222,7 @@ void shouldFailToCommitAfterSingleWrongQuery()
ResultCursor cursor = await( tx.runAsync( "RETURN" ) );

Exception e = assertThrows( Exception.class, () -> await( cursor.consumeAsync() ) );
assertThat( e, is( syntaxError( "Unexpected end of input" ) ) );
assertThat( e, is( syntaxError() ) );

assertThrows( ClientException.class, () -> await( tx.commitAsync() ) );
}
@@ -235,7 +235,7 @@ void shouldAllowRollbackAfterSingleWrongQuery()
ResultCursor cursor = await( tx.runAsync( "RETURN" ) );

Exception e = assertThrows( Exception.class, () -> await( cursor.nextAsync() ) );
assertThat( e, is( syntaxError( "Unexpected end of input" ) ) );
assertThat( e, is( syntaxError() ) );
assertThat( await( tx.rollbackAsync() ), is( nullValue() ) );
}

@@ -257,7 +257,7 @@ void shouldFailToCommitAfterCoupleCorrectAndSingleWrongQuery()
ResultCursor cursor3 = await( tx.runAsync( "RETURN" ) );

Exception e = assertThrows( Exception.class, () -> await( cursor3.consumeAsync() ) );
assertThat( e, is( syntaxError( "Unexpected end of input" ) ) );
assertThat( e, is( syntaxError() ) );

assertThrows( ClientException.class, () -> await( tx.commitAsync() ) );
}
@@ -280,7 +280,7 @@ void shouldAllowRollbackAfterCoupleCorrectAndSingleWrongQuery()
ResultCursor cursor3 = await( tx.runAsync( "RETURN" ) );

Exception e = assertThrows( Exception.class, () -> await( cursor3.consumeAsync() ) );
assertThat( e, is( syntaxError( "Unexpected end of input" ) ) );
assertThat( e, is( syntaxError() ) );
assertThat( await( tx.rollbackAsync() ), is( nullValue() ) );
}

@@ -292,7 +292,7 @@ void shouldNotAllowNewQueriesAfterAnIncorrectQuery()
ResultCursor cursor = await( tx.runAsync( "RETURN" ) );

Exception e1 = assertThrows( Exception.class, () -> await( cursor.nextAsync() ) );
assertThat( e1, is( syntaxError( "Unexpected end of input" ) ) );
assertThat( e1, is( syntaxError() ) );

ClientException e2 = assertThrows( ClientException.class, () -> tx.runAsync( "CREATE ()" ) );
assertThat( e2.getMessage(), startsWith( "Cannot run more queries in this transaction" ) );
Original file line number Diff line number Diff line change
@@ -38,17 +38,17 @@
import java.util.stream.Stream;

import org.neo4j.driver.Bookmark;
import org.neo4j.driver.Record;
import org.neo4j.driver.Query;
import org.neo4j.driver.Record;
import org.neo4j.driver.Value;
import org.neo4j.driver.exceptions.ClientException;
import org.neo4j.driver.exceptions.ServiceUnavailableException;
import org.neo4j.driver.internal.util.EnabledOnNeo4jWith;
import org.neo4j.driver.reactive.RxSession;
import org.neo4j.driver.reactive.RxResult;
import org.neo4j.driver.reactive.RxSession;
import org.neo4j.driver.reactive.RxTransaction;
import org.neo4j.driver.summary.ResultSummary;
import org.neo4j.driver.summary.QueryType;
import org.neo4j.driver.summary.ResultSummary;
import org.neo4j.driver.types.Node;
import org.neo4j.driver.util.DatabaseExtension;
import org.neo4j.driver.util.ParallelizableIT;
@@ -899,7 +899,7 @@ private static void assertFailToRunWrongQuery(RxTransaction tx )
{
RxResult result = tx.run( "RETURN" );
Exception e = assertThrows( Exception.class, () -> await( result.records() ) );
assertThat( e, is( syntaxError( "Unexpected end of input" ) ) );
assertThat( e, is( syntaxError() ) );
}

private void assertCanRunReturnOne( RxTransaction tx )
Original file line number Diff line number Diff line change
@@ -25,6 +25,8 @@
import java.util.Objects;
import java.util.concurrent.TimeUnit;

import org.neo4j.driver.Driver;
import org.neo4j.driver.exceptions.ClientException;
import org.neo4j.driver.internal.BoltServerAddress;
import org.neo4j.driver.internal.DirectConnectionProvider;
import org.neo4j.driver.internal.InternalDriver;
@@ -34,8 +36,6 @@
import org.neo4j.driver.internal.cluster.RoutingTable;
import org.neo4j.driver.internal.cluster.loadbalancing.LoadBalancer;
import org.neo4j.driver.internal.spi.ConnectionProvider;
import org.neo4j.driver.Driver;
import org.neo4j.driver.exceptions.ClientException;
import org.neo4j.driver.summary.ResultSummary;

public final class Matchers
@@ -200,6 +200,11 @@ public void describeTo( Description description )
};
}

public static Matcher<Throwable> syntaxError()
{
return syntaxError( null );
}

public static Matcher<Throwable> syntaxError( String messagePrefix )
{
return new TypeSafeMatcher<Throwable>()
@@ -211,7 +216,7 @@ protected boolean matchesSafely( Throwable error )
{
ClientException clientError = (ClientException) error;
return clientError.code().contains( "SyntaxError" ) &&
clientError.getMessage().startsWith( messagePrefix );
(messagePrefix == null || clientError.getMessage().startsWith( messagePrefix ));
}
return false;
}

0 comments on commit db59604

Please sign in to comment.