Skip to content

Improve test-framework: add support for semantically comparison of cypher queries #324

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
Aug 29, 2024
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
19 changes: 18 additions & 1 deletion core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,13 @@
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-cypher-dsl</artifactId>
<version>2024.0.3</version>
<version>${neo4j-cypher-dsl.version}</version>
</dependency>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-cypher-dsl-parser</artifactId>
<version>${neo4j-cypher-dsl.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.graphql-java</groupId>
Expand Down Expand Up @@ -94,6 +100,17 @@
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.17.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-kotlin</artifactId>
<version>2.17.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<dependencyManagement>
Expand Down
53 changes: 29 additions & 24 deletions core/src/test/kotlin/org/neo4j/graphql/utils/AsciiDocTestSuite.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package org.neo4j.graphql.utils

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
import com.intellij.rt.execution.junit.FileComparisonFailure
import org.junit.jupiter.api.DynamicContainer
import org.junit.jupiter.api.DynamicNode
import org.junit.jupiter.api.DynamicTest
import java.io.File
import java.io.FileWriter
import java.math.BigInteger
import java.net.URI
import java.util.*
import java.util.regex.Pattern
Expand Down Expand Up @@ -57,6 +57,7 @@ open class AsciiDocTestSuite(
var start: Int? = null
var end: Int? = null
var adjustedCode: String? = null
var reformattedCode: String? = null
val code: StringBuilder = StringBuilder()

fun code() = code.trim().toString()
Expand Down Expand Up @@ -153,6 +154,10 @@ open class AsciiDocTestSuite(
this@AsciiDocTestSuite::writeAdjustedTestFile
)
)
} else if (REFORMAT_TEST_FILE) {
root?.afterTests?.add(
DynamicTest.dynamicTest("Reformat Testfile", srcLocation, this@AsciiDocTestSuite::reformatTestFile)
)
} else if (GENERATE_TEST_FILE_DIFF) {
// this test prints out the adjusted test file
root?.afterTests?.add(
Expand Down Expand Up @@ -207,29 +212,40 @@ open class AsciiDocTestSuite(
}
}

private fun reformatTestFile() {
val content = generateAdjustedFileContent { it.reformattedCode }
FileWriter(File("src/test/resources/", fileName)).use {
it.write(content)
}
}

private fun printAdjustedTestFile() {
val rebuildTest = generateAdjustedFileContent()
if (!Objects.equals(rebuildTest, fileContent.toString())) {
// This special exception will be handled by intellij so that you can diff directly with the file
throw FileComparisonFailure(
null, rebuildTest, fileContent.toString(),
null, File("src/test/resources/", fileName).absolutePath
null, fileContent.toString(), rebuildTest,
File("src/test/resources/", fileName).absolutePath, null
)
}
}

private fun generateAdjustedFileContent(): String {
private fun generateAdjustedFileContent(extractor: (ParsedBlock) -> String? = { it.adjustedCode }): String {
knownBlocks.sortWith(compareByDescending<ParsedBlock> { it.start }
.thenByDescending { testCaseMarkers.indexOf(it.marker) })
val rebuildTest = StringBuffer(fileContent)
knownBlocks.filter { it.adjustedCode != null }.forEach { block ->
val start = block.start ?: throw IllegalArgumentException("unknown start position")
if (block.end == null) {
rebuildTest.insert(start, ".${block.headline}\n${block.marker}\n----\n${block.adjustedCode}\n----\n\n")
} else {
rebuildTest.replace(start, block.end!!, block.adjustedCode + "\n")
knownBlocks.filter { extractor(it) != null }
.forEach { block ->
val start = block.start ?: throw IllegalArgumentException("unknown start position")
if (block.end == null) {
rebuildTest.insert(
start,
".${block.headline}\n${block.marker}\n----\n${extractor(block)}\n----\n\n"
)
} else {
rebuildTest.replace(start, block.end!!, extractor(block) + "\n")
}
}
}
return rebuildTest.toString()
}

Expand Down Expand Up @@ -278,8 +294,9 @@ open class AsciiDocTestSuite(
*/
val FLATTEN_TESTS = System.getProperty("neo4j-graphql-java.flatten-tests", "false") == "true"
val GENERATE_TEST_FILE_DIFF = System.getProperty("neo4j-graphql-java.generate-test-file-diff", "true") == "true"
val REFORMAT_TEST_FILE = System.getProperty("neo4j-graphql-java.reformat", "false") == "true"
val UPDATE_TEST_FILE = System.getProperty("neo4j-graphql-java.update-test-file", "false") == "true"
val MAPPER = ObjectMapper()
val MAPPER = ObjectMapper().registerKotlinModule()
val HEADLINE_PATTERN: Pattern = Pattern.compile("^(=+) (.*)$")

const val SCHEMA_MARKER = "[source,graphql,schema=true]"
Expand Down Expand Up @@ -330,18 +347,6 @@ open class AsciiDocTestSuite(
}
}

fun fixNumber(v: Any?): Any? = when (v) {
is Float -> v.toDouble()
is Int -> v.toLong()
is BigInteger -> v.toLong()
is Iterable<*> -> v.map { fixNumber(it) }
is Sequence<*> -> v.map { fixNumber(it) }
is Map<*, *> -> v.mapValues { fixNumber(it.value) }
else -> v
}

fun fixNumbers(params: Map<String, Any?>) = params.mapValues { (_, v) -> fixNumber(v) }

fun String.parseJsonMap(): Map<String, Any?> = this.let {
@Suppress("UNCHECKED_CAST")
MAPPER.readValue(this, Map::class.java) as Map<String, Any?>
Expand Down
Loading
Loading