Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active June 19, 2024 22:08
Show Gist options
  • Save dacr/c81b82d5bde0947c5e3f781819cc963e to your computer and use it in GitHub Desktop.
Save dacr/c81b82d5bde0947c5e3f781819cc963e to your computer and use it in GitHub Desktop.
neo4j cypher queries - simple embedded test example with test framework / published by https://github.com/dacr/code-examples-manager #182a2ba7-79e2-4e37-b58f-a4423bfffc11/bb6046247746461aea11ce358aac477fa30bb3d6
// summary : neo4j cypher queries - simple embedded test example with test framework
// keywords : scala, scalatest, neo4j, cypher, @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 : 182a2ba7-79e2-4e37-b58f-a4423bfffc11
// created-on : 2023-06-24T09:20:30+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.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 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 query = "MATCH (n) RETURN count(n) AS count"
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