@Test public void shouldHandleMapWithPointsAsInput() { // Given Point point1 = (Point) db.execute( "RETURN point({latitude: 12.78, longitude: 56.7}) as point" ).next().get( "point" ); Point point2 = (Point) db.execute( "RETURN point({latitude: 12.18, longitude: 56.2}) as point" ).next().get( "point" ); // When double distance = (double) db.execute( "RETURN distance({points}['p1'], {points}['p2']) as dist", map( "points", map("p1", point1, "p2", point2) ) ).next().get( "dist" ); // Then assertThat(Math.round( distance ), equalTo(86107L)); }
@SafeVarargs private static Result mockExecutionResult( ExecutionPlanDescription planDescription, Iterable<Notification> notifications, Map<String, Object>... rows ) { Set<String> keys = new TreeSet<>(); for ( Map<String, Object> row : rows ) { keys.addAll( row.keySet() ); } Result executionResult = mock( Result.class ); when( executionResult.columns() ).thenReturn( new ArrayList<>( keys ) ); final Iterator<Map<String, Object>> inner = asList( rows ).iterator(); when( executionResult.hasNext() ).thenAnswer( invocation -> inner.hasNext() ); when( executionResult.next() ).thenAnswer( invocation -> inner.next() ); when( executionResult.getQueryExecutionType() ) .thenReturn( null != planDescription ? QueryExecutionType.profiled( QueryExecutionType.QueryType.READ_WRITE ) : QueryExecutionType.query( QueryExecutionType.QueryType.READ_WRITE ) ); if ( executionResult.getQueryExecutionType().requestedExecutionPlanDescription() ) { when( executionResult.getExecutionPlanDescription() ).thenReturn( planDescription ); } mockAccept( executionResult ); when( executionResult.getNotifications() ).thenReturn( notifications ); return executionResult; }
public CypherResultRepresentation( final Result result, boolean includeStats, boolean includePlan ) { super( RepresentationType.STRING ); resultRepresentation = createResultRepresentation( result ); columns = ListRepresentation.string( result.columns() ); statsRepresentation = includeStats ? new CypherStatisticsRepresentation( result.getQueryStatistics() ) : null; plan = includePlan ? CypherPlanRepresentation.newFromProvider(planProvider(result)) : null; }
ExecutionResultSerializer serializer = getSerializerWith( output ); Map<String, Object> data = map( "column1", "value1", "column2", "value2" ); Result executionResult = mock( Result.class ); mockAccept( executionResult ); when( executionResult.columns() ).thenReturn( new ArrayList<>( data.keySet() ) ); when( executionResult.hasNext() ).thenReturn( true, true, false ); when( executionResult.next() ).thenReturn( data ).thenThrow( new RuntimeException( "Stuff went wrong!" ) );
@Test public void mustBeAbleToConsistencyCheckNodeIndexWithManyLabelsAndOneProperty() throws Exception { GraphDatabaseService db = createDatabase(); // Enough labels to prevent inlining them into the node record, and instead require a dynamic label record to be allocated. String[] labels = {"L1", "L2", "L3", "L4", "L5", "L6", "L7", "L8", "L9", "L10", "L11", "L12", "L13", "L14", "L15", "L16"}; try ( Transaction tx = db.beginTx() ) { db.execute( format( NODE_CREATE, "nodes", array( labels ), array( "prop" ) ) ).close(); tx.success(); } try ( Transaction tx = db.beginTx() ) { db.schema().awaitIndexesOnline( 1, TimeUnit.MINUTES ); db.createNode( Stream.of( labels ).map( Label::label ).toArray( Label[]::new ) ).setProperty( "prop", "value" ); tx.success(); } db.shutdown(); assertIsConsistent( checkConsistency() ); }
@SuppressWarnings( "unchecked" ) @Test public void shouldNotReturnInternalPointWhenInArray() { // when Result execute = graphDb.execute( "RETURN [point({longitude: 144.317718, latitude: -37.031738})] AS ps" ); // then List<Point> points = (List<Point>)execute.next().get( "ps" ); assertThat( points.get(0), Matchers.instanceOf(Point.class)); }
@Test public void shouldHandleColumnAsWithNull() { assertThat( db.execute( "RETURN toLower(null) AS lower" ).<String>columnAs( "lower" ).next(), nullValue() ); }
@Test public void collections_in_collections_look_alright() { Result result = db.execute( "CREATE (n:TheNode) RETURN [[ [1,2],[3,4] ],[[5,6]]] as x" ); Map<String, Object> next = result.next(); @SuppressWarnings( "unchecked" ) //We know it's a collection. List<List<Object>> x = (List<List<Object>>)next.get( "x" ); Iterable objects = x.get( 0 ); assertThat(objects, isA(Iterable.class)); } }
private void assertResultsAreEqual( String query1, String query2 ) { try ( Transaction ignore = db.beginTx() ) { Result result1 = db.execute( query1 ); Result result2 = db.execute( query2 ); while ( result1.hasNext() ) { assertTrue( result2.hasNext() ); assertThat( result1.next(), equalTo( result2.next() ) ); } assertFalse( result2.hasNext() ); } }
private static void addNode( GraphDatabaseService db ) { if ( USE_CYPHER ) { Result result = db.execute( "create (:Person {name: 'Sneaky Steve'})" ); System.out.println( result.resultAsString() ); } else { try ( Transaction tx = db.beginTx() ) { db.createNode( label( "Person" ) ).setProperty( "name", "Sneaky Steve" ); tx.success(); } } }
@Test public void listDenseNodesShouldWork() throws Throwable { // Given new JarBuilder().createJarFor( plugins.newFile( "myProcedures.jar" ), ProcedureExample.class ); db = new TestGraphDatabaseFactory() .newImpermanentDatabaseBuilder() .setConfig( GraphDatabaseSettings.plugin_dir, plugins.getRoot().getAbsolutePath() ) .newGraphDatabase(); try ( Transaction ignore = db.beginTx() ) { Node node1 = db.createNode(); Node node2 = db.createNode(); Node node3 = db.createNode(); node1.createRelationshipTo( node1, RelationshipType.withName( "KNOWS" ) ); node1.createRelationshipTo( node2, RelationshipType.withName( "KNOWS" ) ); node1.createRelationshipTo( node3, RelationshipType.withName( "KNOWS" ) ); // When Result res = db.execute( "CALL org.neo4j.examples.findDenseNodes(2)" ); // Then assertEquals( map( "degree", 3l, "nodeId", node1.getId() ), res.next() ); assertFalse( res.hasNext() ); } }
@Test public void queryNodesMustThrowWhenQueryingRelationshipIndex() { db = createDatabase(); try ( Transaction tx = db.beginTx() ) { createSimpleRelationshipIndex(); tx.success(); } awaitIndexesOnline(); try ( Transaction tx = db.beginTx() ) { expectedException.expect( Exception.class ); db.execute( format( QUERY_NODES, "rels", "bla bla" ) ).close(); tx.success(); } }
@Test public void testCommunity() { db.execute("CREATE (n {id: 0, partition: 1}) " + "CREATE (n)-[:X]->({id: 1, weight: 1.0, partition: 1})" + "CREATE (n)-[:X]->({id: 2, weight: 2.0, partition: 1})" + "CREATE (n)-[:X]->({id: 3, weight: 1.0, partition: 1})" + "CREATE (n)-[:X]->({id: 4, weight: 1.0, partition: 1})" + "CREATE (n)-[:X]->({id: 5, weight: 8.0, partition: 2})" ).close(); db.execute("CALL apoc.algo.community(1,null,'partition','X','OUTGOING','weight',1)").close(); testCall( db, "MATCH (n) WHERE n.id = 0 RETURN n.partition AS partition", (r) -> assertThat(r.get("partition"), equalTo(2L)) ); } }
@Test public void dropMustNotApplyToRegularSchemaIndexes() { db = createDatabase(); try ( Transaction tx = db.beginTx() ) { db.schema().indexFor( LABEL ).on( PROP ).create(); tx.success(); } awaitIndexesOnline(); String schemaIndexName; try ( Transaction ignore = db.beginTx() ) { try ( Result result = db.execute( "call db.indexes" ) ) { assertTrue( result.hasNext() ); schemaIndexName = result.next().get( "indexName" ).toString(); } expectedException.expect( QueryExecutionException.class ); db.execute( format( DROP, schemaIndexName ) ).close(); } }
try ( ResourceIterator<String> iterator = db.execute( LIST_AVAILABLE_ANALYZERS ).columnAs( "analyzer" ) ) assertThat( analyzers, hasItem( "swedish" ) ); assertThat( analyzers, hasItem( "standard" ) ); tx.success(); while ( result.hasNext() ) Map<String,Object> row = result.next(); Object description = row.get( "description" ); if ( !row.containsKey( "description" ) || !(description instanceof String) || ((String) description).trim().isEmpty() ) tx.success();
@Override public String listTransactions() { String res; try ( Transaction tx = graphDatabaseAPI.beginTx() ) { res = graphDatabaseAPI.execute( "CALL dbms.listTransactions()" ).resultAsString(); tx.success(); } catch ( QueryExecutionException e ) { res = "dbms.listTransactions() is not available"; } return res; }
private Object executeStatement(BlockingQueue<RowResult> queue, String stmt, Map<String, Object> params, boolean addStatistics, long timeout) throws InterruptedException { try (Result result = db.execute(stmt,params)) { long time = System.currentTimeMillis(); int row = 0; while (result.hasNext()) { terminationGuard.check(); queue.put(new RowResult(row++, result.next())); } if (addStatistics) { queue.offer(new RowResult(-1, toMap(result.getQueryStatistics(), System.currentTimeMillis() - time, row)), timeout,TimeUnit.SECONDS); } return row; } }