Skip to content

Commit 13f8fd7

Browse files
authored
Move validation tests to apollo-ast (#6868)
1 parent 7add149 commit 13f8fd7

File tree

184 files changed

+205
-107
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

184 files changed

+205
-107
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.apollographql.apollo.graphql.ast.test
2+
3+
import com.apollographql.apollo.ast.DirectiveRedefinition
4+
import com.apollographql.apollo.ast.GraphQLIssue
5+
import com.apollographql.apollo.ast.Schema
6+
import com.apollographql.apollo.ast.parseAsGQLDocument
7+
import com.apollographql.apollo.ast.toGQLDocument
8+
import com.apollographql.apollo.ast.validateAsExecutable
9+
import com.apollographql.apollo.ast.validateAsSchema
10+
import com.google.testing.junit.testparameterinjector.TestParameter
11+
import com.google.testing.junit.testparameterinjector.TestParameterInjector
12+
import com.google.testing.junit.testparameterinjector.TestParameterValuesProvider
13+
import okio.buffer
14+
import okio.source
15+
import org.junit.Test
16+
import org.junit.runner.RunWith
17+
import java.io.File
18+
19+
@RunWith(TestParameterInjector::class)
20+
class ExecutableValidationTest {
21+
@Test
22+
fun testValidation(@TestParameter(valuesProvider = ParametersProvider::class) graphQLFile: File) {
23+
findSchemaAndCheck(graphQLFile) { schema ->
24+
val parseResult = graphQLFile.source().buffer().parseAsGQLDocument(graphQLFile.name)
25+
val issues = if (parseResult.issues.isNotEmpty()) {
26+
parseResult.issues
27+
} else {
28+
parseResult.getOrThrow().validateAsExecutable(schema = schema).issues
29+
}
30+
31+
issues.serialize()
32+
}
33+
}
34+
35+
private fun findSchemaAndCheck(graphQLFile: File, block: (schema: Schema) -> String) {
36+
var parent = graphQLFile.parentFile
37+
38+
// We're in src/test/validation/operation/...
39+
// Find the closest schema
40+
var schema: Schema? = null
41+
while (parent.name != "test") {
42+
val candidate = parent.resolve("schema.graphqls")
43+
if (candidate.exists()) {
44+
val pragmas = candidate.pragmas()
45+
schema = candidate.toGQLDocument().validateAsSchema(
46+
candidate.pragmas().toSchemaValidationOptions()
47+
).let {
48+
it.issues.forEach {
49+
when (it) {
50+
is DirectiveRedefinition -> if (Pragma.allowDirectiveRedefinition in pragmas) return@forEach
51+
is GraphQLIssue -> error("Cannot validate schema: $it")
52+
else -> error("Unexpected issue type: ${it::class.simpleName}")
53+
}
54+
}
55+
it.value!!
56+
}
57+
break
58+
}
59+
parent = parent.parentFile
60+
}
61+
check(schema != null) {
62+
"Cannot find a schema for $graphQLFile"
63+
}
64+
65+
val actual = block(schema)
66+
67+
checkExpected(graphQLFile) {
68+
actual
69+
}
70+
}
71+
class ParametersProvider : TestParameterValuesProvider() {
72+
override fun provideValues(context: Context?): List<File> {
73+
return findFiles("test-fixtures/validation/executable")
74+
.filter { it.name.endsWith(".graphql") }
75+
}
76+
}
77+
}

libraries/apollo-ast/src/jvmTest/kotlin/com/apollographql/apollo/graphql/ast/test/MergerTest.kt

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.apollographql.apollo.graphql.ast.test
22

3-
import com.apollographql.apollo.ast.MergeOptions
4-
import com.apollographql.apollo.ast.ParserOptions
53
import com.apollographql.apollo.ast.mergeExtensions
64
import com.apollographql.apollo.ast.parseAsGQLDocument
75
import com.google.testing.junit.testparameterinjector.TestParameter
@@ -26,13 +24,8 @@ class MergerTest {
2624

2725
class ParametersProvider : TestParameterValuesProvider() {
2826
override fun provideValues(context: Context?): List<File> {
29-
return File("test-fixtures/merger/")
30-
.listFiles()!!
27+
return findFiles("test-fixtures/merger")
3128
.filter { it.name.endsWith(".graphql") && !it.name.endsWith(".expected.graphql") }
32-
.sortedBy { it.name }
33-
.filter {
34-
testFilterMatches(it.name)
35-
}
3629
}
3730
}
3831
}

libraries/apollo-ast/src/jvmTest/kotlin/com/apollographql/apollo/graphql/ast/test/ParserTest.kt

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,8 @@ class ParserTest {
2121

2222
class ParametersProvider : TestParameterValuesProvider() {
2323
override fun provideValues(context: Context?): List<File> {
24-
return File("test-fixtures/parser/")
25-
.listFiles()!!
26-
.filter { it.name.endsWith(".graphql") && !it.name.endsWith(".expected.graphql") }
27-
.sortedBy { it.name }
28-
.filter {
29-
testFilterMatches(it.name)
30-
}
24+
return findFiles("test-fixtures/parser")
25+
.filter { it.extension in setOf("graphql", "graphqls") }
3126
}
3227
}
3328
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.apollographql.apollo.graphql.ast.test
2+
3+
import com.apollographql.apollo.ast.toGQLDocument
4+
import com.apollographql.apollo.ast.validateAsSchema
5+
import com.google.testing.junit.testparameterinjector.TestParameter
6+
import com.google.testing.junit.testparameterinjector.TestParameterInjector
7+
import com.google.testing.junit.testparameterinjector.TestParameterValuesProvider
8+
import org.junit.Test
9+
import org.junit.runner.RunWith
10+
import java.io.File
11+
12+
@RunWith(TestParameterInjector::class)
13+
class SchemaValidationTest {
14+
@Test
15+
fun testValidation(@TestParameter(valuesProvider = ParametersProvider::class) graphqlsFile: File) {
16+
17+
checkExpected(graphqlsFile) {
18+
val pragmas = graphqlsFile.pragmas()
19+
val parseResult = graphqlsFile.toGQLDocument().validateAsSchema(
20+
pragmas.toSchemaValidationOptions()
21+
)
22+
23+
parseResult.issues.serialize()
24+
}
25+
}
26+
27+
class ParametersProvider : TestParameterValuesProvider() {
28+
override fun provideValues(context: Context?): List<File> {
29+
return findFiles("test-fixtures/validation/schema")
30+
.filter { it.name.endsWith(".graphqls") }
31+
}
32+
}
33+
}

libraries/apollo-ast/src/jvmTest/kotlin/com/apollographql/apollo/graphql/ast/test/fixtures.kt

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ import com.apollographql.apollo.ast.Issue
66
import com.apollographql.apollo.ast.MergeOptions
77
import com.apollographql.apollo.ast.ParserOptions
88
import com.apollographql.apollo.ast.SchemaValidationOptions
9+
import com.apollographql.apollo.ast.builtinForeignSchemas
910
import com.apollographql.apollo.ast.toUtf8
1011
import java.io.File
1112
import kotlin.test.assertEquals
1213

1314
private const val separator = "\n------------\n"
1415

15-
private fun List<Issue>.serialize() = joinToString(separator) {
16+
internal fun List<Issue>.serialize() = joinToString(separator) {
1617
"${it.javaClass.simpleName} (${it.sourceLocation?.line}:${it.sourceLocation?.column})\n${it.message}"
1718
}
1819

@@ -37,14 +38,14 @@ internal fun testFilterMatches(value: String): Boolean {
3738
}
3839

3940
/**
40-
* Run the block and checks the result against the .expected file. Will overwrite the result if required
41+
* Run the block and checks the result against the .expected.graphql file. Will overwrite the result if required
4142
*
4243
* @param block the callback to produce the result.
4344
*/
4445
internal fun checkExpected(graphQLFile: File, block: (File) -> String) {
4546
val actual = block(graphQLFile)
4647

47-
val expectedFile = File(graphQLFile.parent, graphQLFile.nameWithoutExtension + ".expected.graphql")
48+
val expectedFile = File(graphQLFile.parent, graphQLFile.nameWithoutExtension + ".expected")
4849
val expected = try {
4950
expectedFile.readText()
5051
} catch (e: Exception) {
@@ -58,10 +59,26 @@ internal fun checkExpected(graphQLFile: File, block: (File) -> String) {
5859
}
5960
}
6061

62+
internal fun findFiles(path: String): List<File> {
63+
return File(path)
64+
.walk()
65+
.filter {
66+
testFilterMatches(it.name)
67+
}
68+
.toList()
69+
.sortedBy { it.name }
70+
}
6171
enum class Pragma {
62-
allowMergingFieldDefinitions,
72+
// Parser
6373
allowDirectivesOnDirectives,
6474
allowServiceCapabilities,
75+
// Merger
76+
allowMergingFieldDefinitions,
77+
// Schema validation
78+
addBuiltinForeignSchemas,
79+
addKotlinLabsDefinitions,
80+
//
81+
allowDirectiveRedefinition,
6582
}
6683

6784
fun File.pragmas(): List<Pragma> =
@@ -82,4 +99,17 @@ fun List<Pragma>.toParserOptions(): ParserOptions {
8299

83100
fun List<Pragma>.toMergerOptions(): MergeOptions {
84101
return MergeOptions(Pragma.allowMergingFieldDefinitions in this)
102+
}
103+
104+
fun List<Pragma>.toSchemaValidationOptions(): SchemaValidationOptions {
105+
return SchemaValidationOptions.Builder()
106+
.apply {
107+
if (Pragma.addBuiltinForeignSchemas in this@toSchemaValidationOptions) {
108+
foreignSchemas(builtinForeignSchemas())
109+
}
110+
if (Pragma.addKotlinLabsDefinitions in this@toSchemaValidationOptions) {
111+
addKotlinLabsDefinitions(true)
112+
}
113+
}
114+
.build()
85115
}

libraries/apollo-ast/test-fixtures/merger/argument-default-value-error.expected.graphql renamed to libraries/apollo-ast/test-fixtures/merger/argument-default-value-error.expected

File renamed without changes.

libraries/apollo-ast/test-fixtures/merger/argument-description-error.expected.graphql renamed to libraries/apollo-ast/test-fixtures/merger/argument-description-error.expected

File renamed without changes.

libraries/apollo-ast/test-fixtures/merger/description-is-preserved.expected.graphql renamed to libraries/apollo-ast/test-fixtures/merger/description-is-preserved.expected

File renamed without changes.

libraries/apollo-ast/test-fixtures/merger/field-description-error.expected.graphql renamed to libraries/apollo-ast/test-fixtures/merger/field-description-error.expected

File renamed without changes.

libraries/apollo-ast/test-fixtures/merger/field-type-error.expected.graphql renamed to libraries/apollo-ast/test-fixtures/merger/field-type-error.expected

File renamed without changes.

0 commit comments

Comments
 (0)