Skip to content

Commit

Permalink
Migrate legacy tests (#662)
Browse files Browse the repository at this point in the history
* Migrate legacy tests

* Fixes to tests

* Reformat tests

* Rewrite tests with updater

* Continue even on error

* Better default never wiring factory

* Reformat

* Use map for byId fields

* Add missing tests

* Rewrite test snapshots

* Undo NeverWiringFactory changes

* Fix tests after NeverWiringFactory revert

* Fix doco

* Reformat

* More map for byId fields
  • Loading branch information
gnawf authored Jan 7, 2025
1 parent 232aab9 commit b5800a0
Show file tree
Hide file tree
Showing 879 changed files with 93,003 additions and 1,106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ import graphql.schema.GraphQLFieldsContainer
import graphql.schema.GraphQLInputObjectType
import graphql.schema.GraphQLInputType
import graphql.schema.GraphQLInterfaceType
import graphql.schema.GraphQLList
import graphql.schema.GraphQLNamedType
import graphql.schema.GraphQLNonNull
import graphql.schema.GraphQLObjectType
import graphql.schema.GraphQLOutputType
import graphql.schema.GraphQLScalarType
import graphql.schema.GraphQLType
import graphql.schema.GraphQLUnionType
import graphql.schema.GraphQLUnmodifiedType

internal inline fun <T> GraphQLFieldsContainer.whenType(
inline fun <T> GraphQLFieldsContainer.whenType(
interfaceType: (GraphQLInterfaceType) -> T,
objectType: (GraphQLObjectType) -> T,
): T {
Expand All @@ -27,7 +31,7 @@ internal inline fun <T> GraphQLFieldsContainer.whenType(
}
}

internal inline fun <T> GraphQLNamedType.whenType(
inline fun <T> GraphQLNamedType.whenType(
enumType: (GraphQLEnumType) -> T,
inputObjectType: (GraphQLInputObjectType) -> T,
interfaceType: (GraphQLInterfaceType) -> T,
Expand All @@ -46,7 +50,7 @@ internal inline fun <T> GraphQLNamedType.whenType(
}
}

internal inline fun <T> GraphQLOutputType.whenUnmodifiedType(
inline fun <T> GraphQLOutputType.whenUnmodifiedType(
enumType: (GraphQLEnumType) -> T,
interfaceType: (GraphQLInterfaceType) -> T,
objectType: (GraphQLObjectType) -> T,
Expand All @@ -63,7 +67,7 @@ internal inline fun <T> GraphQLOutputType.whenUnmodifiedType(
}
}

internal inline fun <T> GraphQLInputType.whenUnmodifiedType(
inline fun <T> GraphQLInputType.whenUnmodifiedType(
enumType: (GraphQLEnumType) -> T,
inputObjectType: (GraphQLInputObjectType) -> T,
scalarType: (GraphQLScalarType) -> T,
Expand All @@ -76,3 +80,15 @@ internal inline fun <T> GraphQLInputType.whenUnmodifiedType(
}
}

inline fun <T> GraphQLType.whenType(
listType: (GraphQLList) -> T,
nonNull: (GraphQLNonNull) -> T,
unmodifiedType: (GraphQLUnmodifiedType) -> T,
): T {
return when (this) {
is GraphQLList -> listType(this)
is GraphQLNonNull -> nonNull(this)
is GraphQLUnmodifiedType -> unmodifiedType(this)
else -> throw IllegalStateException("Should never happen")
}
}
8 changes: 6 additions & 2 deletions test/src/test/kotlin/graphql/nadel/tests/EngineTestHook.kt
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ interface EngineTestHook {
/**
* Allows you to wrap the base test service execution call, so you can do things before or after it
*/
fun wrapServiceExecution(baseTestServiceExecution: ServiceExecution): ServiceExecution {
fun wrapServiceExecution(serviceName: String, baseTestServiceExecution: ServiceExecution): ServiceExecution {
return baseTestServiceExecution
}
}
Expand All @@ -78,9 +78,13 @@ private val hooksPackage: String = join(
)

internal fun getTestHook(fixture: TestFixture): EngineTestHook? {
return getTestHook(fixture.name)
}

internal fun getTestHook(name: String): EngineTestHook? {
require(Util.validated) { "Tests hooks are not valid" }

val className = when (val name = fixture.name) {
val className = when (name) {
"new transformer on hydration fields",
"new can generate legacy operation name on batch hydration for specific service",
"new can generate legacy operation name on batch hydration",
Expand Down
2 changes: 1 addition & 1 deletion test/src/test/kotlin/graphql/nadel/tests/EngineTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ private suspend fun execute(
fail("Unable to invoke service '$serviceName'", e)
}
}
return testHook.wrapServiceExecution(serviceExecution)
return testHook.wrapServiceExecution(serviceName, serviceExecution)
}

private fun transformIncrementalExecutionResult(serviceCall: ServiceCall): CompletableFuture<ServiceExecutionResult> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import graphql.nadel.tests.EngineTestHook
import graphql.nadel.tests.UseHook

abstract class HydrationDetailsHook : EngineTestHook {
override fun wrapServiceExecution(baseTestServiceExecution: ServiceExecution): ServiceExecution {
override fun wrapServiceExecution(serviceName: String, baseTestServiceExecution: ServiceExecution): ServiceExecution {
return ServiceExecution { params ->
if (params.hydrationDetails != null) {
assertHydrationDetails(params.hydrationDetails!!)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class `abort-begin-execute-within-instrumentation-still-calls-enhancing-instrume
ExecutionResultImpl
.newExecutionResult()
.from(executionResult)
.data("enhanced beginExecute")
.data(mapOf("step" to "beginExecute"))
.build(),
)
}
Expand Down Expand Up @@ -65,7 +65,7 @@ class `abort-begin-execute-in-cf-within-instrumentation-still-calls-enhancing-in
ExecutionResultImpl
.newExecutionResult()
.from(executionResult)
.data("enhanced beginExecute")
.data(mapOf("step" to "beginExecute"))
.build(),
)
}
Expand Down Expand Up @@ -93,7 +93,7 @@ class `abort-begin-query-execution-within-instrumentation-still-calls-enhancing-
ExecutionResultImpl
.newExecutionResult()
.from(executionResult)
.data("enhanced beginQueryExecution")
.data(mapOf("step" to "beginQueryExecution"))
.build(),
)
}
Expand Down Expand Up @@ -121,7 +121,7 @@ class `abort-begin-validation-within-instrumentation-still-calls-enhancing-instr
ExecutionResultImpl
.newExecutionResult()
.from(executionResult)
.data("enhanced beginValidation")
.data(mapOf("step" to "beginValidation"))
.build(),
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,15 @@
package graphql.nadel.tests.hooks

import graphql.ExecutionResult
import graphql.nadel.engine.util.AnyList
import graphql.nadel.schema.SchemaTransformationHook
import graphql.nadel.tests.EngineTestHook
import graphql.nadel.tests.UseHook
import graphql.nadel.tests.assertJsonKeys
import graphql.nadel.tests.util.data
import graphql.schema.GraphQLFieldDefinition
import graphql.schema.GraphQLObjectType
import graphql.schema.GraphQLSchemaElement
import graphql.schema.GraphQLTypeVisitorStub
import graphql.schema.SchemaTransformer.transformSchema
import graphql.util.TraversalControl
import graphql.util.TraverserContext
import strikt.api.expectThat
import strikt.assertions.get
import strikt.assertions.isA
import strikt.assertions.isEqualTo
import strikt.assertions.isNotNull
import strikt.assertions.none
import strikt.assertions.one

@UseHook
class `can-delete-fields-and-types` : EngineTestHook {
Expand All @@ -47,23 +36,4 @@ class `can-delete-fields-and-types` : EngineTestHook {
}
})
}

override fun assertResult(result: ExecutionResult) {
expectThat(result)
.data
.isNotNull()
.assertJsonKeys()["__schema"]
.isNotNull()
.isAJsonMap()["types"]
.isA<AnyList>()
.none {
isNotNull().isAJsonMap()["name"].isEqualTo("Foo")
}
.one {
isNotNull().isAJsonMap()["name"].isEqualTo("String")
}
.one {
isNotNull().isAJsonMap()["name"].isEqualTo("Query")
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package graphql.nadel.tests.hooks

import graphql.ExecutionResult
import graphql.nadel.Nadel
import graphql.execution.ExecutionId
import graphql.nadel.NadelExecutionInput
import graphql.nadel.ServiceExecution
import graphql.nadel.ServiceExecutionFactory
import graphql.nadel.engine.util.AnyMap
import graphql.nadel.engine.util.JsonMap
import graphql.nadel.tests.EngineTestHook
Expand All @@ -12,8 +12,6 @@ import graphql.nadel.tests.assertJsonKeys
import graphql.nadel.tests.util.data
import graphql.nadel.tests.util.errors
import graphql.nadel.tests.util.message
import graphql.nadel.tests.util.serviceExecutionFactory
import graphql.schema.idl.TypeDefinitionRegistry
import strikt.api.Assertion
import strikt.api.expectThat
import strikt.assertions.contains
Expand All @@ -25,21 +23,19 @@ import strikt.assertions.single

@UseHook
class `exceptions-in-hydration-call-that-fail-with-errors-are-reflected-in-the-result` : EngineTestHook {
override fun makeNadel(builder: Nadel.Builder): Nadel.Builder {
val serviceExecutionFactory = builder.serviceExecutionFactory
override fun wrapServiceExecution(serviceName: String, baseTestServiceExecution: ServiceExecution): ServiceExecution {
return when (serviceName) {
// This is the hydration service, we die on hydration
"Bar" -> ServiceExecution {
throw RuntimeException("Pop goes the weasel")
}
else -> baseTestServiceExecution
}
}

return builder
.serviceExecutionFactory(object : ServiceExecutionFactory {
override fun getServiceExecution(serviceName: String): ServiceExecution {
return when (serviceName) {
// This is the hydration service, we die on hydration
"Bar" -> ServiceExecution {
throw RuntimeException("Pop goes the weasel")
}
else -> serviceExecutionFactory.getServiceExecution(serviceName)
}
}
})
override fun makeExecutionInput(builder: NadelExecutionInput.Builder): NadelExecutionInput.Builder {
return super.makeExecutionInput(builder)
.executionId(ExecutionId.from("test"))
}

override fun assertResult(result: ExecutionResult) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package graphql.nadel.tests.hooks

import graphql.ExecutionResult
import graphql.execution.ExecutionId
import graphql.nadel.Nadel
import graphql.nadel.NadelExecutionInput
import graphql.nadel.ServiceExecution
import graphql.nadel.ServiceExecutionFactory
import graphql.nadel.instrumentation.NadelInstrumentation
import graphql.nadel.instrumentation.parameters.ErrorData
import graphql.nadel.instrumentation.parameters.NadelInstrumentationOnErrorParameters
Expand All @@ -28,13 +29,6 @@ class `exceptions-in-service-execution-call-result-in-graphql-errors-and-call-on
var errorMessage: String? = null
override fun makeNadel(builder: Nadel.Builder): Nadel.Builder {
return builder
.serviceExecutionFactory(object : ServiceExecutionFactory {
override fun getServiceExecution(serviceName: String): ServiceExecution {
return ServiceExecution {
throw RuntimeException("Pop goes the weasel")
}
}
})
.instrumentation(object : NadelInstrumentation {
override fun onError(parameters: NadelInstrumentationOnErrorParameters) {
serviceName = (parameters.errorData as ErrorData.ServiceExecutionErrorData).serviceName
Expand All @@ -43,6 +37,20 @@ class `exceptions-in-service-execution-call-result-in-graphql-errors-and-call-on
})
}

override fun makeExecutionInput(builder: NadelExecutionInput.Builder): NadelExecutionInput.Builder {
return super.makeExecutionInput(builder)
.executionId(ExecutionId.from("test"))
}

override fun wrapServiceExecution(
serviceName: String,
baseTestServiceExecution: ServiceExecution,
): ServiceExecution {
return ServiceExecution {
throw RuntimeException("Pop goes the weasel")
}
}

override fun assertResult(result: ExecutionResult) {
expectThat(result).data
.isNotNull()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package graphql.nadel.tests.hooks

import graphql.ExecutionResult
import graphql.nadel.Nadel
import graphql.execution.ExecutionId
import graphql.nadel.NadelExecutionInput
import graphql.nadel.ServiceExecution
import graphql.nadel.ServiceExecutionFactory
import graphql.nadel.tests.EngineTestHook
import graphql.nadel.tests.UseHook
import graphql.nadel.tests.assertJsonKeys
import graphql.nadel.tests.util.data
import graphql.nadel.tests.util.errors
import graphql.nadel.tests.util.message
import graphql.nadel.tests.util.serviceExecutionFactory
import strikt.api.expectThat
import strikt.assertions.contains
import strikt.assertions.get
Expand All @@ -21,18 +20,18 @@ import java.util.concurrent.CompletableFuture

@UseHook
class `exceptions-in-service-execution-result-completable-future-in-graphql-errors` : EngineTestHook {
override fun makeNadel(builder: Nadel.Builder): Nadel.Builder {
return builder
.serviceExecutionFactory(object : ServiceExecutionFactory {
override fun getServiceExecution(serviceName: String): ServiceExecution {
return ServiceExecution {
CompletableFuture.completedFuture(null)
.thenCompose {
throw RuntimeException("Pop goes the weasel")
}
}
override fun makeExecutionInput(builder: NadelExecutionInput.Builder): NadelExecutionInput.Builder {
return super.makeExecutionInput(builder)
.executionId(ExecutionId.from("test"))
}

override fun wrapServiceExecution(serviceName: String, baseTestServiceExecution: ServiceExecution): ServiceExecution {
return ServiceExecution {
CompletableFuture.completedFuture(null)
.thenCompose {
throw RuntimeException("Pop goes the weasel")
}
})
}
}

override fun assertResult(result: ExecutionResult) {
Expand Down
Loading

0 comments on commit b5800a0

Please sign in to comment.