Skip to content

Commit

Permalink
Make all of apollo-testing-support deprecated except for QueueTestNet…
Browse files Browse the repository at this point in the history
…workTransport and MapTestNetworkTransport
  • Loading branch information
martinbonnin committed Jun 3, 2024
1 parent e8621ac commit 524c535
Show file tree
Hide file tree
Showing 25 changed files with 196 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.apollographql.apollo3.annotations.ApolloExperimental
import com.apollographql.apollo3.api.internal.ResponseParser
import com.apollographql.apollo3.api.json.JsonReader
import com.apollographql.apollo3.api.json.JsonWriter
import com.apollographql.apollo3.api.json.buildJsonString
import com.apollographql.apollo3.api.json.writeObject
import com.apollographql.apollo3.exception.ApolloException
import com.apollographql.apollo3.exception.ApolloNetworkException
Expand Down Expand Up @@ -145,6 +146,19 @@ fun <D : Operation.Data> Operation<D>.composeJsonResponse(
}
}

@ApolloExperimental
fun <D : Operation.Data> Operation<D>.composeJsonResponse(
data: D,
customScalarAdapters: CustomScalarAdapters = CustomScalarAdapters.Empty,
): String {
return buildJsonString {
writeObject {
name("data")
adapter().toJson(this, customScalarAdapters, data)
}
}
}

/**
* Parses the [JsonReader] into an [ApolloResponse]
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.apollographql.apollo3.interceptor.addRetryOnErrorInterceptor
import com.apollographql.apollo3.mockserver.CloseFrame
import com.apollographql.apollo3.mockserver.MockServer
import com.apollographql.apollo3.mockserver.TextMessage
import com.apollographql.apollo3.mockserver.WebSocketBody
import com.apollographql.apollo3.mockserver.awaitWebSocketRequest
import com.apollographql.apollo3.mockserver.enqueueWebSocket
import com.apollographql.apollo3.mpp.Platform
Expand Down Expand Up @@ -351,4 +352,8 @@ class WebSocketNetworkTransportTest {
assertEquals("oh no!", message)
}
}
}
}

internal fun WebSocketBody.enqueueMessage(message: String) {
enqueueMessage(TextMessage(message))
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import kotlinx.coroutines.flow.take
import kotlinx.coroutines.launch
import kotlinx.coroutines.withTimeout
import okio.use
import test.network.enqueueMessage
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertIs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ public final class com/apollographql/apollo3/testing/FooQuery$Companion {
}

public final class com/apollographql/apollo3/testing/FooSubscription$Companion {
public final fun completeMessage (Ljava/lang/String;)Lcom/apollographql/apollo3/mockserver/TextMessage;
public final fun errorMessage (Ljava/lang/String;Ljava/lang/String;)Lcom/apollographql/apollo3/mockserver/TextMessage;
public final fun nextMessage (Ljava/lang/String;I)Lcom/apollographql/apollo3/mockserver/TextMessage;
public final fun completeMessage (Ljava/lang/String;)Ljava/lang/String;
public final fun errorMessage (Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
public final fun nextMessage (Ljava/lang/String;I)Ljava/lang/String;
}

public final class com/apollographql/apollo3/testing/MockserverKt {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package com.apollographql.apollo3.testing

import com.apollographql.apollo3.annotations.ApolloDeprecatedSince

@ApolloDeprecatedSince(ApolloDeprecatedSince.Version.v4_0_0)
@Deprecated("This is only used for internal Apollo tests and will be removed in a future version.")
actual fun shouldUpdateTestFixtures(): Boolean = false

@ApolloDeprecatedSince(ApolloDeprecatedSince.Version.v4_0_0)
@Deprecated("This is only used for internal Apollo tests and will be removed in a future version.")
actual val testsPath: String = "../"
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

package com.apollographql.apollo3.testing

import com.apollographql.apollo3.annotations.ApolloDeprecatedSince
import com.apollographql.apollo3.api.json.JsonReader
import com.apollographql.apollo3.api.json.jsonReader
import okio.IOException
import okio.Path
import okio.Path.Companion.toPath
import okio.buffer
import okio.use
Expand All @@ -17,10 +19,14 @@ import kotlin.jvm.JvmName
*
* @param path: the path to the file, from the "tests" directory
*/
@Deprecated("This is only used for internal Apollo tests and will be removed in a future version.")
@ApolloDeprecatedSince(ApolloDeprecatedSince.Version.v4_0_0)
fun checkFile(actualText: String, path: String) {
@Suppress("DEPRECATION")
val updateTestFixtures = shouldUpdateTestFixtures()
val expected = path.toTestsPath()
val expectedText = try {
@Suppress("DEPRECATION")
HostFileSystem.openReadOnly(expected).source().buffer().readUtf8()
} catch (e: IOException) {
if (updateTestFixtures) {
Expand All @@ -34,7 +40,9 @@ fun checkFile(actualText: String, path: String) {

if (actualText != expectedText) {
if (updateTestFixtures) {
@Suppress("DEPRECATION")
HostFileSystem.delete(expected)
@Suppress("DEPRECATION")
HostFileSystem.openReadWrite(
file = expected,
).use {
Expand All @@ -53,18 +61,27 @@ fun checkFile(actualText: String, path: String) {
}
}

private fun String.toTestsPath() = testsPath.toPath().resolve(this.toPath())
private fun String.toTestsPath(): Path {
@Suppress("DEPRECATION")
return testsPath.toPath().resolve(this.toPath())
}

/**
* @param path: the path to the file, from the "tests" directory
*/
@Deprecated("This function is not Apollo specific and will be removed in a future version. Copy/paste it in your codebase if you need it")
@ApolloDeprecatedSince(ApolloDeprecatedSince.Version.v4_0_0)
fun pathToUtf8(path: String): String {
@Suppress("DEPRECATION")
return HostFileSystem.openReadOnly(path.toTestsPath()).source().buffer().readUtf8()
}

/**
* @param path: the path to the file, from the "tests" directory
*/
@Deprecated("This function is not Apollo specific and will be removed in a future version. Copy/paste it in your codebase if you need it")
@ApolloDeprecatedSince(ApolloDeprecatedSince.Version.v4_0_0)
fun pathToJsonReader(path: String): JsonReader {
@Suppress("DEPRECATION")
return HostFileSystem.openReadOnly(path.toTestsPath()).source().buffer().jsonReader()
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.apollographql.apollo3.testing

import com.apollographql.apollo3.annotations.ApolloDeprecatedSince
import com.apollographql.apollo3.annotations.ApolloExperimental
import com.apollographql.apollo3.api.Adapter
import com.apollographql.apollo3.api.CompiledField
Expand All @@ -14,14 +15,16 @@ import com.apollographql.apollo3.api.json.buildJsonString
import com.apollographql.apollo3.api.json.writeArray
import com.apollographql.apollo3.api.json.writeObject
import com.apollographql.apollo3.api.missingField
import com.apollographql.apollo3.mockserver.TextMessage

/**
* [FooQuery] is a query for tests that doesn't require codegen.
*
* Use it to test parts of the runtime without having to use included builds.
*/
@ApolloExperimental
@Deprecated("This is only used for internal Apollo tests and will be removed in a future version.")
@ApolloDeprecatedSince(ApolloDeprecatedSince.Version.v4_0_0)
@Suppress("DEPRECATION")
class FooQuery: FooOperation("query"), Query<FooOperation.Data> {
companion object {
val successResponse = "{\"data\": {\"foo\": 42}}"
Expand All @@ -34,9 +37,12 @@ class FooQuery: FooOperation("query"), Query<FooOperation.Data> {
* Use it to test parts of the runtime without having to use included builds.
*/
@ApolloExperimental
@Deprecated("This is only used for internal Apollo tests and will be removed in a future version.")
@ApolloDeprecatedSince(ApolloDeprecatedSince.Version.v4_0_0)
@Suppress("DEPRECATION")
class FooSubscription: FooOperation("subscription"), Subscription<FooOperation.Data> {
companion object {
fun nextMessage(id: String, foo: Int): TextMessage {
fun nextMessage(id: String, foo: Int): String {
return buildJsonString {
writeObject {
name("id")
Expand All @@ -52,21 +58,21 @@ class FooSubscription: FooOperation("subscription"), Subscription<FooOperation.D
}
}
}
}.let { TextMessage(it) }
}
}

fun completeMessage(id: String): TextMessage {
fun completeMessage(id: String): String {
return buildJsonString {
writeObject {
name("id")
value(id)
name("type")
value("complete")
}
}.let { TextMessage(it) }
}
}

fun errorMessage(id: String, message: String): TextMessage {
fun errorMessage(id: String, message: String): String {
return buildJsonString {
writeObject {
name("id")
Expand All @@ -81,7 +87,7 @@ class FooSubscription: FooOperation("subscription"), Subscription<FooOperation.D
}
}
}
}.let { TextMessage(it) }
}
}
}
}
Expand All @@ -91,6 +97,9 @@ class FooSubscription: FooOperation("subscription"), Subscription<FooOperation.D
* Note we can't make [FooOperation] extend both [Query] and [Subscription] because that confuses [ApolloClient] when deciding whant NetworkTransport to use.
*/
@ApolloExperimental
@Deprecated("This is only used for internal Apollo tests and will be removed in a future version.")
@ApolloDeprecatedSince(ApolloDeprecatedSince.Version.v4_0_0)
@Suppress("DEPRECATION")
abstract class FooOperation(private val operationType: String): Operation<FooOperation.Data> {
class Data(val foo: Int): Query.Data, Subscription.Data {
override fun toString(): String {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package com.apollographql.apollo3.testing

import com.apollographql.apollo3.annotations.ApolloDeprecatedSince
import com.apollographql.apollo3.annotations.ApolloExperimental
import kotlinx.coroutines.TimeoutCancellationException
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.withTimeout

@ApolloExperimental
@ApolloDeprecatedSince(ApolloDeprecatedSince.Version.v4_0_0)
@Deprecated("This function is not Apollo specific and will be removed in a future version. Copy/paste it in your codebase if you need it")
suspend fun <T> Channel<T>.awaitElement(timeoutMillis: Long = 30000) = withTimeout(timeoutMillis) {
receive()
}

@ApolloExperimental
@ApolloDeprecatedSince(ApolloDeprecatedSince.Version.v4_0_0)
@Deprecated("This function is not Apollo specific and will be removed in a future version. Copy/paste it in your codebase if you need it")
suspend fun <T> Channel<T>.assertNoElement(timeoutMillis: Long = 300): Unit {
try {
withTimeout(timeoutMillis) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.apollographql.apollo3.testing

import com.apollographql.apollo3.annotations.ApolloDeprecatedSince
import com.apollographql.apollo3.annotations.ApolloExperimental
import com.apollographql.apollo3.api.AnyAdapter
import com.apollographql.apollo3.api.CustomScalarAdapters
Expand All @@ -12,13 +13,21 @@ import com.apollographql.apollo3.api.toJson
import com.apollographql.apollo3.mockserver.MockResponse
import com.apollographql.apollo3.mockserver.MockServer
import com.apollographql.apollo3.mockserver.TextMessage
import com.apollographql.apollo3.mockserver.WebSocketMessage
import com.apollographql.apollo3.mockserver.WebsocketMockRequest
import com.apollographql.apollo3.mockserver.enqueueString
import okio.Buffer
import kotlin.time.Duration
import kotlin.time.Duration.Companion.seconds

@ApolloDeprecatedSince(ApolloDeprecatedSince.Version.v4_0_0)
@Deprecated(
"This is only used for internal Apollo tests and will be removed in a future version.",
ReplaceWith(
"enqueueString(operation.composeJsonResponse(data, customScalarAdapters), delayMs)",
"com.apollographql.apollo3.mockserver.enqueueString",
"com.apollographql.apollo3.api.composeJsonResponse",
)
)
fun <D : Operation.Data> MockServer.enqueue(
operation: Operation<D>,
data: D,
Expand All @@ -31,6 +40,10 @@ fun <D : Operation.Data> MockServer.enqueue(
enqueueString(json, delayMs)
}

@Deprecated(
"This is only used for internal Apollo tests and will be removed in a future version.",
)
@ApolloDeprecatedSince(ApolloDeprecatedSince.Version.v4_0_0)
fun MockServer.enqueueData(
data: Map<String, Any?>,
customScalarAdapters: CustomScalarAdapters = CustomScalarAdapters.Empty,
Expand All @@ -50,7 +63,15 @@ fun MockServer.enqueueData(
)
}


@Deprecated(
"This is only used for internal Apollo tests and will be removed in a future version.",
ReplaceWith(
"enqueueString(data.toResponseJson(customScalarAdapters), delayMillis, statusCode)",
"com.apollographql.apollo3.mockserver.enqueueString",
"com.apollographql.apollo3.api.toResponseJson",
)
)
@ApolloDeprecatedSince(ApolloDeprecatedSince.Version.v4_0_0)
fun MockServer.enqueueData(
data: Operation.Data,
customScalarAdapters: CustomScalarAdapters = CustomScalarAdapters.Empty,
Expand Down Expand Up @@ -110,5 +131,5 @@ suspend fun WebsocketMockRequest.awaitComplete(timeout: Duration = 1.seconds) {
}

@ApolloExperimental
fun connectionAckMessage(): WebSocketMessage = TextMessage("{\"type\": \"connection_ack\"}")
fun connectionAckMessage(): String = "{\"type\": \"connection_ack\"}"

Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
package com.apollographql.apollo3.testing

import com.apollographql.apollo3.annotations.ApolloDeprecatedSince
import com.apollographql.apollo3.annotations.ApolloExperimental
import okio.FileSystem

/**
* The host filesystem
*/
@ApolloExperimental
@ApolloDeprecatedSince(ApolloDeprecatedSince.Version.v4_0_0)
@Deprecated("This function is not Apollo specific and will be removed in a future version. Copy/paste it in your codebase if you need it")
expect val HostFileSystem: FileSystem

@ApolloExperimental
@ApolloDeprecatedSince(ApolloDeprecatedSince.Version.v4_0_0)
@Deprecated("This is only used for internal Apollo tests and will be removed in a future version.")
expect fun shouldUpdateTestFixtures(): Boolean

/**
* The path to the "tests" directory. This assumes all tests are run from a predictable place relative to "tests"
* We need this for JS tests where the CWD is not properly set at the beginning of tests
*/
@ApolloExperimental
@ApolloDeprecatedSince(ApolloDeprecatedSince.Version.v4_0_0)
@Deprecated("This is only used for internal Apollo tests and will be removed in a future version.")
expect val testsPath: String

Loading

0 comments on commit 524c535

Please sign in to comment.