Skip to content

Commit

Permalink
Updating tests according to the server-side changes (neo4j#834)
Browse files Browse the repository at this point in the history
  • Loading branch information
injectives committed Mar 1, 2021
1 parent 09db20b commit eb413c0
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 );
}

Expand All @@ -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
Expand All @@ -489,9 +489,9 @@ void shouldFailWhenListTransformationFunctionFails()

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

Expand Down Expand Up @@ -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() );
}
} );

Expand All @@ -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() );
}
} );

Expand All @@ -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 );
} );
}
} );

Expand All @@ -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 );
} );
}
} );

Expand Down Expand Up @@ -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" ) );
Expand All @@ -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" ) ) );
Expand All @@ -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 );
}
Expand Down Expand Up @@ -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 )
{
Expand All @@ -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 )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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() ) );
}
Expand All @@ -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() ) );
}

Expand All @@ -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() ) );
}
Expand All @@ -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() ) );
}

Expand All @@ -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" ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 )
Expand Down
Loading

0 comments on commit eb413c0

Please sign in to comment.