Skip to content

Commit 74d80bd

Browse files
authored
test(datastore): Fix DeleteAll test stability (#2356)
1 parent f03d67f commit 74d80bd

File tree

1 file changed

+35
-50
lines changed

1 file changed

+35
-50
lines changed

AmplifyPlugins/DataStore/AWSDataStoreCategoryPluginIntegrationTests/Connection/DataStoreConnectionScenario6Tests.swift

Lines changed: 35 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -217,12 +217,17 @@ class DataStoreConnectionScenario6Tests: SyncEngineIntegrationTestBase {
217217
}
218218
}
219219

220+
/// Ensure that the delete with `.all` predicate works as expected.
221+
/// There may be additional blogs/post/comments from other tests that are being deleted as part of this test
222+
/// We ignore those and only assert that that 6 models created in this test were deleted successfully.
220223
func testDeleteAll() throws {
221224
setUp(withModels: TestModelRegistration())
222225
try startAmplifyAndWaitForReady()
223226
var cancellables = Set<AnyCancellable>()
224227
let remoteEventReceived = expectation(description: "received mutation event with version 1")
225-
let commentId = UUID().uuidString
228+
remoteEventReceived.expectedFulfillmentCount = 2
229+
let commentId1 = UUID().uuidString
230+
let commentId2 = UUID().uuidString
226231
Amplify.DataStore.publisher(for: Comment6.self).sink { completion in
227232
switch completion {
228233
case .finished:
@@ -231,70 +236,50 @@ class DataStoreConnectionScenario6Tests: SyncEngineIntegrationTestBase {
231236
XCTFail("Failed \(error)")
232237
}
233238
} receiveValue: { mutationEvent in
234-
if mutationEvent.modelId == commentId && mutationEvent.version == 1 {
239+
if mutationEvent.modelId == commentId1 && mutationEvent.version == 1 {
240+
remoteEventReceived.fulfill()
241+
}
242+
if mutationEvent.modelId == commentId2 && mutationEvent.version == 1 {
235243
remoteEventReceived.fulfill()
236244
}
237245
}.store(in: &cancellables)
238246
guard let blog = saveBlog(name: "name"),
239247
let post = savePost(title: "title", blog: blog),
240-
saveComment(id: commentId, post: post, content: "content") != nil else {
241-
XCTFail("Could not create blog, post, and comment")
248+
saveComment(id: commentId1, post: post, content: "content") != nil else {
249+
XCTFail("Could not create first set of blog, post, and comment")
242250
return
243251
}
244-
wait(for: [remoteEventReceived], timeout: 5)
245-
246-
var blogCount = 0
247-
let retrievedBlogCount = expectation(description: "retrieved blog count")
248-
Amplify.DataStore.query(Blog6.self) { result in
249-
switch result {
250-
case .success(let blogs):
251-
blogCount = blogs.count
252-
retrievedBlogCount.fulfill()
253-
case .failure(let error):
254-
XCTFail("\(error)")
255-
}
256-
}
257-
wait(for: [retrievedBlogCount], timeout: 10)
258-
var postCount = 0
259-
let retrievedPostCount = expectation(description: "retrieved post count")
260-
Amplify.DataStore.query(Post6.self) { result in
261-
switch result {
262-
case .success(let posts):
263-
postCount = posts.count
264-
retrievedPostCount.fulfill()
265-
case .failure(let error):
266-
XCTFail("\(error)")
267-
}
268-
}
269-
wait(for: [retrievedPostCount], timeout: 10)
270-
271-
var commentCount = 0
272-
let retrievedCommentCount = expectation(description: "retrieved comment count")
273-
Amplify.DataStore.query(Comment6.self) { result in
274-
switch result {
275-
case .success(let comments):
276-
commentCount = comments.count
277-
retrievedCommentCount.fulfill()
278-
case .failure(let error):
279-
XCTFail("\(error)")
280-
}
252+
guard let blog2 = saveBlog(name: "name"),
253+
let post2 = savePost(title: "title", blog: blog2),
254+
saveComment(id: commentId2, post: post2, content: "content") != nil else {
255+
XCTFail("Could not create second set of blog, post, and comment")
256+
return
281257
}
282-
283-
wait(for: [retrievedCommentCount], timeout: 10)
284-
285-
let totalCount = blogCount + postCount + commentCount
286-
Amplify.Logging.verbose("Retrieved blog \(blogCount) post \(postCount) comment \(commentCount)")
258+
wait(for: [remoteEventReceived], timeout: 10)
287259

288260
let outboxMutationProcessed = expectation(description: "received outboxMutationProcessed")
289261
var processedSoFar = 0
290262
Amplify.Hub.publisher(for: .dataStore)
291263
.sink { payload in
292264
let event = DataStoreHubEvent(payload: payload)
293265
switch event {
294-
case .outboxMutationProcessed:
295-
processedSoFar += 1
296-
print("Processed so far \(processedSoFar)")
297-
if processedSoFar == totalCount {
266+
case .outboxMutationProcessed(let mutationEvent):
267+
if mutationEvent.modelName == Blog6.modelName,
268+
let model = mutationEvent.element.model as? Blog6,
269+
model.id == blog.id || model.id == blog2.id {
270+
processedSoFar += 1
271+
} else if mutationEvent.modelName == Post6.modelName,
272+
let model = mutationEvent.element.model as? Post6,
273+
model.id == post.id || model.id == post2.id {
274+
processedSoFar += 1
275+
} else if mutationEvent.modelName == Comment6.modelName,
276+
let model = mutationEvent.element.model as? Comment6,
277+
model.id == commentId1 || model.id == commentId2 {
278+
processedSoFar += 1
279+
}
280+
281+
Amplify.Logging.verbose("Processed so far \(processedSoFar)/6")
282+
if processedSoFar == 6 {
298283
outboxMutationProcessed.fulfill()
299284
}
300285
default:

0 commit comments

Comments
 (0)