Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active June 19, 2024 22:07
Show Gist options
  • Save dacr/46cab84aacacb58fbff6b9dda0cfcf9d to your computer and use it in GitHub Desktop.
Save dacr/46cab84aacacb58fbff6b9dda0cfcf9d to your computer and use it in GitHub Desktop.
neo4j cypher dsl queries - simple embedded test example with test framework / published by https://github.com/dacr/code-examples-manager #6f755353-e1d6-4928-843d-1d6cbf833455/be5149fb30d7ef707ff856f1d8f052497605f397
// summary : neo4j cypher dsl queries - simple embedded test example with test framework
// keywords : scala, scalatest, neo4j, cypher, dsl, @testable
// publish : gist
// authors : David Crosson
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2)
// id : 6f755353-e1d6-4928-843d-1d6cbf833455
// created-on : 2023-06-24T10:24:54+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"
//> using dep "org.scalatest::scalatest:3.2.18"
// ---------------------
import org.scalatest.*
import flatspec.*
import matchers.*
import org.neo4j.harness.{Neo4j, Neo4jBuilders}
import org.neo4j.driver.{AuthTokens, Driver, GraphDatabase}
import org.neo4j.cypherdsl.core.*
import org.neo4j.cypherdsl.core.renderer.Renderer
import scala.compiletime.uninitialized
import scala.util.Using
object Neo4jCypherTest extends AnyFlatSpec with should.Matchers with BeforeAndAfterAll {
val fixture =
"""
|CREATE (:Person {name: 'Jane'})
|CREATE (:Person {name: 'Joe'})
|""".stripMargin
var embedded: Neo4j = uninitialized
var driver: Driver = uninitialized
override def beforeAll(): Unit = {
val builder = Neo4jBuilders.newInProcessBuilder().withFixture(fixture)
embedded = builder.build()
driver = GraphDatabase.driver(embedded.boltURI(), AuthTokens.none())
}
override def afterAll(): Unit = {
driver.close()
embedded.close()
}
"Cypher count query" should "return the right number of nodes" in {
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 = driver.session().run(query) // in tests we don't care about closing session
val count = result.single().get("count").asInt()
count shouldBe 2
}
}
Neo4jCypherTest.execute()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment