Last active
February 3, 2026 20:21
-
-
Save dacr/4ac18ae163b9800898ae4444aba6c2fa to your computer and use it in GitHub Desktop.
neo4j cypher dsl queries - simple embedded test example / published by https://github.com/dacr/code-examples-manager #b5e57ba2-a5cb-44f8-b0e1-5efadbe2f684/da8d6a551132677c27cf314efb7476f9d9e8a175
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // summary : neo4j cypher dsl queries - simple embedded test example | |
| // keywords : scala, scalatest, neo4j, cypher, dsl, @testable | |
| // publish : gist | |
| // authors : David Crosson | |
| // license : Apache License Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0.txt) | |
| // id : b5e57ba2-a5cb-44f8-b0e1-5efadbe2f684 | |
| // created-on : 2024-06-19T08:30:46+02:00 | |
| // managed-by : https://github.com/dacr/code-examples-manager | |
| // run-with : scala-cli $file | |
| // --------------------- | |
| //> using scala "3.4.2" | |
| //> using dep "org.neo4j.test:neo4j-harness:5.20.0" | |
| //> using dep "org.neo4j.driver:neo4j-java-driver:5.21.0" | |
| //> using dep "org.neo4j:neo4j-cypher-dsl:2024.0.1" | |
| // --------------------- | |
| import org.neo4j.cypherdsl.core.Cypher | |
| import org.neo4j.cypherdsl.core.renderer.Renderer | |
| import org.neo4j.driver.{AuthTokens, GraphDatabase} | |
| import scala.util.Using | |
| val fixture = | |
| """ | |
| |CREATE (:Person {name: 'Jane'}) | |
| |CREATE (:Person {name: 'Joe'}) | |
| |""".stripMargin | |
| val builder = | |
| org.neo4j.harness.Neo4jBuilders | |
| .newInProcessBuilder() | |
| .withFixture(fixture) | |
| Using(builder.build()) { embedded => | |
| Using(GraphDatabase.driver(embedded.boltURI(), AuthTokens.none())) { driver => | |
| Using(driver.session()) { session => | |
| val node = Cypher.anyNode().named("n") | |
| val statement = | |
| Cypher | |
| .`match`(node) | |
| .returning(Cypher.count(node).as("count")) | |
| .build() | |
| val renderer = Renderer.getDefaultRenderer | |
| val query = renderer.render(statement) | |
| val result = session.run(query) // in tests we don't care about closing session | |
| val count = result.single().get("count").asInt() | |
| println(s"'$query' : count = $count") | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment