@Test @Graph( value = {"John friend Sara", "John friend Joe", "Sara friend Maria", "Joe friend Steve"}, autoIndexNodes = true) public void intro_examples() throws Exception { try (Transaction ignored = graphdb.beginTx()) { Writer fw = AsciiDocGenerator.getFW(DOCS_TARGET, gen.get().getTitle()); data.get(); fw.append("\nImagine an example graph like the following one:\n\n"); fw.append( AsciiDocGenerator.dumpToSeparateFileWithType( new File(DOCS_TARGET), "intro.graph", AsciidocHelper.createGraphVizWithNodeId("Example Graph", graphdb(), "cypher-intro"))); fw.append( "\nFor example, here is a query which finds a user called John and John's friends (though not " + "his direct friends) before returning both John and any friends-of-friends that are found."); fw.append("\n\n"); String query = "MATCH (john {name: 'John'})-[:friend]->()-[:friend]->(fof) RETURN john, fof "; fw.append( AsciiDocGenerator.dumpToSeparateFileWithType( new File(DOCS_TARGET), "intro.query", createCypherSnippet(query))); fw.append("\nResulting in:\n\n"); fw.append( AsciiDocGenerator.dumpToSeparateFileWithType( new File(DOCS_TARGET), "intro.result", createQueryResultSnippet(engine.execute(query).dumpToString()))); fw.append( "\nNext up we will add filtering to set more parts " + "in motion:\n\nWe take a list of user names " + "and find all nodes with names from this list, match their friends and return " + "only those followed users who have a +name+ property starting with +S+."); query = "MATCH (user)-[:friend]->(follower) WHERE " + "user.name IN ['Joe', 'John', 'Sara', 'Maria', 'Steve'] AND follower.name =~ 'S.*' " + "RETURN user, follower.name "; fw.append("\n\n"); fw.append( AsciiDocGenerator.dumpToSeparateFileWithType( new File(DOCS_TARGET), "intro.query", createCypherSnippet(query))); fw.append("\nResulting in:\n\n"); fw.append( AsciiDocGenerator.dumpToSeparateFileWithType( new File(DOCS_TARGET), "intro.result", createQueryResultSnippet(engine.execute(query).dumpToString()))); fw.close(); } }
@Test public void exampleWithParametersForQuery() throws Exception { // START SNIPPET: exampleWithParametersForQuery Map<String, Object> params = new HashMap<String, Object>(); params.put("query", "name:Andreas"); ExecutionResult result = engine.execute("start n=node:people({query}) return n", params); // END SNIPPET: exampleWithParametersForQuery assertEquals(asList(andreasNode), this.<Node>toList(result, "n")); }
@Test public void exampleWithParameterForMultipleNodeIds() throws Exception { // START SNIPPET: exampleWithParameterForMultipleNodeIds Map<String, Object> params = new HashMap<String, Object>(); params.put("id", Arrays.asList(0, 1, 2)); ExecutionResult result = engine.execute("start n=node({id}) return n.name", params); // END SNIPPET: exampleWithParameterForMultipleNodeIds assertEquals(asList("Michaela", "Andreas", "Johan"), this.<String>toList(result, "n.name")); }
@Test public void exampleWithStringLiteralAsParameter() throws Exception { // START SNIPPET: exampleWithStringLiteralAsParameter Map<String, Object> params = new HashMap<String, Object>(); params.put("name", "Johan"); ExecutionResult result = engine.execute("start n=node(0,1,2) where n.name = {name} return n", params); // END SNIPPET: exampleWithStringLiteralAsParameter assertEquals(asList(johanNode), this.<Node>toList(result, "n")); }
@Test public void exampleQuery() throws Exception { // START SNIPPET: JavaQuery ExecutionEngine engine = new ExecutionEngine(db); ExecutionResult result = engine.execute("start n=node(0) where 1=1 return n"); assertThat(result.columns(), hasItem("n")); Iterator<Node> n_column = result.columnAs("n"); assertThat(asIterable(n_column), hasItem(db.getNodeById(0))); assertThat(result.toString(), containsString("Node[0]")); // END SNIPPET: JavaQuery }
@Test public void exampleWithParameterForNodeObject() throws Exception { // START SNIPPET: exampleWithParameterForNodeObject Map<String, Object> params = new HashMap<String, Object>(); params.put("node", andreasNode); ExecutionResult result = engine.execute("start n=node({node}) return n.name", params); // END SNIPPET: exampleWithParameterForNodeObject assertThat(result.columns(), hasItem("n.name")); Iterator<Object> n_column = result.columnAs("n.name"); assertEquals("Andreas", n_column.next()); }
@Test public void exampleWithParametersForIndexKeyAndValue() throws Exception { // START SNIPPET: exampleWithParametersForIndexKeyAndValue Map<String, Object> params = new HashMap<String, Object>(); params.put("key", "name"); params.put("value", "Michaela"); ExecutionResult result = engine.execute("start n=node:people({key} = {value}) return n", params); // END SNIPPET: exampleWithParametersForIndexKeyAndValue assertEquals(asList(michaelaNode), this.<Node>toList(result, "n")); }
@Test public void testColumnAreInTheRightOrder() throws Exception { createTenNodes(); String q = "start one=node(1), two=node(2), three=node(3), four=node(4), five=node(5), six=node(6), " + "seven=node(7), eight=node(8), nine=node(9), ten=node(10) " + "return one, two, three, four, five, six, seven, eight, nine, ten"; ExecutionResult result = engine.execute(q); assertThat( result.toString(), matchesPattern("one.*two.*three.*four.*five.*six.*seven.*eight.*nine.*ten")); }
@Test public void shouldBeAbleToEmitJavaIterables() throws Exception { makeFriends(michaelaNode, andreasNode); makeFriends(michaelaNode, johanNode); ExecutionEngine engine = new ExecutionEngine(db); ExecutionResult result = engine.execute("start n=node(0) match n-->friend return collect(friend)"); Iterable<Node> friends = (Iterable<Node>) result.columnAs("collect(friend)").next(); assertThat(friends, hasItems(andreasNode, johanNode)); assertThat(friends, instanceOf(Iterable.class)); }
@Test public void exampleWithParameterRegularExpression() throws Exception { // START SNIPPET: exampleWithParameterRegularExpression Map<String, Object> params = new HashMap<String, Object>(); params.put("regex", ".*h.*"); ExecutionResult result = engine.execute("start n=node(0,1,2) where n.name =~ {regex} return n.name", params); // END SNIPPET: exampleWithParameterRegularExpression assertThat(result.columns(), hasItem("n.name")); Iterator<Object> n_column = result.columnAs("n.name"); assertEquals("Michaela", n_column.next()); assertEquals("Johan", n_column.next()); }
@Test public void exampleWithParameterForSkipAndLimit() throws Exception { // START SNIPPET: exampleWithParameterForSkipLimit Map<String, Object> params = new HashMap<String, Object>(); params.put("s", 1); params.put("l", 1); ExecutionResult result = engine.execute("start n=node(0,1,2) return n.name skip {s} limit {l}", params); // END SNIPPET: exampleWithParameterForSkipLimit assertThat(result.columns(), hasItem("n.name")); Iterator<Object> n_column = result.columnAs("n.name"); assertEquals("Andreas", n_column.next()); }
@Test public void shouldCloseTransactionsWhenIteratingOverSingleColumn() throws Exception { // Given an execution result that has been started but not exhausted createNode(); createNode(); ExecutionResult executionResult = engine.execute("MATCH (n) RETURN n"); ResourceIterator<Node> resultIterator = executionResult.columnAs("n"); resultIterator.next(); assertThat(activeTransaction(), is(notNullValue())); // When resultIterator.close(); // Then assertThat(activeTransaction(), is(nullValue())); }