Skip to content

Commit de53bfc

Browse files
committed
chore(test): refactor subscription test cases with template function
1 parent c72809a commit de53bfc

5 files changed

+61
-321
lines changed

AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKChildSansTests.swift

+8-60
Original file line numberDiff line numberDiff line change
@@ -118,31 +118,13 @@ extension GraphQLLazyLoadCompositePKTests {
118118
*/
119119
func testSubscribeChildSansBelongsToOnCreate() async throws {
120120
await setup(withModels: CompositePKModels())
121-
let connected = asyncExpectation(description: "Subscription connected")
122-
let onCreate = asyncExpectation(description: "onCreate received")
123121

124122
let parent = CompositePKParent(customId: UUID().uuidString, content: UUID().uuidString)
125123
let child = initChildSansBelongsTo(with: parent)
126-
let subscription = Amplify.API.subscribe(request: .subscription(of: ChildSansBelongsTo.self, type: .onCreate))
127-
Task {
128-
for try await subscriptionEvent in subscription {
129-
if subscriptionEvent.isConnected() {
130-
await connected.fulfill()
131-
}
132-
133-
if let error = subscriptionEvent.extractError() {
134-
XCTFail("Failed to create ChildSansBelongsTo, error: \(error.errorDescription)")
135-
}
136-
137-
if let data = subscriptionEvent.extractData(),
138-
data.identifier == child.identifier
139-
{
140-
await onCreate.fulfill()
141-
}
142-
}
124+
let (onCreate, subscription) = try await subscribe(of: ChildSansBelongsTo.self, type: .onCreate) { createdChild in
125+
createdChild.identifier == child.identifier
143126
}
144127

145-
await waitForExpectations([connected], timeout: 10)
146128
try await mutate(.create(parent))
147129
try await mutate(.create(child))
148130
await waitForExpectations([onCreate], timeout: 10)
@@ -161,30 +143,13 @@ extension GraphQLLazyLoadCompositePKTests {
161143
*/
162144
func testSubscribeChildSansBelongsToOnUpdate() async throws {
163145
await setup(withModels: CompositePKModels())
164-
let connected = asyncExpectation(description: "Subscription connected")
165-
let onUpdate = asyncExpectation(description: "onUpdate received")
146+
166147
let parent = CompositePKParent(customId: UUID().uuidString, content: UUID().uuidString)
167148
let child = initChildSansBelongsTo(with: parent)
168-
let subscription = Amplify.API.subscribe(request: .subscription(of: ChildSansBelongsTo.self, type: .onUpdate))
169-
Task {
170-
for try await subscriptionEvent in subscription {
171-
if subscriptionEvent.isConnected() {
172-
await connected.fulfill()
173-
}
174-
175-
if let error = subscriptionEvent.extractError() {
176-
XCTFail("Failed to update ChildSansBelongsTo, error: \(error.errorDescription)")
177-
}
178-
179-
if let data = subscriptionEvent.extractData(),
180-
data.identifier == child.identifier
181-
{
182-
await onUpdate.fulfill()
183-
}
184-
}
149+
let (onUpdate, subscription) = try await subscribe(of: ChildSansBelongsTo.self, type: .onUpdate) { updatedChild in
150+
updatedChild.identifier == child.identifier
185151
}
186152

187-
await waitForExpectations([connected], timeout: 10)
188153
try await mutate(.create(parent))
189154
try await mutate(.create(child))
190155
try await mutate(.update(child))
@@ -204,30 +169,13 @@ extension GraphQLLazyLoadCompositePKTests {
204169
*/
205170
func testSubscribeChildSansBelongsToOnDelete() async throws {
206171
await setup(withModels: CompositePKModels())
207-
let connected = asyncExpectation(description: "Subscription connected")
208-
let onDelete = asyncExpectation(description: "onUpdate received")
172+
209173
let parent = CompositePKParent(customId: UUID().uuidString, content: UUID().uuidString)
210174
let child = initChildSansBelongsTo(with: parent)
211-
let subscription = Amplify.API.subscribe(request: .subscription(of: ChildSansBelongsTo.self, type: .onDelete))
212-
Task {
213-
for try await subscriptionEvent in subscription {
214-
if subscriptionEvent.isConnected() {
215-
await connected.fulfill()
216-
}
217-
218-
if let error = subscriptionEvent.extractError() {
219-
XCTFail("Failed to delete ChildSansBelongsTo, error: \(error.errorDescription)")
220-
}
221-
222-
if let data = subscriptionEvent.extractData(),
223-
data.identifier == child.identifier
224-
{
225-
await onDelete.fulfill()
226-
}
227-
}
175+
let (onDelete, subscription) = try await subscribe(of: ChildSansBelongsTo.self, type: .onDelete) { deletedChild in
176+
deletedChild.identifier == child.identifier
228177
}
229178

230-
await waitForExpectations([connected], timeout: 10)
231179
try await mutate(.create(parent))
232180
try await mutate(.create(child))
233181
try await mutate(.delete(child))

AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKChildTests.swift

+12-61
Original file line numberDiff line numberDiff line change
@@ -132,29 +132,12 @@ extension GraphQLLazyLoadCompositePKTests {
132132
*/
133133
func testSubscribeCompositePKChildOnCreate() async throws {
134134
await setup(withModels: CompositePKModels())
135-
let connected = asyncExpectation(description: "Subscription connected")
136-
let onCreate = asyncExpectation(description: "onCreate received")
135+
137136
let child = CompositePKChild(childId: UUID().uuidString, content: UUID().uuidString)
138-
let subscription = Amplify.API.subscribe(request: .subscription(of: CompositePKChild.self, type: .onCreate))
139-
Task {
140-
for try await subscriptionEvent in subscription {
141-
if subscriptionEvent.isConnected() {
142-
await connected.fulfill()
143-
}
144-
145-
if let error = subscriptionEvent.extractError() {
146-
XCTFail("Failed to create CompositePKChild, error: \(error.errorDescription)")
147-
}
148-
149-
if let data = subscriptionEvent.extractData(),
150-
data.identifier == child.identifier
151-
{
152-
await onCreate.fulfill()
153-
}
154-
}
137+
let (onCreate, subscription) = try await subscribe(of: CompositePKChild.self, type: .onCreate) { createdChild in
138+
createdChild.identifier == child.identifier
155139
}
156140

157-
await waitForExpectations([connected], timeout: 10)
158141
try await mutate(.create(child))
159142
await waitForExpectations([onCreate], timeout: 10)
160143
subscription.cancel()
@@ -172,32 +155,17 @@ extension GraphQLLazyLoadCompositePKTests {
172155
*/
173156
func testSubscribeCompositePKChildOnUpdate() async throws {
174157
await setup(withModels: CompositePKModels())
175-
let connected = asyncExpectation(description: "Subscription connected")
176-
let onUpdate = asyncExpectation(description: "onUpdate received")
158+
177159
let child = CompositePKChild(childId: UUID().uuidString, content: UUID().uuidString)
178160
let parent = CompositePKParent(customId: UUID().uuidString, content: UUID().uuidString)
179-
let subscription = Amplify.API.subscribe(request: .subscription(of: CompositePKChild.self, type: .onUpdate))
180-
Task {
181-
for try await subscriptionEvent in subscription {
182-
if subscriptionEvent.isConnected() {
183-
await connected.fulfill()
184-
}
185-
186-
if let error = subscriptionEvent.extractError() {
187-
XCTFail("Failed to update CompositePKChild, error: \(error.errorDescription)")
188-
}
189-
190-
if let data = subscriptionEvent.extractData(),
191-
data.identifier == child.identifier,
192-
let associatedParent = try await data.parent,
193-
associatedParent.identifier == parent.identifier
194-
{
195-
await onUpdate.fulfill()
196-
}
161+
let (onUpdate, subscription) = try await subscribe(of: CompositePKChild.self, type: .onUpdate) { updatedChild in
162+
if let associatedParent = try await updatedChild.parent {
163+
return associatedParent.identifier == parent.identifier
164+
&& updatedChild.identifier == child.identifier
197165
}
166+
return false
198167
}
199168

200-
await waitForExpectations([connected], timeout: 10)
201169
try await mutate(.create(child))
202170
try await mutate(.create(parent))
203171

@@ -219,29 +187,12 @@ extension GraphQLLazyLoadCompositePKTests {
219187
*/
220188
func testSubscribeCompositePKChildOnDelete() async throws {
221189
await setup(withModels: CompositePKModels())
222-
let connected = asyncExpectation(description: "Subscription connected")
223-
let onDelete = asyncExpectation(description: "onUpdate received")
190+
224191
let child = CompositePKChild(childId: UUID().uuidString, content: UUID().uuidString)
225-
let subscription = Amplify.API.subscribe(request: .subscription(of: CompositePKChild.self, type: .onDelete))
226-
Task {
227-
for try await subscriptionEvent in subscription {
228-
if subscriptionEvent.isConnected() {
229-
await connected.fulfill()
230-
}
231-
232-
if let error = subscriptionEvent.extractError() {
233-
XCTFail("Failed to delete CompositePKChild, error: \(error.errorDescription)")
234-
}
235-
236-
if let data = subscriptionEvent.extractData(),
237-
data.identifier == child.identifier
238-
{
239-
await onDelete.fulfill()
240-
}
241-
}
192+
let (onDelete, subscription) = try await subscribe(of: CompositePKChild.self, type: .onDelete) { deletedChild in
193+
deletedChild.identifier == child.identifier
242194
}
243195

244-
await waitForExpectations([connected], timeout: 10)
245196
try await mutate(.create(child))
246197
try await mutate(.delete(child))
247198
await waitForExpectations([onDelete], timeout: 10)

AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKImplicitTests.swift

+11-65
Original file line numberDiff line numberDiff line change
@@ -119,31 +119,13 @@ extension GraphQLLazyLoadCompositePKTests {
119119
*/
120120
func testSubscribeImplicitChildOnCreate() async throws {
121121
await setup(withModels: CompositePKModels())
122-
let connected = asyncExpectation(description: "Subscription connected")
123-
let onCreate = asyncExpectation(description: "onCreate received")
124122

125123
let parent = CompositePKParent(customId: UUID().uuidString, content: UUID().uuidString)
126124
let child = ImplicitChild(childId: UUID().uuidString, content: UUID().uuidString, parent: parent)
127-
let subscription = Amplify.API.subscribe(request: .subscription(of: ImplicitChild.self, type: .onCreate))
128-
Task {
129-
for try await subscriptionEvent in subscription {
130-
if subscriptionEvent.isConnected() {
131-
await connected.fulfill()
132-
}
133-
134-
if let error = subscriptionEvent.extractError() {
135-
XCTFail("Failed to create ImplicitChild, error: \(error.errorDescription)")
136-
}
137-
138-
if let data = subscriptionEvent.extractData(),
139-
data.identifier == child.identifier
140-
{
141-
await onCreate.fulfill()
142-
}
143-
}
125+
let (onCreate, subscription) = try await subscribe(of: ImplicitChild.self, type: .onCreate) { createdChild in
126+
createdChild.identifier == child.identifier
144127
}
145128

146-
await waitForExpectations([connected], timeout: 10)
147129
try await mutate(.create(parent))
148130
try await mutate(.create(child))
149131
await waitForExpectations([onCreate], timeout: 10)
@@ -162,34 +144,15 @@ extension GraphQLLazyLoadCompositePKTests {
162144
*/
163145
func testSubscribeImplicitChildOnUpdate() async throws {
164146
await setup(withModels: CompositePKModels())
165-
let connected = asyncExpectation(description: "Subscription connected")
166-
let onUpdate = asyncExpectation(description: "onUpdate received")
147+
167148
let parent = CompositePKParent(customId: UUID().uuidString, content: UUID().uuidString)
168149
let child = ImplicitChild(childId: UUID().uuidString, content: UUID().uuidString, parent: parent)
169-
let subscription = Amplify.API.subscribe(request: .subscription(of: ImplicitChild.self, type: .onUpdate))
170-
Task {
171-
do {
172-
for try await subscriptionEvent in subscription {
173-
if subscriptionEvent.isConnected() {
174-
await connected.fulfill()
175-
}
150+
let (onUpdate, subscription) = try await subscribe(of: ImplicitChild.self, type: .onUpdate, verifyChange: { updatedChild in
151+
let associatedParent = try await updatedChild.parent
152+
return associatedParent.identifier == parent.identifier
153+
&& updatedChild.identifier == child.identifier
154+
})
176155

177-
if let error = subscriptionEvent.extractError() {
178-
XCTFail("Failed to update ImplicitChild, error: \(error.errorDescription)")
179-
}
180-
181-
if let data = subscriptionEvent.extractData(),
182-
data.identifier == child.identifier,
183-
let associatedParent = try? await data.parent,
184-
associatedParent.identifier == parent.identifier
185-
{
186-
await onUpdate.fulfill()
187-
}
188-
}
189-
}
190-
}
191-
192-
await waitForExpectations([connected], timeout: 10)
193156
try await mutate(.create(parent))
194157
try await mutate(.create(child))
195158

@@ -212,30 +175,13 @@ extension GraphQLLazyLoadCompositePKTests {
212175
*/
213176
func testSubscribeImplicitChildOnDelete() async throws {
214177
await setup(withModels: CompositePKModels())
215-
let connected = asyncExpectation(description: "Subscription connected")
216-
let onDelete = asyncExpectation(description: "onUpdate received")
178+
217179
let parent = CompositePKParent(customId: UUID().uuidString, content: UUID().uuidString)
218180
let child = ImplicitChild(childId: UUID().uuidString, content: UUID().uuidString, parent: parent)
219-
let subscription = Amplify.API.subscribe(request: .subscription(of: ImplicitChild.self, type: .onDelete))
220-
Task {
221-
for try await subscriptionEvent in subscription {
222-
if subscriptionEvent.isConnected() {
223-
await connected.fulfill()
224-
}
225-
226-
if let error = subscriptionEvent.extractError() {
227-
XCTFail("Failed to update ImplicitChild, error: \(error.errorDescription)")
228-
}
229-
230-
if let data = subscriptionEvent.extractData(),
231-
data.identifier == child.identifier
232-
{
233-
await onDelete.fulfill()
234-
}
235-
}
181+
let (onDelete, subscription) = try await subscribe(of: ImplicitChild.self, type: .onDelete) { deletedChild in
182+
deletedChild.identifier == child.identifier
236183
}
237184

238-
await waitForExpectations([connected], timeout: 10)
239185
try await mutate(.create(parent))
240186
try await mutate(.create(child))
241187
try await mutate(.delete(child))

0 commit comments

Comments
 (0)