Search in sources :

Example 6 with Record

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

the class BoltStateHandlerTest method triesAgainOnSessionExpired.

@Test
public void triesAgainOnSessionExpired() throws Exception {
    Session sessionMock = mock(Session.class);
    Result resultMock = mock(Result.class);
    Record recordMock = mock(Record.class);
    Value valueMock = mock(Value.class);
    Driver driverMock = stubResultSummaryInAnOpenSession(resultMock, sessionMock, "neo4j-version");
    when(resultMock.list()).thenReturn(singletonList(recordMock));
    when(valueMock.toString()).thenReturn("999");
    when(recordMock.get(0)).thenReturn(valueMock);
    when(sessionMock.run(any(Query.class))).thenThrow(new SessionExpiredException("leaderswitch")).thenReturn(resultMock);
    OfflineBoltStateHandler boltStateHandler = new OfflineBoltStateHandler(driverMock);
    boltStateHandler.connect();
    BoltResult boltResult = boltStateHandler.runCypher("RETURN 999", new HashMap<>()).get();
    verify(driverMock, times(2)).session(any());
    verify(sessionMock, times(2)).run(any(Query.class));
    assertEquals("999", boltResult.getRecords().get(0).get(0).toString());
}
Also used : Query(org.neo4j.driver.Query) HashMap(java.util.HashMap) Value(org.neo4j.driver.Value) Driver(org.neo4j.driver.Driver) FakeDriver(org.neo4j.shell.test.bolt.FakeDriver) Record(org.neo4j.driver.Record) SessionExpiredException(org.neo4j.driver.exceptions.SessionExpiredException) Session(org.neo4j.driver.Session) FakeSession(org.neo4j.shell.test.bolt.FakeSession) Result(org.neo4j.driver.Result) Test(org.junit.Test)

Example 7 with Record

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

the class PrettyPrinterTest method printRelationshipsAndNodesWithEscapingForSpecialCharacters.

@Test
public void printRelationshipsAndNodesWithEscapingForSpecialCharacters() {
    // given
    Record record = mock(Record.class);
    Value relVal = mock(Value.class);
    Value nodeVal = mock(Value.class);
    Relationship relationship = mock(Relationship.class);
    HashMap<String, Object> relProp = new HashMap<>();
    relProp.put("prop1", "\"prop1, value\"");
    relProp.put("prop2", "prop2_value");
    Node node = mock(Node.class);
    HashMap<String, Object> nodeProp = new HashMap<>();
    nodeProp.put("prop1", "\"prop1:value\"");
    nodeProp.put("1prop2", "\"\"");
    nodeProp.put("ä", "not-escaped");
    when(relVal.type()).thenReturn(InternalTypeSystem.TYPE_SYSTEM.RELATIONSHIP());
    when(nodeVal.type()).thenReturn(InternalTypeSystem.TYPE_SYSTEM.NODE());
    when(relVal.asRelationship()).thenReturn(relationship);
    when(relationship.type()).thenReturn("RELATIONSHIP,TYPE");
    when(relationship.asMap(anyObject())).thenReturn(unmodifiableMap(relProp));
    when(nodeVal.asNode()).thenReturn(node);
    when(node.labels()).thenReturn(asList("label `1", "label2"));
    when(node.asMap(anyObject())).thenReturn(unmodifiableMap(nodeProp));
    when(record.keys()).thenReturn(asList("rel", "node"));
    when(record.values()).thenReturn(asList(relVal, nodeVal));
    BoltResult result = new ListBoltResult(asList(record), mock(ResultSummary.class));
    // when
    String actual = plainPrinter.format(result);
    // then
    assertThat(actual, is("rel, node" + NEWLINE + "[:`RELATIONSHIP,TYPE` {prop2: prop2_value, prop1: \"prop1, value\"}], " + "(:`label ``1`:label2 {prop1: \"prop1:value\", `1prop2`: \"\", ä: not-escaped})" + NEWLINE));
}
Also used : ListBoltResult(org.neo4j.shell.state.ListBoltResult) HashMap(java.util.HashMap) Relationship(org.neo4j.driver.types.Relationship) Node(org.neo4j.driver.types.Node) Value(org.neo4j.driver.Value) ResultSummary(org.neo4j.driver.summary.ResultSummary) Record(org.neo4j.driver.Record) Matchers.anyObject(org.mockito.Matchers.anyObject) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) BoltResult(org.neo4j.shell.state.BoltResult) ListBoltResult(org.neo4j.shell.state.ListBoltResult) Test(org.junit.Test)

Example 8 with Record

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

the class PrettyPrinterTest method prettyPrintThreeSegmentPath.

@Test
public void prettyPrintThreeSegmentPath() {
    // given
    Record record = mock(Record.class);
    Value value = mock(Value.class);
    Node start = mock(Node.class);
    when(start.labels()).thenReturn(asList("start"));
    when(start.id()).thenReturn(1L);
    Node second = mock(Node.class);
    when(second.labels()).thenReturn(asList("second"));
    when(second.id()).thenReturn(2L);
    Node third = mock(Node.class);
    when(third.labels()).thenReturn(asList("third"));
    when(third.id()).thenReturn(3L);
    Node end = mock(Node.class);
    when(end.labels()).thenReturn(asList("end"));
    when(end.id()).thenReturn(4L);
    Path path = mock(Path.class);
    when(path.start()).thenReturn(start);
    Relationship relationship = mock(Relationship.class);
    when(relationship.type()).thenReturn("RELATIONSHIP_TYPE");
    when(relationship.startNodeId()).thenReturn(1L).thenReturn(3L).thenReturn(3L);
    Path.Segment segment1 = mock(Path.Segment.class);
    when(segment1.start()).thenReturn(start);
    when(segment1.end()).thenReturn(second);
    when(segment1.relationship()).thenReturn(relationship);
    Path.Segment segment2 = mock(Path.Segment.class);
    when(segment2.start()).thenReturn(second);
    when(segment2.end()).thenReturn(third);
    when(segment2.relationship()).thenReturn(relationship);
    Path.Segment segment3 = mock(Path.Segment.class);
    when(segment3.start()).thenReturn(third);
    when(segment3.end()).thenReturn(end);
    when(segment3.relationship()).thenReturn(relationship);
    when(value.type()).thenReturn(InternalTypeSystem.TYPE_SYSTEM.PATH());
    when(value.asPath()).thenReturn(path);
    when(path.iterator()).thenReturn(asList(segment1, segment2, segment3).iterator());
    when(record.keys()).thenReturn(asList("path"));
    when(record.values()).thenReturn(asList(value));
    BoltResult result = new ListBoltResult(singletonList(record), mock(ResultSummary.class));
    // when
    String actual = plainPrinter.format(result);
    // then
    assertThat(actual, is("path" + NEWLINE + "(:start)-[:RELATIONSHIP_TYPE]->" + "(:second)<-[:RELATIONSHIP_TYPE]-(:third)-[:RELATIONSHIP_TYPE]->(:end)" + NEWLINE));
}
Also used : Path(org.neo4j.driver.types.Path) ListBoltResult(org.neo4j.shell.state.ListBoltResult) Node(org.neo4j.driver.types.Node) Relationship(org.neo4j.driver.types.Relationship) Value(org.neo4j.driver.Value) ResultSummary(org.neo4j.driver.summary.ResultSummary) Record(org.neo4j.driver.Record) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) BoltResult(org.neo4j.shell.state.BoltResult) ListBoltResult(org.neo4j.shell.state.ListBoltResult) Test(org.junit.Test)

Example 9 with Record

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

the class SimpleOutputFormatter method formatAndCount.

@Override
public int formatAndCount(@Nonnull BoltResult result, @Nonnull LinePrinter output) {
    Iterator<Record> records = result.iterate();
    int numberOfRows = 0;
    if (records.hasNext()) {
        Record firstRow = records.next();
        output.printOut(String.join(COMMA_SEPARATOR, firstRow.keys()));
        output.printOut(formatRecord(firstRow));
        numberOfRows++;
        while (records.hasNext()) {
            output.printOut(formatRecord(records.next()));
            numberOfRows++;
        }
    }
    return numberOfRows;
}
Also used : Record(org.neo4j.driver.Record)

Example 10 with Record

use of org.neo4j.driver.Record 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

Record (org.neo4j.driver.Record)33 Test (org.junit.Test)21 Value (org.neo4j.driver.Value)21 ListBoltResult (org.neo4j.shell.state.ListBoltResult)18 ResultSummary (org.neo4j.driver.summary.ResultSummary)12 BoltResult (org.neo4j.shell.state.BoltResult)11 InternalRecord (org.neo4j.driver.internal.InternalRecord)10 HashMap (java.util.HashMap)9 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)8 Result (org.neo4j.driver.Result)8 StringContains.containsString (org.hamcrest.core.StringContains.containsString)7 Node (org.neo4j.driver.types.Node)7 Relationship (org.neo4j.driver.types.Relationship)7 Session (org.neo4j.driver.Session)6 ArrayList (java.util.ArrayList)5 Driver (org.neo4j.driver.Driver)5 List (java.util.List)4 DurationValue (org.neo4j.driver.internal.value.DurationValue)4 NodeValue (org.neo4j.driver.internal.value.NodeValue)4 PathValue (org.neo4j.driver.internal.value.PathValue)4