-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathAmplitudeObjCExampleTests.m
More file actions
344 lines (291 loc) · 15 KB
/
AmplitudeObjCExampleTests.m
File metadata and controls
344 lines (291 loc) · 15 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
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#import <AmplitudeObjCExampleTests-Swift.h>
#import <XCTest/XCTest.h>
@import AmplitudeSwift;
@interface AmplitudeObjCExampleTests : XCTestCase
@end
@implementation AmplitudeObjCExampleTests
- (void)testTrack {
Amplitude* amplitude = [self getAmplitude:@"track"];
NSDictionary* eventProperties = @{
@"prop-string": @"string-value",
@"prop-int": @111,
@"prop-number": @12.3,
@"prop-boolean": @true,
@"prop-string-array": @[@"item-1", @"item-2"],
@"prop-int-array": @[@1, @2, @3],
@"prop-number-array": @[@1.1, @2.2, @3.3],
@"prop-bool-array": @[@true, @false, @true],
@"prop-object": @{@"nested-prop-1": @555, @"nested-prop-2": @"nested-string"}
};
AMPBaseEvent *event = [[AMPBaseEvent alloc] initWithEventType:@"Event-A"
eventProperties:eventProperties];
XCTestExpectation *expectation = [[XCTestExpectation alloc] initWithDescription:@"event tracked"];
[amplitude track:event callback:^(AMPBaseEvent *event, NSInteger code, NSString *message) {
[expectation fulfill];
}];
[self waitForExpectations:@[expectation]];
NSArray<NSString*>* eventsStrings = [amplitude.storage getEventsStrings];
NSArray* events = [self parseEvents:eventsStrings];
XCTAssertEqual(events.count, 1);
XCTAssertEqualObjects(@"Event-A", [events[0] objectForKey:@"event_type"]);
XCTAssertEqualObjects(eventProperties, [events[0] objectForKey:@"event_properties"]);
}
- (void)testTrack_Options {
Amplitude* amplitude = [self getAmplitude:@"track_options"];
AMPEventOptions* eventOptions = [AMPEventOptions new];
eventOptions.locationLat = 12;
eventOptions.locationLng = 34;
NSDictionary* eventProperties = @{
@"prop-string": @"string-value",
@"prop-int": @111
};
AMPBaseEvent *event = [[AMPBaseEvent alloc] initWithEventType:@"Event-A"
eventProperties:eventProperties];
XCTestExpectation *expectation = [[XCTestExpectation alloc] initWithDescription:@"event tracked"];
[amplitude track:event
options:eventOptions
callback:^(AMPBaseEvent *event, NSInteger code, NSString *message) {
[expectation fulfill];
}];
[self waitForExpectations:@[expectation]];
NSArray<NSString*>* eventsStrings = [amplitude.storage getEventsStrings];
NSArray* events = [self parseEvents:eventsStrings];
XCTAssertEqual(events.count, 1);
XCTAssertEqualObjects(@"Event-A", [events[0] objectForKey:@"event_type"]);
XCTAssertEqualObjects(eventProperties, [events[0] objectForKey:@"event_properties"]);
XCTAssertEqualObjects(@12, [events[0] objectForKey:@"location_lat"]);
XCTAssertEqualObjects(@34, [events[0] objectForKey:@"location_lng"]);
}
- (void)testIdentify {
Amplitude* amplitude = [self getAmplitude:@"identify"];
AMPIdentify* identify = [AMPIdentify new];
[identify set:@"user-string-prop" value:@"string-value"];
[identify setOnce:@"user-int-prop" value:@111];
[identify append:@"user-number-prop" value:@123.4];
[identify prepend:@"user-bool-prop" value:@true];
[identify add:@"user-sum-prop" valueInt:7];
[identify remove:@"user-agg-prop" value: @"item1"];
[identify unset:@"user-deprecated-prop"];
XCTestExpectation *expectation = [[XCTestExpectation alloc] initWithDescription:@"identify tracked"];
AMPEventOptions *options = [[AMPEventOptions alloc] init];
options.callback = ^(AMPBaseEvent *event, NSInteger code, NSString *message) {
[expectation fulfill];
};
[amplitude identify:identify options:options];
[self waitForExpectations:@[expectation]];
NSDictionary* expectedUserProperties = @{
@"$set": @{@"user-string-prop": @"string-value"},
@"$setOnce": @{@"user-int-prop": @111},
@"$append": @{@"user-number-prop": @123.4},
@"$prepend": @{@"user-bool-prop": @true},
@"$add": @{@"user-sum-prop": @7},
@"$remove": @{@"user-agg-prop": @"item1"},
@"$unset": @{@"user-deprecated-prop": @"-"}
};
NSArray<NSString*>* eventsStrings = [amplitude.storage getEventsStrings];
NSArray* events = [self parseEvents:eventsStrings];
XCTAssertEqual(events.count, 1);
XCTAssertEqualObjects(@"$identify", [events[0] objectForKey:@"event_type"]);
XCTAssertEqualObjects(expectedUserProperties, [events[0] objectForKey:@"user_properties"]);
}
- (void)testIdentify_ClearAll {
Amplitude* amplitude = [self getAmplitude:@"identify-clearAll"];
AMPIdentify* identify = [AMPIdentify new];
[identify clearAll];
XCTestExpectation *expectation = [[XCTestExpectation alloc] initWithDescription:@"identify tracked"];
AMPEventOptions *options = [[AMPEventOptions alloc] init];
options.callback = ^(AMPBaseEvent *event, NSInteger code, NSString *message) {
[expectation fulfill];
};
[amplitude identify:identify options:options];
[self waitForExpectations:@[expectation]];
NSDictionary* expectedUserProperties = @{
@"$clearAll": @"-"
};
NSArray<NSString*>* eventsStrings = [amplitude.storage getEventsStrings];
NSArray* events = [self parseEvents:eventsStrings];
XCTAssertEqual(events.count, 1);
XCTAssertEqualObjects(@"$identify", [events[0] objectForKey:@"event_type"]);
XCTAssertEqualObjects(expectedUserProperties, [events[0] objectForKey:@"user_properties"]);
}
- (void)testIdentify_InterceptedIdentifies {
Amplitude* amplitude = [self getAmplitude:@"identify-interceptedIdentifies"];
AMPIdentify* identify1 = [AMPIdentify new];
[identify1 set:@"user-string-prop" value:@"string-value"];
XCTestExpectation *expectation1 = [[XCTestExpectation alloc] initWithDescription:@"identify 1 tracked"];
AMPEventOptions *options1 = [[AMPEventOptions alloc] init];
options1.callback = ^(AMPBaseEvent *event, NSInteger code, NSString *message) {
[expectation1 fulfill];
};
[amplitude identify:identify1 options:options1];
AMPIdentify* identify2 = [AMPIdentify new];
[identify2 set:@"user-int-prop" value:@111];
XCTestExpectation *expectation2 = [[XCTestExpectation alloc] initWithDescription:@"identify 1 tracked"];
AMPEventOptions *options2 = [[AMPEventOptions alloc] init];
options2.callback = ^(AMPBaseEvent *event, NSInteger code, NSString *message) {
[expectation2 fulfill];
};
[amplitude identify:identify2 options:options2];
[self waitForExpectations:@[expectation1, expectation2]];
NSDictionary* expectedUserProperties1 = @{
@"$set": @{@"user-string-prop": @"string-value"}
};
NSDictionary* expectedUserProperties2 = @{
@"$set": @{@"user-int-prop": @111}
};
NSArray<NSString*>* eventsStrings = [amplitude.storage getInterceptedIdentifiesStrings];
NSArray* events = [self parseEvents:eventsStrings];
XCTAssertEqual(events.count, 2);
XCTAssertEqualObjects(@"$identify", [events[0] objectForKey:@"event_type"]);
XCTAssertEqualObjects(expectedUserProperties1, [events[0] objectForKey:@"user_properties"]);
XCTAssertEqualObjects(@"$identify", [events[1] objectForKey:@"event_type"]);
XCTAssertEqualObjects(expectedUserProperties2, [events[1] objectForKey:@"user_properties"]);
}
- (void)testGroupIdentify {
Amplitude* amplitude = [self getAmplitude:@"groupIdentify"];
AMPIdentify* identify = [AMPIdentify new];
[identify set:@"user-string-prop" value:@"string-value"];
[identify setOnce:@"user-int-prop" value:@111];
[identify append:@"user-number-prop" value:@123.4];
[identify prepend:@"user-bool-prop" value:@true];
[identify add:@"user-sum-prop" valueInt:7];
[identify remove:@"user-agg-prop" value: @"item1"];
[identify unset:@"user-deprecated-prop"];
XCTestExpectation *expectation = [[XCTestExpectation alloc] initWithDescription:@"group identify tracked"];
AMPEventOptions *options = [[AMPEventOptions alloc] init];
options.callback = ^(AMPBaseEvent *event, NSInteger code, NSString *message) {
[expectation fulfill];
};
[amplitude groupIdentify:@"type-1" groupName:@"name-1" identify:identify options:options];
[self waitForExpectations:@[expectation]];
NSDictionary* expectedGroupProperties = @{
@"$set": @{@"user-string-prop": @"string-value"},
@"$setOnce": @{@"user-int-prop": @111},
@"$append": @{@"user-number-prop": @123.4},
@"$prepend": @{@"user-bool-prop": @true},
@"$add": @{@"user-sum-prop": @7},
@"$remove": @{@"user-agg-prop": @"item1"},
@"$unset": @{@"user-deprecated-prop": @"-"}
};
NSArray<NSString*>* eventsStrings = [amplitude.storage getEventsStrings];
NSArray* events = [self parseEvents:eventsStrings];
XCTAssertEqual(events.count, 1);
XCTAssertEqualObjects(@"$groupidentify", [events[0] objectForKey:@"event_type"]);
XCTAssertEqualObjects(@{@"type-1": @"name-1"}, [events[0] objectForKey:@"groups"]);
XCTAssertEqualObjects(expectedGroupProperties, [events[0] objectForKey:@"group_properties"]);
}
- (void)testSetGroup {
Amplitude* amplitude = [self getAmplitude:@"setGroup"];
XCTestExpectation *expectation = [[XCTestExpectation alloc] initWithDescription:@"set group tracked"];
AMPEventOptions *options = [[AMPEventOptions alloc] init];
options.callback = ^(AMPBaseEvent *event, NSInteger code, NSString *message) {
[expectation fulfill];
};
NSString *groupName = @"name-1";
[amplitude setGroup:@"type-1" groupName:groupName options:options];
[self waitForExpectations:@[expectation]];
NSArray<NSString*>* eventsStrings = [amplitude.storage getEventsStrings];
NSArray* events = [self parseEvents:eventsStrings];
XCTAssertEqual(events.count, 1);
XCTAssertEqualObjects(@"$identify", [events[0] objectForKey:@"event_type"]);
XCTAssertEqualObjects(@{@"type-1": groupName}, [events[0] objectForKey:@"groups"]);
XCTAssertEqualObjects(@{@"$set": @{@"type-1": groupName}}, [events[0] objectForKey:@"user_properties"]);
}
- (void)testSetGroup_Multiple {
Amplitude* amplitude = [self getAmplitude:@"setGroup_Multiple"];
XCTestExpectation *expectation = [[XCTestExpectation alloc] initWithDescription:@"set group tracked"];
AMPEventOptions *options = [[AMPEventOptions alloc] init];
options.callback = ^(AMPBaseEvent *event, NSInteger code, NSString *message) {
[expectation fulfill];
};
NSArray<NSString*>* groupNames = @[@"name-1", @"name-2"];
[amplitude setGroup:@"type-1" groupNames:groupNames options:options];
[self waitForExpectations:@[expectation]];
NSArray<NSString*>* eventsStrings = [amplitude.storage getEventsStrings];
NSArray* events = [self parseEvents:eventsStrings];
XCTAssertEqual(events.count, 1);
XCTAssertEqualObjects(@"$identify", [events[0] objectForKey:@"event_type"]);
XCTAssertEqualObjects(@{@"type-1": groupNames}, [events[0] objectForKey:@"groups"]);
XCTAssertEqualObjects(@{@"$set": @{@"type-1": groupNames}}, [events[0] objectForKey:@"user_properties"]);
}
- (void)testPlugin {
Amplitude* amplitude = [self getAmplitude:@"plugin"];
[amplitude add:[AMPPlugin initWithType:AMPPluginTypeBefore execute:^AMPBaseEvent* _Nullable(AMPBaseEvent* _Nonnull event) {
[event.eventProperties set:@"plugin-prop" value:@"plugin-value"];
event.locationLat = 12;
event.locationLng = 34;
return event;
}]];
XCTestExpectation *expectation = [[XCTestExpectation alloc] initWithDescription:@"group identify tracked"];
AMPEventOptions *options = [[AMPEventOptions alloc] init];
options.callback = ^(AMPBaseEvent *event, NSInteger code, NSString *message) {
[expectation fulfill];
};
[amplitude track:@"Event-A"
eventProperties: @{@"prop-string": @"string-value", @"prop-int": @111}
options:options];
[self waitForExpectations:@[expectation]];
NSDictionary* expectedEventProperties = @{
@"prop-string": @"string-value",
@"prop-int": @111,
@"plugin-prop": @"plugin-value"
};
NSArray<NSString*>* eventsStrings = [amplitude.storage getEventsStrings];
NSArray* events = [self parseEvents:eventsStrings];
XCTAssertEqual(events.count, 1);
XCTAssertEqualObjects(@"Event-A", [events[0] objectForKey:@"event_type"]);
XCTAssertEqualObjects(expectedEventProperties, [events[0] objectForKey:@"event_properties"]);
XCTAssertEqualObjects(@12, [events[0] objectForKey:@"location_lat"]);
XCTAssertEqualObjects(@34, [events[0] objectForKey:@"location_lng"]);
}
- (void)testDestinationPlugin {
XCTestExpectation* expectation = [self expectationWithDescription:@"flush"];
NSMutableArray<AMPBaseEvent*>* collectedEvents = [NSMutableArray array];
Amplitude* amplitude = [self getAmplitude:@"plugin"];
[amplitude add:[AMPPlugin initWithType:AMPPluginTypeDestination execute:^AMPBaseEvent* _Nullable(AMPBaseEvent* _Nonnull event) {
[collectedEvents addObject:event];
return nil;
} flush:^() {
[expectation fulfill];
}]];
[amplitude track:@"Event-A" eventProperties:nil];
[amplitude track:@"Event-B" eventProperties:nil];
[amplitude track:@"Event-C" eventProperties:nil];
[amplitude flush];
[self waitForExpectationsWithTimeout:5.0 handler:^(NSError* error) {
if (error) {
XCTFail("Expectation failed with error: %@", error);
}
}];
XCTAssertEqual(collectedEvents.count, 3);
XCTAssertEqualObjects(@"Event-A", [collectedEvents objectAtIndex:0].eventType);
XCTAssertEqualObjects(@"Event-B", [collectedEvents objectAtIndex:1].eventType);
XCTAssertEqualObjects(@"Event-C", [collectedEvents objectAtIndex:2].eventType);
}
- (void)testEventProperties {
AMPBaseEvent* event = [AMPBaseEvent initWithEventType:@"Event-A" eventProperties:@{@"prop-1": @"123"}];
[event.eventProperties set:@"prop-2" value:@111];
XCTAssertEqualObjects(@"123", [event.eventProperties get:@"prop-1"]);
XCTAssertEqualObjects(@111, [event.eventProperties get:@"prop-2"]);
[event.eventProperties remove:@"prop-1"];
XCTAssertEqualObjects(nil, [event.eventProperties get:@"prop-1"]);
XCTAssertEqualObjects(@111, [event.eventProperties get:@"prop-2"]);
}
- (Amplitude *)getAmplitude:(NSString *)instancePrefix {
NSString* instanceName = [NSString stringWithFormat:@"%@-%f", instancePrefix, [[NSDate date] timeIntervalSince1970]];
AMPConfiguration* configuration = [AMPConfiguration initWithApiKey:@"API-KEY" instanceName:instanceName];
AMPAutocaptureOptions* autocaptureOptions = [AMPAutocaptureOptions new];
autocaptureOptions.sessions = NO;
configuration.autocapture = autocaptureOptions;
Amplitude *amplitude = [[TestAmplitude alloc] initWithConfiguration:configuration];
return amplitude;
}
- (NSArray *)parseEvents:(NSArray<NSString*>*)eventsStrings {
XCTAssertEqual(1, eventsStrings.count);
NSError* jsonError = nil;
NSData* data = [eventsStrings[0] dataUsingEncoding:NSUTF8StringEncoding];
NSArray* events = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&jsonError];
XCTAssertNil(jsonError);
return events;
}
@end