@@ -112,29 +112,11 @@ final class GraphQLLazyLoadDefaultPKTests: GraphQLLazyLoadBaseTest {
112112 */
113113 func testSubscribeDefaultPKChildOnCreate( ) async throws {
114114 await setup ( withModels: DefaultPKModels ( ) )
115- let connected = asyncExpectation ( description: " Subscription connected " )
116- let onCreate = asyncExpectation ( description: " onCreate received " )
117115 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
135118 }
136119
137- await waitForExpectations ( [ connected] , timeout: 10 )
138120 try await mutate ( . create( child) )
139121 await waitForExpectations ( [ onCreate] , timeout: 10 )
140122 subscription. cancel ( )
@@ -150,35 +132,18 @@ final class GraphQLLazyLoadDefaultPKTests: GraphQLLazyLoadBaseTest {
150132 */
151133 func testSubscribeDefaultPKParentOnCreate( ) async throws {
152134 await setup ( withModels: DefaultPKModels ( ) )
153- let connected = asyncExpectation ( description: " Subscription connected " )
154- let onCreate = asyncExpectation ( description: " onCreate received " )
135+
155136 let parent = DefaultPKParent ( )
156137 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)
178143 }
144+ return false
179145 }
180146
181- await waitForExpectations ( [ connected] , timeout: 10 )
182147 try await mutate ( . create( child) )
183148 try await mutate ( . create( parent) )
184149 await waitForExpectations ( [ onCreate] , timeout: 10 )
@@ -196,30 +161,12 @@ final class GraphQLLazyLoadDefaultPKTests: GraphQLLazyLoadBaseTest {
196161 */
197162 func testSubscriptionDefaultPKChildOnUpdate( ) async throws {
198163 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- }
209164
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
220168 }
221169
222- await waitForExpectations ( [ connected] , timeout: 10 )
223170 try await mutate ( . create( child) )
224171 var updatingChild = child
225172 updatingChild. content = UUID ( ) . uuidString
@@ -240,40 +187,21 @@ final class GraphQLLazyLoadDefaultPKTests: GraphQLLazyLoadBaseTest {
240187 */
241188 func testSubscriptionDefaultPKParentOnUpdate( ) async throws {
242189 await setup ( withModels: DefaultPKModels ( ) )
243- let connected = asyncExpectation ( description: " Subscription connected " )
244- let onUpdate = asyncExpectation ( description: " onUpdate received " )
245190
246191 let parent = DefaultPKParent ( )
247192 let child = DefaultPKChild ( parent: parent)
248193 let anotherChild = DefaultPKChild ( parent: parent)
249194
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)
273201 }
202+ return false
274203 }
275204
276- await waitForExpectations ( [ connected] , timeout: 10 )
277205 try await mutate ( . create( child) )
278206 try await mutate ( . create( parent) )
279207 try await mutate ( . create( anotherChild) )
@@ -295,30 +223,11 @@ final class GraphQLLazyLoadDefaultPKTests: GraphQLLazyLoadBaseTest {
295223 */
296224 func testSubscriptionDefaultPKChildOnDelete( ) async throws {
297225 await setup ( withModels: DefaultPKModels ( ) )
298- let connected = asyncExpectation ( description: " Subscription connected " )
299- let onDelete = asyncExpectation ( description: " onDelete received " )
300226 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+ } )
320230
321- await waitForExpectations ( [ connected] , timeout: 10 )
322231 try await mutate ( . create( child) )
323232 try await mutate ( . delete( child) )
324233 await waitForExpectations ( [ onDelete] , timeout: 10 )
@@ -337,36 +246,18 @@ final class GraphQLLazyLoadDefaultPKTests: GraphQLLazyLoadBaseTest {
337246 */
338247 func testSubscriptionDefaultPKParentOnDelete( ) async throws {
339248 await setup ( withModels: DefaultPKModels ( ) )
340- let connected = asyncExpectation ( description: " Subscription connected " )
341- let onDelete = asyncExpectation ( description: " onDelete received " )
249+
342250 let parent = DefaultPKParent ( )
343251 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)
366257 }
367- }
258+ return false
259+ } )
368260
369- await waitForExpectations ( [ connected] , timeout: 10 )
370261 try await mutate ( . create( child) )
371262 try await mutate ( . create( parent) )
372263 try await mutate ( . delete( parent) )
0 commit comments