Skip to content

Commit 2c57ccc

Browse files
authored
test(AWSDataStoreCategoryPluginFlutterIntegrationTests): FlutterSerializedModel tests (#1546)
1 parent f5cb9b6 commit 2c57ccc

32 files changed

+3799
-1
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//
2+
// Copyright Amazon.com Inc. or its affiliates.
3+
// All Rights Reserved.
4+
//
5+
// SPDX-License-Identifier: Apache-2.0
6+
//
7+
import XCTest
8+
import Amplify
9+
import AWSDataStoreCategoryPlugin
10+
11+
class AWSDataStorePluginFlutterConfigurationTests: XCTestCase {
12+
13+
// Note this test requires the ability to write a new database in the Documents directory, so it must be embedded
14+
// in a host app
15+
func testDoesNotThrowOnMissingConfig() throws {
16+
let plugin = AWSDataStorePlugin(modelRegistration: TestFlutterModelRegistration())
17+
try Amplify.add(plugin: plugin)
18+
19+
let amplifyConfig = AmplifyConfiguration()
20+
do {
21+
try Amplify.configure(amplifyConfig)
22+
} catch {
23+
XCTAssertNil(error, "Should not throw even if not supplied with a plugin-specific config.")
24+
}
25+
}
26+
27+
}
28+

AmplifyPlugins/DataStore/AWSDataStoreCategoryPluginFlutterIntegrationTests/Connection/DataStoreConnectionScenario1FlutterTests.swift

Lines changed: 377 additions & 0 deletions
Large diffs are not rendered by default.

AmplifyPlugins/DataStore/AWSDataStoreCategoryPluginFlutterIntegrationTests/Connection/DataStoreConnectionScenario2FlutterTests.swift

Lines changed: 383 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
//
2+
// Copyright Amazon.com Inc. or its affiliates.
3+
// All Rights Reserved.
4+
//
5+
// SPDX-License-Identifier: Apache-2.0
6+
//
7+
import XCTest
8+
import AmplifyPlugins
9+
import AWSMobileClient
10+
11+
@testable import Amplify
12+
@testable import AmplifyTestCommon
13+
@testable import AWSDataStoreCategoryPlugin
14+
15+
/*
16+
(HasMany) A Post that can have many comments
17+
```
18+
type Post3 @model {
19+
id: ID!
20+
title: String!
21+
comments: [Comment3] @connection(keyName: "byPost3", fields: ["id"])
22+
}
23+
type Comment3 @model
24+
@key(name: "byPost3", fields: ["postID", "content"]) {
25+
id: ID!
26+
postID: ID!
27+
content: String!
28+
}
29+
```
30+
See https://docs.amplify.aws/cli/graphql-transformer/connection for more details
31+
*/
32+
33+
class DataStoreConnectionScenario3FlutterTests: SyncEngineFlutterIntegrationTestBase {
34+
35+
func testSavePostAndCommentSyncToCloud() throws {
36+
try startAmplifyAndWaitForSync()
37+
let plugin: AWSDataStorePlugin = try Amplify.DataStore.getPlugin(for: "awsDataStorePlugin") as! AWSDataStorePlugin
38+
let post = try Post3Wrapper(title: "title")
39+
let comment = try Comment3Wrapper(postID: post.idString(), content: "content")
40+
let syncedPostReceived = expectation(description: "received post from sync event")
41+
let syncCommentReceived = expectation(description: "received comment from sync event")
42+
let hubListener = Amplify.Hub.listen(to: .dataStore,
43+
eventName: HubPayload.EventName.DataStore.syncReceived) { payload in
44+
guard let mutationEvent = payload.data as? MutationEvent else {
45+
XCTFail("Could not cast payload to mutation event")
46+
return
47+
}
48+
if let syncedPost = mutationEvent.modelId as String?,
49+
syncedPost == post.idString() {
50+
syncedPostReceived.fulfill()
51+
} else if let syncComment = mutationEvent.modelId as String?,
52+
syncComment == comment.idString() {
53+
syncCommentReceived.fulfill()
54+
}
55+
56+
}
57+
guard try HubListenerTestUtilities.waitForListener(with: hubListener, timeout: 5.0) else {
58+
XCTFail("Listener not registered for hub")
59+
return
60+
}
61+
let savePostCompleted = expectation(description: "save post completed")
62+
plugin.save(post.model, modelSchema: Post3.schema) { result in
63+
switch result {
64+
case .success:
65+
savePostCompleted.fulfill()
66+
case .failure(let error):
67+
XCTFail("failed \(error)")
68+
}
69+
}
70+
wait(for: [savePostCompleted, syncedPostReceived], timeout: networkTimeout)
71+
let saveCommentCompleted = expectation(description: "save comment completed")
72+
plugin.save(comment.model, modelSchema: Comment3.schema) { result in
73+
switch result {
74+
case .success:
75+
saveCommentCompleted.fulfill()
76+
case .failure(let error):
77+
XCTFail("failed \(error)")
78+
}
79+
}
80+
wait(for: [saveCommentCompleted, syncCommentReceived], timeout: networkTimeout)
81+
let queriedCommentCompleted = expectation(description: "query comment completed")
82+
plugin.query(FlutterSerializedModel.self, modelSchema: Comment3.schema, where: Comment3.keys.id.eq(comment.model.id)) { result in
83+
switch result {
84+
case .success(let queriedComment):
85+
let returnedComent = Comment3Wrapper(model: queriedComment[0])
86+
XCTAssertEqual(returnedComent, comment)
87+
queriedCommentCompleted.fulfill()
88+
case .failure(let error):
89+
XCTFail("failed \(error)")
90+
}
91+
}
92+
wait(for: [queriedCommentCompleted], timeout: networkTimeout)
93+
}
94+
95+
/// TODO: Include testSaveCommentAndGetPostWithComments test when nested model lazy loading is implemented
96+
func testUpdateComment() throws {
97+
try startAmplifyAndWaitForSync()
98+
let plugin: AWSDataStorePlugin = try Amplify.DataStore.getPlugin(for: "awsDataStorePlugin") as! AWSDataStorePlugin
99+
guard let post = try savePost(title: "title", plugin: plugin) else {
100+
XCTFail("Could not create post")
101+
return
102+
}
103+
guard let comment = try saveComment(postID: post.idString(), content: "content", plugin: plugin) else {
104+
XCTFail("Could not create comment")
105+
return
106+
}
107+
guard let anotherPost = try savePost(title: "title", plugin: plugin) else {
108+
XCTFail("Could not create post")
109+
return
110+
}
111+
let updateCommentSuccessful = expectation(description: "update comment")
112+
try comment.setPostId(postId: anotherPost.idString())
113+
plugin.save(comment.model, modelSchema: Comment3.schema) { result in
114+
switch result {
115+
case .success(let updatedComment):
116+
let queriedComment = Comment3Wrapper(model: updatedComment)
117+
XCTAssertEqual(queriedComment.postId(), anotherPost.id())
118+
updateCommentSuccessful.fulfill()
119+
case .failure(let error):
120+
XCTFail("\(error)")
121+
}
122+
}
123+
wait(for: [updateCommentSuccessful], timeout: TestCommonConstants.networkTimeout)
124+
}
125+
126+
func testDeleteAndGetComment() throws {
127+
try startAmplifyAndWaitForSync()
128+
let plugin: AWSDataStorePlugin = try Amplify.DataStore.getPlugin(for: "awsDataStorePlugin") as! AWSDataStorePlugin
129+
guard let post = try savePost(title: "title", plugin: plugin) else {
130+
XCTFail("Could not create post")
131+
return
132+
}
133+
guard let comment = try saveComment(postID: post.idString(), content: "content", plugin: plugin) else {
134+
XCTFail("Could not create comment")
135+
return
136+
}
137+
let deleteCommentSuccessful = expectation(description: "delete comment")
138+
plugin.delete(comment.model, modelSchema: Comment3.schema) { result in
139+
switch result {
140+
case .success:
141+
deleteCommentSuccessful.fulfill()
142+
case .failure(let error):
143+
XCTFail("\(error)")
144+
}
145+
}
146+
wait(for: [deleteCommentSuccessful], timeout: TestCommonConstants.networkTimeout)
147+
let getCommentAfterDeleteCompleted = expectation(description: "get comment after deleted complete")
148+
plugin.query(FlutterSerializedModel.self, modelSchema: Comment3.schema, where: Comment3.keys.id.eq(comment.idString())) { result in
149+
switch result {
150+
case .success(let comment):
151+
guard comment.isEmpty else {
152+
XCTFail("Should be nil after deletion")
153+
return
154+
}
155+
getCommentAfterDeleteCompleted.fulfill()
156+
case .failure(let error):
157+
XCTFail("\(error)")
158+
}
159+
}
160+
wait(for: [getCommentAfterDeleteCompleted], timeout: TestCommonConstants.networkTimeout)
161+
}
162+
163+
func testListCommentsByPostID() throws {
164+
try startAmplifyAndWaitForSync()
165+
let plugin: AWSDataStorePlugin = try Amplify.DataStore.getPlugin(for: "awsDataStorePlugin") as! AWSDataStorePlugin
166+
guard let post = try savePost(title: "title", plugin: plugin) else {
167+
XCTFail("Could not create post")
168+
return
169+
}
170+
guard try saveComment(postID: post.idString(), content: "content", plugin: plugin) != nil else {
171+
XCTFail("Could not create comment")
172+
return
173+
}
174+
let listCommentByPostIDCompleted = expectation(description: "list projects completed")
175+
let predicate = Comment3.keys.postID.eq(post.idString())
176+
plugin.query(FlutterSerializedModel.self, modelSchema: Comment3.schema, where: predicate) { result in
177+
switch result {
178+
case .success(let projects):
179+
XCTAssertEqual(projects.count, 1)
180+
listCommentByPostIDCompleted.fulfill()
181+
case .failure(let error):
182+
XCTFail("\(error)")
183+
}
184+
}
185+
wait(for: [listCommentByPostIDCompleted], timeout: TestCommonConstants.networkTimeout)
186+
}
187+
188+
func savePost(id: String = UUID().uuidString, title: String, plugin: AWSDataStorePlugin) throws -> Post3Wrapper? {
189+
let post = try Post3Wrapper(
190+
id: id,
191+
title: title)
192+
var result: Post3Wrapper?
193+
let completeInvoked = expectation(description: "request completed")
194+
plugin.save(post.model, modelSchema: Post3.schema) { event in
195+
switch event {
196+
case .success(let project):
197+
result = Post3Wrapper(model: project)
198+
completeInvoked.fulfill()
199+
case .failure(let error):
200+
XCTFail("failed \(error)")
201+
}
202+
}
203+
wait(for: [completeInvoked], timeout: TestCommonConstants.networkTimeout)
204+
return result
205+
}
206+
207+
func saveComment(id: String = UUID().uuidString, postID: String, content: String, plugin: AWSDataStorePlugin) throws -> Comment3Wrapper? {
208+
let comment = try Comment3Wrapper(id: id, postID: postID, content: content)
209+
var result: Comment3Wrapper?
210+
let completeInvoked = expectation(description: "request completed")
211+
plugin.save(comment.model, modelSchema: Comment3.schema) { event in
212+
switch event {
213+
case .success(let comment):
214+
result = Comment3Wrapper(model: comment)
215+
completeInvoked.fulfill()
216+
case .failure(let error):
217+
XCTFail("failed \(error)")
218+
}
219+
}
220+
wait(for: [completeInvoked], timeout: TestCommonConstants.networkTimeout)
221+
return result
222+
}
223+
}

0 commit comments

Comments
 (0)