From 0e31001f02e20ea9d1ed1cf1a2d787c36e0e9040 Mon Sep 17 00:00:00 2001 From: Di Wu Date: Tue, 14 Feb 2023 15:24:29 -0800 Subject: [PATCH 1/4] chore(test): add API subscription integration test case for LazyLoading --- .../GraphQLLazyLoadDefaultPKTests.swift | 28 +++++++++---------- .../GraphQLLazyLoadHasOneTests.swift | 28 +++++++++---------- 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/DefaultPK/GraphQLLazyLoadDefaultPKTests.swift b/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/DefaultPK/GraphQLLazyLoadDefaultPKTests.swift index 017e546030..1683db6f11 100644 --- a/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/DefaultPK/GraphQLLazyLoadDefaultPKTests.swift +++ b/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/DefaultPK/GraphQLLazyLoadDefaultPKTests.swift @@ -17,67 +17,67 @@ final class GraphQLLazyLoadDefaultPKTests: GraphQLLazyLoadBaseTest { func testConfigure() async throws { await setup(withModels: DefaultPKModels()) } - + func testDefaultPKParentChild() async throws { await setup(withModels: DefaultPKModels()) let defaultPKParent = DefaultPKParent() let savedParent = try await mutate(.create(defaultPKParent)) let defaultPKChild = DefaultPKChild(parent: savedParent) let savedChild = try await mutate(.create(defaultPKChild)) - + assertLazyReference(savedChild._parent, state: .notLoaded(identifiers: [.init(name: "id", value: savedParent.id)])) let loadedParent = try await savedChild.parent assertLazyReference(savedChild._parent, state: .loaded(model: loadedParent)) } - + func testDefaultParentChildUpdate() async throws { await setup(withModels: DefaultPKModels()) let defaultPKParent = DefaultPKParent() let savedParent = try await mutate(.create(defaultPKParent)) let defaultPKChild = DefaultPKChild(parent: savedParent) var savedChild = try await mutate(.create(defaultPKChild)) - + let newParent = DefaultPKParent() let savedNewParent = try await mutate(.create(newParent)) savedChild.setParent(savedNewParent) var updatedChild = try await mutate(.update(savedChild)) - + assertLazyReference(updatedChild._parent, state: .notLoaded(identifiers: [.init(name: "id", value: newParent.id)])) let loadedParent = try await updatedChild.parent assertLazyReference(updatedChild._parent, state: .loaded(model: loadedParent)) } - + func testDefaultParentChildDelete() async throws { await setup(withModels: DefaultPKModels()) let defaultPKParent = DefaultPKParent() let savedParent = try await mutate(.create(defaultPKParent)) let defaultPKChild = DefaultPKChild(parent: savedParent) let savedChild = try await mutate(.create(defaultPKChild)) - + try await mutate(.delete(savedParent)) try await assertModelDoesNotExist(savedParent) try await assertModelExists(savedChild) try await mutate(.delete(savedChild)) try await assertModelDoesNotExist(savedChild) } - + func testDefaultPKParentChildGet() async throws { await setup(withModels: DefaultPKModels()) let defaultPKParent = DefaultPKParent() let savedParent = try await mutate(.create(defaultPKParent)) let defaultPKChild = DefaultPKChild(parent: savedParent) let savedChild = try await mutate(.create(defaultPKChild)) - + let queriedParent = try await query(.get(DefaultPKParent.self, byId: savedParent.id))! assertList(queriedParent.children!, state: .isNotLoaded(associatedIdentifiers: [queriedParent.id], associatedFields: ["parent"])) try await queriedParent.children?.fetch() assertList(queriedParent.children!, state: .isLoaded(count: 1)) - + let queriedChild = try await query(.get(DefaultPKChild.self, byId: savedChild.id))! assertLazyReference(queriedChild._parent, state: .notLoaded(identifiers: [.init(name: "id", value: savedParent.id)])) @@ -85,18 +85,18 @@ final class GraphQLLazyLoadDefaultPKTests: GraphQLLazyLoadBaseTest { assertLazyReference(queriedChild._parent, state: .loaded(model: loadedParent)) } - + func testDefaultPKParentChildList() async throws { await setup(withModels: DefaultPKModels()) let defaultPKParent = DefaultPKParent() let savedParent = try await mutate(.create(defaultPKParent)) let defaultPKChild = DefaultPKChild(parent: savedParent) let savedChild = try await mutate(.create(defaultPKChild)) - + let queriedParents = try await listQuery(.list(DefaultPKParent.self, where: DefaultPKParent.keys.id == defaultPKParent.id)) assertList(queriedParents, state: .isLoaded(count: 1)) - + let queriedChildren = try await listQuery(.list(DefaultPKChild.self, where: DefaultPKChild.keys.id == defaultPKChild.id)) assertList(queriedChildren, state: .isLoaded(count: 1)) @@ -269,7 +269,7 @@ final class GraphQLLazyLoadDefaultPKTests: GraphQLLazyLoadBaseTest { extension GraphQLLazyLoadDefaultPKTests: DefaultLogger { } extension GraphQLLazyLoadDefaultPKTests { - + struct DefaultPKModels: AmplifyModelRegistration { public let version: String = "version" func registerModels(registry: ModelRegistry.Type) { diff --git a/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/HasOneParentChild/GraphQLLazyLoadHasOneTests.swift b/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/HasOneParentChild/GraphQLLazyLoadHasOneTests.swift index 86311ce52c..dfd31382d1 100644 --- a/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/HasOneParentChild/GraphQLLazyLoadHasOneTests.swift +++ b/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/HasOneParentChild/GraphQLLazyLoadHasOneTests.swift @@ -17,82 +17,82 @@ final class GraphQLLazyLoadHasOneTests: GraphQLLazyLoadBaseTest { func testConfigure() async throws { await setup(withModels: HasOneParentChildModels()) } - + func testHasOneParentChild() async throws { await setup(withModels: HasOneParentChildModels()) let hasOneChild = HasOneChild() let savedChild = try await mutate(.create(hasOneChild)) let hasOneParent = HasOneParent(child: hasOneChild) let savedParent = try await mutate(.create(hasOneParent)) - + assertLazyReference(savedParent._child, state: .notLoaded(identifiers: [.init(name: "id", value: savedChild.id)])) let loadedChild = try await savedParent.child assertLazyReference(savedParent._child, state: .loaded(model: loadedChild)) } - + func testHasOneParentChildUpdate() async throws { await setup(withModels: HasOneParentChildModels()) let hasOneChild = HasOneChild() let savedChild = try await mutate(.create(hasOneChild)) let hasOneParent = HasOneParent(child: hasOneChild) var savedParent = try await mutate(.create(hasOneParent)) - + let newChild = HasOneChild() let savedNewChild = try await mutate(.create(newChild)) savedParent.setChild(newChild) var updatedParent = try await mutate(.update(savedParent)) - + assertLazyReference(updatedParent._child, state: .notLoaded(identifiers: [.init(name: "id", value: newChild.id)])) let loadedChild = try await updatedParent.child assertLazyReference(updatedParent._child, state: .loaded(model: loadedChild)) } - + func testHasOneParentChildDelete() async throws { await setup(withModels: HasOneParentChildModels()) let hasOneChild = HasOneChild() let savedChild = try await mutate(.create(hasOneChild)) let hasOneParent = HasOneParent(child: hasOneChild) var savedParent = try await mutate(.create(hasOneParent)) - + try await mutate(.delete(savedParent)) try await assertModelDoesNotExist(savedParent) try await assertModelExists(savedChild) try await mutate(.delete(savedChild)) try await assertModelDoesNotExist(savedChild) } - + func testHasOneParentChildGet() async throws { await setup(withModels: HasOneParentChildModels()) let hasOneChild = HasOneChild() let savedChild = try await mutate(.create(hasOneChild)) let hasOneParent = HasOneParent(child: hasOneChild) let savedParent = try await mutate(.create(hasOneParent)) - + let queriedParent = try await query(.get(HasOneParent.self, byId: savedParent.id))! assertLazyReference(queriedParent._child, state: .notLoaded(identifiers: [.init(name: "id", value: savedChild.id)])) let loadedChild = try await queriedParent.child assertLazyReference(queriedParent._child, state: .loaded(model: loadedChild)) - + let queriedChild = try await query(.get(HasOneChild.self, byId: savedChild.id))! } - + func testHasOneParentChildList() async throws { await setup(withModels: HasOneParentChildModels()) let hasOneChild = HasOneChild() let savedChild = try await mutate(.create(hasOneChild)) let hasOneParent = HasOneParent(child: hasOneChild) let savedParent = try await mutate(.create(hasOneParent)) - + let queriedParents = try await listQuery(.list(HasOneParent.self, where: HasOneParent.keys.id == hasOneParent.id)) assertList(queriedParents, state: .isLoaded(count: 1)) - + let queriedChildren = try await listQuery(.list(HasOneChild.self, where: HasOneChild.keys.id == hasOneChild.id)) assertList(queriedChildren, state: .isLoaded(count: 1)) @@ -256,7 +256,7 @@ final class GraphQLLazyLoadHasOneTests: GraphQLLazyLoadBaseTest { extension GraphQLLazyLoadHasOneTests: DefaultLogger { } extension GraphQLLazyLoadHasOneTests { - + struct HasOneParentChildModels: AmplifyModelRegistration { public let version: String = "version" func registerModels(registry: ModelRegistry.Type) { From b6a48dcf36232e0ed670fd9aca7566d287d4df98 Mon Sep 17 00:00:00 2001 From: Di Wu Date: Tue, 14 Feb 2023 18:25:36 -0800 Subject: [PATCH 2/4] chore(test): add more integration test cases for API lazy loading --- ...hQLLazyLoadCompositePKChildSansTests.swift | 128 ++++++++++++++++ ...GraphQLLazyLoadCompositePKChildTests.swift | 127 ++++++++++++++++ ...phQLLazyLoadCompositePKImplicitTests.swift | 134 +++++++++++++++++ ...yLoadCompositePKStrangeExplicitTests.swift | 134 +++++++++++++++++ .../GraphQLLazyLoadCompositePKTests.swift | 142 ++++++++++++++++++ 5 files changed, 665 insertions(+) diff --git a/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKChildSansTests.swift b/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKChildSansTests.swift index a98c432fa5..81d2710ffd 100644 --- a/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKChildSansTests.swift +++ b/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKChildSansTests.swift @@ -106,4 +106,132 @@ extension GraphQLLazyLoadCompositePKTests { } assertList(queriedChild, state: .isLoaded(count: 1)) } + + /* + - Given: Api category setup with CompositePKModels + - When: + - Subscribe onCreate events of ChildSansBelongsTo + - Create new CompositePKParent instance with API + - Create new ChildSansBelongsTo instance with API + - Then: + - the newly created instance is successfully created through API. onCreate event is received. + */ + func testSubscribeChildSansBelongsToOnCreate() async throws { + await setup(withModels: CompositePKModels()) + let connected = asyncExpectation(description: "Subscription connected") + let onCreate = asyncExpectation(description: "onCreate received") + + let parent = CompositePKParent(customId: UUID().uuidString, content: UUID().uuidString) + let child = initChildSansBelongsTo(with: parent) + let subscription = Amplify.API.subscribe(request: .subscription(of: ChildSansBelongsTo.self, type: .onCreate)) + Task { + do { + for try await subscriptionEvent in subscription { + switch subscriptionEvent { + case .connection(.connected): + await connected.fulfill() + case let .data(.success(newModel)): + if newModel.identifier == child.identifier { + await onCreate.fulfill() + } + case let .data(.failure(error)): + XCTFail("Failed to create ChildSansBelongsTo, error: \(error.errorDescription)") + default: () + } + } + } + } + + await waitForExpectations([connected], timeout: 10) + try await mutate(.create(parent)) + try await mutate(.create(child)) + await waitForExpectations([onCreate], timeout: 10) + subscription.cancel() + } + + /* + - Given: Api category setup with CompositePKModels + - When: + - Subscribe onCreate events of ChildSansBelongsTo + - Create new CompositePKParent instance with API + - Create new ChildSansBelongsTo instance with API + - Update newly created ChildSansBelongsTo instance with API + - Then: + - the newly created instance is successfully updated through API. onUpdate event is received. + */ + func testSubscribeChildSansBelongsToOnUpdate() async throws { + await setup(withModels: CompositePKModels()) + let connected = asyncExpectation(description: "Subscription connected") + let onUpdate = asyncExpectation(description: "onUpdate received") + let parent = CompositePKParent(customId: UUID().uuidString, content: UUID().uuidString) + let child = initChildSansBelongsTo(with: parent) + let subscription = Amplify.API.subscribe(request: .subscription(of: ChildSansBelongsTo.self, type: .onUpdate)) + Task { + do { + for try await subscriptionEvent in subscription { + switch subscriptionEvent { + case .connection(.connected): + await connected.fulfill() + case let .data(.success(newModel)): + if newModel.identifier == child.identifier { + await onUpdate.fulfill() + } + case let .data(.failure(error)): + XCTFail("Failed to update ChildSansBelongsTo, error: \(error.errorDescription)") + default: () + } + } + } + } + + await waitForExpectations([connected], timeout: 10) + try await mutate(.create(parent)) + try await mutate(.create(child)) + try await mutate(.update(child)) + await waitForExpectations([onUpdate], timeout: 10) + subscription.cancel() + } + + /* + - Given: Api category setup with CompositePKModels + - When: + - Subscribe onCreate events of ChildSansBelongsTo + - Create new CompositePKParent instance with API + - Create new ChildSansBelongsTo instance with API + - Delete newly created ChildSansBelongsTo with API + - Then: + - the newly created instance is successfully deleted through API. onDelete event is received. + */ + func testSubscribeChildSansBelongsToOnDelete() async throws { + await setup(withModels: CompositePKModels()) + let connected = asyncExpectation(description: "Subscription connected") + let onDelete = asyncExpectation(description: "onUpdate received") + let parent = CompositePKParent(customId: UUID().uuidString, content: UUID().uuidString) + let child = initChildSansBelongsTo(with: parent) + let subscription = Amplify.API.subscribe(request: .subscription(of: ChildSansBelongsTo.self, type: .onDelete)) + Task { + do { + for try await subscriptionEvent in subscription { + switch subscriptionEvent { + case .connection(.connected): + await connected.fulfill() + case let .data(.success(newModel)): + if newModel.identifier == child.identifier { + await onDelete.fulfill() + } + case let .data(.failure(error)): + XCTFail("Failed to update ChildSansBelongsTo, error: \(error.errorDescription)") + default: () + } + } + } + } + + await waitForExpectations([connected], timeout: 10) + try await mutate(.create(parent)) + try await mutate(.create(child)) + try await mutate(.delete(child)) + await waitForExpectations([onDelete], timeout: 10) + subscription.cancel() + } } diff --git a/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKChildTests.swift b/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKChildTests.swift index d9bc6411d1..b987ddbcb9 100644 --- a/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKChildTests.swift +++ b/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKChildTests.swift @@ -121,4 +121,131 @@ extension GraphQLLazyLoadCompositePKTests { } assertList(queriedChild, state: .isLoaded(count: 1)) } + + /* + - Given: Api category setup with CompositePKModels + - When: + - Subscribe onCreate events of CompositePKChild + - Create new CompositePKChild instance with API + - Then: + - the newly created instance is successfully created through API. onCreate event is received. + */ + func testSubscribeCompositePKChildOnCreate() async throws { + await setup(withModels: CompositePKModels()) + let connected = asyncExpectation(description: "Subscription connected") + let onCreate = asyncExpectation(description: "onCreate received") + let child = CompositePKChild(childId: UUID().uuidString, content: UUID().uuidString) + let subscription = Amplify.API.subscribe(request: .subscription(of: CompositePKChild.self, type: .onCreate)) + Task { + do { + for try await subscriptionEvent in subscription { + switch subscriptionEvent { + case .connection(.connected): + await connected.fulfill() + case let .data(.success(newModel)): + if newModel.identifier == child.identifier { + await onCreate.fulfill() + } + case let .data(.failure(error)): + XCTFail("Failed to create CompositePKChild, error: \(error.errorDescription)") + default: () + } + } + } + } + + await waitForExpectations([connected], timeout: 10) + try await mutate(.create(child)) + await waitForExpectations([onCreate], timeout: 10) + subscription.cancel() + } + + /* + - Given: Api category setup with CompositePKModels + - When: + - Subscribe onCreate events of CompositePKChild + - Create new CompositePKChild instance with API + - Create new CompositePKParent instance with API + - Update newly created CompositePKParent instance with API + - Then: + - the newly created instance is successfully updated through API. onUpdate event is received. + */ + func testSubscribeCompositePKChildOnUpdate() async throws { + await setup(withModels: CompositePKModels()) + let connected = asyncExpectation(description: "Subscription connected") + let onUpdate = asyncExpectation(description: "onUpdate received") + let child = CompositePKChild(childId: UUID().uuidString, content: UUID().uuidString) + let parent = CompositePKParent(customId: UUID().uuidString, content: UUID().uuidString) + let subscription = Amplify.API.subscribe(request: .subscription(of: CompositePKChild.self, type: .onUpdate)) + Task { + do { + for try await subscriptionEvent in subscription { + switch subscriptionEvent { + case .connection(.connected): + await connected.fulfill() + case let .data(.success(newModel)): + let associatedParent = try await newModel.parent + if newModel.identifier == child.identifier, + associatedParent?.identifier == parent.identifier + { + await onUpdate.fulfill() + } + case let .data(.failure(error)): + XCTFail("Failed to update CompositePKParent, error: \(error.errorDescription)") + default: () + } + } + } + } + + await waitForExpectations([connected], timeout: 10) + try await mutate(.create(child)) + try await mutate(.create(parent)) + + var updatingChild = child + updatingChild.setParent(parent) + try await mutate(.update(updatingChild)) + await waitForExpectations([onUpdate], timeout: 10) + subscription.cancel() + } + + /* + - Given: Api category setup with CompositePKModels + - When: + - Subscribe onCreate events of CompositePKChild + - Create new CompositePKChild instance with API + - Delete newly created CompositePKChild with API + - Then: + - the newly created instance is successfully deleted through API. onDelete event is received. + */ + func testSubscribeCompositePKChildOnDelete() async throws { + await setup(withModels: CompositePKModels()) + let connected = asyncExpectation(description: "Subscription connected") + let onDelete = asyncExpectation(description: "onUpdate received") + let child = CompositePKChild(childId: UUID().uuidString, content: UUID().uuidString) + let subscription = Amplify.API.subscribe(request: .subscription(of: CompositePKChild.self, type: .onDelete)) + Task { + do { + for try await subscriptionEvent in subscription { + switch subscriptionEvent { + case .connection(.connected): + await connected.fulfill() + case let .data(.success(newModel)): + if newModel.identifier == child.identifier { + await onDelete.fulfill() + } + case let .data(.failure(error)): + XCTFail("Failed to update CompositePKParent, error: \(error.errorDescription)") + default: () + } + } + } + } + + await waitForExpectations([connected], timeout: 10) + try await mutate(.create(child)) + try await mutate(.delete(child)) + await waitForExpectations([onDelete], timeout: 10) + subscription.cancel() + } } diff --git a/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKImplicitTests.swift b/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKImplicitTests.swift index 694b7904f7..02c982d5ca 100644 --- a/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKImplicitTests.swift +++ b/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKImplicitTests.swift @@ -107,4 +107,138 @@ extension GraphQLLazyLoadCompositePKTests { } assertList(queriedChild, state: .isLoaded(count: 1)) } + + /* + - Given: Api category setup with CompositePKModels + - When: + - Subscribe onCreate events of ImplicitChild + - Create new CompositePKParent instance with API + - Create new ImplicitChild instance with API + - Then: + - the newly created instance is successfully created through API. onCreate event is received. + */ + func testSubscribeImplicitChildOnCreate() async throws { + await setup(withModels: CompositePKModels()) + let connected = asyncExpectation(description: "Subscription connected") + let onCreate = asyncExpectation(description: "onCreate received") + + let parent = CompositePKParent(customId: UUID().uuidString, content: UUID().uuidString) + let child = ImplicitChild(childId: UUID().uuidString, content: UUID().uuidString, parent: parent) + let subscription = Amplify.API.subscribe(request: .subscription(of: ImplicitChild.self, type: .onCreate)) + Task { + do { + for try await subscriptionEvent in subscription { + switch subscriptionEvent { + case .connection(.connected): + await connected.fulfill() + case let .data(.success(newModel)): + if newModel.identifier == child.identifier { + await onCreate.fulfill() + } + case let .data(.failure(error)): + XCTFail("Failed to create CompositePKChild, error: \(error.errorDescription)") + default: () + } + } + } + } + + await waitForExpectations([connected], timeout: 10) + try await mutate(.create(parent)) + try await mutate(.create(child)) + await waitForExpectations([onCreate], timeout: 10) + subscription.cancel() + } + + /* + - Given: Api category setup with CompositePKModels + - When: + - Subscribe onCreate events of ImplicitChild + - Create new CompositePKParent instance with API + - Create new ImplicitChild instance with API + - Update newly created ImplicitChild instance with API + - Then: + - the newly created instance is successfully updated through API. onUpdate event is received. + */ + func testSubscribeImplicitChildOnUpdate() async throws { + await setup(withModels: CompositePKModels()) + let connected = asyncExpectation(description: "Subscription connected") + let onUpdate = asyncExpectation(description: "onUpdate received") + let parent = CompositePKParent(customId: UUID().uuidString, content: UUID().uuidString) + let child = ImplicitChild(childId: UUID().uuidString, content: UUID().uuidString, parent: parent) + let subscription = Amplify.API.subscribe(request: .subscription(of: ImplicitChild.self, type: .onUpdate)) + Task { + do { + for try await subscriptionEvent in subscription { + switch subscriptionEvent { + case .connection(.connected): + await connected.fulfill() + case let .data(.success(newModel)): + let associatedParent = try await newModel.parent + if newModel.identifier == child.identifier, + associatedParent.identifier == parent.identifier + { + await onUpdate.fulfill() + } + case let .data(.failure(error)): + XCTFail("Failed to update CompositePKParent, error: \(error.errorDescription)") + default: () + } + } + } + } + + await waitForExpectations([connected], timeout: 10) + try await mutate(.create(parent)) + try await mutate(.create(child)) + + var updatingChild = child + updatingChild.setParent(parent) + try await mutate(.update(updatingChild)) + await waitForExpectations([onUpdate], timeout: 10) + subscription.cancel() + } + + /* + - Given: Api category setup with CompositePKModels + - When: + - Subscribe onCreate events of CompositePKChild + - Create new CompositePKParent instance with API + - Create new CompositePKChild instance with API + - Delete newly created CompositePKChild with API + - Then: + - the newly created instance is successfully deleted through API. onDelete event is received. + */ + func testSubscribeImplicitChildOnDelete() async throws { + await setup(withModels: CompositePKModels()) + let connected = asyncExpectation(description: "Subscription connected") + let onDelete = asyncExpectation(description: "onUpdate received") + let parent = CompositePKParent(customId: UUID().uuidString, content: UUID().uuidString) + let child = ImplicitChild(childId: UUID().uuidString, content: UUID().uuidString, parent: parent) + let subscription = Amplify.API.subscribe(request: .subscription(of: ImplicitChild.self, type: .onDelete)) + Task { + do { + for try await subscriptionEvent in subscription { + switch subscriptionEvent { + case .connection(.connected): + await connected.fulfill() + case let .data(.success(newModel)): + if newModel.identifier == child.identifier { + await onDelete.fulfill() + } + case let .data(.failure(error)): + XCTFail("Failed to update CompositePKParent, error: \(error.errorDescription)") + default: () + } + } + } + } + + await waitForExpectations([connected], timeout: 10) + try await mutate(.create(parent)) + try await mutate(.create(child)) + try await mutate(.delete(child)) + await waitForExpectations([onDelete], timeout: 10) + subscription.cancel() + } } diff --git a/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKStrangeExplicitTests.swift b/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKStrangeExplicitTests.swift index 1b74ee70c6..83a73d8a21 100644 --- a/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKStrangeExplicitTests.swift +++ b/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKStrangeExplicitTests.swift @@ -106,4 +106,138 @@ extension GraphQLLazyLoadCompositePKTests { } assertList(queriedChild, state: .isLoaded(count: 1)) } + + /* + - Given: Api category setup with CompositePKModels + - When: + - Subscribe onCreate events of StrangeExplicitChild + - Create new CompositePKParent instance with API + - Create new StrangeExplicitChild instance with API + - Then: + - the newly created instance is successfully created through API. onCreate event is received. + */ + func testSubscribeStrangeExplicitChildOnCreate() async throws { + await setup(withModels: CompositePKModels()) + let connected = asyncExpectation(description: "Subscription connected") + let onCreate = asyncExpectation(description: "onCreate received") + + let parent = CompositePKParent(customId: UUID().uuidString, content: UUID().uuidString) + let child = StrangeExplicitChild(strangeId: UUID().uuidString, content: UUID().uuidString, parent: parent) + let subscription = Amplify.API.subscribe(request: .subscription(of: StrangeExplicitChild.self, type: .onCreate)) + Task { + do { + for try await subscriptionEvent in subscription { + switch subscriptionEvent { + case .connection(.connected): + await connected.fulfill() + case let .data(.success(newModel)): + if newModel.identifier == child.identifier { + await onCreate.fulfill() + } + case let .data(.failure(error)): + XCTFail("Failed to create CompositePKChild, error: \(error.errorDescription)") + default: () + } + } + } + } + + await waitForExpectations([connected], timeout: 10) + try await mutate(.create(parent)) + try await mutate(.create(child)) + await waitForExpectations([onCreate], timeout: 10) + subscription.cancel() + } + + /* + - Given: Api category setup with CompositePKModels + - When: + - Subscribe onCreate events of StrangeExplicitChild + - Create new CompositePKParent instance with API + - Create new StrangeExplicitChild instance with API + - Update newly created StrangeExplicitChild instance with API + - Then: + - the newly created instance is successfully updated through API. onUpdate event is received. + */ + func testSubscribeStrangeExplicitChildOnUpdate() async throws { + await setup(withModels: CompositePKModels()) + let connected = asyncExpectation(description: "Subscription connected") + let onUpdate = asyncExpectation(description: "onUpdate received") + let parent = CompositePKParent(customId: UUID().uuidString, content: UUID().uuidString) + let child = StrangeExplicitChild(strangeId: UUID().uuidString, content: UUID().uuidString, parent: parent) + let subscription = Amplify.API.subscribe(request: .subscription(of: StrangeExplicitChild.self, type: .onUpdate)) + Task { + do { + for try await subscriptionEvent in subscription { + switch subscriptionEvent { + case .connection(.connected): + await connected.fulfill() + case let .data(.success(newModel)): + let associatedParent = try await newModel.parent + if newModel.identifier == child.identifier, + associatedParent.identifier == parent.identifier + { + await onUpdate.fulfill() + } + case let .data(.failure(error)): + XCTFail("Failed to update CompositePKParent, error: \(error.errorDescription)") + default: () + } + } + } + } + + await waitForExpectations([connected], timeout: 10) + try await mutate(.create(parent)) + try await mutate(.create(child)) + + var updatingChild = child + updatingChild.setParent(parent) + try await mutate(.update(updatingChild)) + await waitForExpectations([onUpdate], timeout: 10) + subscription.cancel() + } + + /* + - Given: Api category setup with CompositePKModels + - When: + - Subscribe onCreate events of StrangeExplicitChild + - Create new CompositePKParent instance with API + - Create new StrangeExplicitChild instance with API + - Delete newly created StrangeExplicitChild with API + - Then: + - the newly created instance is successfully deleted through API. onDelete event is received. + */ + func testSubscribeStrangeExplicitChildOnDelete() async throws { + await setup(withModels: CompositePKModels()) + let connected = asyncExpectation(description: "Subscription connected") + let onDelete = asyncExpectation(description: "onUpdate received") + let parent = CompositePKParent(customId: UUID().uuidString, content: UUID().uuidString) + let child = StrangeExplicitChild(strangeId: UUID().uuidString, content: UUID().uuidString, parent: parent) + let subscription = Amplify.API.subscribe(request: .subscription(of: StrangeExplicitChild.self, type: .onDelete)) + Task { + do { + for try await subscriptionEvent in subscription { + switch subscriptionEvent { + case .connection(.connected): + await connected.fulfill() + case let .data(.success(newModel)): + if newModel.identifier == child.identifier { + await onDelete.fulfill() + } + case let .data(.failure(error)): + XCTFail("Failed to update CompositePKParent, error: \(error.errorDescription)") + default: () + } + } + } + } + + await waitForExpectations([connected], timeout: 10) + try await mutate(.create(parent)) + try await mutate(.create(child)) + try await mutate(.delete(child)) + await waitForExpectations([onDelete], timeout: 10) + subscription.cancel() + } } diff --git a/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKTests.swift b/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKTests.swift index 9f7e8bbf74..a1ed8e5ed0 100644 --- a/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKTests.swift +++ b/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKTests.swift @@ -107,6 +107,148 @@ final class GraphQLLazyLoadCompositePKTests: GraphQLLazyLoadBaseTest { let queriedParent = try await query(request)! XCTAssertNotNil(queriedParent) } + + /* + - Given: Api category setup with CompositePKModels + - When: + - Subscribe onCreate events of CompositePKChild + - Create new CompositePKChild instance with API + - Then: + - the newly created instance is successfully created through API. onCreate event is received. + */ + func testSubscribeCompositePKParentOnCreate() async throws { + await setup(withModels: CompositePKModels()) + let connected = asyncExpectation(description: "Subscription connected") + let onCreate = asyncExpectation(description: "onCreate received") + let parent = CompositePKParent(customId: UUID().uuidString, content: UUID().uuidString) + let child = CompositePKChild(childId: UUID().uuidString, content: UUID().uuidString, parent: parent) + let subscription = Amplify.API.subscribe(request: .subscription(of: CompositePKParent.self, type: .onCreate)) + Task { + do { + for try await subscriptionEvent in subscription { + switch subscriptionEvent { + case .connection(.connected): + await connected.fulfill() + case let .data(.success(newModel)): + try await newModel.children?.fetch() + let associatedChildren = newModel.children?.loadedState + if newModel.identifier == parent.identifier, + case .some(.loaded(let associatedChildren)) = associatedChildren, + associatedChildren.map(\.identifier).contains(child.identifier) + { + await onCreate.fulfill() + } + case let .data(.failure(error)): + XCTFail("Failed to create CompositePKParent, error: \(error.errorDescription)") + default: () + } + } + } + } + + await waitForExpectations([connected], timeout: 10) + try await mutate(.create(child)) + try await mutate(.create(parent)) + await waitForExpectations([onCreate], timeout: 10) + subscription.cancel() + } + + /* + - Given: Api category setup with CompositePKModels + - When: + - Subscribe onCreate events of CompositePKChild + - Create new CompositePKChild instance with API + - Create new CompositePKParent instance with API + - Update the newly created CompositePKParent instance + - Then: + - the newly created instance is successfully updated through API. onUpdate event is received. + */ + func testSubscribeCompositePKParentOnUpdate() async throws { + await setup(withModels: CompositePKModels()) + let connected = asyncExpectation(description: "Subscription connected") + let onUpdate = asyncExpectation(description: "onUpdate received") + let parent = CompositePKParent(customId: UUID().uuidString, content: UUID().uuidString) + let child = CompositePKChild(childId: UUID().uuidString, content: UUID().uuidString, parent: parent) + + let subscription = Amplify.API.subscribe(request: .subscription(of: CompositePKParent.self, type: .onUpdate)) + Task { + do { + for try await subscriptionEvent in subscription { + switch subscriptionEvent { + case .connection(.connected): + await connected.fulfill() + case let .data(.success(newModel)): + try await newModel.children?.fetch() + let associatedChildren = newModel.children?.loadedState + if newModel.identifier == parent.identifier, + case .some(.loaded(let associatedChildren)) = associatedChildren, + associatedChildren.map(\.identifier).contains(child.identifier) + { + await onUpdate.fulfill() + } + case let .data(.failure(error)): + XCTFail("Failed to update CompositePKParent, error: \(error.errorDescription)") + default: () + } + } + } + } + + await waitForExpectations([connected], timeout: 10) + try await mutate(.create(child)) + try await mutate(.create(parent)) + try await mutate(.update(parent)) + await waitForExpectations([onUpdate], timeout: 10) + subscription.cancel() + } + + /* + - Given: Api category setup with CompositePKModels + - When: + - Subscribe onDelete events of CompositePKParent + - Create new CompositePKChild instance with API + - Create new CompositePKParent instance with API + - Then: + - the newly created instance is successfully deleted through API. onDelete event is received. + */ + func testSubscribeCompositePKParentOnDelete() async throws { + await setup(withModels: CompositePKModels()) + let connected = asyncExpectation(description: "Subscription connected") + let onDelete = asyncExpectation(description: "onUpdate received") + let parent = CompositePKParent(customId: UUID().uuidString, content: UUID().uuidString) + let child = CompositePKChild(childId: UUID().uuidString, content: UUID().uuidString, parent: parent) + + let subscription = Amplify.API.subscribe(request: .subscription(of: CompositePKParent.self, type: .onDelete)) + Task { + do { + for try await subscriptionEvent in subscription { + switch subscriptionEvent { + case .connection(.connected): + await connected.fulfill() + case let .data(.success(newModel)): + try await newModel.children?.fetch() + let associatedChildren = newModel.children?.loadedState + if newModel.identifier == parent.identifier, + case .some(.loaded(let associatedChildren)) = associatedChildren, + associatedChildren.map(\.identifier).contains(child.identifier) + { + await onDelete.fulfill() + } + case let .data(.failure(error)): + XCTFail("Failed to delete CompositePKParent, error: \(error.errorDescription)") + default: () + } + } + } + } + + await waitForExpectations([connected], timeout: 10) + try await mutate(.create(child)) + try await mutate(.create(parent)) + try await mutate(.delete(parent)) + await waitForExpectations([onDelete], timeout: 10) + subscription.cancel() + } } extension GraphQLLazyLoadCompositePKTests: DefaultLogger { } From 4259bffb029c7998eac56064f0cb56459a43eacb Mon Sep 17 00:00:00 2001 From: Di Wu Date: Wed, 15 Feb 2023 16:09:07 -0800 Subject: [PATCH 3/4] chore(test): refactor subscription integration tests --- ...hQLLazyLoadCompositePKChildSansTests.swift | 78 ++++++------- ...GraphQLLazyLoadCompositePKChildTests.swift | 83 +++++++------- ...phQLLazyLoadCompositePKImplicitTests.swift | 77 +++++++------ ...yLoadCompositePKStrangeExplicitTests.swift | 83 +++++++------- .../GraphQLLazyLoadCompositePKTests.swift | 108 +++++++++--------- 5 files changed, 214 insertions(+), 215 deletions(-) diff --git a/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKChildSansTests.swift b/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKChildSansTests.swift index 81d2710ffd..382309e146 100644 --- a/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKChildSansTests.swift +++ b/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKChildSansTests.swift @@ -125,19 +125,19 @@ extension GraphQLLazyLoadCompositePKTests { let child = initChildSansBelongsTo(with: parent) let subscription = Amplify.API.subscribe(request: .subscription(of: ChildSansBelongsTo.self, type: .onCreate)) Task { - do { - for try await subscriptionEvent in subscription { - switch subscriptionEvent { - case .connection(.connected): - await connected.fulfill() - case let .data(.success(newModel)): - if newModel.identifier == child.identifier { - await onCreate.fulfill() - } - case let .data(.failure(error)): - XCTFail("Failed to create ChildSansBelongsTo, error: \(error.errorDescription)") - default: () - } + for try await subscriptionEvent in subscription { + if subscriptionEvent.isConnected() { + await connected.fulfill() + } + + if let error = subscriptionEvent.extractError() { + XCTFail("Failed to create ChildSansBelongsTo, error: \(error.errorDescription)") + } + + if let data = subscriptionEvent.extractData(), + data.identifier == child.identifier + { + await onCreate.fulfill() } } } @@ -167,19 +167,19 @@ extension GraphQLLazyLoadCompositePKTests { let child = initChildSansBelongsTo(with: parent) let subscription = Amplify.API.subscribe(request: .subscription(of: ChildSansBelongsTo.self, type: .onUpdate)) Task { - do { - for try await subscriptionEvent in subscription { - switch subscriptionEvent { - case .connection(.connected): - await connected.fulfill() - case let .data(.success(newModel)): - if newModel.identifier == child.identifier { - await onUpdate.fulfill() - } - case let .data(.failure(error)): - XCTFail("Failed to update ChildSansBelongsTo, error: \(error.errorDescription)") - default: () - } + for try await subscriptionEvent in subscription { + if subscriptionEvent.isConnected() { + await connected.fulfill() + } + + if let error = subscriptionEvent.extractError() { + XCTFail("Failed to update ChildSansBelongsTo, error: \(error.errorDescription)") + } + + if let data = subscriptionEvent.extractData(), + data.identifier == child.identifier + { + await onUpdate.fulfill() } } } @@ -210,19 +210,19 @@ extension GraphQLLazyLoadCompositePKTests { let child = initChildSansBelongsTo(with: parent) let subscription = Amplify.API.subscribe(request: .subscription(of: ChildSansBelongsTo.self, type: .onDelete)) Task { - do { - for try await subscriptionEvent in subscription { - switch subscriptionEvent { - case .connection(.connected): - await connected.fulfill() - case let .data(.success(newModel)): - if newModel.identifier == child.identifier { - await onDelete.fulfill() - } - case let .data(.failure(error)): - XCTFail("Failed to update ChildSansBelongsTo, error: \(error.errorDescription)") - default: () - } + for try await subscriptionEvent in subscription { + if subscriptionEvent.isConnected() { + await connected.fulfill() + } + + if let error = subscriptionEvent.extractError() { + XCTFail("Failed to delete ChildSansBelongsTo, error: \(error.errorDescription)") + } + + if let data = subscriptionEvent.extractData(), + data.identifier == child.identifier + { + await onDelete.fulfill() } } } diff --git a/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKChildTests.swift b/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKChildTests.swift index b987ddbcb9..a1de94b78e 100644 --- a/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKChildTests.swift +++ b/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKChildTests.swift @@ -137,19 +137,19 @@ extension GraphQLLazyLoadCompositePKTests { let child = CompositePKChild(childId: UUID().uuidString, content: UUID().uuidString) let subscription = Amplify.API.subscribe(request: .subscription(of: CompositePKChild.self, type: .onCreate)) Task { - do { - for try await subscriptionEvent in subscription { - switch subscriptionEvent { - case .connection(.connected): - await connected.fulfill() - case let .data(.success(newModel)): - if newModel.identifier == child.identifier { - await onCreate.fulfill() - } - case let .data(.failure(error)): - XCTFail("Failed to create CompositePKChild, error: \(error.errorDescription)") - default: () - } + for try await subscriptionEvent in subscription { + if subscriptionEvent.isConnected() { + await connected.fulfill() + } + + if let error = subscriptionEvent.extractError() { + XCTFail("Failed to create CompositePKChild, error: \(error.errorDescription)") + } + + if let data = subscriptionEvent.extractData(), + data.identifier == child.identifier + { + await onCreate.fulfill() } } } @@ -178,22 +178,21 @@ extension GraphQLLazyLoadCompositePKTests { let parent = CompositePKParent(customId: UUID().uuidString, content: UUID().uuidString) let subscription = Amplify.API.subscribe(request: .subscription(of: CompositePKChild.self, type: .onUpdate)) Task { - do { - for try await subscriptionEvent in subscription { - switch subscriptionEvent { - case .connection(.connected): - await connected.fulfill() - case let .data(.success(newModel)): - let associatedParent = try await newModel.parent - if newModel.identifier == child.identifier, - associatedParent?.identifier == parent.identifier - { - await onUpdate.fulfill() - } - case let .data(.failure(error)): - XCTFail("Failed to update CompositePKParent, error: \(error.errorDescription)") - default: () - } + for try await subscriptionEvent in subscription { + if subscriptionEvent.isConnected() { + await connected.fulfill() + } + + if let error = subscriptionEvent.extractError() { + XCTFail("Failed to update CompositePKChild, error: \(error.errorDescription)") + } + + if let data = subscriptionEvent.extractData(), + data.identifier == child.identifier, + let associatedParent = try await data.parent, + associatedParent.identifier == parent.identifier + { + await onUpdate.fulfill() } } } @@ -225,19 +224,19 @@ extension GraphQLLazyLoadCompositePKTests { let child = CompositePKChild(childId: UUID().uuidString, content: UUID().uuidString) let subscription = Amplify.API.subscribe(request: .subscription(of: CompositePKChild.self, type: .onDelete)) Task { - do { - for try await subscriptionEvent in subscription { - switch subscriptionEvent { - case .connection(.connected): - await connected.fulfill() - case let .data(.success(newModel)): - if newModel.identifier == child.identifier { - await onDelete.fulfill() - } - case let .data(.failure(error)): - XCTFail("Failed to update CompositePKParent, error: \(error.errorDescription)") - default: () - } + for try await subscriptionEvent in subscription { + if subscriptionEvent.isConnected() { + await connected.fulfill() + } + + if let error = subscriptionEvent.extractError() { + XCTFail("Failed to delete CompositePKChild, error: \(error.errorDescription)") + } + + if let data = subscriptionEvent.extractData(), + data.identifier == child.identifier + { + await onDelete.fulfill() } } } diff --git a/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKImplicitTests.swift b/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKImplicitTests.swift index 02c982d5ca..70632da3a3 100644 --- a/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKImplicitTests.swift +++ b/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKImplicitTests.swift @@ -126,19 +126,19 @@ extension GraphQLLazyLoadCompositePKTests { let child = ImplicitChild(childId: UUID().uuidString, content: UUID().uuidString, parent: parent) let subscription = Amplify.API.subscribe(request: .subscription(of: ImplicitChild.self, type: .onCreate)) Task { - do { - for try await subscriptionEvent in subscription { - switch subscriptionEvent { - case .connection(.connected): - await connected.fulfill() - case let .data(.success(newModel)): - if newModel.identifier == child.identifier { - await onCreate.fulfill() - } - case let .data(.failure(error)): - XCTFail("Failed to create CompositePKChild, error: \(error.errorDescription)") - default: () - } + for try await subscriptionEvent in subscription { + if subscriptionEvent.isConnected() { + await connected.fulfill() + } + + if let error = subscriptionEvent.extractError() { + XCTFail("Failed to create ImplicitChild, error: \(error.errorDescription)") + } + + if let data = subscriptionEvent.extractData(), + data.identifier == child.identifier + { + await onCreate.fulfill() } } } @@ -170,19 +170,20 @@ extension GraphQLLazyLoadCompositePKTests { Task { do { for try await subscriptionEvent in subscription { - switch subscriptionEvent { - case .connection(.connected): + if subscriptionEvent.isConnected() { await connected.fulfill() - case let .data(.success(newModel)): - let associatedParent = try await newModel.parent - if newModel.identifier == child.identifier, - associatedParent.identifier == parent.identifier - { - await onUpdate.fulfill() - } - case let .data(.failure(error)): - XCTFail("Failed to update CompositePKParent, error: \(error.errorDescription)") - default: () + } + + if let error = subscriptionEvent.extractError() { + XCTFail("Failed to update ImplicitChild, error: \(error.errorDescription)") + } + + if let data = subscriptionEvent.extractData(), + data.identifier == child.identifier, + let associatedParent = try? await data.parent, + associatedParent.identifier == parent.identifier + { + await onUpdate.fulfill() } } } @@ -217,19 +218,19 @@ extension GraphQLLazyLoadCompositePKTests { let child = ImplicitChild(childId: UUID().uuidString, content: UUID().uuidString, parent: parent) let subscription = Amplify.API.subscribe(request: .subscription(of: ImplicitChild.self, type: .onDelete)) Task { - do { - for try await subscriptionEvent in subscription { - switch subscriptionEvent { - case .connection(.connected): - await connected.fulfill() - case let .data(.success(newModel)): - if newModel.identifier == child.identifier { - await onDelete.fulfill() - } - case let .data(.failure(error)): - XCTFail("Failed to update CompositePKParent, error: \(error.errorDescription)") - default: () - } + for try await subscriptionEvent in subscription { + if subscriptionEvent.isConnected() { + await connected.fulfill() + } + + if let error = subscriptionEvent.extractError() { + XCTFail("Failed to update ImplicitChild, error: \(error.errorDescription)") + } + + if let data = subscriptionEvent.extractData(), + data.identifier == child.identifier + { + await onDelete.fulfill() } } } diff --git a/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKStrangeExplicitTests.swift b/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKStrangeExplicitTests.swift index 83a73d8a21..8abf6563bf 100644 --- a/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKStrangeExplicitTests.swift +++ b/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKStrangeExplicitTests.swift @@ -125,19 +125,19 @@ extension GraphQLLazyLoadCompositePKTests { let child = StrangeExplicitChild(strangeId: UUID().uuidString, content: UUID().uuidString, parent: parent) let subscription = Amplify.API.subscribe(request: .subscription(of: StrangeExplicitChild.self, type: .onCreate)) Task { - do { - for try await subscriptionEvent in subscription { - switch subscriptionEvent { - case .connection(.connected): - await connected.fulfill() - case let .data(.success(newModel)): - if newModel.identifier == child.identifier { - await onCreate.fulfill() - } - case let .data(.failure(error)): - XCTFail("Failed to create CompositePKChild, error: \(error.errorDescription)") - default: () - } + for try await subscriptionEvent in subscription { + if subscriptionEvent.isConnected() { + await connected.fulfill() + } + + if let error = subscriptionEvent.extractError() { + XCTFail("Failed to create StrangeExplicitChild, error: \(error.errorDescription)") + } + + if let data = subscriptionEvent.extractData(), + data.identifier == child.identifier + { + await onCreate.fulfill() } } } @@ -167,22 +167,21 @@ extension GraphQLLazyLoadCompositePKTests { let child = StrangeExplicitChild(strangeId: UUID().uuidString, content: UUID().uuidString, parent: parent) let subscription = Amplify.API.subscribe(request: .subscription(of: StrangeExplicitChild.self, type: .onUpdate)) Task { - do { - for try await subscriptionEvent in subscription { - switch subscriptionEvent { - case .connection(.connected): - await connected.fulfill() - case let .data(.success(newModel)): - let associatedParent = try await newModel.parent - if newModel.identifier == child.identifier, - associatedParent.identifier == parent.identifier - { - await onUpdate.fulfill() - } - case let .data(.failure(error)): - XCTFail("Failed to update CompositePKParent, error: \(error.errorDescription)") - default: () - } + for try await subscriptionEvent in subscription { + if subscriptionEvent.isConnected() { + await connected.fulfill() + } + + if let error = subscriptionEvent.extractError() { + XCTFail("Failed to update StrangeExplicitChild, error: \(error.errorDescription)") + } + + if let data = subscriptionEvent.extractData(), + data.identifier == child.identifier, + let associatedParent = try? await data.parent, + associatedParent.identifier == parent.identifier + { + await onUpdate.fulfill() } } } @@ -216,19 +215,19 @@ extension GraphQLLazyLoadCompositePKTests { let child = StrangeExplicitChild(strangeId: UUID().uuidString, content: UUID().uuidString, parent: parent) let subscription = Amplify.API.subscribe(request: .subscription(of: StrangeExplicitChild.self, type: .onDelete)) Task { - do { - for try await subscriptionEvent in subscription { - switch subscriptionEvent { - case .connection(.connected): - await connected.fulfill() - case let .data(.success(newModel)): - if newModel.identifier == child.identifier { - await onDelete.fulfill() - } - case let .data(.failure(error)): - XCTFail("Failed to update CompositePKParent, error: \(error.errorDescription)") - default: () - } + for try await subscriptionEvent in subscription { + if subscriptionEvent.isConnected() { + await connected.fulfill() + } + + if let error = subscriptionEvent.extractError() { + XCTFail("Failed to update StrangeExplicitChild, error: \(error.errorDescription)") + } + + if let data = subscriptionEvent.extractData(), + data.identifier == child.identifier + { + await onDelete.fulfill() } } } diff --git a/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKTests.swift b/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKTests.swift index a1ed8e5ed0..5292af115d 100644 --- a/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKTests.swift +++ b/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKTests.swift @@ -124,24 +124,24 @@ final class GraphQLLazyLoadCompositePKTests: GraphQLLazyLoadBaseTest { let child = CompositePKChild(childId: UUID().uuidString, content: UUID().uuidString, parent: parent) let subscription = Amplify.API.subscribe(request: .subscription(of: CompositePKParent.self, type: .onCreate)) Task { - do { - for try await subscriptionEvent in subscription { - switch subscriptionEvent { - case .connection(.connected): - await connected.fulfill() - case let .data(.success(newModel)): - try await newModel.children?.fetch() - let associatedChildren = newModel.children?.loadedState - if newModel.identifier == parent.identifier, - case .some(.loaded(let associatedChildren)) = associatedChildren, - associatedChildren.map(\.identifier).contains(child.identifier) - { - await onCreate.fulfill() - } - case let .data(.failure(error)): - XCTFail("Failed to create CompositePKParent, error: \(error.errorDescription)") - default: () - } + for try await subscriptionEvent in subscription { + if subscriptionEvent.isConnected() { + await connected.fulfill() + } + + if let error = subscriptionEvent.extractError() { + XCTFail("Failed to create CompositePKParent, error: \(error.errorDescription)") + } + + guard let data = subscriptionEvent.extractData(), + data.identifier == parent.identifier + else { continue } + + try await data.children?.fetch() + if case .some(.loaded(let associatedchildren)) = data.children?.loadedState, + associatedchildren.map(\.identifier).contains(child.identifier) + { + await onCreate.fulfill() } } } @@ -172,24 +172,24 @@ final class GraphQLLazyLoadCompositePKTests: GraphQLLazyLoadBaseTest { let subscription = Amplify.API.subscribe(request: .subscription(of: CompositePKParent.self, type: .onUpdate)) Task { - do { - for try await subscriptionEvent in subscription { - switch subscriptionEvent { - case .connection(.connected): - await connected.fulfill() - case let .data(.success(newModel)): - try await newModel.children?.fetch() - let associatedChildren = newModel.children?.loadedState - if newModel.identifier == parent.identifier, - case .some(.loaded(let associatedChildren)) = associatedChildren, - associatedChildren.map(\.identifier).contains(child.identifier) - { - await onUpdate.fulfill() - } - case let .data(.failure(error)): - XCTFail("Failed to update CompositePKParent, error: \(error.errorDescription)") - default: () - } + for try await subscriptionEvent in subscription { + if subscriptionEvent.isConnected() { + await connected.fulfill() + } + + if let error = subscriptionEvent.extractError() { + XCTFail("Failed to update CompositePKParent, error: \(error.errorDescription)") + } + + guard let data = subscriptionEvent.extractData(), + data.identifier == parent.identifier + else { continue } + + try await data.children?.fetch() + if case .some(.loaded(let associatedChildren)) = data.children?.loadedState, + associatedChildren.map(\.identifier).contains(child.identifier) + { + await onUpdate.fulfill() } } } @@ -220,24 +220,24 @@ final class GraphQLLazyLoadCompositePKTests: GraphQLLazyLoadBaseTest { let subscription = Amplify.API.subscribe(request: .subscription(of: CompositePKParent.self, type: .onDelete)) Task { - do { - for try await subscriptionEvent in subscription { - switch subscriptionEvent { - case .connection(.connected): - await connected.fulfill() - case let .data(.success(newModel)): - try await newModel.children?.fetch() - let associatedChildren = newModel.children?.loadedState - if newModel.identifier == parent.identifier, - case .some(.loaded(let associatedChildren)) = associatedChildren, - associatedChildren.map(\.identifier).contains(child.identifier) - { - await onDelete.fulfill() - } - case let .data(.failure(error)): - XCTFail("Failed to delete CompositePKParent, error: \(error.errorDescription)") - default: () - } + for try await subscriptionEvent in subscription { + if subscriptionEvent.isConnected() { + await connected.fulfill() + } + + if let error = subscriptionEvent.extractError() { + XCTFail("Failed to delete CompositePKParent, error: \(error.errorDescription)") + } + + guard let data = subscriptionEvent.extractData(), + data.identifier == parent.identifier + else { continue } + + try await data.children?.fetch() + if case .some(.loaded(let associatedChildren)) = data.children?.loadedState, + associatedChildren.map(\.identifier).contains(child.identifier) + { + await onDelete.fulfill() } } } From dd09f50313589dd64c15179af3962c252529bd25 Mon Sep 17 00:00:00 2001 From: Di Wu Date: Thu, 16 Feb 2023 10:47:31 -0800 Subject: [PATCH 4/4] chore(test): refactor subscription test cases with template function --- ...hQLLazyLoadCompositePKChildSansTests.swift | 68 ++------------ ...GraphQLLazyLoadCompositePKChildTests.swift | 73 +++------------ ...phQLLazyLoadCompositePKImplicitTests.swift | 76 +++------------- ...yLoadCompositePKStrangeExplicitTests.swift | 74 +++------------ .../GraphQLLazyLoadCompositePKTests.swift | 91 ++++--------------- 5 files changed, 61 insertions(+), 321 deletions(-) diff --git a/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKChildSansTests.swift b/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKChildSansTests.swift index 382309e146..f38a37e189 100644 --- a/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKChildSansTests.swift +++ b/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKChildSansTests.swift @@ -118,31 +118,13 @@ extension GraphQLLazyLoadCompositePKTests { */ func testSubscribeChildSansBelongsToOnCreate() async throws { await setup(withModels: CompositePKModels()) - let connected = asyncExpectation(description: "Subscription connected") - let onCreate = asyncExpectation(description: "onCreate received") let parent = CompositePKParent(customId: UUID().uuidString, content: UUID().uuidString) let child = initChildSansBelongsTo(with: parent) - let subscription = Amplify.API.subscribe(request: .subscription(of: ChildSansBelongsTo.self, type: .onCreate)) - Task { - for try await subscriptionEvent in subscription { - if subscriptionEvent.isConnected() { - await connected.fulfill() - } - - if let error = subscriptionEvent.extractError() { - XCTFail("Failed to create ChildSansBelongsTo, error: \(error.errorDescription)") - } - - if let data = subscriptionEvent.extractData(), - data.identifier == child.identifier - { - await onCreate.fulfill() - } - } + let (onCreate, subscription) = try await subscribe(of: ChildSansBelongsTo.self, type: .onCreate) { createdChild in + createdChild.identifier == child.identifier } - await waitForExpectations([connected], timeout: 10) try await mutate(.create(parent)) try await mutate(.create(child)) await waitForExpectations([onCreate], timeout: 10) @@ -161,30 +143,13 @@ extension GraphQLLazyLoadCompositePKTests { */ func testSubscribeChildSansBelongsToOnUpdate() async throws { await setup(withModels: CompositePKModels()) - let connected = asyncExpectation(description: "Subscription connected") - let onUpdate = asyncExpectation(description: "onUpdate received") + let parent = CompositePKParent(customId: UUID().uuidString, content: UUID().uuidString) let child = initChildSansBelongsTo(with: parent) - let subscription = Amplify.API.subscribe(request: .subscription(of: ChildSansBelongsTo.self, type: .onUpdate)) - Task { - for try await subscriptionEvent in subscription { - if subscriptionEvent.isConnected() { - await connected.fulfill() - } - - if let error = subscriptionEvent.extractError() { - XCTFail("Failed to update ChildSansBelongsTo, error: \(error.errorDescription)") - } - - if let data = subscriptionEvent.extractData(), - data.identifier == child.identifier - { - await onUpdate.fulfill() - } - } + let (onUpdate, subscription) = try await subscribe(of: ChildSansBelongsTo.self, type: .onUpdate) { updatedChild in + updatedChild.identifier == child.identifier } - await waitForExpectations([connected], timeout: 10) try await mutate(.create(parent)) try await mutate(.create(child)) try await mutate(.update(child)) @@ -204,30 +169,13 @@ extension GraphQLLazyLoadCompositePKTests { */ func testSubscribeChildSansBelongsToOnDelete() async throws { await setup(withModels: CompositePKModels()) - let connected = asyncExpectation(description: "Subscription connected") - let onDelete = asyncExpectation(description: "onUpdate received") + let parent = CompositePKParent(customId: UUID().uuidString, content: UUID().uuidString) let child = initChildSansBelongsTo(with: parent) - let subscription = Amplify.API.subscribe(request: .subscription(of: ChildSansBelongsTo.self, type: .onDelete)) - Task { - for try await subscriptionEvent in subscription { - if subscriptionEvent.isConnected() { - await connected.fulfill() - } - - if let error = subscriptionEvent.extractError() { - XCTFail("Failed to delete ChildSansBelongsTo, error: \(error.errorDescription)") - } - - if let data = subscriptionEvent.extractData(), - data.identifier == child.identifier - { - await onDelete.fulfill() - } - } + let (onDelete, subscription) = try await subscribe(of: ChildSansBelongsTo.self, type: .onDelete) { deletedChild in + deletedChild.identifier == child.identifier } - await waitForExpectations([connected], timeout: 10) try await mutate(.create(parent)) try await mutate(.create(child)) try await mutate(.delete(child)) diff --git a/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKChildTests.swift b/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKChildTests.swift index a1de94b78e..44e4efb113 100644 --- a/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKChildTests.swift +++ b/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKChildTests.swift @@ -132,29 +132,12 @@ extension GraphQLLazyLoadCompositePKTests { */ func testSubscribeCompositePKChildOnCreate() async throws { await setup(withModels: CompositePKModels()) - let connected = asyncExpectation(description: "Subscription connected") - let onCreate = asyncExpectation(description: "onCreate received") + let child = CompositePKChild(childId: UUID().uuidString, content: UUID().uuidString) - let subscription = Amplify.API.subscribe(request: .subscription(of: CompositePKChild.self, type: .onCreate)) - Task { - for try await subscriptionEvent in subscription { - if subscriptionEvent.isConnected() { - await connected.fulfill() - } - - if let error = subscriptionEvent.extractError() { - XCTFail("Failed to create CompositePKChild, error: \(error.errorDescription)") - } - - if let data = subscriptionEvent.extractData(), - data.identifier == child.identifier - { - await onCreate.fulfill() - } - } + let (onCreate, subscription) = try await subscribe(of: CompositePKChild.self, type: .onCreate) { createdChild in + createdChild.identifier == child.identifier } - await waitForExpectations([connected], timeout: 10) try await mutate(.create(child)) await waitForExpectations([onCreate], timeout: 10) subscription.cancel() @@ -172,32 +155,17 @@ extension GraphQLLazyLoadCompositePKTests { */ func testSubscribeCompositePKChildOnUpdate() async throws { await setup(withModels: CompositePKModels()) - let connected = asyncExpectation(description: "Subscription connected") - let onUpdate = asyncExpectation(description: "onUpdate received") + let child = CompositePKChild(childId: UUID().uuidString, content: UUID().uuidString) let parent = CompositePKParent(customId: UUID().uuidString, content: UUID().uuidString) - let subscription = Amplify.API.subscribe(request: .subscription(of: CompositePKChild.self, type: .onUpdate)) - Task { - for try await subscriptionEvent in subscription { - if subscriptionEvent.isConnected() { - await connected.fulfill() - } - - if let error = subscriptionEvent.extractError() { - XCTFail("Failed to update CompositePKChild, error: \(error.errorDescription)") - } - - if let data = subscriptionEvent.extractData(), - data.identifier == child.identifier, - let associatedParent = try await data.parent, - associatedParent.identifier == parent.identifier - { - await onUpdate.fulfill() - } + let (onUpdate, subscription) = try await subscribe(of: CompositePKChild.self, type: .onUpdate) { updatedChild in + if let associatedParent = try await updatedChild.parent { + return associatedParent.identifier == parent.identifier + && updatedChild.identifier == child.identifier } + return false } - await waitForExpectations([connected], timeout: 10) try await mutate(.create(child)) try await mutate(.create(parent)) @@ -219,29 +187,12 @@ extension GraphQLLazyLoadCompositePKTests { */ func testSubscribeCompositePKChildOnDelete() async throws { await setup(withModels: CompositePKModels()) - let connected = asyncExpectation(description: "Subscription connected") - let onDelete = asyncExpectation(description: "onUpdate received") + let child = CompositePKChild(childId: UUID().uuidString, content: UUID().uuidString) - let subscription = Amplify.API.subscribe(request: .subscription(of: CompositePKChild.self, type: .onDelete)) - Task { - for try await subscriptionEvent in subscription { - if subscriptionEvent.isConnected() { - await connected.fulfill() - } - - if let error = subscriptionEvent.extractError() { - XCTFail("Failed to delete CompositePKChild, error: \(error.errorDescription)") - } - - if let data = subscriptionEvent.extractData(), - data.identifier == child.identifier - { - await onDelete.fulfill() - } - } + let (onDelete, subscription) = try await subscribe(of: CompositePKChild.self, type: .onDelete) { deletedChild in + deletedChild.identifier == child.identifier } - await waitForExpectations([connected], timeout: 10) try await mutate(.create(child)) try await mutate(.delete(child)) await waitForExpectations([onDelete], timeout: 10) diff --git a/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKImplicitTests.swift b/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKImplicitTests.swift index 70632da3a3..cbcf7f3800 100644 --- a/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKImplicitTests.swift +++ b/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKImplicitTests.swift @@ -119,31 +119,13 @@ extension GraphQLLazyLoadCompositePKTests { */ func testSubscribeImplicitChildOnCreate() async throws { await setup(withModels: CompositePKModels()) - let connected = asyncExpectation(description: "Subscription connected") - let onCreate = asyncExpectation(description: "onCreate received") let parent = CompositePKParent(customId: UUID().uuidString, content: UUID().uuidString) let child = ImplicitChild(childId: UUID().uuidString, content: UUID().uuidString, parent: parent) - let subscription = Amplify.API.subscribe(request: .subscription(of: ImplicitChild.self, type: .onCreate)) - Task { - for try await subscriptionEvent in subscription { - if subscriptionEvent.isConnected() { - await connected.fulfill() - } - - if let error = subscriptionEvent.extractError() { - XCTFail("Failed to create ImplicitChild, error: \(error.errorDescription)") - } - - if let data = subscriptionEvent.extractData(), - data.identifier == child.identifier - { - await onCreate.fulfill() - } - } + let (onCreate, subscription) = try await subscribe(of: ImplicitChild.self, type: .onCreate) { createdChild in + createdChild.identifier == child.identifier } - await waitForExpectations([connected], timeout: 10) try await mutate(.create(parent)) try await mutate(.create(child)) await waitForExpectations([onCreate], timeout: 10) @@ -162,34 +144,15 @@ extension GraphQLLazyLoadCompositePKTests { */ func testSubscribeImplicitChildOnUpdate() async throws { await setup(withModels: CompositePKModels()) - let connected = asyncExpectation(description: "Subscription connected") - let onUpdate = asyncExpectation(description: "onUpdate received") + let parent = CompositePKParent(customId: UUID().uuidString, content: UUID().uuidString) let child = ImplicitChild(childId: UUID().uuidString, content: UUID().uuidString, parent: parent) - let subscription = Amplify.API.subscribe(request: .subscription(of: ImplicitChild.self, type: .onUpdate)) - Task { - do { - for try await subscriptionEvent in subscription { - if subscriptionEvent.isConnected() { - await connected.fulfill() - } + let (onUpdate, subscription) = try await subscribe(of: ImplicitChild.self, type: .onUpdate, verifyChange: { updatedChild in + let associatedParent = try await updatedChild.parent + return associatedParent.identifier == parent.identifier + && updatedChild.identifier == child.identifier + }) - if let error = subscriptionEvent.extractError() { - XCTFail("Failed to update ImplicitChild, error: \(error.errorDescription)") - } - - if let data = subscriptionEvent.extractData(), - data.identifier == child.identifier, - let associatedParent = try? await data.parent, - associatedParent.identifier == parent.identifier - { - await onUpdate.fulfill() - } - } - } - } - - await waitForExpectations([connected], timeout: 10) try await mutate(.create(parent)) try await mutate(.create(child)) @@ -212,30 +175,13 @@ extension GraphQLLazyLoadCompositePKTests { */ func testSubscribeImplicitChildOnDelete() async throws { await setup(withModels: CompositePKModels()) - let connected = asyncExpectation(description: "Subscription connected") - let onDelete = asyncExpectation(description: "onUpdate received") + let parent = CompositePKParent(customId: UUID().uuidString, content: UUID().uuidString) let child = ImplicitChild(childId: UUID().uuidString, content: UUID().uuidString, parent: parent) - let subscription = Amplify.API.subscribe(request: .subscription(of: ImplicitChild.self, type: .onDelete)) - Task { - for try await subscriptionEvent in subscription { - if subscriptionEvent.isConnected() { - await connected.fulfill() - } - - if let error = subscriptionEvent.extractError() { - XCTFail("Failed to update ImplicitChild, error: \(error.errorDescription)") - } - - if let data = subscriptionEvent.extractData(), - data.identifier == child.identifier - { - await onDelete.fulfill() - } - } + let (onDelete, subscription) = try await subscribe(of: ImplicitChild.self, type: .onDelete) { deletedChild in + deletedChild.identifier == child.identifier } - await waitForExpectations([connected], timeout: 10) try await mutate(.create(parent)) try await mutate(.create(child)) try await mutate(.delete(child)) diff --git a/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKStrangeExplicitTests.swift b/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKStrangeExplicitTests.swift index 8abf6563bf..de59cd095b 100644 --- a/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKStrangeExplicitTests.swift +++ b/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKStrangeExplicitTests.swift @@ -118,31 +118,13 @@ extension GraphQLLazyLoadCompositePKTests { */ func testSubscribeStrangeExplicitChildOnCreate() async throws { await setup(withModels: CompositePKModels()) - let connected = asyncExpectation(description: "Subscription connected") - let onCreate = asyncExpectation(description: "onCreate received") let parent = CompositePKParent(customId: UUID().uuidString, content: UUID().uuidString) let child = StrangeExplicitChild(strangeId: UUID().uuidString, content: UUID().uuidString, parent: parent) - let subscription = Amplify.API.subscribe(request: .subscription(of: StrangeExplicitChild.self, type: .onCreate)) - Task { - for try await subscriptionEvent in subscription { - if subscriptionEvent.isConnected() { - await connected.fulfill() - } - - if let error = subscriptionEvent.extractError() { - XCTFail("Failed to create StrangeExplicitChild, error: \(error.errorDescription)") - } - - if let data = subscriptionEvent.extractData(), - data.identifier == child.identifier - { - await onCreate.fulfill() - } - } + let (onCreate, subscription) = try await subscribe(of: StrangeExplicitChild.self, type: .onCreate) { createdChild in + createdChild.identifier == child.identifier } - await waitForExpectations([connected], timeout: 10) try await mutate(.create(parent)) try await mutate(.create(child)) await waitForExpectations([onCreate], timeout: 10) @@ -161,32 +143,15 @@ extension GraphQLLazyLoadCompositePKTests { */ func testSubscribeStrangeExplicitChildOnUpdate() async throws { await setup(withModels: CompositePKModels()) - let connected = asyncExpectation(description: "Subscription connected") - let onUpdate = asyncExpectation(description: "onUpdate received") + let parent = CompositePKParent(customId: UUID().uuidString, content: UUID().uuidString) let child = StrangeExplicitChild(strangeId: UUID().uuidString, content: UUID().uuidString, parent: parent) - let subscription = Amplify.API.subscribe(request: .subscription(of: StrangeExplicitChild.self, type: .onUpdate)) - Task { - for try await subscriptionEvent in subscription { - if subscriptionEvent.isConnected() { - await connected.fulfill() - } + let (onUpdate, subscription) = try await subscribe(of: StrangeExplicitChild.self, type: .onUpdate, verifyChange: { updatedChild in + let associatedParent = try await updatedChild.parent + return associatedParent.identifier == parent.identifier + && updatedChild.identifier == child.identifier + }) - if let error = subscriptionEvent.extractError() { - XCTFail("Failed to update StrangeExplicitChild, error: \(error.errorDescription)") - } - - if let data = subscriptionEvent.extractData(), - data.identifier == child.identifier, - let associatedParent = try? await data.parent, - associatedParent.identifier == parent.identifier - { - await onUpdate.fulfill() - } - } - } - - await waitForExpectations([connected], timeout: 10) try await mutate(.create(parent)) try await mutate(.create(child)) @@ -209,30 +174,13 @@ extension GraphQLLazyLoadCompositePKTests { */ func testSubscribeStrangeExplicitChildOnDelete() async throws { await setup(withModels: CompositePKModels()) - let connected = asyncExpectation(description: "Subscription connected") - let onDelete = asyncExpectation(description: "onUpdate received") + let parent = CompositePKParent(customId: UUID().uuidString, content: UUID().uuidString) let child = StrangeExplicitChild(strangeId: UUID().uuidString, content: UUID().uuidString, parent: parent) - let subscription = Amplify.API.subscribe(request: .subscription(of: StrangeExplicitChild.self, type: .onDelete)) - Task { - for try await subscriptionEvent in subscription { - if subscriptionEvent.isConnected() { - await connected.fulfill() - } - - if let error = subscriptionEvent.extractError() { - XCTFail("Failed to update StrangeExplicitChild, error: \(error.errorDescription)") - } - - if let data = subscriptionEvent.extractData(), - data.identifier == child.identifier - { - await onDelete.fulfill() - } - } + let (onDelete, subscription) = try await subscribe(of: StrangeExplicitChild.self, type: .onDelete) { deletedChild in + deletedChild.identifier == child.identifier } - await waitForExpectations([connected], timeout: 10) try await mutate(.create(parent)) try await mutate(.create(child)) try await mutate(.delete(child)) diff --git a/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKTests.swift b/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKTests.swift index 5292af115d..b4d7126531 100644 --- a/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKTests.swift +++ b/AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKTests.swift @@ -118,35 +118,17 @@ final class GraphQLLazyLoadCompositePKTests: GraphQLLazyLoadBaseTest { */ func testSubscribeCompositePKParentOnCreate() async throws { await setup(withModels: CompositePKModels()) - let connected = asyncExpectation(description: "Subscription connected") - let onCreate = asyncExpectation(description: "onCreate received") let parent = CompositePKParent(customId: UUID().uuidString, content: UUID().uuidString) let child = CompositePKChild(childId: UUID().uuidString, content: UUID().uuidString, parent: parent) - let subscription = Amplify.API.subscribe(request: .subscription(of: CompositePKParent.self, type: .onCreate)) - Task { - for try await subscriptionEvent in subscription { - if subscriptionEvent.isConnected() { - await connected.fulfill() - } - - if let error = subscriptionEvent.extractError() { - XCTFail("Failed to create CompositePKParent, error: \(error.errorDescription)") - } - - guard let data = subscriptionEvent.extractData(), - data.identifier == parent.identifier - else { continue } - - try await data.children?.fetch() - if case .some(.loaded(let associatedchildren)) = data.children?.loadedState, - associatedchildren.map(\.identifier).contains(child.identifier) - { - await onCreate.fulfill() - } + let (onCreate, subscription) = try await subscribe(of: CompositePKParent.self, type: .onCreate) { createdParent in + try await createdParent.children?.fetch() + if case .some(.loaded(let associatedChildren)) = createdParent.children?.loadedState { + return createdParent.identifier == parent.identifier + && associatedChildren.map(\.identifier).contains(child.identifier) } + return false } - await waitForExpectations([connected], timeout: 10) try await mutate(.create(child)) try await mutate(.create(parent)) await waitForExpectations([onCreate], timeout: 10) @@ -165,36 +147,18 @@ final class GraphQLLazyLoadCompositePKTests: GraphQLLazyLoadBaseTest { */ func testSubscribeCompositePKParentOnUpdate() async throws { await setup(withModels: CompositePKModels()) - let connected = asyncExpectation(description: "Subscription connected") - let onUpdate = asyncExpectation(description: "onUpdate received") let parent = CompositePKParent(customId: UUID().uuidString, content: UUID().uuidString) let child = CompositePKChild(childId: UUID().uuidString, content: UUID().uuidString, parent: parent) - let subscription = Amplify.API.subscribe(request: .subscription(of: CompositePKParent.self, type: .onUpdate)) - Task { - for try await subscriptionEvent in subscription { - if subscriptionEvent.isConnected() { - await connected.fulfill() - } - - if let error = subscriptionEvent.extractError() { - XCTFail("Failed to update CompositePKParent, error: \(error.errorDescription)") - } - - guard let data = subscriptionEvent.extractData(), - data.identifier == parent.identifier - else { continue } - - try await data.children?.fetch() - if case .some(.loaded(let associatedChildren)) = data.children?.loadedState, - associatedChildren.map(\.identifier).contains(child.identifier) - { - await onUpdate.fulfill() - } + let (onUpdate, subscription) = try await subscribe(of: CompositePKParent.self, type: .onCreate) { updatedParent in + try await updatedParent.children?.fetch() + if case .some(.loaded(let associatedChildren)) = updatedParent.children?.loadedState { + return updatedParent.identifier == parent.identifier + && associatedChildren.map(\.identifier).contains(child.identifier) } + return false } - await waitForExpectations([connected], timeout: 10) try await mutate(.create(child)) try await mutate(.create(parent)) try await mutate(.update(parent)) @@ -213,36 +177,19 @@ final class GraphQLLazyLoadCompositePKTests: GraphQLLazyLoadBaseTest { */ func testSubscribeCompositePKParentOnDelete() async throws { await setup(withModels: CompositePKModels()) - let connected = asyncExpectation(description: "Subscription connected") - let onDelete = asyncExpectation(description: "onUpdate received") + let parent = CompositePKParent(customId: UUID().uuidString, content: UUID().uuidString) let child = CompositePKChild(childId: UUID().uuidString, content: UUID().uuidString, parent: parent) - let subscription = Amplify.API.subscribe(request: .subscription(of: CompositePKParent.self, type: .onDelete)) - Task { - for try await subscriptionEvent in subscription { - if subscriptionEvent.isConnected() { - await connected.fulfill() - } - - if let error = subscriptionEvent.extractError() { - XCTFail("Failed to delete CompositePKParent, error: \(error.errorDescription)") - } - - guard let data = subscriptionEvent.extractData(), - data.identifier == parent.identifier - else { continue } - - try await data.children?.fetch() - if case .some(.loaded(let associatedChildren)) = data.children?.loadedState, - associatedChildren.map(\.identifier).contains(child.identifier) - { - await onDelete.fulfill() - } + let (onDelete, subscription) = try await subscribe(of: CompositePKParent.self, type: .onDelete) { deletedParent in + try await deletedParent.children?.fetch() + if case .some(.loaded(let associatedChildren)) = deletedParent.children?.loadedState { + return deletedParent.identifier == parent.identifier + && associatedChildren.map(\.identifier).contains(child.identifier) } + return false } - await waitForExpectations([connected], timeout: 10) try await mutate(.create(child)) try await mutate(.create(parent)) try await mutate(.delete(parent))