forked from facebook/facebook-ios-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAEMAdvertiserRuleFactoryTests.swift
311 lines (298 loc) · 9.98 KB
/
AEMAdvertiserRuleFactoryTests.swift
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the license found in the
* LICENSE file in the root directory of this source tree.
*/
@testable import FBAEMKit
import XCTest
final class AEMAdvertiserRuleFactoryTests: XCTestCase {
let factory = AEMAdvertiserRuleFactory()
func testCreateRuleWithJson() {
XCTAssertNil(
factory.createRule(json: nil),
"Should not create valid Single Entry Rule with nil"
)
XCTAssertNil(
factory.createRule(json: ""),
"Should not create valid Single Entry Rule with empty string"
)
XCTAssertNil(
factory.createRule(json: nil),
"Should not create valid Single Entry Rule with nil"
)
XCTAssertNotNil(
factory.createRule(json: #"{"and": [{"value": {"contains": "abc"}}]}"#),
"Should create expected Multi Entry Rule"
)
XCTAssertNotNil(
factory.createRule(json: #"{"value": {"contains": "abc"}}"#),
"Should create expected Single Entry Rule"
)
XCTAssertNotNil(
factory.createRule(json: #"{"and": [{"event": {"eq": "Lead"}}, {"or": [{"URL": {"contains": "achievetestprep.com"}}]}]}"#), // swiftlint:disable:this line_length
"Should create expected nested Multi Entry Rule"
)
}
func testCreateRuleWithDict() {
XCTAssertNil(
factory.createRule(dictionary: [:]),
"Should not create valid Single Entry Rule with emtpy dictionary"
)
XCTAssertNil(
factory.createRule(dictionary: ["value": ["contains": 10]]),
"Should not create valid Single Entry Rule with invalid dictionary"
)
let multiEntryRule = factory.createRule(dictionary: ["and": [SampleAEMData.validAdvertiserSingleEntryRuleJson1]]) as? AEMAdvertiserMultiEntryRule // swiftlint:disable:this line_length
XCTAssertNotNil(
multiEntryRule,
"Should create expected Multi Entry Rule"
)
let singleEntryRule = factory.createRule(dictionary: ["value": ["contains": "abc"]]) as? AEMAdvertiserSingleEntryRule // swiftlint:disable:this line_length
XCTAssertNotNil(
singleEntryRule,
"Should create expected Single Entry Rule"
)
}
func testCreateSingleEntryRuleWithValidDict() {
var rule: AEMAdvertiserSingleEntryRule?
rule = factory.createSingleEntryRule(from: SampleAEMData.validAdvertiserSingleEntryRuleJson1)
XCTAssertTrue(
SampleAEMData.advertiserSingleEntryRule1.isEqual(rule),
"Should create the expected Single Entry Rule"
)
rule = factory.createSingleEntryRule(from: SampleAEMData.validAdvertiserSingleEntryRuleJson2)
XCTAssertTrue(
SampleAEMData.advertiserSingleEntryRule2.isEqual(rule),
"Should create the expected Single Entry Rule"
)
rule = factory.createSingleEntryRule(from: SampleAEMData.validAdvertiserSingleEntryRuleJson3)
XCTAssertTrue(
SampleAEMData.advertiserSingleEntryRule3.isEqual(rule),
"Should create the expected Single Entry Rule"
)
}
func testCreateSingleEntryRuleWithInvalidDict() {
XCTAssertNil(
factory.createSingleEntryRule(from: [:]),
"Should not create valid Single Entry Rule with empty dictionary"
)
XCTAssertNil(
factory.createSingleEntryRule(from: ["contains": []]),
"Should not create valid Single Entry Rule with invalid dictionary"
)
XCTAssertNil(
factory.createSingleEntryRule(from: ["value": ["contains": 10]]),
"Should not create valid Single Entry Rule with invalid dictionary"
)
XCTAssertNil(
factory.createSingleEntryRule(from: ["value": ["lt": "abc"]]),
"Should not create valid Single Entry Rule with invalid dictionary"
)
}
func testCreateMultiEntryRuleWithValidDict() {
zip(
["and", "or", "not"],
[
.and,
.or,
.not,
] as [AEMAdvertiserRuleOperator]
).forEach { opString, expectedOperator in
let rule: AEMAdvertiserMultiEntryRule? = factory.createMultiEntryRule(
from: [opString: [SampleAEMData.validAdvertiserSingleEntryRuleJson1, SampleAEMData.validAdvertiserSingleEntryRuleJson2]]) // swiftlint:disable:this line_length
XCTAssertNotNil(
rule,
"Should create Multi Entry Rule with valid dictionary"
)
XCTAssertEqual(
expectedOperator,
rule?.operator,
"Multi Entry Rule should have the expected operator"
)
XCTAssertEqual(
2,
rule?.rules.count,
"Multi Entry Rule should have the expected number of subrules"
)
} // swiftlint:disable:this closure_end_indentation
}
func testCreateMultiEntryRuleWithInvalidDict() {
XCTAssertNil(
factory.createMultiEntryRule(from: [:]),
"Should not create valid Multi Entry Rule with empty dictionary"
)
XCTAssertNil(
factory.createMultiEntryRule(from: ["contains": [["content": ["starts_with": "abc"]]]]),
"Should not create valid Multi Entry Rule with invlaid operator"
)
XCTAssertNil(
factory.createMultiEntryRule(from: ["and": []]),
"Should not create valid Multi Entry Rule with empty subrules"
)
XCTAssertNil(
factory.createMultiEntryRule(from: ["and": ["value": ["contains": 10]]]),
"Should not create valid Multi Entry Rule with invlaid subrule"
)
}
func testGetPrimaryKey() {
XCTAssertEqual(
factory.primaryKey(for: ["test_key": "abc"]),
"test_key",
"Should get the expected key of the dictionary"
)
XCTAssertNil(
factory.primaryKey(for: [:]),
"Should not get the unexpected key while the dictionay is empty"
)
}
func testGetOperator() {
XCTAssertEqual(
factory.getOperator(from: ["test_key": "abc"]),
.unknown,
"Should get the expected Unknown operator of the dictionary"
)
XCTAssertEqual(
factory.getOperator(from: ["And": "abc"]),
.and,
"Should get the expected AND operator of the dictionary"
)
XCTAssertEqual(
factory.getOperator(from: ["and": "abc"]),
.and,
"Should get the expected AND operator of the dictionary"
)
XCTAssertEqual(
factory.getOperator(from: ["or": "abc"]),
.or,
"Should get the expected OR operator of the dictionary"
)
XCTAssertEqual(
factory.getOperator(from: ["not": "abc"]),
.not,
"Should get the expected NOT operator of the dictionary"
)
XCTAssertEqual(
factory.getOperator(from: ["contains": "abc"]),
.contains,
"Should get the expected Contains operator of the dictionary"
)
XCTAssertEqual(
factory.getOperator(from: ["not_contains": "abc"]),
.notContains,
"Should get the expected NotContains operator of the dictionary"
)
XCTAssertEqual(
factory.getOperator(from: ["starts_with": "abc"]),
.startsWith,
"Should get the expected StartsWith operator of the dictionary"
)
XCTAssertEqual(
factory.getOperator(from: ["i_contains": "abc"]),
.caseInsensitiveContains,
"Should get the expected i_contains operator of the dictionary"
)
XCTAssertEqual(
factory.getOperator(from: ["i_not_contains": "abc"]),
.caseInsensitiveNotContains,
"Should get the expected I_NotContains operator of the dictionary"
)
XCTAssertEqual(
factory.getOperator(from: ["i_starts_with": "abc"]),
.caseInsensitiveStartsWith,
"Should get the expected I_StartsWith operator of the dictionary"
)
XCTAssertEqual(
factory.getOperator(from: ["regex_match": "abc"]),
.regexMatch,
"Should get the expected REGEX_MATCH operator of the dictionary"
)
XCTAssertEqual(
factory.getOperator(from: ["eq": "abc"]),
.equal,
"Should get the expected EQ operator of the dictionary"
)
XCTAssertEqual(
factory.getOperator(from: ["neq": "abc"]),
.notEqual,
"Should get the expected NEQ operator of the dictionary"
)
XCTAssertEqual(
factory.getOperator(from: ["lt": 10]),
.lessThan,
"Should get the expected LT operator of the dictionary"
)
XCTAssertEqual(
factory.getOperator(from: ["lte": 10]),
.lessThanOrEqual,
"Should get the expected LTE operator of the dictionary"
)
XCTAssertEqual(
factory.getOperator(from: ["gt": 10]),
.greaterThan,
"Should get the expected GT operator of the dictionary"
)
XCTAssertEqual(
factory.getOperator(from: ["gte": 10]),
.greaterThanOrEqual,
"Should get the expected GTE operator of the dictionary"
)
XCTAssertEqual(
factory.getOperator(from: ["i_is_any": ["abc"]]),
.caseInsensitiveIsAny,
"Should get the expected I_IsAny operator of the dictionary"
)
XCTAssertEqual(
factory.getOperator(from: ["i_is_not_any": ["abc"]]),
.caseInsensitiveIsNotAny,
"Should get the expected I_IsNotAny operator of the dictionary"
)
XCTAssertEqual(
factory.getOperator(from: ["is_any": ["abc"]]),
.isAny,
"Should get the expected IsAny operator of the dictionary"
)
XCTAssertEqual(
factory.getOperator(from: ["is_not_any": ["abc"]]),
.isNotAny,
"Should get the expected IsNotAny operator of the dictionary"
)
}
func testIsOperatorForMultiEntryRule() {
for ruleOperator in [
.and,
.or,
.not,
] as [AEMAdvertiserRuleOperator] {
XCTAssertTrue(
factory.isOperatorForMultiEntryRule(ruleOperator),
"Should expect the operator for multi entry rule"
)
}
for ruleOperator in [
.contains,
.notContains,
.startsWith,
.caseInsensitiveContains,
.caseInsensitiveNotContains,
.caseInsensitiveStartsWith,
.regexMatch,
.equal,
.notEqual,
.lessThan,
.lessThanOrEqual,
.greaterThan,
.greaterThanOrEqual,
.caseInsensitiveIsAny,
.caseInsensitiveIsNotAny,
.isAny,
.isNotAny,
] as [AEMAdvertiserRuleOperator] {
XCTAssertFalse(
factory.isOperatorForMultiEntryRule(ruleOperator),
"Should expect the operator not for multi entry rule"
)
}
}
}