public void applyTo( InProcessServerControls controls ) { GraphDatabaseService db = controls.graph(); for ( String fixtureStatement : fixtureStatements ) { try ( Transaction tx = db.beginTx() ) { db.execute( fixtureStatement ); tx.success(); } } for ( Function<GraphDatabaseService,Void> fixtureFunction : fixtureFunctions ) { fixtureFunction.apply( db ); } } }
@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)); } }
@Test public void shouldFormatMapsProperly() throws Exception { GraphDatabaseService graphdb = database.getGraphDatabaseAPI(); Result result = graphdb.execute( "RETURN {one:{two:['wait for it...', {three: 'GO!'}]}}" ); CypherResultRepresentation representation = new CypherResultRepresentation( result, false, false ); // When Map<String, Object> serialized = serializeToStringThenParseAsToMap( representation ); // Then Map one = (Map) ((Map) ((List) ((List) serialized.get( "data" )).get( 0 )).get( 0 )).get( "one" ); List two = (List) one.get( "two" ); assertThat( two.get( 0 ), is( "wait for it..." ) ); Map foo = (Map) two.get( 1 ); assertThat( foo.get( "three" ), is( "GO!" ) ); }
@Test public void shouldFailIfTryingToReturnLabelsOfDeletedNodeGraph() { // given graphdb().execute( "CREATE (:NodeToDelete)" ); // execute and commit Response commit = http.POST( commitResource, queryAsJsonGraph( "MATCH (n:NodeToDelete) DELETE n RETURN labels(n)" ) ); assertThat( commit, hasErrors( Status.Statement.EntityNotFound ) ); assertThat( commit.status(), equalTo( 200 ) ); assertThat( nodesInDatabase(), equalTo( 1L ) ); }
@Test public void consistencyCheckerMustBeAbleToRunOnStoreWithFulltextIndexes() throws Exception RandomValues randomValues = RandomValues.create(); try ( Transaction tx = db.beginTx() ) try ( Transaction tx = db.beginTx() ) db.execute( format( NODE_CREATE, "nodes" + i, array( Arrays.stream( labels ).limit( i ).map( Label::name ).toArray( String[]::new ) ), array( Arrays.copyOf( propertyKeys, i ) ) ) ).close(); db.execute( format( RELATIONSHIP_CREATE, "rels" + i, array( Arrays.stream( relTypes ).limit( i ).map( RelationshipType::name ).toArray( String[]::new ) ), array( Arrays.copyOf( propertyKeys, i ) ) ) ).close(); try ( Transaction tx = db.beginTx() )
@Test( expected = QueryExecutionException.class ) public void dirtyContextDuringResultVisitResultInUnstableSnapshotException() throws Exception { Result result = database.execute( "MATCH (n) RETURN n.c" ); List<String> values = new ArrayList<>(); result.accept( (Result.ResultVisitor<Exception>) row -> { testCursorContext.markAsDirty(); values.add( row.getString( "n.c" ) ); return false; } ); }
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 long executeNumericResultStatement(@Name("statement") String statement, @Name("params") Map<String, Object> parameters) { long sum = 0; try (Result result = db.execute(statement, parameters)) { while (result.hasNext()) { Collection<Object> row = result.next().values(); for (Object value : row) { if (value instanceof Number) { sum += ((Number)value).longValue(); } } } } return sum; }
static void assertQueryFindsIds( GraphDatabaseService db, boolean queryNodes, String index, String query, long... ids ) { try ( Transaction tx = db.beginTx() ) { String queryCall = queryNodes ? QUERY_NODES : QUERY_RELS; Result result = db.execute( format( queryCall, index, query ) ); int num = 0; Double score = Double.MAX_VALUE; while ( result.hasNext() ) { Map entry = result.next(); Long nextId = ((Entity) entry.get( queryNodes ? NODE : RELATIONSHIP )).getId(); Double nextScore = (Double) entry.get( SCORE ); assertThat( nextScore, lessThanOrEqualTo( score ) ); score = nextScore; if ( num < ids.length ) { assertEquals( format( "Result returned id %d, expected %d", nextId, ids[num] ), ids[num], nextId.longValue() ); } else { fail( format( "Result returned id %d, which is beyond the number of ids (%d) that were expected.", nextId, ids.length ) ); } num++; } assertEquals( "Number of results differ from expected", ids.length, num ); tx.success(); } }
@Test public void shouldRenderNestedEntities() throws Exception { try ( Transaction ignored = database.getGraphDatabaseAPI().beginTx() ) { GraphDatabaseService graphdb = database.getGraphDatabaseAPI(); graphdb.execute( "CREATE (n {name: 'Sally'}), (m {age: 42}), (n)-[r:FOO {drunk: false}]->(m)" ); Result result = graphdb.execute( "MATCH p=(n)-[r]->(m) RETURN n, r, p, {node: n, edge: r, path: p}" ); CypherResultRepresentation representation = new CypherResultRepresentation( result, false, false ); // When Map<String, Object> serialized = serializeToStringThenParseAsToMap( representation ); // Then Object firstRow = ((List) serialized.get( "data" )).get( 0 ); Map nested = (Map) ((List) firstRow).get( 3 ); assertThat( nested.get( "node" ), is( equalTo( ((List) firstRow).get( 0 ) ) ) ); assertThat( nested.get( "edge" ), is( equalTo( ((List) firstRow).get( 1 ) ) ) ); assertThat( nested.get( "path" ), is( equalTo( ((List) firstRow).get( 2 ) ) ) ); } }
@Test public void shouldFailIfTryingToReturnPropsOfDeletedRelationshipRow() { // given graphdb().execute( "CREATE (:Start)-[:R {p: 'a property'}]->(:End)" ); // execute and commit Response commit = http.POST( commitResource, queryAsJsonRow( "MATCH (s)-[r:R]->(e) DELETE r RETURN r.p" ) ); assertThat( commit, hasErrors( Status.Statement.EntityNotFound ) ); assertThat( commit.status(), equalTo( 200 ) ); assertThat( nodesInDatabase(), equalTo( 2L ) ); }
@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() ); }
@Test( expected = QueryExecutionException.class ) public void dirtyContextEntityNotFoundExceptionDuringResultVisitResultInUnstableSnapshotException() throws Exception { Result result = database.execute( "MATCH (n) RETURN n.c" ); result.accept( (Result.ResultVisitor<Exception>) row -> { testCursorContext.markAsDirty(); throw new NotFoundException( new RuntimeException() ); } ); }
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; } }
long[] expectedIds = ids.toArray(); MutableLongSet actualIds = new LongHashSet(); try ( Transaction tx = db.beginTx() ) Result result = db.execute( format( queryCall, index, query ) ); Double score = Double.MAX_VALUE; while ( result.hasNext() ) Map entry = result.next(); long nextId = ((Entity) entry.get( queryNodes ? NODE : RELATIONSHIP )).getId(); Double nextScore = (Double) entry.get( SCORE ); assertThat( nextScore, lessThanOrEqualTo( score ) ); score = nextScore;