Skip to content

Commit 4021a31

Browse files
committed
chore(test): refactor subscription integration tests
1 parent 39e7de3 commit 4021a31

File tree

3 files changed

+203
-181
lines changed

3 files changed

+203
-181
lines changed

AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/GraphQLLazyLoadBaseTest.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,28 @@ extension LazyReferenceIdentifier: Equatable {
192192
return lhs.name == rhs.name && lhs.value == rhs.value
193193
}
194194
}
195+
196+
197+
extension GraphQLSubscriptionEvent {
198+
func isConnected() -> Bool {
199+
if case .connection(.connected) = self {
200+
return true
201+
}
202+
return false
203+
}
204+
205+
func extractData() -> T? {
206+
if case .data(.success(let data)) = self {
207+
return data
208+
}
209+
return nil
210+
}
211+
212+
func extractError() -> GraphQLResponseError<T>? {
213+
if case .data(.failure(let error)) = self {
214+
return error
215+
}
216+
return nil
217+
}
218+
219+
}

AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginLazyLoadTests/LL12/DefaultPK/GraphQLLazyLoadDefaultPKTests.swift

Lines changed: 94 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -117,19 +117,19 @@ final class GraphQLLazyLoadDefaultPKTests: GraphQLLazyLoadBaseTest {
117117
let child = DefaultPKChild()
118118
let subscription = Amplify.API.subscribe(request: .subscription(of: DefaultPKChild.self, type: .onCreate))
119119
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-
}
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()
133133
}
134134
}
135135
}
@@ -156,24 +156,24 @@ final class GraphQLLazyLoadDefaultPKTests: GraphQLLazyLoadBaseTest {
156156
let child = DefaultPKChild(parent: parent)
157157
let subscription = Amplify.API.subscribe(request: .subscription(of: DefaultPKParent.self, type: .onCreate))
158158
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-
}
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()
177177
}
178178
}
179179
}
@@ -202,19 +202,19 @@ final class GraphQLLazyLoadDefaultPKTests: GraphQLLazyLoadBaseTest {
202202
let subscription = Amplify.API.subscribe(request: .subscription(of: DefaultPKChild.self, type: .onUpdate))
203203

204204
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-
}
205+
for try await subscriptionEvent in subscription {
206+
if subscriptionEvent.isConnected() {
207+
await connected.fulfill()
208+
}
209+
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()
218218
}
219219
}
220220
}
@@ -250,25 +250,25 @@ final class GraphQLLazyLoadDefaultPKTests: GraphQLLazyLoadBaseTest {
250250
let subscription = Amplify.API.subscribe(request: .subscription(of: DefaultPKParent.self, type: .onUpdate))
251251

252252
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-
}
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()
272272
}
273273
}
274274
}
@@ -301,19 +301,19 @@ final class GraphQLLazyLoadDefaultPKTests: GraphQLLazyLoadBaseTest {
301301
let subscription = Amplify.API.subscribe(request: .subscription(of: DefaultPKChild.self, type: .onDelete))
302302

303303
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-
}
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()
317317
}
318318
}
319319
}
@@ -344,24 +344,24 @@ final class GraphQLLazyLoadDefaultPKTests: GraphQLLazyLoadBaseTest {
344344
let subscription = Amplify.API.subscribe(request: .subscription(of: DefaultPKParent.self, type: .onDelete))
345345

346346
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-
}
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()
365365
}
366366
}
367367
}

0 commit comments

Comments
 (0)