Skip to content

Commit c787436

Browse files
author
Di Wu
authored
test(datastore-v1): fix ObserveQuery with predicate test case (#2785)
1 parent 245d8d0 commit c787436

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed

AmplifyPlugins/DataStore/AWSDataStoreCategoryPluginIntegrationTests/DataStoreObserveQueryTests.swift

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class DataStoreObserveQueryTests: SyncEngineIntegrationTestBase {
110110
sink.cancel()
111111
}
112112

113-
/// Apply a query predicate "title begins with 'xyz'"
113+
/// Apply a query predicate "title begins with same random UUID"
114114
///
115115
/// - Given: DataStore is set up with an empty local store
116116
/// - When:
@@ -119,18 +119,34 @@ class DataStoreObserveQueryTests: SyncEngineIntegrationTestBase {
119119
/// - The models only contain models based on the predicate
120120
///
121121
func testInitialSyncWithPredicate() throws {
122-
setUp(withModels: TestModelRegistration(), logLevel: .info)
122+
let startTime = Temporal.DateTime.now()
123+
setUp(
124+
withModels: TestModelRegistration(),
125+
logLevel: .info,
126+
dataStoreConfiguration: .custom(
127+
syncMaxRecords: 100,
128+
syncExpressions: [
129+
DataStoreSyncExpression(
130+
modelSchema: Post.schema,
131+
modelPredicate: { Post.keys.createdAt.ge(startTime) }
132+
)
133+
]
134+
)
135+
)
123136
try startAmplify()
124-
savePostAndWaitForSync(Post(title: "xyz 1", content: "content", createdAt: .now()))
125-
savePostAndWaitForSync(Post(title: "xyz 2", content: "content", createdAt: .now()))
126-
savePostAndWaitForSync(Post(title: "xyz 3", content: "content", createdAt: .now()))
137+
138+
let randomTitle = UUID().uuidString
139+
savePostAndWaitForSync(Post(title: "\(randomTitle) 1", content: "content", createdAt: .now()))
140+
savePostAndWaitForSync(Post(title: "\(randomTitle) 2", content: "content", createdAt: .now()))
141+
savePostAndWaitForSync(Post(title: "\(randomTitle) 3", content: "content", createdAt: .now()))
127142
clearDataStore()
128143
var snapshots = [DataStoreQuerySnapshot<Post>]()
129144
let snapshotWithIsSynced = expectation(description: "query snapshot with isSynced true")
130145
let receivedPostFromObserveQuery = expectation(description: "received Post")
131146
snapshotWithIsSynced.assertForOverFulfill = false
147+
receivedPostFromObserveQuery.assertForOverFulfill = false
132148
var snapshotWithIsSyncedFulfilled = false
133-
let predicate = Post.keys.title.beginsWith("xyz")
149+
let predicate = Post.keys.title.beginsWith(randomTitle)
134150
let sink = Amplify.DataStore.observeQuery(for: Post.self, where: predicate).sink { completed in
135151
switch completed {
136152
case .finished:
@@ -144,15 +160,14 @@ class DataStoreObserveQueryTests: SyncEngineIntegrationTestBase {
144160
snapshotWithIsSyncedFulfilled = true
145161
snapshotWithIsSynced.fulfill()
146162
} else if snapshotWithIsSyncedFulfilled {
147-
if querySnapshot.items.count >= 4 && querySnapshot.items.contains(where: { post in
148-
post.title.contains("xyz")
149-
}) {
163+
if querySnapshot.items.count >= 4
164+
&& querySnapshot.items.allSatisfy({ $0.title.contains(randomTitle) }) {
150165
receivedPostFromObserveQuery.fulfill()
151166
}
152167
}
153168
}
154169

155-
savePostAndWaitForSync(Post(title: "xyz 4", content: "content", createdAt: .now()))
170+
savePostAndWaitForSync(Post(title: "\(randomTitle) 4", content: "content", createdAt: .now()))
156171
wait(for: [snapshotWithIsSynced, receivedPostFromObserveQuery], timeout: 200)
157172
XCTAssertTrue(snapshots.count >= 2)
158173
XCTAssertFalse(snapshots[0].isSynced)

AmplifyPlugins/DataStore/AWSDataStoreCategoryPluginIntegrationTests/TestSupport/SyncEngineIntegrationTestBase.swift

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ class SyncEngineIntegrationTestBase: DataStoreTestBase {
3535
// swiftlint:enable force_try
3636
// swiftlint:enable force_cast
3737

38-
func setUp(withModels models: AmplifyModelRegistration, logLevel: LogLevel = .error) {
38+
func setUp(
39+
withModels models: AmplifyModelRegistration,
40+
logLevel: LogLevel = .error,
41+
dataStoreConfiguration: DataStoreConfiguration? = nil
42+
) {
3943

4044
continueAfterFailure = false
4145

@@ -45,7 +49,12 @@ class SyncEngineIntegrationTestBase: DataStoreTestBase {
4549

4650
do {
4751
try Amplify.add(plugin: AWSAPIPlugin(modelRegistration: models))
48-
try Amplify.add(plugin: AWSDataStorePlugin(modelRegistration: models))
52+
try Amplify.add(
53+
plugin: AWSDataStorePlugin(
54+
modelRegistration: models,
55+
configuration: dataStoreConfiguration ?? .default
56+
)
57+
)
4958
} catch {
5059
XCTFail(String(describing: error))
5160
return

0 commit comments

Comments
 (0)