Skip to content

Commit e22b59b

Browse files
committed
Fixed bool and NSManagedObject comparison predicate
1 parent 4783a7d commit e22b59b

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

Sources/SafeFetching/Predicate/DatabaseTestValue.swift

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,20 @@
33
// Copyright © 2021-present Alexis Bridoux.
44
// MIT license, see LICENSE file for details
55

6+
import CoreData
7+
68
/// A type that can be used in a predicate as a test value
9+
///
10+
/// - Default implementations are provided to ensure formatting.
11+
/// - Implementation for `NSManagedObject` is provided (for the relationships) and will use the [`objectID`](https://developer.apple.com/documentation/coredata/nsmanagedobject/1506848-objectid) property
712
public protocol DatabaseTestValue {
813

914
/// Formatted string value that can be used in the format of a `NSPredicate`
1015
var testValue: String { get }
1116
}
1217

18+
// MARK: - Scalar
19+
1320
extension String: DatabaseTestValue {
1421

1522
public var testValue: String { #""\#(self)""# }
@@ -45,29 +52,46 @@ extension Float: DatabaseTestValue {
4552
public var testValue: String { String(describing: self) }
4653
}
4754

55+
extension Bool: DatabaseTestValue {
56+
public var testValue: String { String(describing: self) }
57+
}
58+
59+
// MARK: - Optional
60+
4861
extension Optional: DatabaseTestValue where Wrapped: DatabaseTestValue {
4962

5063
public var testValue: String {
5164
switch self {
5265
case .none: return "nil"
53-
case .some(let wrapped):
66+
case let .some(wrapped):
5467
return wrapped.testValue
5568
}
5669
}
5770
}
5871

72+
extension NSManagedObject: DatabaseTestValue {
73+
74+
public var testValue: String { String(describing: objectID) }
75+
}
76+
77+
// MARK: - Array
78+
5979
extension Array: DatabaseTestValue where Element: DatabaseTestValue {
6080

6181
public var testValue: String {
6282
"{ \(map(\.testValue).joined(separator: ","))}"
6383
}
6484
}
6585

86+
// MARK: - RawRepresentable
87+
6688
extension DatabaseTestValue where Self: RawRepresentable, RawValue: DatabaseValue {
6789

6890
public var testValue: String { String(describing: rawValue) }
6991
}
7092

93+
// MARK: - Range
94+
7195
extension ClosedRange: DatabaseTestValue where Bound: Numeric {
7296

7397
public var testValue: String {

Sources/SafeFetching/Predicate/DatabaseValue.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ extension URL: DatabaseValue {
7171

7272
// MARK: Relationship
7373

74+
extension NSManagedObject: DatabaseValue {
75+
public static let identification = DatabaseValueIdentification()
76+
}
77+
7478
extension Set: DatabaseValue where Element: NSManagedObject {
7579
public static var identification: DatabaseValueIdentification {
7680
DatabaseValueIdentification()

Tests/SafeFetchingTests/BooleanPredicateBuilderTests.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ final class BooleanPredicateTests: XCTestCase {
1515
testNSFormat(predicate: \.score >= 10, expecting: "score >= 10")
1616
testNSFormat(predicate: \.score < 10, expecting: "score < 10")
1717
testNSFormat(predicate: \.score <= 10, expecting: "score <= 10")
18+
testNSFormat(predicate: \.isAdmin == true, expecting: "isAdmin == 1")
19+
testNSFormat(predicate: \.isAdmin == false, expecting: "isAdmin == 0")
1820
testNSFormat(predicate: \.property == nil, expecting: "property == nil")
21+
testNSFormat(predicate: \.stubRelationship == nil, expecting: "stubRelationship == nil")
1922
}
2023

2124
func testString() {
@@ -109,11 +112,14 @@ extension BooleanPredicateTests {
109112

110113
final class StubEntity: NSManagedObject {
111114

115+
@objc var isAdmin = false
112116
@objc var score = 0.0
113117
@objc var property: String? = ""
114118
@objc var stubRawValue: Int = 0
115119
@objc var stubRawOption: Int = 0
116120

121+
@objc var stubRelationship: StubEntity?
122+
117123
var stubRaw: StubEnum? { StubEnum(rawValue: stubRawValue) }
118124
var stubForcedRaw: StubEnum { StubEnum(rawValue: stubRawValue)! }
119125

0 commit comments

Comments
 (0)