@@ -101,6 +101,169 @@ final class GraphQLLazyLoadDefaultPKTests: GraphQLLazyLoadBaseTest {
101101 where: DefaultPKChild . keys. id == defaultPKChild. id) )
102102 assertList ( queriedChildren, state: . isLoaded( count: 1 ) )
103103 }
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 child = DefaultPKChild ( )
116+ let ( onCreate, subscription) = try await subscribe ( of: DefaultPKChild . self, type: . onCreate) { newChild in
117+ newChild. identifier == child. identifier
118+ }
119+
120+ try await mutate ( . create( child) )
121+ await waitForExpectations ( [ onCreate] , timeout: 10 )
122+ subscription. cancel ( )
123+ }
124+
125+ /*
126+ - Given: Api category setup with DefaultPKModels
127+ - When:
128+ - Subscribe onCreate events of DefaultPKParent
129+ - Create new DefaultPKChild and DefaultPKParent instances with API
130+ - Then:
131+ - the newly created parent is successfully created through API. onCreate event is received.
132+ */
133+ func testSubscribeDefaultPKParentOnCreate( ) async throws {
134+ await setup ( withModels: DefaultPKModels ( ) )
135+
136+ let parent = DefaultPKParent ( )
137+ let child = DefaultPKChild ( parent: parent)
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)
143+ }
144+ return false
145+ }
146+
147+ try await mutate ( . create( child) )
148+ try await mutate ( . create( parent) )
149+ await waitForExpectations ( [ onCreate] , timeout: 10 )
150+ subscription. cancel ( )
151+ }
152+
153+ /*
154+ - Given: Api category setup with DefaultPKModels
155+ - When:
156+ - Subscribe onUpdate events of DefaultPKChild
157+ - Create new DefaultPKChild instance with API
158+ - Update the newly created with API
159+ - Then:
160+ - an onUpdate event is received, the identifier is same to the updated one.
161+ */
162+ func testSubscriptionDefaultPKChildOnUpdate( ) async throws {
163+ await setup ( withModels: DefaultPKModels ( ) )
164+
165+ let child = DefaultPKChild ( )
166+ let ( onUpdate, subscription) = try await subscribe ( of: DefaultPKChild . self, type: . onUpdate) { updatedChild in
167+ updatedChild. identifier == child. identifier
168+ }
169+
170+ try await mutate ( . create( child) )
171+ var updatingChild = child
172+ updatingChild. content = UUID ( ) . uuidString
173+ try await mutate ( . update( updatingChild) )
174+ await waitForExpectations ( [ onUpdate] , timeout: 10 )
175+ subscription. cancel ( )
176+ }
177+
178+ /*
179+ - Given: Api category setup with DefaultPKModels
180+ - When:
181+ - Subscribe onUpdate events of HasOneParent
182+ - Create new DefaultPKChild instance with API
183+ - Create new DefaultPKParent instance with API
184+ - Update the newly created parent to another DefaultPKChild instance
185+ - Then:
186+ - an onUpdate event is received, the identifier is same to the updated one.
187+ */
188+ func testSubscriptionDefaultPKParentOnUpdate( ) async throws {
189+ await setup ( withModels: DefaultPKModels ( ) )
190+
191+ let parent = DefaultPKParent ( )
192+ let child = DefaultPKChild ( parent: parent)
193+ let anotherChild = DefaultPKChild ( parent: parent)
194+
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)
201+ }
202+ return false
203+ }
204+
205+ try await mutate ( . create( child) )
206+ try await mutate ( . create( parent) )
207+ try await mutate ( . create( anotherChild) )
208+ var updatingParent = parent
209+ updatingParent. content = UUID ( ) . uuidString
210+ try await mutate ( . update( updatingParent) )
211+ await waitForExpectations ( [ onUpdate] , timeout: 10 )
212+ subscription. cancel ( )
213+ }
214+
215+ /*
216+ - Given: Api category setup with DefaultPKModels
217+ - When:
218+ - Subscribe onDelete events of DefaultPKChild
219+ - Create new DefaultPKChild instance with API
220+ - Delete the newly created one with API
221+ - Then:
222+ - an onDelete event is received, the identifier is same to the newly created one.
223+ */
224+ func testSubscriptionDefaultPKChildOnDelete( ) async throws {
225+ await setup ( withModels: DefaultPKModels ( ) )
226+ let child = DefaultPKChild ( )
227+ let ( onDelete, subscription) = try await subscribe ( of: DefaultPKChild . self, type: . onDelete, verifyChange: { deletedChild in
228+ deletedChild. identifier == child. identifier
229+ } )
230+
231+ try await mutate ( . create( child) )
232+ try await mutate ( . delete( child) )
233+ await waitForExpectations ( [ onDelete] , timeout: 10 )
234+ subscription. cancel ( )
235+ }
236+
237+ /*
238+ - Given: Api category setup with DefaultPKModels
239+ - When:
240+ - Subscribe onDelete events of DefaultPKParent
241+ - Create new DefaultPKChild instance with API
242+ - Create new DefaultPKParent instance with API
243+ - Delete the newly created parent with API
244+ - Then:
245+ - an onDelete event is received, the identifier is same to the newly created one.
246+ */
247+ func testSubscriptionDefaultPKParentOnDelete( ) async throws {
248+ await setup ( withModels: DefaultPKModels ( ) )
249+
250+ let parent = DefaultPKParent ( )
251+ let child = DefaultPKChild ( parent: parent)
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)
257+ }
258+ return false
259+ } )
260+
261+ try await mutate ( . create( child) )
262+ try await mutate ( . create( parent) )
263+ try await mutate ( . delete( parent) )
264+ await waitForExpectations ( [ onDelete] , timeout: 10 )
265+ subscription. cancel ( )
266+ }
104267}
105268
106269extension GraphQLLazyLoadDefaultPKTests : DefaultLogger { }
0 commit comments