private Number countStaff() { try ( Result countResult = databaseRule.execute( "MATCH (n:staff) return count(n.name) as count" ) ) { return (Number) countResult.columnAs( "count" ).next(); } }
@Test public void shouldHandleColumnAsWithNull() { assertThat( db.execute( "RETURN toLower(null) AS lower" ).<String>columnAs( "lower" ).next(), nullValue() ); }
@Override public void evaluate() { // Then the database is not empty Result result = ruleWithDirectory.getGraphDatabaseService().execute( "MATCH (n) RETURN count(n) AS " + "count" ); List<Object> column = Iterators.asList( result.columnAs( "count" ) ); assertEquals( 1, column.size() ); assertEquals( 1L, column.get( 0 ) ); } }, null );
private long nodesInDatabase() { Result r = graphdb().execute( "MATCH (n) RETURN count(n) AS c" ); Long nodes = (Long) r.columnAs( "c" ).next(); r.close(); return nodes; }
@Test public void shouldCloseTransactionsWhenIteratingOverSingleColumn() { // Given an execution result that has been started but not exhausted createNode(); createNode(); Result executionResult = db.execute( "CYPHER runtime=interpreted MATCH (n) RETURN n" ); ResourceIterator<Node> resultIterator = executionResult.columnAs( "n" ); resultIterator.next(); assertThat( activeTransaction(), is( notNullValue() ) ); // When resultIterator.close(); // Then assertThat( activeTransaction(), is( nullValue() ) ); }
try ( ResourceIterator<String> iterator = db.execute( LIST_AVAILABLE_ANALYZERS ).columnAs( "analyzer" ) )
@Test public void useColumnAsOnEagerResult() { Result result = database.execute( "MATCH (n) RETURN n.c as c, n.b as b" ); assertEquals( 1, testCursorContext.getAdditionalAttempts() ); ResourceIterator<Object> cValues = result.columnAs( "c" ); int rows = 0; while ( cValues.hasNext() ) { cValues.next(); rows++; } assertEquals( 2, rows ); }
@Test public void testRegexGroups() { testResult(db, "RETURN apoc.text.regexGroups('abc <link xxx1>yyy1</link> def <link xxx2>yyy2</link>','<link (\\\\w+)>(\\\\w+)</link>') AS result", result -> { final List<Object> r = Iterators.single(result.columnAs("result")); List<List<String>> expected = new ArrayList<>(asList( new ArrayList<String>(asList("<link xxx1>yyy1</link>", "xxx1", "yyy1")), new ArrayList<String>(asList("<link xxx2>yyy2</link>", "xxx2", "yyy2")) )); assertTrue(r.containsAll(expected)); }); }
@Test public void testLoadXmlFromZipByUrl() { testResult(db, "call apoc.load.xml('https://github.com/neo4j-contrib/neo4j-apoc-procedures/blob/3.4/src/test/resources/testload.zip?raw=true!xml/books.xml') yield value as catalog\n" + "UNWIND catalog._children as book\n" + "RETURN book.id as id\n", result -> { List<Object> ids = Iterators.asList(result.columnAs("id")); assertTrue(IntStream.rangeClosed(1,12).allMatch(value -> ids.contains(String.format("bk1%02d",value)))); }); }
@Test public void testLoadXmlFromTarGzByUrl() { testResult(db, "call apoc.load.xml('https://github.com/neo4j-contrib/neo4j-apoc-procedures/blob/3.4/src/test/resources/testload.tar.gz?raw=true!xml/books.xml') yield value as catalog\n" + "UNWIND catalog._children as book\n" + "RETURN book.id as id\n", result -> { List<Object> ids = Iterators.asList(result.columnAs("id")); assertTrue(IntStream.rangeClosed(1,12).allMatch(value -> ids.contains(String.format("bk1%02d",value)))); }); }
@Test public void testBookIds() { testResult(db, "call apoc.load.xml('file:src/test/resources/xml/books.xml') yield value as catalog\n" + "UNWIND catalog._children as book\n" + "RETURN book.id as id\n", result -> { List<Object> ids = Iterators.asList(result.columnAs("id")); assertTrue(IntStream.rangeClosed(1,12).allMatch(value -> ids.contains(String.format("bk1%02d",value)))); }); }
@Test public void testRunFirstColumnBugCompiled() throws Exception { ResourceIterator<Node> it = db.execute("CREATE (m:Movie {title:'MovieA'})<-[:ACTED_IN]-(p:Person {name:'PersonA'})-[:ACTED_IN]->(m2:Movie {title:'MovieB'}) RETURN m").columnAs("m"); Node movie = it.next(); it.close(); String query = "WITH {m} AS m MATCH (m)<-[:ACTED_IN]-(:Person)-[:ACTED_IN]->(rec:Movie) RETURN rec LIMIT 10"; System.out.println(db.execute("EXPLAIN "+query).getExecutionPlanDescription().toString()); ResourceIterator<Node> rec = db.execute(query, map("m",movie)).columnAs("rec"); assertEquals(1, rec.stream().count()); } @Test
@Test public void testRelType() { assertEquals("REL", db.execute("CREATE (f)-[rel:REL {foo:'bar'}]->(f) RETURN apoc.rel.type(rel) AS type").columnAs("type").next()); assertEquals("REL", db.execute("CREATE (f) WITH f CALL apoc.create.vRelationship(f,'REL',{foo:'bar'},f) YIELD rel RETURN apoc.rel.type(rel) AS type").columnAs("type").next()); assertNull(db.execute("RETURN apoc.rel.type(null) AS type").columnAs("type").next()); }
@Test public void testNodes() throws Exception { Collection<Object> ids = Iterators.asSet(db.execute("UNWIND range(0,2) as id CREATE (n:Node {id:id}) return id(n) as id").columnAs("id")); TestUtil.testResult(db, "CALL apoc.get.nodes({ids})", map("ids",ids), r -> { assertEquals(true, ids.contains(((Node) r.next().get("node")).getId())); assertEquals(true, ids.contains(((Node) r.next().get("node")).getId())); assertEquals(true, ids.contains(((Node) r.next().get("node")).getId())); }); }
@Test public void shouldSearchInNumericRange() throws Exception { // given execute("UNWIND range(1, 10000) AS num CREATE (:Number{name:'The ' + num + 'th',number:num})"); execute("CALL apoc.index.addAllNodes('numbers', {Number:['name','number']})"); // when ResourceIterator<Object> names = db.execute( "CALL apoc.index.search('numbers', 'Number.number:{100 TO 105]') YIELD node\n" + "RETURN node.name").columnAs("node.name"); // then assertThat(Iterators.asList(names), Matchers.containsInAnyOrder("The 101th", "The 102th", "The 103th", "The 104th", "The 105th")); }
@Test public void testCombinePropertiesTargetSingleValueSourceArrayValuesDifferentType(){ long id = db.execute("Create (d:Person {name:'Daniele'})\n" + "Create (p:Country {name:'USA'})\n" + "Create (d)-[:TRAVELS_TO {year:1995, reason:\"work\"}]->(p)\n" + "Create (d)-[:GOES_TO {year:[\"2010\",\"2015\"], reason:\"fun\"}]->(p)\n" + "Create (d)-[:FLIGHTS_TO {company:\"Air America\"}]->(p) RETURN id(p) as id ").<Long>columnAs("id").next(); testCall(db, QUERY , (r) -> { Relationship rel1 = (Relationship) r.get("rel1"); Relationship rel2 = (Relationship) r.get("rel2"); PropertiesManager.mergeProperties(rel2.getProperties("year"), rel1, new RefactorConfig(map("properties","combine"))); assertEquals(asList("1995","2010", "2015").toArray(), new ArrayBackedList(rel1.getProperty("year")).toArray()); }); }
@Test public void link() throws Exception { db.execute("UNWIND range(1,10) as id CREATE (n:Foo {id:id}) WITH collect(n) as nodes call apoc.nodes.link(nodes,'BAR') RETURN size(nodes) as len").close(); ResourceIterator<Long> it = db.execute("MATCH (n:Foo {id:1})-[r:BAR*9]->() RETURN size(r) as len").columnAs("len"); assertEquals(9L,(long)it.next()); it.close(); } @Test
@Test public void testFilterMax() throws Exception { db.execute("CREATE (:User {name:'Joe',gender:'male'}), (:User {gender:'female',name:'Jane'}), (:User {gender:'female',name:'Jenny'})").close(); assertEquals("male",((Node)db.execute("CALL apoc.nodes.group(['User'],['gender'],null,{filter:{`User.count_*.max`:1}})").columnAs("node").next()).getProperty("gender")); assertFalse(db.execute("CALL apoc.nodes.group(['User'],['gender'],null,{filter:{`User.count_*.max`:0}})").hasNext()); }
@Test public void shouldGetPageRankWithCypherExpectedResult() throws IOException { db.execute( COMPANIES_QUERY ).close(); Result result = db.execute("CALL apoc.algo.pageRankWithCypher({iterations:20, write:true}) "); ResourceIterator<Double> it = db.execute("MATCH (n) RETURN n.name as name, n.pagerank as score ORDER BY score DESC LIMIT 1").columnAs("score"); assertTrue( it.hasNext() ); assertEquals(PageRankAlgoTest.EXPECTED, it.next(), 0.1D); assertFalse( it.hasNext() ); it.close(); }
@Test public void shouldGetPageRankWithCypherExpectedResultWithoutNodes() throws IOException { db.execute( COMPANIES_QUERY_LABEL ).close(); Result result = db.execute("CALL apoc.algo.pageRankWithCypher({iterations:20, write:true, node_cypher:'none', rel_cypher:'MATCH (n:Company)-->(o) RETURN id(n) as source, id(o) as target', weight:null}) "); ResourceIterator<Double> it = db.execute("MATCH (n) WHERE exists(n.pagerank) RETURN n.name as name, n.pagerank as score ORDER BY score DESC LIMIT 1").columnAs("score"); assertTrue( it.hasNext() ); assertEquals( PageRankAlgoTest.EXPECTED, it.next(), 0.1D ); assertFalse( it.hasNext() ); it.close(); } @Test