Skip to content

Commit 235b49f

Browse files
authored
fix(DataStore): Fixing a crash when attempting to create a model with a predicate. (#3600)
1 parent b955a71 commit 235b49f

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

AmplifyPlugins/DataStore/Sources/AWSDataStorePlugin/Storage/StorageEngine.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ final class StorageEngine: StorageEngineBehavior {
206206
"Cannot apply a condition on model which does not exist.",
207207
"Save the model instance without a condition first.")
208208
completion(.failure(causedBy: dataStoreError))
209+
return
209210
}
210211

211212
do {

AmplifyPlugins/DataStore/Tests/AWSDataStorePluginTests/Storage/StorageEngineTestsHasOne.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,28 @@ class StorageEngineTestsHasOne: StorageEngineTestsBase {
7070
}
7171
}
7272

73+
/// Given: A model that does not exist
74+
/// When: save is called with a predicate
75+
/// Then: A DataStoreError.invalidCondition error is returned
76+
func testSaveModelWithPredicate_shouldFail() {
77+
let team = Team(name: "Team")
78+
let saveFinished = expectation(description: "Save finished")
79+
storageEngine.save(team, condition: Team.keys.name.beginsWith("T")) { result in
80+
defer {
81+
saveFinished.fulfill()
82+
}
83+
guard case .failure(let error) = result,
84+
case . invalidCondition(let errorDescription, let recoverySuggestion, _) = error else {
85+
XCTFail("Expected failure with .invalidCondition, got \(result)")
86+
return
87+
}
88+
89+
XCTAssertEqual(errorDescription, "Cannot apply a condition on model which does not exist.")
90+
XCTAssertEqual(recoverySuggestion, "Save the model instance without a condition first.")
91+
}
92+
wait(for: [saveFinished], timeout: defaultTimeout)
93+
}
94+
7395
func testBelongsToRelationshipWithoutOwner() {
7496
let teamA = Team(name: "A-Team")
7597
let projectA = Project(name: "ProjectA", team: teamA)

0 commit comments

Comments
 (0)