Skip to content

make tests using the same database instance for all integration tests #204

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 54 additions & 13 deletions core/src/test/kotlin/org/neo4j/graphql/CypherTests.kt
Original file line number Diff line number Diff line change
@@ -1,43 +1,84 @@
package org.neo4j.graphql

import org.junit.jupiter.api.TestFactory
import apoc.cypher.CypherFunctions
import org.junit.jupiter.api.*
import org.neo4j.graphql.utils.CypherTestSuite
import org.neo4j.harness.Neo4j
import org.neo4j.harness.Neo4jBuilders
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import java.util.stream.Stream

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class CypherTests {

private var neo4j: Neo4j? = null

@BeforeAll
fun setup() {
if (INTEGRATION_TESTS) {
neo4j = Neo4jBuilders
.newInProcessBuilder(Path.of("target/test-db"))
.withProcedure(apoc.cypher.Cypher::class.java)
.withFunction(CypherFunctions::class.java)
.build()
}
}

@AfterAll
fun tearDown() {
neo4j?.close()
}

@TestFactory
fun `cypher-directive-tests`() = CypherTestSuite("cypher-directive-tests.adoc").generateTests()
fun `cypher-directive-tests`() = CypherTestSuite("cypher-directive-tests.adoc", neo4j).generateTests()

@TestFactory
fun `dynamic-property-tests`() = CypherTestSuite("dynamic-property-tests.adoc").generateTests()
fun `dynamic-property-tests`() = CypherTestSuite("dynamic-property-tests.adoc", neo4j).generateTests()

@TestFactory
fun `filter-tests`() = CypherTestSuite("filter-tests.adoc").generateTests()
fun `filter-tests`() = CypherTestSuite("filter-tests.adoc", neo4j).generateTests()

@TestFactory
fun `relationship-tests`() = CypherTestSuite("relationship-tests.adoc").generateTests()
fun `relationship-tests`() = CypherTestSuite("relationship-tests.adoc", neo4j).generateTests()

@TestFactory
fun `movie-tests`() = CypherTestSuite("movie-tests.adoc").generateTests()
fun `movie-tests`() = CypherTestSuite("movie-tests.adoc", neo4j).generateTests()

@TestFactory
fun `property-tests`() = CypherTestSuite("property-tests.adoc").generateTests()
fun `property-tests`() = CypherTestSuite("property-tests.adoc", neo4j).generateTests()

@TestFactory
fun `translator-tests1`() = CypherTestSuite("translator-tests1.adoc").generateTests()
fun `translator-tests1`() = CypherTestSuite("translator-tests1.adoc", neo4j).generateTests()

@TestFactory
fun `translator-tests2`() = CypherTestSuite("translator-tests2.adoc").generateTests()
fun `translator-tests2`() = CypherTestSuite("translator-tests2.adoc", neo4j).generateTests()

@TestFactory
fun `translator-tests3`() = CypherTestSuite("translator-tests3.adoc").generateTests()
fun `translator-tests3`() = CypherTestSuite("translator-tests3.adoc", neo4j).generateTests()

@TestFactory
fun `translator-tests-custom-scalars`() = CypherTestSuite("translator-tests-custom-scalars.adoc").generateTests()
fun `translator-tests-custom-scalars`() = CypherTestSuite("translator-tests-custom-scalars.adoc", neo4j).generateTests()

@TestFactory
fun `optimized-query-for-filter`() = CypherTestSuite("optimized-query-for-filter.adoc").generateTests()
fun `optimized-query-for-filter`() = CypherTestSuite("optimized-query-for-filter.adoc", neo4j).generateTests()

@TestFactory
fun `custom-fields`() = CypherTestSuite("custom-fields.adoc").generateTests()
fun `custom-fields`() = CypherTestSuite("custom-fields.adoc", neo4j).generateTests()

@TestFactory
fun `test issues`(): Stream<DynamicNode>? = Files
.list(Paths.get("src/test/resources/issues"))
.map {
DynamicContainer.dynamicContainer(
it.fileName.toString(),
it.toUri(),
CypherTestSuite("issues/${it.fileName}", neo4j).generateTests()
)
}

companion object {
private val INTEGRATION_TESTS = System.getProperty("neo4j-graphql-java.integration-tests", "false") == "true"
}
}
23 changes: 0 additions & 23 deletions core/src/test/kotlin/org/neo4j/graphql/IssuesTests.kt

This file was deleted.

65 changes: 28 additions & 37 deletions core/src/test/kotlin/org/neo4j/graphql/utils/CypherTestSuite.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package org.neo4j.graphql.utils

import apoc.cypher.CypherFunctions
import org.assertj.core.api.Assertions
import org.junit.jupiter.api.Assumptions
import org.junit.jupiter.api.DynamicNode
import org.junit.jupiter.api.DynamicTest
import org.neo4j.graphql.*
import org.neo4j.harness.Neo4jBuilders
import org.neo4j.harness.Neo4j
import org.opentest4j.AssertionFailedError
import java.nio.file.Path
import java.util.*
import java.util.concurrent.FutureTask
import kotlin.streams.toList

class CypherTestSuite(fileName: String) : AsciiDocTestSuite(
class CypherTestSuite(fileName: String, val neo4j: Neo4j? = null) : AsciiDocTestSuite(
fileName,
listOf(
SCHEMA_CONFIG_MARKER,
Expand Down Expand Up @@ -43,7 +41,7 @@ class CypherTestSuite(fileName: String) : AsciiDocTestSuite(
tests.add(printGeneratedQuery(result))
tests.add(printReplacedParameter(result))
}
if (INTEGRATION_TESTS) {
if (neo4j != null) {
val testData = globalBlocks[TEST_DATA_MARKER]
val response = getOrCreateBlock(codeBlocks, GRAPHQL_RESPONSE_MARKER, "GraphQL-Response")

Expand Down Expand Up @@ -142,48 +140,41 @@ class CypherTestSuite(fileName: String) : AsciiDocTestSuite(
}

private fun integrationTest(testData: ParsedBlock, response: ParsedBlock, result: () -> Cypher): DynamicNode = DynamicTest.dynamicTest("Integration Test", response.uri) {
Neo4jBuilders
.newInProcessBuilder(Path.of("target/test-db"))
.withProcedure(apoc.cypher.Cypher::class.java)
.withFunction(CypherFunctions::class.java)
.also { builder ->
if (testData.code().isNotBlank()) {
testData.code()
.split(";")
.filter { it.isNotBlank() }
.forEach { builder.withFixture(it) }
}
neo4j?.defaultDatabaseService()?.let { db ->
db.executeTransactionally("MATCH (n) DETACH DELETE n")
if (testData.code().isNotBlank()) {
testData.code()
.split(";")
.filter { it.isNotBlank() }
.forEach { db.executeTransactionally(it) }
}
val (cypher, params, type, variable) = result()
val values = db.executeTransactionally(cypher, params) { result ->
mutableMapOf(variable to result.stream().map { it[variable] }.let {
when {
type?.isList() == true -> it.toList()
else -> it.findFirst().orElse(null)
}
})
}
.build()
.use { neo4j ->
val (cypher, params, type, variable) = result()
val values = neo4j.defaultDatabaseService().executeTransactionally(cypher, params) { result ->
mutableMapOf(variable to result.stream().map { it[variable] }.let {
when {
type?.isList() == true -> it.toList()
else -> it.findFirst().orElse(null)
}
})
}

if (response.code.isEmpty()) {
if (response.code.isEmpty()) {
val actualCode = MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(values)
response.adjustedCode = actualCode
} else {
val expected = fixNumbers(response.code().parseJsonMap())
val actual = fixNumber(values)
if (!Objects.equals(expected, actual)) {
val actualCode = MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(values)
response.adjustedCode = actualCode
} else {
val expected = fixNumbers(response.code().parseJsonMap())
val actual = fixNumber(values)
if (!Objects.equals(expected, actual)) {
val actualCode = MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(values)
response.adjustedCode = actualCode
}
Assertions.assertThat(actual).isEqualTo(expected)
}
Assertions.assertThat(actual).isEqualTo(expected)
}
}
}

companion object {
private val DEBUG = System.getProperty("neo4j-graphql-java.debug", "false") == "true"
private val INTEGRATION_TESTS = System.getProperty("neo4j-graphql-java.integration-tests", "false") == "true"

private const val TEST_DATA_MARKER = "[source,cypher,test-data=true]"
private const val CYPHER_MARKER = "[source,cypher]"
Expand Down