@@ -106,4 +106,132 @@ extension GraphQLLazyLoadCompositePKTests {
106106 }
107107 assertList ( queriedChild, state: . isLoaded( count: 1 ) )
108108 }
109+
110+ /*
111+ - Given: Api category setup with CompositePKModels
112+ - When:
113+ - Subscribe onCreate events of ChildSansBelongsTo
114+ - Create new CompositePKParent instance with API
115+ - Create new ChildSansBelongsTo instance with API
116+ - Then:
117+ - the newly created instance is successfully created through API. onCreate event is received.
118+ */
119+ func testSubscribeChildSansBelongsToOnCreate( ) async throws {
120+ await setup ( withModels: CompositePKModels ( ) )
121+ let connected = asyncExpectation ( description: " Subscription connected " )
122+ let onCreate = asyncExpectation ( description: " onCreate received " )
123+
124+ let parent = CompositePKParent ( customId: UUID ( ) . uuidString, content: UUID ( ) . uuidString)
125+ let child = initChildSansBelongsTo ( with: parent)
126+ let subscription = Amplify . API. subscribe ( request: . subscription( of: ChildSansBelongsTo . self, type: . onCreate) )
127+ Task {
128+ do {
129+ for try await subscriptionEvent in subscription {
130+ switch subscriptionEvent {
131+ case . connection( . connected) :
132+ await connected. fulfill ( )
133+ case let . data( . success( newModel) ) :
134+ if newModel. identifier == child. identifier {
135+ await onCreate. fulfill ( )
136+ }
137+ case let . data( . failure( error) ) :
138+ XCTFail ( " Failed to create ChildSansBelongsTo, error: \( error. errorDescription) " )
139+ default : ( )
140+ }
141+ }
142+ }
143+ }
144+
145+ await waitForExpectations ( [ connected] , timeout: 10 )
146+ try await mutate ( . create( parent) )
147+ try await mutate ( . create( child) )
148+ await waitForExpectations ( [ onCreate] , timeout: 10 )
149+ subscription. cancel ( )
150+ }
151+
152+ /*
153+ - Given: Api category setup with CompositePKModels
154+ - When:
155+ - Subscribe onCreate events of ChildSansBelongsTo
156+ - Create new CompositePKParent instance with API
157+ - Create new ChildSansBelongsTo instance with API
158+ - Update newly created ChildSansBelongsTo instance with API
159+ - Then:
160+ - the newly created instance is successfully updated through API. onUpdate event is received.
161+ */
162+ func testSubscribeChildSansBelongsToOnUpdate( ) async throws {
163+ await setup ( withModels: CompositePKModels ( ) )
164+ let connected = asyncExpectation ( description: " Subscription connected " )
165+ let onUpdate = asyncExpectation ( description: " onUpdate received " )
166+ let parent = CompositePKParent ( customId: UUID ( ) . uuidString, content: UUID ( ) . uuidString)
167+ let child = initChildSansBelongsTo ( with: parent)
168+ let subscription = Amplify . API. subscribe ( request: . subscription( of: ChildSansBelongsTo . self, type: . onUpdate) )
169+ Task {
170+ do {
171+ for try await subscriptionEvent in subscription {
172+ switch subscriptionEvent {
173+ case . connection( . connected) :
174+ await connected. fulfill ( )
175+ case let . data( . success( newModel) ) :
176+ if newModel. identifier == child. identifier {
177+ await onUpdate. fulfill ( )
178+ }
179+ case let . data( . failure( error) ) :
180+ XCTFail ( " Failed to update ChildSansBelongsTo, error: \( error. errorDescription) " )
181+ default : ( )
182+ }
183+ }
184+ }
185+ }
186+
187+ await waitForExpectations ( [ connected] , timeout: 10 )
188+ try await mutate ( . create( parent) )
189+ try await mutate ( . create( child) )
190+ try await mutate ( . update( child) )
191+ await waitForExpectations ( [ onUpdate] , timeout: 10 )
192+ subscription. cancel ( )
193+ }
194+
195+ /*
196+ - Given: Api category setup with CompositePKModels
197+ - When:
198+ - Subscribe onCreate events of ChildSansBelongsTo
199+ - Create new CompositePKParent instance with API
200+ - Create new ChildSansBelongsTo instance with API
201+ - Delete newly created ChildSansBelongsTo with API
202+ - Then:
203+ - the newly created instance is successfully deleted through API. onDelete event is received.
204+ */
205+ func testSubscribeChildSansBelongsToOnDelete( ) async throws {
206+ await setup ( withModels: CompositePKModels ( ) )
207+ let connected = asyncExpectation ( description: " Subscription connected " )
208+ let onDelete = asyncExpectation ( description: " onUpdate received " )
209+ let parent = CompositePKParent ( customId: UUID ( ) . uuidString, content: UUID ( ) . uuidString)
210+ let child = initChildSansBelongsTo ( with: parent)
211+ let subscription = Amplify . API. subscribe ( request: . subscription( of: ChildSansBelongsTo . self, type: . onDelete) )
212+ Task {
213+ do {
214+ for try await subscriptionEvent in subscription {
215+ switch subscriptionEvent {
216+ case . connection( . connected) :
217+ await connected. fulfill ( )
218+ case let . data( . success( newModel) ) :
219+ if newModel. identifier == child. identifier {
220+ await onDelete. fulfill ( )
221+ }
222+ case let . data( . failure( error) ) :
223+ XCTFail ( " Failed to update ChildSansBelongsTo, error: \( error. errorDescription) " )
224+ default : ( )
225+ }
226+ }
227+ }
228+ }
229+
230+ await waitForExpectations ( [ connected] , timeout: 10 )
231+ try await mutate ( . create( parent) )
232+ try await mutate ( . create( child) )
233+ try await mutate ( . delete( child) )
234+ await waitForExpectations ( [ onDelete] , timeout: 10 )
235+ subscription. cancel ( )
236+ }
109237}
0 commit comments