Skip to content

Commit 301268b

Browse files
committed
chore(test): refactor subscription integration tests
1 parent b5473e4 commit 301268b

5 files changed

+214
-215
lines changed

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

+39-39
Original file line numberDiff line numberDiff line change
@@ -125,19 +125,19 @@ extension GraphQLLazyLoadCompositePKTests {
125125
let child = initChildSansBelongsTo(with: parent)
126126
let subscription = Amplify.API.subscribe(request: .subscription(of: ChildSansBelongsTo.self, type: .onCreate))
127127
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-
}
128+
for try await subscriptionEvent in subscription {
129+
if subscriptionEvent.isConnected() {
130+
await connected.fulfill()
131+
}
132+
133+
if let error = subscriptionEvent.extractError() {
134+
XCTFail("Failed to create ChildSansBelongsTo, error: \(error.errorDescription)")
135+
}
136+
137+
if let data = subscriptionEvent.extractData(),
138+
data.identifier == child.identifier
139+
{
140+
await onCreate.fulfill()
141141
}
142142
}
143143
}
@@ -167,19 +167,19 @@ extension GraphQLLazyLoadCompositePKTests {
167167
let child = initChildSansBelongsTo(with: parent)
168168
let subscription = Amplify.API.subscribe(request: .subscription(of: ChildSansBelongsTo.self, type: .onUpdate))
169169
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-
}
170+
for try await subscriptionEvent in subscription {
171+
if subscriptionEvent.isConnected() {
172+
await connected.fulfill()
173+
}
174+
175+
if let error = subscriptionEvent.extractError() {
176+
XCTFail("Failed to update ChildSansBelongsTo, error: \(error.errorDescription)")
177+
}
178+
179+
if let data = subscriptionEvent.extractData(),
180+
data.identifier == child.identifier
181+
{
182+
await onUpdate.fulfill()
183183
}
184184
}
185185
}
@@ -210,19 +210,19 @@ extension GraphQLLazyLoadCompositePKTests {
210210
let child = initChildSansBelongsTo(with: parent)
211211
let subscription = Amplify.API.subscribe(request: .subscription(of: ChildSansBelongsTo.self, type: .onDelete))
212212
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-
}
213+
for try await subscriptionEvent in subscription {
214+
if subscriptionEvent.isConnected() {
215+
await connected.fulfill()
216+
}
217+
218+
if let error = subscriptionEvent.extractError() {
219+
XCTFail("Failed to delete ChildSansBelongsTo, error: \(error.errorDescription)")
220+
}
221+
222+
if let data = subscriptionEvent.extractData(),
223+
data.identifier == child.identifier
224+
{
225+
await onDelete.fulfill()
226226
}
227227
}
228228
}

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

+41-42
Original file line numberDiff line numberDiff line change
@@ -137,19 +137,19 @@ extension GraphQLLazyLoadCompositePKTests {
137137
let child = CompositePKChild(childId: UUID().uuidString, content: UUID().uuidString)
138138
let subscription = Amplify.API.subscribe(request: .subscription(of: CompositePKChild.self, type: .onCreate))
139139
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-
}
140+
for try await subscriptionEvent in subscription {
141+
if subscriptionEvent.isConnected() {
142+
await connected.fulfill()
143+
}
144+
145+
if let error = subscriptionEvent.extractError() {
146+
XCTFail("Failed to create CompositePKChild, error: \(error.errorDescription)")
147+
}
148+
149+
if let data = subscriptionEvent.extractData(),
150+
data.identifier == child.identifier
151+
{
152+
await onCreate.fulfill()
153153
}
154154
}
155155
}
@@ -178,22 +178,21 @@ extension GraphQLLazyLoadCompositePKTests {
178178
let parent = CompositePKParent(customId: UUID().uuidString, content: UUID().uuidString)
179179
let subscription = Amplify.API.subscribe(request: .subscription(of: CompositePKChild.self, type: .onUpdate))
180180
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-
}
181+
for try await subscriptionEvent in subscription {
182+
if subscriptionEvent.isConnected() {
183+
await connected.fulfill()
184+
}
185+
186+
if let error = subscriptionEvent.extractError() {
187+
XCTFail("Failed to update CompositePKChild, error: \(error.errorDescription)")
188+
}
189+
190+
if let data = subscriptionEvent.extractData(),
191+
data.identifier == child.identifier,
192+
let associatedParent = try await data.parent,
193+
associatedParent.identifier == parent.identifier
194+
{
195+
await onUpdate.fulfill()
197196
}
198197
}
199198
}
@@ -225,19 +224,19 @@ extension GraphQLLazyLoadCompositePKTests {
225224
let child = CompositePKChild(childId: UUID().uuidString, content: UUID().uuidString)
226225
let subscription = Amplify.API.subscribe(request: .subscription(of: CompositePKChild.self, type: .onDelete))
227226
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-
}
227+
for try await subscriptionEvent in subscription {
228+
if subscriptionEvent.isConnected() {
229+
await connected.fulfill()
230+
}
231+
232+
if let error = subscriptionEvent.extractError() {
233+
XCTFail("Failed to delete CompositePKChild, error: \(error.errorDescription)")
234+
}
235+
236+
if let data = subscriptionEvent.extractData(),
237+
data.identifier == child.identifier
238+
{
239+
await onDelete.fulfill()
241240
}
242241
}
243242
}

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

+39-38
Original file line numberDiff line numberDiff line change
@@ -126,19 +126,19 @@ extension GraphQLLazyLoadCompositePKTests {
126126
let child = ImplicitChild(childId: UUID().uuidString, content: UUID().uuidString, parent: parent)
127127
let subscription = Amplify.API.subscribe(request: .subscription(of: ImplicitChild.self, type: .onCreate))
128128
Task {
129-
do {
130-
for try await subscriptionEvent in subscription {
131-
switch subscriptionEvent {
132-
case .connection(.connected):
133-
await connected.fulfill()
134-
case let .data(.success(newModel)):
135-
if newModel.identifier == child.identifier {
136-
await onCreate.fulfill()
137-
}
138-
case let .data(.failure(error)):
139-
XCTFail("Failed to create CompositePKChild, error: \(error.errorDescription)")
140-
default: ()
141-
}
129+
for try await subscriptionEvent in subscription {
130+
if subscriptionEvent.isConnected() {
131+
await connected.fulfill()
132+
}
133+
134+
if let error = subscriptionEvent.extractError() {
135+
XCTFail("Failed to create ImplicitChild, error: \(error.errorDescription)")
136+
}
137+
138+
if let data = subscriptionEvent.extractData(),
139+
data.identifier == child.identifier
140+
{
141+
await onCreate.fulfill()
142142
}
143143
}
144144
}
@@ -170,19 +170,20 @@ extension GraphQLLazyLoadCompositePKTests {
170170
Task {
171171
do {
172172
for try await subscriptionEvent in subscription {
173-
switch subscriptionEvent {
174-
case .connection(.connected):
173+
if subscriptionEvent.isConnected() {
175174
await connected.fulfill()
176-
case let .data(.success(newModel)):
177-
let associatedParent = try await newModel.parent
178-
if newModel.identifier == child.identifier,
179-
associatedParent.identifier == parent.identifier
180-
{
181-
await onUpdate.fulfill()
182-
}
183-
case let .data(.failure(error)):
184-
XCTFail("Failed to update CompositePKParent, error: \(error.errorDescription)")
185-
default: ()
175+
}
176+
177+
if let error = subscriptionEvent.extractError() {
178+
XCTFail("Failed to update ImplicitChild, error: \(error.errorDescription)")
179+
}
180+
181+
if let data = subscriptionEvent.extractData(),
182+
data.identifier == child.identifier,
183+
let associatedParent = try? await data.parent,
184+
associatedParent.identifier == parent.identifier
185+
{
186+
await onUpdate.fulfill()
186187
}
187188
}
188189
}
@@ -217,19 +218,19 @@ extension GraphQLLazyLoadCompositePKTests {
217218
let child = ImplicitChild(childId: UUID().uuidString, content: UUID().uuidString, parent: parent)
218219
let subscription = Amplify.API.subscribe(request: .subscription(of: ImplicitChild.self, type: .onDelete))
219220
Task {
220-
do {
221-
for try await subscriptionEvent in subscription {
222-
switch subscriptionEvent {
223-
case .connection(.connected):
224-
await connected.fulfill()
225-
case let .data(.success(newModel)):
226-
if newModel.identifier == child.identifier {
227-
await onDelete.fulfill()
228-
}
229-
case let .data(.failure(error)):
230-
XCTFail("Failed to update CompositePKParent, error: \(error.errorDescription)")
231-
default: ()
232-
}
221+
for try await subscriptionEvent in subscription {
222+
if subscriptionEvent.isConnected() {
223+
await connected.fulfill()
224+
}
225+
226+
if let error = subscriptionEvent.extractError() {
227+
XCTFail("Failed to update ImplicitChild, error: \(error.errorDescription)")
228+
}
229+
230+
if let data = subscriptionEvent.extractData(),
231+
data.identifier == child.identifier
232+
{
233+
await onDelete.fulfill()
233234
}
234235
}
235236
}

0 commit comments

Comments
 (0)