Skip to content

Commit 12d140d

Browse files
authored
test(DataStore): Integration tests to verify eager loading in belongs-to relationship (#1425)
* test(DataStore): add integration tests for verifying eager loading in belongs to relationship between models * Update setup() method in SyncEngineTestBase * Address review comments * Address review comments
1 parent c6d870a commit 12d140d

File tree

3 files changed

+115
-2
lines changed

3 files changed

+115
-2
lines changed

AmplifyPlugins/DataStore/AWSDataStoreCategoryPluginIntegrationTests/Connection/DataStoreConnectionScenario6Tests.swift

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,119 @@ class DataStoreConnectionScenario6Tests: SyncEngineIntegrationTestBase {
9090
}
9191
}
9292

93+
func testGetCommentThenFetchPostThenFetchBlog() throws {
94+
try startAmplifyAndWaitForSync()
95+
guard let blog = saveBlog(name: "name"),
96+
let post = savePost(title: "title", blog: blog),
97+
let comment = saveComment(post: post, content: "content") else {
98+
XCTFail("Could not create blog, post, and comment")
99+
return
100+
}
101+
102+
let getCommentCompleted = expectation(description: "get comment complete")
103+
var resultComment: Comment6?
104+
Amplify.DataStore.query(Comment6.self, byId: comment.id) { result in
105+
switch result {
106+
case .success(let queriedCommentOptional):
107+
guard let queriedComment = queriedCommentOptional else {
108+
XCTFail("Could not get comment")
109+
return
110+
}
111+
XCTAssertEqual(queriedComment.id, comment.id)
112+
resultComment = queriedComment
113+
getCommentCompleted.fulfill()
114+
case .failure(let response):
115+
XCTFail("Failed with: \(response)")
116+
}
117+
}
118+
wait(for: [getCommentCompleted], timeout: TestCommonConstants.networkTimeout)
119+
120+
guard let fetchedComment = resultComment else {
121+
XCTFail("Could not get comment")
122+
return
123+
}
124+
125+
guard let fetchedPost = fetchedComment.post else {
126+
XCTFail("Post is nil, should be loaded")
127+
return
128+
}
129+
130+
guard let fetchedBlog = fetchedPost.blog else {
131+
XCTFail("Blog is nil, should be loaded")
132+
return
133+
}
134+
135+
XCTAssertEqual(fetchedPost.id, post.id)
136+
XCTAssertEqual(fetchedPost.title, post.title)
137+
138+
XCTAssertEqual(fetchedBlog.id, blog.id)
139+
XCTAssertEqual(fetchedBlog.name, blog.name)
140+
}
141+
142+
func testGetPostThenFetchBlogAndComment() throws {
143+
try startAmplifyAndWaitForSync()
144+
guard let blog = saveBlog(name: "name"),
145+
let post = savePost(title: "title", blog: blog),
146+
let comment = saveComment(post: post, content: "content") else {
147+
XCTFail("Could not create blog, post, and comment")
148+
return
149+
}
150+
151+
let getPostCompleted = expectation(description: "get post complete")
152+
var resultPost: Post6?
153+
Amplify.DataStore.query(Post6.self, byId: post.id) { result in
154+
switch result {
155+
case .success(let queriedPostOptional):
156+
guard let queriedPost = queriedPostOptional else {
157+
XCTFail("Could not get post")
158+
return
159+
}
160+
XCTAssertEqual(queriedPost.id, post.id)
161+
resultPost = queriedPost
162+
getPostCompleted.fulfill()
163+
case .failure(let response):
164+
XCTFail("Failed with: \(response)")
165+
}
166+
}
167+
wait(for: [getPostCompleted], timeout: TestCommonConstants.networkTimeout)
168+
169+
guard let fetchedPost = resultPost else {
170+
XCTFail("Could not get post")
171+
return
172+
}
173+
174+
guard let eagerlyLoadedBlog = fetchedPost.blog else {
175+
XCTFail("Blog is nil, should be loaded")
176+
return
177+
}
178+
179+
XCTAssertEqual(eagerlyLoadedBlog.id, blog.id)
180+
XCTAssertEqual(eagerlyLoadedBlog.name, blog.name)
181+
if let postsInEagerlyLoadedBlog = eagerlyLoadedBlog.posts {
182+
XCTAssertEqual(postsInEagerlyLoadedBlog.count, 1)
183+
XCTAssertTrue(postsInEagerlyLoadedBlog.contains(where: {(postIn) -> Bool in
184+
postIn.id == post.id
185+
}))
186+
XCTAssertEqual(postsInEagerlyLoadedBlog[0].id, post.id)
187+
}
188+
189+
guard let lazilyLoadedComments = fetchedPost.comments else {
190+
XCTFail("Could not get comments")
191+
return
192+
}
193+
194+
guard case .notLoaded = lazilyLoadedComments.loadedState else {
195+
XCTFail("Should not be in loaded state")
196+
return
197+
}
198+
XCTAssertEqual(lazilyLoadedComments.count, 1)
199+
XCTAssertEqual(lazilyLoadedComments[0].id, comment.id)
200+
if let fetchedPost = lazilyLoadedComments[0].post {
201+
XCTAssertEqual(fetchedPost.id, post.id)
202+
XCTAssertEqual(fetchedPost.comments?.count, 1)
203+
}
204+
}
205+
93206
func saveBlog(id: String = UUID().uuidString, name: String) -> Blog6? {
94207
let blog = Blog6(id: id, name: name)
95208
var result: Blog6?

AmplifyPlugins/DataStore/AWSDataStoreCategoryPluginIntegrationTests/DataStoreEndToEndTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ class DataStoreEndToEndTests: SyncEngineIntegrationTestBase {
342342
/// - Ensure the expected mutation event with version 2 (synced from cloud) is received
343343
///
344344
func testConcurrentSave() throws {
345-
try startAmplifyAndWaitForReady()
345+
try startAmplifyAndWaitForSync()
346346

347347
var posts = [Post]()
348348
let count = 5

AmplifyPlugins/DataStore/AWSDataStoreCategoryPluginTests/TestSupport/SyncEngineTestBase.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ class SyncEngineTestBase: XCTestCase {
4545
continueAfterFailure = false
4646

4747
Amplify.reset()
48-
Amplify.Logging.logLevel = .verbose
4948

5049
let apiConfig = APICategoryConfiguration(plugins: [
5150
"MockAPICategoryPlugin": true
@@ -74,6 +73,7 @@ class SyncEngineTestBase: XCTestCase {
7473
authPlugin = MockAuthCategoryPlugin()
7574
try Amplify.add(plugin: apiPlugin)
7675
try Amplify.add(plugin: authPlugin)
76+
Amplify.Logging.logLevel = .verbose
7777
}
7878

7979
/// Sets up a StorageAdapter backed by `connection`. Optionally registers and sets up models in

0 commit comments

Comments
 (0)