-
-
Notifications
You must be signed in to change notification settings - Fork 875
/
Copy pathQueryUtilitiesTests.m
121 lines (93 loc) · 4.76 KB
/
QueryUtilitiesTests.m
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
/**
* Copyright (c) 2015-present, Parse, LLC.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#import "PFConstants.h"
#import "PFQueryUtilities.h"
#import "PFTestCase.h"
@interface QueryUtilitiesTests : PFTestCase
@end
@implementation QueryUtilitiesTests
///--------------------------------------
#pragma mark - Utilities
///--------------------------------------
- (NSPredicate *)sampleCompoundPredicate {
return [NSPredicate predicateWithFormat:@""];
}
- (void)assertPredicateFormat:(NSString *)toNormalize normalizesToFormat:(NSString *)expected {
NSPredicate *normalizedPredicate = [NSPredicate predicateWithFormat:toNormalize];
NSPredicate *expectedPredicate = [NSPredicate predicateWithFormat:expected];
XCTAssertEqualObjects(expectedPredicate, [PFQueryUtilities predicateByNormalizingPredicate:normalizedPredicate]);
XCTAssertEqualObjects(expectedPredicate, [PFQueryUtilities predicateByNormalizingPredicate:expectedPredicate]);
}
- (void)assertPredicateThrows:(NSString *)expected {
NSPredicate *expectedPredicate = [NSPredicate predicateWithFormat:expected];
XCTAssertThrows([PFQueryUtilities predicateByNormalizingPredicate:expectedPredicate]);
}
///--------------------------------------
#pragma mark - Tests
///--------------------------------------
- (void)testReversesYodaExpressions {
[self assertPredicateFormat:@"3 == x" normalizesToFormat:@"x == 3"];
[self assertPredicateFormat:@"3 != x" normalizesToFormat:@"x != 3"];
[self assertPredicateFormat:@"3 > x" normalizesToFormat:@"x < 3"];
[self assertPredicateFormat:@"3 < x" normalizesToFormat:@"x > 3"];
[self assertPredicateFormat:@"3 >= x" normalizesToFormat:@"x <= 3"];
[self assertPredicateFormat:@"3 <= x" normalizesToFormat:@"x >= 3"];
[self assertPredicateFormat:@"3 in x" normalizesToFormat:@"x = 3"];
[self assertPredicateFormat:@"x contains y" normalizesToFormat:@"x contains y"];
}
- (void)testPushesNegatesToLeaves {
[self assertPredicateFormat:@"!(y == 4)" normalizesToFormat:@"y != 4"];
[self assertPredicateFormat:@"!(y == 4 || !(y == 3))" normalizesToFormat:@"y != 4 && y == 3"];
[self assertPredicateFormat:@"!(y > 3)" normalizesToFormat:@"y <= 3"];
[self assertPredicateFormat:@"!(y < 3)" normalizesToFormat:@"y >= 3"];
[self assertPredicateFormat:@"!(y >= 3)" normalizesToFormat:@"y < 3"];
[self assertPredicateFormat:@"!(y <= 3)" normalizesToFormat:@"y > 3"];
[self assertPredicateFormat:@"!(y IN {x, y, z})" normalizesToFormat:@"y notContainedIn: {x, y, z}"];
[self assertPredicateThrows:@"!(y MATCHES x)"];
}
- (void)testRemovesBeweens {
[self assertPredicateFormat:@"x BETWEEN {1, 10}" normalizesToFormat:@"x >= 1 && x <= 10"];
[self assertPredicateThrows:@"x BETWEEN y"];
[self assertPredicateThrows:@"x BETWEEN {x, y, z}"];
}
- (void)testConvertToDNF {
[self assertPredicateFormat:@"(x == 0) || ((x == 2) && (y != 3 || y == 2))"
normalizesToFormat:@"(x == 0) || (x == 2 && y != 3) || (x == 2 && y == 2)"];
}
- (void)testMergeCommonPredicate {
[self assertPredicateFormat:@"((x == 0) && (y == 1)) || ((x == 0) && (z == 1))"
normalizesToFormat:@"(y == 1 || z == 1) && (x == 0)"];
}
- (void)testBadQueries {
[self assertPredicateThrows:@"ANY x == y"];
NSExpression *expression = [NSExpression expressionWithFormat:@"12345"];
XCTAssertThrows([PFQueryUtilities predicateByNormalizingPredicate:(NSPredicate *)expression]);
}
- (void)testRemovesDoubleNegatives {
NSPredicate *negatedPredicate = [NSPredicate predicateWithFormat:@"!(y != 5)"];
NSPredicate *normalizedPredicate = [NSPredicate predicateWithFormat:@"y == 5"];
XCTAssertNotEqualObjects(negatedPredicate, normalizedPredicate);
XCTAssertEqualObjects(normalizedPredicate, [PFQueryUtilities predicateByNormalizingPredicate:negatedPredicate]);
XCTAssertEqualObjects(normalizedPredicate, [PFQueryUtilities predicateByNormalizingPredicate:normalizedPredicate]);
}
- (void)testRegexString {
NSString *inputString = @"Hello!";
XCTAssertEqualObjects([PFQueryUtilities regexStringForString:inputString], @"\\QHello!\\E");
inputString = @"Hello\\E";
XCTAssertEqualObjects([PFQueryUtilities regexStringForString:inputString], @"\\QHello\\E\\\\E\\Q\\E");
inputString = @"\\QHello";
XCTAssertEqualObjects([PFQueryUtilities regexStringForString:inputString], @"\\Q\\QHello\\E");
}
- (void)testErrors {
NSError *error = [PFQueryUtilities objectNotFoundError];
XCTAssertNotNil(error);
XCTAssertEqual(error.code, kPFErrorObjectNotFound);
XCTAssertEqual(error.domain, PFParseErrorDomain);
}
@end