Skip to content
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

Add ability to assert result of integration test with ignoring the order of lists #209

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
2 changes: 1 addition & 1 deletion .github/workflows/pr-build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
java-version: 11
distribution: adopt
- name: Run Maven build
run: ./mvnw --no-transfer-progress -Dneo4j-graphql-java.integration-tests=true -Dneo4j-graphql-java.generate-test-file-diff=false clean compile test
run: ./mvnw --no-transfer-progress -Dneo4j-graphql-java.integration-tests=true -Dneo4j-graphql-java.generate-test-file-diff=false -Dneo4j-graphql-java.flatten-tests=true clean compile test
- name: Publish Unit Test Results
uses: EnricoMi/publish-unit-test-result-action@v1
if: always()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,20 @@ open class AsciiDocTestSuite(
*/
private val knownBlocks: MutableList<ParsedBlock> = mutableListOf()

fun generateTests(): Stream<DynamicNode> = FileParser().parse()
fun generateTests(): Stream<DynamicNode> {
val stream = FileParser().parse()
return if (FLATTEN_TESTS) flatten(stream, "$fileName:") else stream
}

private fun flatten(stream: Stream<out DynamicNode>, name: String): Stream<DynamicNode> {
return stream.flatMap {
when (it) {
is DynamicContainer -> flatten(it.children, "$name[${it.displayName}]")
is DynamicTest -> Stream.of(DynamicTest.dynamicTest("$name[${it.displayName}]", it.executable))
else -> throw IllegalArgumentException("unknown type ${it.javaClass.name}")
}
}
}

class ParsedBlock(
val marker: String,
Expand Down Expand Up @@ -227,6 +240,10 @@ open class AsciiDocTestSuite(
}

companion object {
/**
* to find broken tests easy by its console output, enable this feature
*/
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 UPDATE_TEST_FILE = System.getProperty("neo4j-graphql-java.update-test-file", "false") == "true"
val MAPPER = ObjectMapper()
Expand Down
39 changes: 35 additions & 4 deletions core/src/test/kotlin/org/neo4j/graphql/utils/CypherTestSuite.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import graphql.schema.DataFetcher
import graphql.schema.DataFetchingEnvironment
import graphql.schema.GraphQLSchema
import org.assertj.core.api.Assertions
import org.assertj.core.api.InstanceOfAssertFactories
import org.junit.jupiter.api.Assumptions
import org.junit.jupiter.api.DynamicNode
import org.junit.jupiter.api.DynamicTest
Expand All @@ -14,6 +15,7 @@ import org.neo4j.harness.Neo4j
import org.opentest4j.AssertionFailedError
import java.util.*
import java.util.concurrent.FutureTask
import java.util.function.Consumer
import kotlin.streams.toList

class CypherTestSuite(fileName: String, val neo4j: Neo4j? = null) : AsciiDocTestSuite(
Expand All @@ -23,6 +25,7 @@ class CypherTestSuite(fileName: String, val neo4j: Neo4j? = null) : AsciiDocTest
GRAPHQL_MARKER,
GRAPHQL_VARIABLES_MARKER,
GRAPHQL_RESPONSE_MARKER,
GRAPHQL_RESPONSE_IGNORE_ORDER_MARKER,
QUERY_CONFIG_MARKER,
CYPHER_PARAMS_MARKER,
CYPHER_MARKER
Expand All @@ -48,9 +51,15 @@ class CypherTestSuite(fileName: String, val neo4j: Neo4j? = null) : AsciiDocTest
}
if (neo4j != null) {
val testData = globalBlocks[TEST_DATA_MARKER]
val response = getOrCreateBlock(codeBlocks, GRAPHQL_RESPONSE_MARKER, "GraphQL-Response")
var response = codeBlocks[GRAPHQL_RESPONSE_IGNORE_ORDER_MARKER]
var ignoreOrder = false;
if (response != null) {
ignoreOrder = true;
} else {
response = getOrCreateBlock(codeBlocks, GRAPHQL_RESPONSE_MARKER, "GraphQL-Response")
}
if (testData != null && response != null) {
tests.add(integrationTest(title, globalBlocks, codeBlocks, testData, response))
tests.add(integrationTest(title, globalBlocks, codeBlocks, testData, response, ignoreOrder))
}
}

Expand Down Expand Up @@ -183,7 +192,8 @@ class CypherTestSuite(fileName: String, val neo4j: Neo4j? = null) : AsciiDocTest
globalBlocks: Map<String, ParsedBlock>,
codeBlocks: Map<String, ParsedBlock>,
testData: ParsedBlock,
response: ParsedBlock
response: ParsedBlock,
ignoreOrder: Boolean
): DynamicNode = DynamicTest.dynamicTest("Integration Test", response.uri) {
val dataFetchingInterceptor = setupDataFetchingInterceptor(testData)
val request = codeBlocks[GRAPHQL_MARKER]?.code()
Expand Down Expand Up @@ -218,7 +228,27 @@ class CypherTestSuite(fileName: String, val neo4j: Neo4j? = null) : AsciiDocTest
val actualCode = MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(values)
response.adjustedCode = actualCode
}
Assertions.assertThat(actual).isEqualTo(expected)
if (ignoreOrder) {
assertEqualIgnoreOrder(expected, actual)
} else {
Assertions.assertThat(actual).isEqualTo(expected)
}
}
}

private fun assertEqualIgnoreOrder(expected: Any?, actual: Any?) {
when (expected) {
is Map<*, *> -> Assertions.assertThat(actual).asInstanceOf(InstanceOfAssertFactories.MAP)
.hasSize(expected.size)
.containsOnlyKeys(*expected.keys.toTypedArray())
.satisfies { it.forEach { (key, value) -> assertEqualIgnoreOrder(expected[key], value) } }
is Collection<*> -> {
val assertions: List<Consumer<Any>> = expected.map{ e -> Consumer<Any> { a -> assertEqualIgnoreOrder(e, a) } }
Assertions.assertThat(actual).asInstanceOf(InstanceOfAssertFactories.LIST)
.hasSize(expected.size)
.satisfiesExactlyInAnyOrder(*assertions.toTypedArray())
}
else -> Assertions.assertThat(actual).isEqualTo(expected)
}
}

Expand All @@ -230,6 +260,7 @@ class CypherTestSuite(fileName: String, val neo4j: Neo4j? = null) : AsciiDocTest
private const val GRAPHQL_MARKER = "[source,graphql]"
private const val GRAPHQL_VARIABLES_MARKER = "[source,json,request=true]"
private const val GRAPHQL_RESPONSE_MARKER = "[source,json,response=true]"
private const val GRAPHQL_RESPONSE_IGNORE_ORDER_MARKER = "[source,json,response=true,ignore-order]"
private const val QUERY_CONFIG_MARKER = "[source,json,query-config=true]"
private const val CYPHER_PARAMS_MARKER = "[source,json]"
}
Expand Down
Loading