Search in sources :

Example 6 with Session

use of org.neo4j.driver.Session in project neo4j by neo4j.

the class ProcedureTest method callsSimplisticProcedure.

@Test
void callsSimplisticProcedure(Neo4j neo4j) {
    try (Driver driver = GraphDatabase.driver(neo4j.boltURI(), configuration());
        Session session = driver.session()) {
        Result result = session.run("CALL " + procedureNamespace + ".theAnswer()");
        assertThat(result.single().get("value").asLong()).isEqualTo(42L);
    }
}
Also used : Driver(org.neo4j.driver.Driver) Session(org.neo4j.driver.Session) Result(org.neo4j.driver.Result) Test(org.junit.jupiter.api.Test)

Example 7 with Session

use of org.neo4j.driver.Session in project neo4j by neo4j.

the class ProcedureTest method callsProceduresWithSimpleInputTypeReturningRecordWithPrimitiveFields.

@Test
void callsProceduresWithSimpleInputTypeReturningRecordWithPrimitiveFields(Neo4j neo4j) {
    try (Driver driver = GraphDatabase.driver(neo4j.boltURI(), configuration());
        Session session = driver.session()) {
        assertThat(session.run("CALL " + procedureNamespace + ".simpleInput11('string') YIELD field04 AS p RETURN p").single()).isNotNull();
        assertThat(session.run("CALL " + procedureNamespace + ".simpleInput12(42)").single()).isNotNull();
        assertThat(session.run("CALL " + procedureNamespace + ".simpleInput13(42)").single()).isNotNull();
        assertThat(session.run("CALL " + procedureNamespace + ".simpleInput14(4.2)").single()).isNotNull();
        assertThat(session.run("CALL " + procedureNamespace + ".simpleInput15(true)").single()).isNotNull();
        assertThat(session.run("CALL " + procedureNamespace + ".simpleInput16(false)").single()).isNotNull();
        assertThat(session.run("CALL " + procedureNamespace + ".simpleInput17({foo:'bar'})").single()).isNotNull();
        assertThat(session.run("CALL " + procedureNamespace + ".simpleInput21()").single()).isNotNull();
    }
}
Also used : Driver(org.neo4j.driver.Driver) Session(org.neo4j.driver.Session) Test(org.junit.jupiter.api.Test)

Example 8 with Session

use of org.neo4j.driver.Session in project neo4j by neo4j.

the class ProcedureTest method callsProceduresWithSimpleInputTypeReturningVoid.

@Test
void callsProceduresWithSimpleInputTypeReturningVoid(Neo4j neo4j) {
    try (Driver driver = GraphDatabase.driver(neo4j.boltURI(), configuration());
        Session session = driver.session()) {
        session.run("CALL " + procedureNamespace + ".simpleInput00()");
        session.run("CALL " + procedureNamespace + ".simpleInput01('string')");
        session.run("CALL " + procedureNamespace + ".simpleInput02(42)");
        session.run("CALL " + procedureNamespace + ".simpleInput03(42)");
        session.run("CALL " + procedureNamespace + ".simpleInput04(4.2)");
        session.run("CALL " + procedureNamespace + ".simpleInput05(true)");
        session.run("CALL " + procedureNamespace + ".simpleInput06(false)");
        session.run("CALL " + procedureNamespace + ".simpleInput07({foo:'bar'})");
        session.run("MATCH (n)            CALL " + procedureNamespace + ".simpleInput08(n) RETURN n");
        session.run("MATCH p=(()-[r]->()) CALL " + procedureNamespace + ".simpleInput09(p) RETURN p");
        session.run("MATCH ()-[r]->()     CALL " + procedureNamespace + ".simpleInput10(r) RETURN r");
    }
}
Also used : Driver(org.neo4j.driver.Driver) Session(org.neo4j.driver.Session) Test(org.junit.jupiter.api.Test)

Example 9 with Session

use of org.neo4j.driver.Session in project neo4j by neo4j.

the class ProcedureTest method callsProceduresWithDifferentModesReturningVoid.

@Test
void callsProceduresWithDifferentModesReturningVoid(Neo4j neo4j) {
    try (Driver driver = GraphDatabase.driver(neo4j.boltURI(), configuration());
        Session session = driver.session()) {
        session.run("CALL " + procedureNamespace + ".defaultMode()");
        session.run("CALL " + procedureNamespace + ".readMode()");
        session.run("CALL " + procedureNamespace + ".writeMode()");
        session.run("CALL " + procedureNamespace + ".schemaMode()");
        session.run("CALL " + procedureNamespace + ".dbmsMode()");
    }
}
Also used : Driver(org.neo4j.driver.Driver) Session(org.neo4j.driver.Session) Test(org.junit.jupiter.api.Test)

Example 10 with Session

use of org.neo4j.driver.Session in project beam by apache.

the class Neo4jIOIT method testLargeWriteUnwind.

@Test
public void testLargeWriteUnwind() throws Exception {
    final int startId = 5000;
    final int endId = 6000;
    // Create 1000 IDs
    List<Integer> idList = new ArrayList<>();
    for (int id = startId; id < endId; id++) {
        idList.add(id);
    }
    PCollection<Integer> idCollection = largeWriteUnwindPipeline.apply(Create.of(idList));
    // Every row is represented by a Map<String, Object> in the parameters map.
    // We accumulate the rows and 'unwind' those to Neo4j for performance reasons.
    // 
    SerializableFunction<Integer, Map<String, Object>> parametersFunction = id -> ImmutableMap.of("id", id, "name", "Casters", "firstName", "Matt");
    // 1000 rows with a batch size of 123 should trigger most scenarios we can think of
    // We've put a unique constraint on Something.id
    // 
    Neo4jIO.WriteUnwind<Integer> read = Neo4jIO.<Integer>writeUnwind().withDriverConfiguration(Neo4jTestUtil.getDriverConfiguration(containerHostname, containerPort)).withSessionConfig(SessionConfig.forDatabase(Neo4jTestUtil.NEO4J_DATABASE)).withBatchSize(123).withUnwindMapName("rows").withCypher("UNWIND $rows AS row CREATE(n:Something { id : row.id })").withParametersFunction(parametersFunction).withCypherLogging();
    idCollection.apply(read);
    // Now run this pipeline
    // 
    PipelineResult pipelineResult = largeWriteUnwindPipeline.run();
    Assert.assertEquals(PipelineResult.State.DONE, pipelineResult.getState());
    // 
    try (Driver driver = Neo4jTestUtil.getDriver(containerHostname, containerPort)) {
        try (Session session = Neo4jTestUtil.getSession(driver, true)) {
            List<Integer> values = session.readTransaction(tx -> {
                List<Integer> v = null;
                int nrRows = 0;
                Result result = tx.run("MATCH(n:Something) RETURN count(n), min(n.id), max(n.id)");
                while (result.hasNext()) {
                    Record record = result.next();
                    v = Arrays.asList(record.get(0).asInt(), record.get(1).asInt(), record.get(2).asInt(), ++nrRows);
                }
                return v;
            });
            Assert.assertNotNull(values);
            assertThat(values, contains(endId - startId, startId, endId - 1, 1));
        }
    }
}
Also used : Session(org.neo4j.driver.Session) Arrays(java.util.Arrays) SerializableCoder(org.apache.beam.sdk.coders.SerializableCoder) BeforeClass(org.junit.BeforeClass) IsIterableContainingInOrder.contains(org.hamcrest.collection.IsIterableContainingInOrder.contains) DockerImageName(org.testcontainers.utility.DockerImageName) IsIterableContainingInAnyOrder.containsInAnyOrder(org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder) PipelineResult(org.apache.beam.sdk.PipelineResult) RunWith(org.junit.runner.RunWith) SerializableFunction(org.apache.beam.sdk.transforms.SerializableFunction) ImmutableMap(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap) ArrayList(java.util.ArrayList) Create(org.apache.beam.sdk.transforms.Create) Map(java.util.Map) TestPipeline(org.apache.beam.sdk.testing.TestPipeline) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Row(org.apache.beam.sdk.values.Row) DoFn(org.apache.beam.sdk.transforms.DoFn) AfterClass(org.junit.AfterClass) Driver(org.neo4j.driver.Driver) Neo4jContainer(org.testcontainers.containers.Neo4jContainer) PAssert(org.apache.beam.sdk.testing.PAssert) Test(org.junit.Test) JUnit4(org.junit.runners.JUnit4) PCollection(org.apache.beam.sdk.values.PCollection) Schema(org.apache.beam.sdk.schemas.Schema) Result(org.neo4j.driver.Result) List(java.util.List) Rule(org.junit.Rule) SessionConfig(org.neo4j.driver.SessionConfig) ParDo(org.apache.beam.sdk.transforms.ParDo) Assert(org.junit.Assert) Collections(java.util.Collections) Record(org.neo4j.driver.Record) ArrayList(java.util.ArrayList) PipelineResult(org.apache.beam.sdk.PipelineResult) Driver(org.neo4j.driver.Driver) PipelineResult(org.apache.beam.sdk.PipelineResult) Result(org.neo4j.driver.Result) Record(org.neo4j.driver.Record) ImmutableMap(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap) Map(java.util.Map) Session(org.neo4j.driver.Session) Test(org.junit.Test)

Aggregations

Session (org.neo4j.driver.Session)24 Driver (org.neo4j.driver.Driver)20 Result (org.neo4j.driver.Result)17 Test (org.junit.Test)13 FakeSession (org.neo4j.shell.test.bolt.FakeSession)10 FakeDriver (org.neo4j.shell.test.bolt.FakeDriver)9 Test (org.junit.jupiter.api.Test)6 SessionConfig (org.neo4j.driver.SessionConfig)6 Record (org.neo4j.driver.Record)5 SessionExpiredException (org.neo4j.driver.exceptions.SessionExpiredException)4 HashMap (java.util.HashMap)3 BeforeClass (org.junit.BeforeClass)3 ClientException (org.neo4j.driver.exceptions.ClientException)3 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 List (java.util.List)2 Map (java.util.Map)2 PipelineResult (org.apache.beam.sdk.PipelineResult)2 SerializableCoder (org.apache.beam.sdk.coders.SerializableCoder)2