Search in sources :

Example 21 with Record

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

the class BoltStateHandlerTest method shouldRunCypherQuery.

@Test
public void shouldRunCypherQuery() throws CommandException {
    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))).thenReturn(resultMock);
    OfflineBoltStateHandler boltStateHandler = new OfflineBoltStateHandler(driverMock);
    boltStateHandler.connect();
    BoltResult boltResult = boltStateHandler.runCypher("RETURN 999", new HashMap<>()).get();
    verify(sessionMock).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) Session(org.neo4j.driver.Session) FakeSession(org.neo4j.shell.test.bolt.FakeSession) Result(org.neo4j.driver.Result) Test(org.junit.Test)

Example 22 with Record

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

the class CypherShellTest method setParamShouldAddParam.

@Test
public void setParamShouldAddParam() throws ParameterException, CommandException {
    Value value = mock(Value.class);
    Record recordMock = mock(Record.class);
    BoltResult boltResult = mock(ListBoltResult.class);
    when(mockedBoltStateHandler.runCypher(anyString(), anyMap())).thenReturn(Optional.of(boltResult));
    when(boltResult.getRecords()).thenReturn(asList(recordMock));
    when(recordMock.get("bob")).thenReturn(value);
    when(value.asObject()).thenReturn("99");
    assertTrue(offlineTestShell.getParameterMap().allParameterValues().isEmpty());
    Object result = offlineTestShell.getParameterMap().setParameter("`bob`", "99");
    assertEquals(99L, result);
    assertEquals(99L, offlineTestShell.getParameterMap().allParameterValues().get("bob"));
}
Also used : Value(org.neo4j.driver.Value) Record(org.neo4j.driver.Record) Mockito.anyObject(org.mockito.Mockito.anyObject) BoltResult(org.neo4j.shell.state.BoltResult) ListBoltResult(org.neo4j.shell.state.ListBoltResult) Test(org.junit.Test)

Example 23 with Record

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

the class TableOutputFormatter method formatResultAndCountRows.

private int formatResultAndCountRows(String[] columns, Iterator<Record> records, LinePrinter output) {
    List<Record> topRecords = take(records, numSampleRows);
    int[] columnSizes = calculateColumnSizes(columns, topRecords, records.hasNext());
    int totalWidth = 1;
    for (int columnSize : columnSizes) {
        totalWidth += columnSize + 3;
    }
    StringBuilder builder = new StringBuilder(totalWidth);
    String headerLine = formatRow(builder, columnSizes, columns, new boolean[columnSizes.length]);
    int lineWidth = totalWidth - 2;
    String dashes = "+" + OutputFormatter.repeat('-', lineWidth) + "+";
    output.printOut(dashes);
    output.printOut(headerLine);
    output.printOut(dashes);
    int numberOfRows = 0;
    for (Record record : topRecords) {
        output.printOut(formatRecord(builder, columnSizes, record));
        numberOfRows++;
    }
    while (records.hasNext()) {
        output.printOut(formatRecord(builder, columnSizes, records.next()));
        numberOfRows++;
    }
    output.printOut(String.format("%s%n", dashes));
    return numberOfRows;
}
Also used : InternalRecord(org.neo4j.driver.internal.InternalRecord) Record(org.neo4j.driver.Record)

Example 24 with Record

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

the class TableOutputFormatter method formatInfo.

@Override
@Nonnull
public String formatInfo(@Nonnull ResultSummary summary) {
    Map<String, Value> info = OutputFormatter.info(summary);
    if (info.isEmpty()) {
        return "";
    }
    String[] columns = info.keySet().toArray(new String[0]);
    StringBuilder sb = new StringBuilder();
    Record record = new InternalRecord(asList(columns), info.values().toArray(new Value[0]));
    formatResultAndCountRows(columns, Collections.singletonList(record).iterator(), line -> sb.append(line).append(OutputFormatter.NEWLINE));
    return sb.toString();
}
Also used : InternalRecord(org.neo4j.driver.internal.InternalRecord) Value(org.neo4j.driver.Value) InternalRecord(org.neo4j.driver.internal.InternalRecord) Record(org.neo4j.driver.Record) Nonnull(javax.annotation.Nonnull)

Example 25 with Record

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

the class PrettyPrinterTest method prettyPrintSingleNodePath.

@Test
public void prettyPrintSingleNodePath() {
    // 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 end = mock(Node.class);
    when(end.labels()).thenReturn(asList("end"));
    when(end.id()).thenReturn(2L);
    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);
    Path.Segment segment1 = mock(Path.Segment.class);
    when(segment1.start()).thenReturn(start);
    when(segment1.end()).thenReturn(end);
    when(segment1.relationship()).thenReturn(relationship);
    when(value.type()).thenReturn(InternalTypeSystem.TYPE_SYSTEM.PATH());
    when(value.asPath()).thenReturn(path);
    when(path.iterator()).thenReturn(asList(segment1).iterator());
    when(record.keys()).thenReturn(asList("path"));
    when(record.values()).thenReturn(asList(value));
    BoltResult result = new ListBoltResult(asList(record), mock(ResultSummary.class));
    // when
    String actual = plainPrinter.format(result);
    // then
    assertThat(actual, is("path" + NEWLINE + "(:start)-[: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)

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