Skip to content

Commit

Permalink
Polishing.
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-simons committed Mar 2, 2020
1 parent 2ddb112 commit a9a069e
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 42 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -99,7 +99,7 @@ To use the driver in a project, please use the released driver via Maven Central

#### Running Tests and Creating a Package

The driver unit tests relies on latest [`boltkit`](https://github.com/neo4j-contrib/boltkit) installed on your local machine.
The driver unit tests relies on latest [`boltkit`](https://github.com/neo4j-drivers/boltkit) installed on your local machine.
If `boltkit` is not installed, then all tests that requires `boltkit` will be ignored and will not be executed.

The following Maven command shows how to run all tests and build the source code:
Expand Down
Expand Up @@ -86,7 +86,7 @@ private static ConditionEvaluationResult checkEditionAvailability( ConditionEval
{
try ( Session session = driver.session() )
{
String value = session.run( "call dbms.components() yield edition" ).single().get( "edition" ).asString();
String value = session.run( "CALL dbms.components() YIELD edition" ).single().get( "edition" ).asString();
boolean editionMatches = edition.matches( value );
return editionMatches
? enabled( previousResult.getReason().map( v -> v + " and enabled" ).orElse( "Enabled" ) + " on " + value + "-edition" )
Expand Down
30 changes: 23 additions & 7 deletions driver/src/test/java/org/neo4j/driver/util/TestUtil.java
Expand Up @@ -47,8 +47,9 @@
import org.neo4j.driver.AccessMode;
import org.neo4j.driver.Bookmark;
import org.neo4j.driver.Driver;
import org.neo4j.driver.Session;
import org.neo4j.driver.Result;
import org.neo4j.driver.Session;
import org.neo4j.driver.SessionConfig;
import org.neo4j.driver.exceptions.ServiceUnavailableException;
import org.neo4j.driver.internal.BoltServerAddress;
import org.neo4j.driver.internal.DefaultBookmarkHolder;
Expand Down Expand Up @@ -249,14 +250,29 @@ public static Bookmark cleanDb( Driver driver )

public static void dropDatabase( Driver driver, String database )
{
boolean databaseExists = databaseExists( driver, database );
if ( !databaseExists )
{
return;
}

try ( Session session = driver.session( forDatabase( "system" ) ) )
{
// No procedure equivalent and `call dbms.database.state("db")` also throws an exception when db doesn't exist
boolean databaseExists = databaseExists( driver, database );
if ( databaseExists )
{
session.run( "DROP DATABASE " + database ).consume();
}
session.run( "DROP DATABASE " + database ).consume();
}
}

public static void createDatabase( Driver driver, String database )
{
boolean databaseExists = databaseExists( driver, database );
if ( databaseExists )
{
return;
}

try ( Session session = driver.session( SessionConfig.forDatabase( "system" ) ) )
{
session.run( "CREATE DATABASE " + database ).consume();
}
}

Expand Down
Expand Up @@ -18,10 +18,11 @@
*/
package org.neo4j.docs.driver;

// tag::database-selection-import[]
import org.neo4j.driver.AccessMode;
import org.neo4j.driver.Session;
import org.neo4j.driver.SessionConfig;
import org.neo4j.driver.summary.ResultSummary;
// end::database-selection-import[]

public class DatabaseSelectionExample extends BaseApplication
{
Expand All @@ -30,28 +31,23 @@ public DatabaseSelectionExample( String uri, String user, String password )
super( uri, user, password );
}

public void createExampleDatabase()
{
try ( Session session = driver.session( SessionConfig.forDatabase( "system" ) ) )
{
ResultSummary summary = session.run( "CREATE DATABASE examples" ).consume();
System.out.println( summary.counters().systemUpdates() + " updates in " + summary.database().name() );
}
}

public void useAnotherDatabaseExample()
{
createExampleDatabase();

// tag::database-selection[]
try ( Session session = driver.session( SessionConfig.forDatabase( "examples" ) ) )
{
session.run( "CREATE (a:Greeting {message: 'Hello, Example-Database'}) RETURN a" ).consume();
}

try ( Session session = driver.session( SessionConfig.builder().withDatabase( "examples" ).withDefaultAccessMode( AccessMode.READ ).build() ) )
SessionConfig sessionConfig = SessionConfig.builder()
.withDatabase( "examples" )
.withDefaultAccessMode( AccessMode.READ )
.build();
try ( Session session = driver.session( sessionConfig ) )
{
String msg = session.run( "MATCH (a:Greeting) RETURN a.message as msg" ).single().get( "msg" ).asString();
System.out.println(msg);
}
// end::database-selection[]
}
}
24 changes: 4 additions & 20 deletions examples/src/test/java/org/neo4j/docs/driver/ExamplesIT.java
Expand Up @@ -63,6 +63,7 @@
import static org.neo4j.driver.util.Neo4jRunner.PASSWORD;
import static org.neo4j.driver.util.Neo4jRunner.USER;
import static org.neo4j.driver.util.TestUtil.await;
import static org.neo4j.driver.util.TestUtil.createDatabase;
import static org.neo4j.driver.util.TestUtil.databaseExists;
import static org.neo4j.driver.util.TestUtil.dropDatabase;

Expand Down Expand Up @@ -661,36 +662,19 @@ void testShouldRunRxResultConsumeExampleRxJava() throws Exception

@Test
@EnabledOnNeo4jWith( value = BOLT_V4, edition = ENTERPRISE)
void testShouldTestThings() throws Exception
void testUseAnotherDatabaseExample() throws Exception
{
Driver driver = neo4j.driver();
dropDatabase( driver, "examples" );
createDatabase( driver, "examples" );

// Given
try ( DatabaseSelectionExample example = new DatabaseSelectionExample( uri, USER, PASSWORD ) )
{
// When
example.createExampleDatabase();

// Then
assertTrue( databaseExists( driver, "examples" ) );
}
}

@Test
@EnabledOnNeo4jWith( value = BOLT_V4, edition = ENTERPRISE)
void morethingstotest() throws Exception
{
dropDatabase( neo4j.driver(), "examples" );

// Given
try ( DatabaseSelectionExample example = new DatabaseSelectionExample( uri, USER, PASSWORD ) )
{
// When
example.useAnotherDatabaseExample();

// Then
int greetingCount = readInt( "examples", "MATCH (a:Greeting) RETURN count(a)", Values.parameters() );
int greetingCount = readInt( "examples", "MATCH (a:Greeting) RETURN count(a)", Values.parameters() );
assertThat( greetingCount, is( 1 ) );
}
}
Expand Down

0 comments on commit a9a069e

Please sign in to comment.