Skip to content

Commit b6a48dc

Browse files
committed
chore(test): add more integration test cases for API lazy loading
1 parent 0e31001 commit b6a48dc

5 files changed

+665
-0
lines changed

AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKChildSansTests.swift

+128
Original file line numberDiff line numberDiff line change
@@ -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
}

AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/CompositePK/GraphQLLazyLoadCompositePKChildTests.swift

+127
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,131 @@ extension GraphQLLazyLoadCompositePKTests {
121121
}
122122
assertList(queriedChild, state: .isLoaded(count: 1))
123123
}
124+
125+
/*
126+
- Given: Api category setup with CompositePKModels
127+
- When:
128+
- Subscribe onCreate events of CompositePKChild
129+
- Create new CompositePKChild instance with API
130+
- Then:
131+
- the newly created instance is successfully created through API. onCreate event is received.
132+
*/
133+
func testSubscribeCompositePKChildOnCreate() async throws {
134+
await setup(withModels: CompositePKModels())
135+
let connected = asyncExpectation(description: "Subscription connected")
136+
let onCreate = asyncExpectation(description: "onCreate received")
137+
let child = CompositePKChild(childId: UUID().uuidString, content: UUID().uuidString)
138+
let subscription = Amplify.API.subscribe(request: .subscription(of: CompositePKChild.self, type: .onCreate))
139+
Task {
140+
do {
141+
for try await subscriptionEvent in subscription {
142+
switch subscriptionEvent {
143+
case .connection(.connected):
144+
await connected.fulfill()
145+
case let .data(.success(newModel)):
146+
if newModel.identifier == child.identifier {
147+
await onCreate.fulfill()
148+
}
149+
case let .data(.failure(error)):
150+
XCTFail("Failed to create CompositePKChild, error: \(error.errorDescription)")
151+
default: ()
152+
}
153+
}
154+
}
155+
}
156+
157+
await waitForExpectations([connected], timeout: 10)
158+
try await mutate(.create(child))
159+
await waitForExpectations([onCreate], timeout: 10)
160+
subscription.cancel()
161+
}
162+
163+
/*
164+
- Given: Api category setup with CompositePKModels
165+
- When:
166+
- Subscribe onCreate events of CompositePKChild
167+
- Create new CompositePKChild instance with API
168+
- Create new CompositePKParent instance with API
169+
- Update newly created CompositePKParent instance with API
170+
- Then:
171+
- the newly created instance is successfully updated through API. onUpdate event is received.
172+
*/
173+
func testSubscribeCompositePKChildOnUpdate() async throws {
174+
await setup(withModels: CompositePKModels())
175+
let connected = asyncExpectation(description: "Subscription connected")
176+
let onUpdate = asyncExpectation(description: "onUpdate received")
177+
let child = CompositePKChild(childId: UUID().uuidString, content: UUID().uuidString)
178+
let parent = CompositePKParent(customId: UUID().uuidString, content: UUID().uuidString)
179+
let subscription = Amplify.API.subscribe(request: .subscription(of: CompositePKChild.self, type: .onUpdate))
180+
Task {
181+
do {
182+
for try await subscriptionEvent in subscription {
183+
switch subscriptionEvent {
184+
case .connection(.connected):
185+
await connected.fulfill()
186+
case let .data(.success(newModel)):
187+
let associatedParent = try await newModel.parent
188+
if newModel.identifier == child.identifier,
189+
associatedParent?.identifier == parent.identifier
190+
{
191+
await onUpdate.fulfill()
192+
}
193+
case let .data(.failure(error)):
194+
XCTFail("Failed to update CompositePKParent, error: \(error.errorDescription)")
195+
default: ()
196+
}
197+
}
198+
}
199+
}
200+
201+
await waitForExpectations([connected], timeout: 10)
202+
try await mutate(.create(child))
203+
try await mutate(.create(parent))
204+
205+
var updatingChild = child
206+
updatingChild.setParent(parent)
207+
try await mutate(.update(updatingChild))
208+
await waitForExpectations([onUpdate], timeout: 10)
209+
subscription.cancel()
210+
}
211+
212+
/*
213+
- Given: Api category setup with CompositePKModels
214+
- When:
215+
- Subscribe onCreate events of CompositePKChild
216+
- Create new CompositePKChild instance with API
217+
- Delete newly created CompositePKChild with API
218+
- Then:
219+
- the newly created instance is successfully deleted through API. onDelete event is received.
220+
*/
221+
func testSubscribeCompositePKChildOnDelete() async throws {
222+
await setup(withModels: CompositePKModels())
223+
let connected = asyncExpectation(description: "Subscription connected")
224+
let onDelete = asyncExpectation(description: "onUpdate received")
225+
let child = CompositePKChild(childId: UUID().uuidString, content: UUID().uuidString)
226+
let subscription = Amplify.API.subscribe(request: .subscription(of: CompositePKChild.self, type: .onDelete))
227+
Task {
228+
do {
229+
for try await subscriptionEvent in subscription {
230+
switch subscriptionEvent {
231+
case .connection(.connected):
232+
await connected.fulfill()
233+
case let .data(.success(newModel)):
234+
if newModel.identifier == child.identifier {
235+
await onDelete.fulfill()
236+
}
237+
case let .data(.failure(error)):
238+
XCTFail("Failed to update CompositePKParent, error: \(error.errorDescription)")
239+
default: ()
240+
}
241+
}
242+
}
243+
}
244+
245+
await waitForExpectations([connected], timeout: 10)
246+
try await mutate(.create(child))
247+
try await mutate(.delete(child))
248+
await waitForExpectations([onDelete], timeout: 10)
249+
subscription.cancel()
250+
}
124251
}

0 commit comments

Comments
 (0)