Skip to content

Commit 832c33f

Browse files
committed
#15 Verify empty to-many ALL semantics match CoreData
1 parent 1e9ca2f commit 832c33f

1 file changed

Lines changed: 18 additions & 6 deletions

File tree

‎Tests/CoreModelTests/CoreDataModelTests.swift‎

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,8 @@ import Testing
182182
try context.insert([wwdc.encode(), other.encode()])
183183
try context.insert([
184184
Person(name: "Alice", age: 30, events: [wwdc.id, other.id]).encode(),
185-
Person(name: "Bob", age: 17, events: [other.id]).encode()
185+
Person(name: "Bob", age: 17, events: [other.id]).encode(),
186+
Person(name: "Carol", age: 25, events: []).encode()
186187
])
187188

188189
// CoreModel API: ANY events.name == "WWDC"
@@ -194,16 +195,27 @@ import Testing
194195
let anyConverted = try FetchRequest.Predicate(#Predicate<PersonRecord> { $0.events.contains { $0.name == "WWDC" } })
195196
#expect(anyConverted == anyDirect)
196197

197-
// CoreModel API: ALL events.name == "Other"
198+
// CoreModel API: ALL events.name == "Other".
199+
// Carol has no events, so the comparison holds vacuously — the same semantics
200+
// CoreModel's in-memory evaluator implements, verified here against CoreData itself.
198201
let allDirect = "events.name".compare(.all, .equalTo, [], .attribute(.string("Other")))
199-
let allResults = try context.fetch(FetchRequest(entity: Person.entityName, predicate: allDirect))
200-
#expect(allResults.map { $0.attributes["name"] } == [.string("Bob")])
202+
let allRequest = FetchRequest(entity: Person.entityName, sortDescriptors: [.init(property: "name")], predicate: allDirect)
203+
let allResults = try context.fetch(allRequest)
204+
#expect(allResults.map { $0.attributes["name"] } == [.string("Bob"), .string("Carol")])
201205

202206
// Foundation.Predicate: allSatisfy converts to the same ALL comparison
203207
let allConverted = try FetchRequest.Predicate(#Predicate<PersonRecord> { $0.events.allSatisfy { $0.name == "Other" } })
204208
#expect(allConverted == allDirect)
205-
let allConvertedResults = try context.fetch(FetchRequest(entity: Person.entityName, predicate: allConverted))
206-
#expect(allConvertedResults.map { $0.attributes["name"] } == [.string("Bob")])
209+
let allConvertedResults = try context.fetch(
210+
FetchRequest(entity: Person.entityName, sortDescriptors: [.init(property: "name")], predicate: allConverted)
211+
)
212+
#expect(allConvertedResults.map { $0.attributes["name"] } == [.string("Bob"), .string("Carol")])
213+
214+
// the in-memory evaluator agrees with CoreData on the same objects
215+
let objects = try context.fetch(FetchRequest(entity: Person.entityName))
216+
+ context.fetch(FetchRequest(entity: Event.entityName))
217+
let inMemory = allRequest.evaluate(objects)
218+
#expect(inMemory.map { $0.attributes["name"] } == [.string("Bob"), .string("Carol")])
207219
}
208220

209221
@Test func nullRelationshipInsert() throws {

0 commit comments

Comments
 (0)