Skip to content

Commit 1cbb1f4

Browse files
author
Di Wu
authored
test(datastore): fix ObserveQuery with predicate test case (#2784)
1 parent ac2b6a0 commit 1cbb1f4

File tree

3 files changed

+47
-20
lines changed

3 files changed

+47
-20
lines changed

AmplifyPlugins/DataStore/Tests/DataStoreHostApp/AWSDataStorePluginIntegrationTests/DataStoreObserveQueryTests.swift

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -156,21 +156,37 @@ class DataStoreObserveQueryTests: SyncEngineIntegrationTestBase {
156156
/// - The models only contain models based on the predicate
157157
///
158158
func testInitialSyncWithPredicate() async throws {
159-
await setUp(withModels: TestModelRegistration(), logLevel: .info)
159+
let startTime = Temporal.DateTime.now()
160+
await setUp(
161+
withModels: TestModelRegistration(),
162+
logLevel: .verbose,
163+
dataStoreConfiguration: .custom(
164+
syncMaxRecords: 100,
165+
syncExpressions: [
166+
DataStoreSyncExpression(
167+
modelSchema: Post.schema,
168+
modelPredicate: { Post.keys.createdAt.ge(startTime) }
169+
)
170+
]
171+
)
172+
)
160173
try startAmplify()
161-
let post1 = Post(title: "xyz 1", content: "content", createdAt: .now())
162-
let post2 = Post(title: "xyz 2", content: "content", createdAt: .now())
163-
let post3 = Post(title: "xyz 3", content: "content", createdAt: .now())
174+
175+
let randomTitle = UUID().uuidString
176+
let post1 = Post(title: "\(randomTitle) 1", content: "content", createdAt: .now())
177+
let post2 = Post(title: "\(randomTitle) 2", content: "content", createdAt: .now())
178+
let post3 = Post(title: "\(randomTitle) 3", content: "content", createdAt: .now())
164179
try await savePostAndWaitForSync(post1)
165180
try await savePostAndWaitForSync(post2)
166181
try await savePostAndWaitForSync(post3)
167182
try await clearDataStore()
183+
168184
var snapshots = [DataStoreQuerySnapshot<Post>]()
169185
let snapshotWithIsSynced = asyncExpectation(description: "query snapshot with isSynced true")
170186
var snapshotWithIsSyncedFulfilled = false
171187
let receivedPostFromObserveQuery = asyncExpectation(description: "received Post")
172-
let post4 = Post(title: "xyz 4", content: "content", createdAt: .now())
173-
let predicate = Post.keys.title.beginsWith("xyz")
188+
let post4 = Post(title: "\(randomTitle) 4", content: "content", createdAt: .now())
189+
let predicate = Post.keys.title.beginsWith(randomTitle)
174190
Amplify.Publisher.create(Amplify.DataStore.observeQuery(for: Post.self, where: predicate)).sink { completed in
175191
switch completed {
176192
case .finished:
@@ -184,17 +200,17 @@ class DataStoreObserveQueryTests: SyncEngineIntegrationTestBase {
184200
snapshotWithIsSyncedFulfilled = true
185201
Task { await snapshotWithIsSynced.fulfill() }
186202
} else if snapshotWithIsSyncedFulfilled {
187-
if querySnapshot.items.count >= 4 && querySnapshot.items.contains(where: { post in
188-
post.title.contains("xyz")
189-
}){
203+
if querySnapshot.items.count >= 4
204+
&& querySnapshot.items.allSatisfy({ $0.title.contains(randomTitle)})
205+
{
190206
Task { await receivedPostFromObserveQuery.fulfill() }
191207
}
192208
}
193209

194210
}.store(in: &cancellables)
195211

196212
await waitForExpectations([snapshotWithIsSynced], timeout: 100)
197-
213+
198214
try await savePostAndWaitForSync(post4)
199215
await waitForExpectations([receivedPostFromObserveQuery], timeout: 100)
200216

AmplifyPlugins/DataStore/Tests/DataStoreHostApp/AWSDataStorePluginIntegrationTests/TestSupport/SyncEngineIntegrationTestBase.swift

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,23 @@ class SyncEngineIntegrationTestBase: DataStoreTestBase {
3232
// swiftlint:enable force_try
3333
// swiftlint:enable force_cast
3434

35-
func setUp(withModels models: AmplifyModelRegistration, logLevel: LogLevel = .error) async {
36-
35+
func setUp(
36+
withModels models: AmplifyModelRegistration,
37+
logLevel: LogLevel = .error,
38+
dataStoreConfiguration: DataStoreConfiguration? = nil
39+
) async {
3740
continueAfterFailure = false
3841

3942
Amplify.Logging.logLevel = logLevel
4043

4144
do {
4245
try Amplify.add(plugin: AWSAPIPlugin(modelRegistration: models))
43-
try Amplify.add(plugin: AWSDataStorePlugin(modelRegistration: models,
44-
configuration: .custom(syncMaxRecords: 100)))
46+
try Amplify.add(
47+
plugin: AWSDataStorePlugin(
48+
modelRegistration: models,
49+
configuration: dataStoreConfiguration ?? .custom(syncMaxRecords: 100)
50+
)
51+
)
4552
} catch {
4653
XCTFail(String(describing: error))
4754
return
@@ -61,9 +68,13 @@ class SyncEngineIntegrationTestBase: DataStoreTestBase {
6168
try await Amplify.DataStore.clear()
6269
}
6370

64-
func startAmplify() throws {
65-
let amplifyConfig = try TestConfigHelper.retrieveAmplifyConfiguration(
71+
func getTestConfiguration() throws -> AmplifyConfiguration {
72+
try TestConfigHelper.retrieveAmplifyConfiguration(
6673
forResource: Self.amplifyConfigurationFile)
74+
}
75+
76+
func startAmplify() throws {
77+
let amplifyConfig = try getTestConfiguration()
6778
do {
6879
try Amplify.configure(amplifyConfig)
6980
} catch {

AmplifyPlugins/DataStore/Tests/DataStoreHostApp/DataStoreHostApp.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)