@@ -101,6 +101,278 @@ final class GraphQLLazyLoadDefaultPKTests: GraphQLLazyLoadBaseTest {
101
101
where: DefaultPKChild . keys. id == defaultPKChild. id) )
102
102
assertList ( queriedChildren, state: . isLoaded( count: 1 ) )
103
103
}
104
+
105
+ /*
106
+ - Given: Api category setup with DefaultPKModels
107
+ - When:
108
+ - Subscribe onCreate events of DefaultPKChild
109
+ - Create new DefaultPKChild instance with API
110
+ - Then:
111
+ - the newly created instance is successfully created through API. onCreate event is received.
112
+ */
113
+ func testSubscribeDefaultPKChildOnCreate( ) async throws {
114
+ await setup ( withModels: DefaultPKModels ( ) )
115
+ let connected = asyncExpectation ( description: " Subscription connected " )
116
+ let onCreate = asyncExpectation ( description: " onCreate received " )
117
+ let child = DefaultPKChild ( )
118
+ let subscription = Amplify . API. subscribe ( request: . subscription( of: DefaultPKChild . self, type: . onCreate) )
119
+ Task {
120
+ do {
121
+ for try await subscriptionEvent in subscription {
122
+ switch subscriptionEvent {
123
+ case . connection( . connected) :
124
+ await connected. fulfill ( )
125
+ case let . data( . success( newModel) ) :
126
+ if newModel. identifier == child. identifier {
127
+ await onCreate. fulfill ( )
128
+ }
129
+ case let . data( . failure( error) ) :
130
+ XCTFail ( " Failed to create DefaultPKChild, error: \( error. errorDescription) " )
131
+ default : ( )
132
+ }
133
+ }
134
+ }
135
+ }
136
+
137
+ await waitForExpectations ( [ connected] , timeout: 10 )
138
+ try await mutate ( . create( child) )
139
+ await waitForExpectations ( [ onCreate] , timeout: 10 )
140
+ subscription. cancel ( )
141
+ }
142
+
143
+ /*
144
+ - Given: Api category setup with DefaultPKModels
145
+ - When:
146
+ - Subscribe onCreate events of DefaultPKParent
147
+ - Create new DefaultPKChild and DefaultPKParent instances with API
148
+ - Then:
149
+ - the newly created parent is successfully created through API. onCreate event is received.
150
+ */
151
+ func testSubscribeDefaultPKParentOnCreate( ) async throws {
152
+ await setup ( withModels: DefaultPKModels ( ) )
153
+ let connected = asyncExpectation ( description: " Subscription connected " )
154
+ let onCreate = asyncExpectation ( description: " onCreate received " )
155
+ let parent = DefaultPKParent ( )
156
+ let child = DefaultPKChild ( parent: parent)
157
+ let subscription = Amplify . API. subscribe ( request: . subscription( of: DefaultPKParent . self, type: . onCreate) )
158
+ Task {
159
+ do {
160
+ for try await subscriptionEvent in subscription {
161
+ switch subscriptionEvent {
162
+ case . connection( . connected) :
163
+ await connected. fulfill ( )
164
+ case let . data( . success( newModel) ) :
165
+ try await newModel. children? . fetch ( )
166
+ let associatedChilden = newModel. children? . loadedState
167
+ if newModel. identifier == parent. identifier,
168
+ case . some( . loaded( let associatedChilden) ) = associatedChilden,
169
+ associatedChilden. map ( \. identifier) . contains ( child. identifier)
170
+ {
171
+ await onCreate. fulfill ( )
172
+ }
173
+ case let . data( . failure( error) ) :
174
+ XCTFail ( " Failed to create DefaultPKParent, error: \( error. errorDescription) " )
175
+ default : ( )
176
+ }
177
+ }
178
+ }
179
+ }
180
+
181
+ await waitForExpectations ( [ connected] , timeout: 10 )
182
+ try await mutate ( . create( child) )
183
+ try await mutate ( . create( parent) )
184
+ await waitForExpectations ( [ onCreate] , timeout: 10 )
185
+ subscription. cancel ( )
186
+ }
187
+
188
+ /*
189
+ - Given: Api category setup with DefaultPKModels
190
+ - When:
191
+ - Subscribe onUpdate events of DefaultPKChild
192
+ - Create new DefaultPKChild instance with API
193
+ - Update the newly created with API
194
+ - Then:
195
+ - an onUpdate event is received, the identifier is same to the updated one.
196
+ */
197
+ func testSubscriptionDefaultPKChildOnUpdate( ) async throws {
198
+ 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
+ do {
206
+ for try await subscriptionEvent in subscription {
207
+ switch subscriptionEvent {
208
+ case . connection( . connected) :
209
+ await connected. fulfill ( )
210
+ case let . data( . success( newModel) ) :
211
+ if newModel. identifier == child. identifier {
212
+ await onUpdate. fulfill ( )
213
+ }
214
+ case let . data( . failure( error) ) :
215
+ XCTFail ( " Failed to update DefaultPKChild, error: \( error. errorDescription) " )
216
+ default : ( )
217
+ }
218
+ }
219
+ }
220
+ }
221
+
222
+ await waitForExpectations ( [ connected] , timeout: 10 )
223
+ try await mutate ( . create( child) )
224
+ var updatingChild = child
225
+ updatingChild. content = UUID ( ) . uuidString
226
+ try await mutate ( . update( updatingChild) )
227
+ await waitForExpectations ( [ onUpdate] , timeout: 10 )
228
+ subscription. cancel ( )
229
+ }
230
+
231
+ /*
232
+ - Given: Api category setup with DefaultPKModels
233
+ - When:
234
+ - Subscribe onUpdate events of HasOneParent
235
+ - Create new DefaultPKChild instance with API
236
+ - Create new DefaultPKParent instance with API
237
+ - Update the newly created parent to another DefaultPKChild instance
238
+ - Then:
239
+ - an onUpdate event is received, the identifier is same to the updated one.
240
+ */
241
+ func testSubscriptionDefaultPKParentOnUpdate( ) async throws {
242
+ await setup ( withModels: DefaultPKModels ( ) )
243
+ let connected = asyncExpectation ( description: " Subscription connected " )
244
+ let onUpdate = asyncExpectation ( description: " onUpdate received " )
245
+
246
+ let parent = DefaultPKParent ( )
247
+ let child = DefaultPKChild ( parent: parent)
248
+ let anotherChild = DefaultPKChild ( parent: parent)
249
+
250
+ let subscription = Amplify . API. subscribe ( request: . subscription( of: DefaultPKParent . self, type: . onUpdate) )
251
+
252
+ Task {
253
+ do {
254
+ for try await subscriptionEvent in subscription {
255
+ switch subscriptionEvent {
256
+ case . connection( . connected) :
257
+ await connected. fulfill ( )
258
+ case let . data( . success( newModel) ) :
259
+ try await newModel. children? . fetch ( )
260
+ let associatedChilden = newModel. children? . loadedState
261
+ if newModel. identifier == parent. identifier,
262
+ case . some( . loaded( let associatedChilden) ) = associatedChilden,
263
+ associatedChilden. map ( \. identifier) . contains ( child. identifier) ,
264
+ associatedChilden. map ( \. identifier) . contains ( anotherChild. identifier)
265
+ {
266
+ await onUpdate. fulfill ( )
267
+ }
268
+ case let . data( . failure( error) ) :
269
+ XCTFail ( " Failed to update HasOneParent, error: \( error. errorDescription) " )
270
+ default : ( )
271
+ }
272
+ }
273
+ }
274
+ }
275
+
276
+ await waitForExpectations ( [ connected] , timeout: 10 )
277
+ try await mutate ( . create( child) )
278
+ try await mutate ( . create( parent) )
279
+ try await mutate ( . create( anotherChild) )
280
+ var updatingParent = parent
281
+ updatingParent. content = UUID ( ) . uuidString
282
+ try await mutate ( . update( updatingParent) )
283
+ await waitForExpectations ( [ onUpdate] , timeout: 10 )
284
+ subscription. cancel ( )
285
+ }
286
+
287
+ /*
288
+ - Given: Api category setup with DefaultPKModels
289
+ - When:
290
+ - Subscribe onDelete events of DefaultPKChild
291
+ - Create new DefaultPKChild instance with API
292
+ - Delete the newly created one with API
293
+ - Then:
294
+ - an onDelete event is received, the identifier is same to the newly created one.
295
+ */
296
+ func testSubscriptionDefaultPKChildOnDelete( ) async throws {
297
+ await setup ( withModels: DefaultPKModels ( ) )
298
+ let connected = asyncExpectation ( description: " Subscription connected " )
299
+ let onDelete = asyncExpectation ( description: " onDelete received " )
300
+ let child = DefaultPKChild ( )
301
+ let subscription = Amplify . API. subscribe ( request: . subscription( of: DefaultPKChild . self, type: . onDelete) )
302
+
303
+ Task {
304
+ do {
305
+ for try await subscriptionEvent in subscription {
306
+ switch subscriptionEvent {
307
+ case . connection( . connected) :
308
+ await connected. fulfill ( )
309
+ case let . data( . success( newModel) ) :
310
+ if newModel. identifier == child. identifier {
311
+ await onDelete. fulfill ( )
312
+ }
313
+ case let . data( . failure( error) ) :
314
+ XCTFail ( " Failed to delete DefaultPKChild, error: \( error. errorDescription) " )
315
+ default : ( )
316
+ }
317
+ }
318
+ }
319
+ }
320
+
321
+ await waitForExpectations ( [ connected] , timeout: 10 )
322
+ try await mutate ( . create( child) )
323
+ try await mutate ( . delete( child) )
324
+ await waitForExpectations ( [ onDelete] , timeout: 10 )
325
+ subscription. cancel ( )
326
+ }
327
+
328
+ /*
329
+ - Given: Api category setup with DefaultPKModels
330
+ - When:
331
+ - Subscribe onDelete events of DefaultPKParent
332
+ - Create new DefaultPKChild instance with API
333
+ - Create new DefaultPKParent instance with API
334
+ - Delete the newly created parent with API
335
+ - Then:
336
+ - an onDelete event is received, the identifier is same to the newly created one.
337
+ */
338
+ func testSubscriptionDefaultPKParentOnDelete( ) async throws {
339
+ await setup ( withModels: DefaultPKModels ( ) )
340
+ let connected = asyncExpectation ( description: " Subscription connected " )
341
+ let onDelete = asyncExpectation ( description: " onDelete received " )
342
+ let parent = DefaultPKParent ( )
343
+ let child = DefaultPKChild ( parent: parent)
344
+ let subscription = Amplify . API. subscribe ( request: . subscription( of: DefaultPKParent . self, type: . onDelete) )
345
+
346
+ Task {
347
+ do {
348
+ for try await subscriptionEvent in subscription {
349
+ switch subscriptionEvent {
350
+ case . connection( . connected) :
351
+ await connected. fulfill ( )
352
+ case let . data( . success( newModel) ) :
353
+ try await newModel. children? . fetch ( )
354
+ let associatedChilden = newModel. children? . loadedState
355
+ if newModel. identifier == parent. identifier,
356
+ case . some( . loaded( let associatedChildren) ) = associatedChilden,
357
+ associatedChildren. map ( \. identifier) . contains ( child. identifier)
358
+ {
359
+ await onDelete. fulfill ( )
360
+ }
361
+ case let . data( . failure( error) ) :
362
+ XCTFail ( " Failed to delete DefaultPKParent, error: \( error. errorDescription) " )
363
+ default : ( )
364
+ }
365
+ }
366
+ }
367
+ }
368
+
369
+ await waitForExpectations ( [ connected] , timeout: 10 )
370
+ try await mutate ( . create( child) )
371
+ try await mutate ( . create( parent) )
372
+ try await mutate ( . delete( parent) )
373
+ await waitForExpectations ( [ onDelete] , timeout: 10 )
374
+ subscription. cancel ( )
375
+ }
104
376
}
105
377
106
378
extension GraphQLLazyLoadDefaultPKTests : DefaultLogger { }
0 commit comments