-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathAttributeTests.swift
More file actions
140 lines (111 loc) · 3.8 KB
/
AttributeTests.swift
File metadata and controls
140 lines (111 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//
// AttributeTests.swift
// QueryKit
//
// Created by Kyle Fuller on 19/06/2014.
//
//
import XCTest
import QueryKit
class AttributeTests: XCTestCase {
var attribute:Attribute<Int>!
override func setUp() {
super.setUp()
attribute = Attribute("age")
}
func testAttributeHasKey() {
XCTAssertEqual(attribute.key, "age")
}
func testAttributeExpression() {
XCTAssertEqual(attribute.expression.keyPath, "age")
}
func testEqualAttributesAreEquatable() {
XCTAssertEqual(attribute, Attribute<Int>("age"))
}
func testCompoundAttributeCreation() {
let personCompanyNameAttribute = Attribute<NSString>(attributes:["company", "name"])
XCTAssertEqual(personCompanyNameAttribute.key, "company.name")
XCTAssertEqual(personCompanyNameAttribute.expression.keyPath, "company.name")
}
// Sorting
func testAscendingSortDescriptor() {
XCTAssertEqual(attribute.ascending(), NSSortDescriptor(key: "age", ascending: true))
}
func testDescendingSortDescriptor() {
XCTAssertEqual(attribute.descending(), NSSortDescriptor(key: "age", ascending: false))
}
// Operators
func testEqualityOperator() {
let predicate:NSPredicate = (attribute == 10)
XCTAssertEqual(predicate, NSPredicate(format:"age == 10"))
}
func testInequalityOperator() {
let predicate:NSPredicate = (attribute != 10)
XCTAssertEqual(predicate, NSPredicate(format:"age != 10"))
}
func testGreaterThanOperator() {
let predicate:NSPredicate = (attribute > 10)
XCTAssertEqual(predicate, NSPredicate(format:"age > 10"))
}
func testGreaterOrEqualThanOperator() {
let predicate:NSPredicate = (attribute >= 10)
XCTAssertEqual(predicate, NSPredicate(format:"age >= 10"))
}
func testLessThanOperator() {
let predicate:NSPredicate = (attribute < 10)
XCTAssertEqual(predicate, NSPredicate(format:"age < 10"))
}
func testLessOrEqualThanOperator() {
let predicate:NSPredicate = (attribute <= 10)
XCTAssertEqual(predicate, NSPredicate(format:"age <= 10"))
}
func testLikeOperator() {
let predicate:NSPredicate = (attribute ~= 10)
XCTAssertEqual(predicate, NSPredicate(format:"age LIKE 10"))
}
func testInOperator() {
let predicate:NSPredicate = (attribute << [5, 10])
XCTAssertEqual(predicate, NSPredicate(format:"age IN %@", [5, 10]))
}
func testBetweenRangeOperator() {
let predicate:NSPredicate = attribute << (5..<10)
XCTAssertEqual(predicate, NSPredicate(format:"age BETWEEN %@", [5, 10]))
}
func testOptionalEqualityOperator() {
let attribute = Attribute<String?>("name")
let predicate:NSPredicate = (attribute == "kyle")
XCTAssertEqual(predicate, NSPredicate(format:"name == 'kyle'"))
}
func testOptionalNSObjectEqualityOperator() {
let attribute = Attribute<NSString?>("name")
let predicate:NSPredicate = (attribute == "kyle")
XCTAssertEqual(predicate, NSPredicate(format:"name == 'kyle'"))
}
func testEqualityOperatorWithNilRHS() {
let attribute = Attribute<String?>("name")
let predicate: NSPredicate = attribute == nil
XCTAssertEqual(predicate.description, "name == <null>")
}
}
class CollectionAttributeTests: XCTestCase {
func testCountOfSet() {
let setAttribute = Attribute<NSSet>("names")
let countAttribute = count(setAttribute)
XCTAssertEqual(countAttribute, Attribute<Int>("names.@count"))
}
func testCountOfOrderedSet() {
let setAttribute = Attribute<NSOrderedSet>("names")
let countAttribute = count(setAttribute)
XCTAssertEqual(countAttribute, Attribute<Int>("names.@count"))
}
}
class BoolAttributeTests: XCTestCase {
var attribute:Attribute<Bool>!
override func setUp() {
super.setUp()
attribute = Attribute("hasName")
}
func testNotAttributeReturnsPredicate() {
XCTAssertEqual(!attribute, NSPredicate(format:"hasName == NO"))
}
}