@@ -112,29 +112,11 @@ final class GraphQLLazyLoadDefaultPKTests: GraphQLLazyLoadBaseTest {
112
112
*/
113
113
func testSubscribeDefaultPKChildOnCreate( ) async throws {
114
114
await setup ( withModels: DefaultPKModels ( ) )
115
- let connected = asyncExpectation ( description: " Subscription connected " )
116
- let onCreate = asyncExpectation ( description: " onCreate received " )
117
115
let child = DefaultPKChild ( )
118
- let subscription = Amplify . API. subscribe ( request: . subscription( of: DefaultPKChild . self, type: . onCreate) )
119
- Task {
120
- for try await subscriptionEvent in subscription {
121
- if subscriptionEvent. isConnected ( ) {
122
- await connected. fulfill ( )
123
- }
124
-
125
- if let error = subscriptionEvent. extractError ( ) {
126
- XCTFail ( " Failed to create DefaultPKChild, error: \( error. errorDescription) " )
127
- }
128
-
129
- if let data = subscriptionEvent. extractData ( ) ,
130
- data. identifier == child. identifier
131
- {
132
- await onCreate. fulfill ( )
133
- }
134
- }
116
+ let ( onCreate, subscription) = try await subscribe ( of: DefaultPKChild . self, type: . onCreate) { newChild in
117
+ newChild. identifier == child. identifier
135
118
}
136
119
137
- await waitForExpectations ( [ connected] , timeout: 10 )
138
120
try await mutate ( . create( child) )
139
121
await waitForExpectations ( [ onCreate] , timeout: 10 )
140
122
subscription. cancel ( )
@@ -150,35 +132,18 @@ final class GraphQLLazyLoadDefaultPKTests: GraphQLLazyLoadBaseTest {
150
132
*/
151
133
func testSubscribeDefaultPKParentOnCreate( ) async throws {
152
134
await setup ( withModels: DefaultPKModels ( ) )
153
- let connected = asyncExpectation ( description: " Subscription connected " )
154
- let onCreate = asyncExpectation ( description: " onCreate received " )
135
+
155
136
let parent = DefaultPKParent ( )
156
137
let child = DefaultPKChild ( parent: parent)
157
- let subscription = Amplify . API. subscribe ( request: . subscription( of: DefaultPKParent . self, type: . onCreate) )
158
- Task {
159
- for try await subscriptionEvent in subscription {
160
- if subscriptionEvent. isConnected ( ) {
161
- await connected. fulfill ( )
162
- }
163
-
164
- if let error = subscriptionEvent. extractError ( ) {
165
- XCTFail ( " Failed to create DefaultPKParent, error: \( error. errorDescription) " )
166
- }
167
-
168
- guard let data = subscriptionEvent. extractData ( ) ,
169
- data. identifier == parent. identifier
170
- else { continue }
171
-
172
- try await data. children? . fetch ( )
173
- if case . some( . loaded( let associatedChildren) ) = data. children? . loadedState,
174
- associatedChildren. map ( \. identifier) . contains ( child. identifier)
175
- {
176
- await onCreate. fulfill ( )
177
- }
138
+ let ( onCreate, subscription) = try await subscribe ( of: DefaultPKParent . self, type: . onCreate) { newParent in
139
+ try await newParent. children? . fetch ( )
140
+ if case . some( . loaded( let associatedChildren) ) = newParent. children? . loadedState {
141
+ return newParent. identifier == parent. identifier
142
+ && associatedChildren. map ( \. identifier) . contains ( child. identifier)
178
143
}
144
+ return false
179
145
}
180
146
181
- await waitForExpectations ( [ connected] , timeout: 10 )
182
147
try await mutate ( . create( child) )
183
148
try await mutate ( . create( parent) )
184
149
await waitForExpectations ( [ onCreate] , timeout: 10 )
@@ -196,30 +161,12 @@ final class GraphQLLazyLoadDefaultPKTests: GraphQLLazyLoadBaseTest {
196
161
*/
197
162
func testSubscriptionDefaultPKChildOnUpdate( ) async throws {
198
163
await setup ( withModels: DefaultPKModels ( ) )
199
- let connected = asyncExpectation ( description: " Subscription connected " )
200
- let onUpdate = asyncExpectation ( description: " onUpdate received " )
201
- let child = DefaultPKChild ( )
202
- let subscription = Amplify . API. subscribe ( request: . subscription( of: DefaultPKChild . self, type: . onUpdate) )
203
-
204
- Task {
205
- for try await subscriptionEvent in subscription {
206
- if subscriptionEvent. isConnected ( ) {
207
- await connected. fulfill ( )
208
- }
209
164
210
- if let error = subscriptionEvent. extractError ( ) {
211
- XCTFail ( " Failed to update DefaultPKChild, error: \( error. errorDescription) " )
212
- }
213
-
214
- if let data = subscriptionEvent. extractData ( ) ,
215
- data. identifier == child. identifier
216
- {
217
- await onUpdate. fulfill ( )
218
- }
219
- }
165
+ let child = DefaultPKChild ( )
166
+ let ( onUpdate, subscription) = try await subscribe ( of: DefaultPKChild . self, type: . onUpdate) { updatedChild in
167
+ updatedChild. identifier == child. identifier
220
168
}
221
169
222
- await waitForExpectations ( [ connected] , timeout: 10 )
223
170
try await mutate ( . create( child) )
224
171
var updatingChild = child
225
172
updatingChild. content = UUID ( ) . uuidString
@@ -240,40 +187,21 @@ final class GraphQLLazyLoadDefaultPKTests: GraphQLLazyLoadBaseTest {
240
187
*/
241
188
func testSubscriptionDefaultPKParentOnUpdate( ) async throws {
242
189
await setup ( withModels: DefaultPKModels ( ) )
243
- let connected = asyncExpectation ( description: " Subscription connected " )
244
- let onUpdate = asyncExpectation ( description: " onUpdate received " )
245
190
246
191
let parent = DefaultPKParent ( )
247
192
let child = DefaultPKChild ( parent: parent)
248
193
let anotherChild = DefaultPKChild ( parent: parent)
249
194
250
- let subscription = Amplify . API. subscribe ( request: . subscription( of: DefaultPKParent . self, type: . onUpdate) )
251
-
252
- Task {
253
- for try await subscriptionEvent in subscription {
254
- if subscriptionEvent. isConnected ( ) {
255
- await connected. fulfill ( )
256
- }
257
-
258
- if let error = subscriptionEvent. extractError ( ) {
259
- XCTFail ( " Failed to update DefaultPKParent, error: \( error. errorDescription) " )
260
- }
261
-
262
- guard let data = subscriptionEvent. extractData ( ) ,
263
- data. identifier == parent. identifier
264
- else { continue }
265
-
266
- try await data. children? . fetch ( )
267
- if case . some( . loaded( let associatedChildren) ) = data. children? . loadedState,
268
- associatedChildren. map ( \. identifier) . contains ( child. identifier) ,
269
- associatedChildren. map ( \. identifier) . contains ( anotherChild. identifier)
270
- {
271
- await onUpdate. fulfill ( )
272
- }
195
+ let ( onUpdate, subscription) = try await subscribe ( of: DefaultPKParent . self, type: . onUpdate) { updatedParent in
196
+ try await updatedParent. children? . fetch ( )
197
+ if case . some( . loaded( let associatedChildren) ) = updatedParent. children? . loadedState {
198
+ return updatedParent. identifier == parent. identifier
199
+ && associatedChildren. map ( \. identifier) . contains ( child. identifier)
200
+ && associatedChildren. map ( \. identifier) . contains ( anotherChild. identifier)
273
201
}
202
+ return false
274
203
}
275
204
276
- await waitForExpectations ( [ connected] , timeout: 10 )
277
205
try await mutate ( . create( child) )
278
206
try await mutate ( . create( parent) )
279
207
try await mutate ( . create( anotherChild) )
@@ -295,30 +223,11 @@ final class GraphQLLazyLoadDefaultPKTests: GraphQLLazyLoadBaseTest {
295
223
*/
296
224
func testSubscriptionDefaultPKChildOnDelete( ) async throws {
297
225
await setup ( withModels: DefaultPKModels ( ) )
298
- let connected = asyncExpectation ( description: " Subscription connected " )
299
- let onDelete = asyncExpectation ( description: " onDelete received " )
300
226
let child = DefaultPKChild ( )
301
- let subscription = Amplify . API. subscribe ( request: . subscription( of: DefaultPKChild . self, type: . onDelete) )
302
-
303
- Task {
304
- for try await subscriptionEvent in subscription {
305
- if subscriptionEvent. isConnected ( ) {
306
- await connected. fulfill ( )
307
- }
308
-
309
- if let error = subscriptionEvent. extractError ( ) {
310
- XCTFail ( " Failed to delete DefaultPKChild, error: \( error. errorDescription) " )
311
- }
312
-
313
- if let data = subscriptionEvent. extractData ( ) ,
314
- data. identifier == child. identifier
315
- {
316
- await onDelete. fulfill ( )
317
- }
318
- }
319
- }
227
+ let ( onDelete, subscription) = try await subscribe ( of: DefaultPKChild . self, type: . onDelete, verifyChange: { deletedChild in
228
+ deletedChild. identifier == child. identifier
229
+ } )
320
230
321
- await waitForExpectations ( [ connected] , timeout: 10 )
322
231
try await mutate ( . create( child) )
323
232
try await mutate ( . delete( child) )
324
233
await waitForExpectations ( [ onDelete] , timeout: 10 )
@@ -337,36 +246,18 @@ final class GraphQLLazyLoadDefaultPKTests: GraphQLLazyLoadBaseTest {
337
246
*/
338
247
func testSubscriptionDefaultPKParentOnDelete( ) async throws {
339
248
await setup ( withModels: DefaultPKModels ( ) )
340
- let connected = asyncExpectation ( description: " Subscription connected " )
341
- let onDelete = asyncExpectation ( description: " onDelete received " )
249
+
342
250
let parent = DefaultPKParent ( )
343
251
let child = DefaultPKChild ( parent: parent)
344
- let subscription = Amplify . API. subscribe ( request: . subscription( of: DefaultPKParent . self, type: . onDelete) )
345
-
346
- Task {
347
- for try await subscriptionEvent in subscription {
348
- if subscriptionEvent. isConnected ( ) {
349
- await connected. fulfill ( )
350
- }
351
-
352
- if let error = subscriptionEvent. extractError ( ) {
353
- XCTFail ( " Failed to delete DefaultPKParent, error: \( error. errorDescription) " )
354
- }
355
-
356
- guard let data = subscriptionEvent. extractData ( ) ,
357
- data. identifier == parent. identifier
358
- else { continue }
359
-
360
- try await data. children? . fetch ( )
361
- if case . some( . loaded( let associatedChildren) ) = data. children? . loadedState,
362
- associatedChildren. map ( \. identifier) . contains ( child. identifier)
363
- {
364
- await onDelete. fulfill ( )
365
- }
252
+ let ( onDelete, subscription) = try await subscribe ( of: DefaultPKParent . self, type: . onDelete, verifyChange: { deletedParent in
253
+ try await deletedParent. children? . fetch ( )
254
+ if case . some( . loaded( let associatedChildren) ) = deletedParent. children? . loadedState {
255
+ return deletedParent. identifier == parent. identifier
256
+ && associatedChildren. map ( \. identifier) . contains ( child. identifier)
366
257
}
367
- }
258
+ return false
259
+ } )
368
260
369
- await waitForExpectations ( [ connected] , timeout: 10 )
370
261
try await mutate ( . create( child) )
371
262
try await mutate ( . create( parent) )
372
263
try await mutate ( . delete( parent) )
0 commit comments