From fa725667295ab47d803dc530d6b1510eddec52e5 Mon Sep 17 00:00:00 2001 From: Di Wu Date: Wed, 24 Apr 2024 17:31:37 -0700 Subject: [PATCH] fix broken unit test cases of AWSAPIPlugin --- .../AppSyncListProviderPaginationTests.swift | 17 ++++------- .../Core/AppSyncListProviderTests.swift | 28 ++++++------------- .../Operation/AWSRESTOperationTests.swift | 5 ++-- 3 files changed, 17 insertions(+), 33 deletions(-) diff --git a/AmplifyPlugins/API/Tests/AWSAPIPluginTests/Core/AppSyncListProviderPaginationTests.swift b/AmplifyPlugins/API/Tests/AWSAPIPluginTests/Core/AppSyncListProviderPaginationTests.swift index 47710842e3..41e0a2f854 100644 --- a/AmplifyPlugins/API/Tests/AWSAPIPluginTests/Core/AppSyncListProviderPaginationTests.swift +++ b/AmplifyPlugins/API/Tests/AWSAPIPluginTests/Core/AppSyncListProviderPaginationTests.swift @@ -52,8 +52,7 @@ extension AppSyncListProviderTests { let nextPage = List(elements: [Comment4(content: "content"), Comment4(content: "content"), Comment4(content: "content")]) - let event: GraphQLOperation>.OperationResult = .success(.success(nextPage)) - return event + return .success(nextPage) } let elements = [Comment4(content: "content")] let provider = AppSyncListProvider(elements: elements, nextToken: "nextToken") @@ -84,10 +83,8 @@ extension AppSyncListProviderTests { } func testLoadedStateGetNextPageFailure_APIError() async { - mockAPIPlugin.responders[.queryRequestResponse] = - QueryRequestResponder> { _ in - let event: GraphQLOperation>.OperationResult = .failure(APIError.unknown("", "", nil)) - return event + mockAPIPlugin.responders[.queryRequestResponse] = QueryRequestResponder> { _ in + throw APIError.unknown("", "", nil) } let elements = [Comment4(content: "content")] let provider = AppSyncListProvider(elements: elements, nextToken: "nextToken") @@ -108,12 +105,10 @@ extension AppSyncListProviderTests { } func testLoadedStateGetNextPageFailure_GraphQLErrorResponse() async { - mockAPIPlugin.responders[.queryRequestResponse] = - QueryRequestResponder> { _ in - let event: GraphQLOperation>.OperationResult = .success( - .failure(GraphQLResponseError.error([GraphQLError]()))) - return event + mockAPIPlugin.responders[.queryRequestResponse] = QueryRequestResponder> { _ in + return .failure(GraphQLResponseError.error([GraphQLError]())) } + let elements = [Comment4(content: "content")] let provider = AppSyncListProvider(elements: elements, nextToken: "nextToken") guard case .loaded = provider.loadedState else { diff --git a/AmplifyPlugins/API/Tests/AWSAPIPluginTests/Core/AppSyncListProviderTests.swift b/AmplifyPlugins/API/Tests/AWSAPIPluginTests/Core/AppSyncListProviderTests.swift index 0116c9c206..39f434aaf6 100644 --- a/AmplifyPlugins/API/Tests/AWSAPIPluginTests/Core/AppSyncListProviderTests.swift +++ b/AmplifyPlugins/API/Tests/AWSAPIPluginTests/Core/AppSyncListProviderTests.swift @@ -133,8 +133,7 @@ class AppSyncListProviderTests: XCTestCase { ], "nextToken": "nextToken" ] - let event: GraphQLOperation.OperationResult = .success(.success(json)) - return event + return .success(json) } let modelMetadata = AppSyncListDecoder.Metadata(appSyncAssociatedIdentifiers: ["postId"], appSyncAssociatedFields: ["post"], @@ -168,10 +167,8 @@ class AppSyncListProviderTests: XCTestCase { } func testNotLoadedStateSynchronousLoadFailure() async { - mockAPIPlugin.responders[.queryRequestResponse] = - QueryRequestResponder { _ in - let event: GraphQLOperation.OperationResult = .failure(APIError.unknown("", "", nil)) - return event + mockAPIPlugin.responders[.queryRequestResponse] = QueryRequestResponder { _ in + throw APIError.unknown("", "", nil) } let modelMetadata = AppSyncListDecoder.Metadata(appSyncAssociatedIdentifiers: ["postId"], appSyncAssociatedFields: ["post"], @@ -217,8 +214,7 @@ class AppSyncListProviderTests: XCTestCase { ], "nextToken": "nextToken" ] - let event: GraphQLOperation.OperationResult = .success(.success(json)) - return event + return .success(json) } let modelMetadata = AppSyncListDecoder.Metadata(appSyncAssociatedIdentifiers: ["postId"], appSyncAssociatedFields: ["post"], @@ -252,10 +248,8 @@ class AppSyncListProviderTests: XCTestCase { } func testNotLoadedStateLoadWithCompletionFailure_APIError() async { - mockAPIPlugin.responders[.queryRequestResponse] = - QueryRequestResponder { _ in - let event: GraphQLOperation.OperationResult = .failure(APIError.unknown("", "", nil)) - return event + mockAPIPlugin.responders[.queryRequestResponse] = QueryRequestResponder { _ in + throw APIError.unknown("", "", nil) } let modelMetadata = AppSyncListDecoder.Metadata(appSyncAssociatedIdentifiers: ["postId"], appSyncAssociatedFields: ["post"], @@ -287,11 +281,8 @@ class AppSyncListProviderTests: XCTestCase { } func testNotLoadedStateLoadWithCompletionFailure_GraphQLErrorResponse() async { - mockAPIPlugin.responders[.queryRequestResponse] = - QueryRequestResponder { _ in - let event: GraphQLOperation.OperationResult = .success( - .failure(GraphQLResponseError.error([GraphQLError]()))) - return event + mockAPIPlugin.responders[.queryRequestResponse] = QueryRequestResponder { _ in + return .failure(GraphQLResponseError.error([GraphQLError]())) } let modelMetadata = AppSyncListDecoder.Metadata(appSyncAssociatedIdentifiers: ["postId"], appSyncAssociatedFields: ["post"], @@ -339,8 +330,7 @@ class AppSyncListProviderTests: XCTestCase { ], "nextToken": "nextToken" ] - let event: GraphQLOperation.OperationResult = .success(.success(json)) - return event + return .success(json) } let modelMetadata = AppSyncListDecoder.Metadata(appSyncAssociatedIdentifiers: ["postId"], appSyncAssociatedFields: ["post"], diff --git a/AmplifyPlugins/API/Tests/AWSAPIPluginTests/Operation/AWSRESTOperationTests.swift b/AmplifyPlugins/API/Tests/AWSAPIPluginTests/Operation/AWSRESTOperationTests.swift index c65f1257ac..633a358d48 100644 --- a/AmplifyPlugins/API/Tests/AWSAPIPluginTests/Operation/AWSRESTOperationTests.swift +++ b/AmplifyPlugins/API/Tests/AWSAPIPluginTests/Operation/AWSRESTOperationTests.swift @@ -33,7 +33,7 @@ class AWSRESTOperationTests: OperationTestBase { } // TODO: Fix this test - func testGetReturnsOperation() throws { + func testGetReturnsOperation() async throws { try setUpPlugin(endpointType: .rest) // Use this as a semaphore to ensure the task is cleaned up before proceeding to the next test @@ -49,8 +49,7 @@ class AWSRESTOperationTests: OperationTestBase { } XCTAssertNotNil(operation.request) - - waitForExpectations(timeout: 1.00) + await fulfillment(of: [listenerWasInvoked], timeout: 1) } func testGetFailsWithBadAPIName() throws {