Skip to content

Commit

Permalink
DXOC-652 set client.userId from Ampli options.userId (#209)
Browse files Browse the repository at this point in the history
* DXOC-652 set client.userId from Ampli options.userId (broken tests WIP)

* DXOC-652 fixed Ampli Swift tests to wait for middleware to complete

* DXOC-652 fix ObjC tests (locally)

* DXOC-652 added dummy api keys to allow tests to build

* DXOC-652 added dummy api keys to Swift tests to fix CI tests

* DXOC-652 revert optional string logic, temporarily commented out optional check in test

* DXOC-652 test code clean up

* DXOC-652 added testIdentifyUserIdOnEvent for Swift
  • Loading branch information
justin-fiedler authored Mar 17, 2023
1 parent 7391a2d commit 085e473
Show file tree
Hide file tree
Showing 10 changed files with 207 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
<EnvironmentVariables>
<EnvironmentVariable
key = "AMPLITUDE_API_KEY"
value = ""
value = "your-amplitude-api-key"
isEnabled = "YES">
</EnvironmentVariable>
</EnvironmentVariables>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,9 @@ - (void)identify:(NSString *_Nullable)userId event:(Identify *)event options:(Ev
if (![self isInitializedAndEnabled]) {
return;
}
if (userId != nil) {
if (options != nil && options.userId != nil) {
[self.client setUserId:options.userId];
} else if (userId != nil) {
[self.client setUserId:userId];
}
if (options != nil && options.deviceId != nil) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,7 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
propertyWithSpace:@"property with space"
]];

// TODO: Do we want to allow to track using direct params? Seems kinda pointless

[ampli flush];
return YES;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,48 @@

@interface AmpliTests : XCTestCase
@property (nonatomic, strong) Ampli *ampli;
@property (nonatomic, weak) XCTestExpectation* middlwareRun;
@end

@implementation AmpliTests

- (void)setUp {
_ampli = [Ampli instance];
_ampli = [Ampli new];
_middlwareRun = [self expectationWithDescription:@"Wait for middleware run"];
}

- (void) initAmpliWithNewInstance:(NSString *) instanceName {
Amplitude *client = [Amplitude instanceWithName:instanceName];
[client initializeApiKey:@"test-api-key"];

[_ampli load:[LoadOptions builderBlock:^(LoadOptionsBuilder *b) {
b.apiKey = @"test-api-key";
b.instance = client;
}]];
}

- (void)testTrackWithNoProperies {
[self initAmpliWithNewInstance:@"testTrackWithNoProperties"];

AMPBlockMiddleware *testMiddleware = [[AMPBlockMiddleware alloc] initWithBlock: ^(AMPMiddlewarePayload * _Nonnull payload, AMPMiddlewareNext _Nonnull next) {
XCTAssertEqualObjects(payload.event[@"event_type"], @"Event No Properties");
[self->_middlwareRun fulfill];
}];
[_ampli.client addEventMiddleware:testMiddleware];
[_ampli eventNoProperties];
[_ampli flush];

[self waitForExpectationsWithTimeout:1.0 handler:^(NSError *error) {
if (error) {
XCTFail(@"test timed out");
}
}];
}

- (void)testTrackEventWithAllTypes {
[self initAmpliWithNewInstance:@"testTrackEventWithAllTypes"];

NSMutableDictionary *extraDict = [NSMutableDictionary new];
[extraDict setObject:@"extra test" forKey:@"test"];

NSArray *requiredArray = [NSArray arrayWithObjects:@"array element 1", @"array element 2", nil];

AMPBlockMiddleware *testMiddleware = [[AMPBlockMiddleware alloc] initWithBlock: ^(AMPMiddlewarePayload * _Nonnull payload, AMPMiddlewareNext _Nonnull next) {
Expand All @@ -45,8 +64,10 @@ - (void)testTrackEventWithAllTypes {
XCTAssertEqualObjects(eventProperties[@"requiredInteger"], @10);
XCTAssertEqualObjects(eventProperties[@"requiredNumber"], @2.0F);
XCTAssertEqualObjects(eventProperties[@"requiredString"], @"required string");
XCTAssertNil(eventProperties[@"optionalString"]);
// FIXME: Uncomment after optional prop fix in DXOC-661
// XCTAssertNil(eventProperties[@"optionalString"]);
XCTAssertEqualObjects(payload.extra[@"test"], @"extra test");
[self->_middlwareRun fulfill];
}];

[_ampli.client addEventMiddleware:testMiddleware];
Expand All @@ -57,19 +78,30 @@ - (void)testTrackEventWithAllTypes {
requiredNumber:2.0F
requiredString:@"required string"
] extra:extraDict];
[_ampli flush];

[self waitForExpectationsWithTimeout:2.0 handler:^(NSError *error) {
if (error) {
NSLog(@"timeout errored: %@", error);
XCTFail(@"test timed out");
}
}];
}

- (void)testIdentify {
[self initAmpliWithNewInstance:@"testIdentify"];

NSString *userId = @"test-user-id";
NSString *deviceId = @"test-device-id";
EventOptions *eventOptions = [EventOptions builderBlock:^(EventOptionsBuilder *builder) {
builder.deviceId = deviceId;
builder.userId = userId;
}];
AMPBlockMiddleware *testMiddleware = [[AMPBlockMiddleware alloc] initWithBlock: ^(AMPMiddlewarePayload * _Nonnull payload, AMPMiddlewareNext _Nonnull next) {
XCTAssertEqualObjects(payload.event[@"event_type"], @"@identify");
XCTAssertEqualObjects(payload.event[@"event_type"], @"$identify");
XCTAssertEqualObjects(payload.event[@"user_id"], userId);
XCTAssertEqualObjects(payload.event[@"device_id"], deviceId);
[self->_middlwareRun fulfill];
}];
[_ampli.client addEventMiddleware:testMiddleware];
[_ampli identify:userId
Expand All @@ -78,43 +110,107 @@ - (void)testIdentify {
}]
options:eventOptions
];
[_ampli flush];
[self waitForExpectationsWithTimeout:2.0 handler:^(NSError *error) {
if (error) {
NSLog(@"timeout errored: %@", error);
XCTFail(@"test timed out");
}
}];
}

- (void)testIdentifyUserIdOnEvent {
[self initAmpliWithNewInstance:@"testIdentifyUserIdOnEvent"];

NSString *eventOptionsUserId = @"test-user-id-options";
NSString *userId = @"test-user-id";
NSString *deviceId = @"test-device-id";
EventOptions *eventOptions = [EventOptions builderBlock:^(EventOptionsBuilder *builder) {
builder.deviceId = deviceId;
builder.userId = eventOptionsUserId;
}];

AMPBlockMiddleware *testMiddleware = [[AMPBlockMiddleware alloc] initWithBlock: ^(AMPMiddlewarePayload * _Nonnull payload, AMPMiddlewareNext _Nonnull next) {
XCTAssertEqualObjects(payload.event[@"event_type"], @"$identify");
XCTAssertEqualObjects(payload.event[@"user_id"], eventOptionsUserId);
XCTAssertEqualObjects(payload.event[@"device_id"], deviceId);
XCTAssertEqualObjects(self->_ampli.client.userId, eventOptionsUserId);
[self->_middlwareRun fulfill];
}];
[_ampli.client addEventMiddleware:testMiddleware];
[_ampli identify:nil
event:[Identify requiredNumber: 22.0F builderBlock:^(IdentifyBuilder *b) {
b.optionalArray = [NSArray arrayWithObjects:@"optional string", nil];
}]
options:eventOptions
];
[_ampli flush];

[self waitForExpectationsWithTimeout:2.0 handler:^(NSError *error) {
if (error) {
NSLog(@"timeout errored: %@", error);
XCTFail(@"test timed out");
}
}];
}

- (void)testSetGroup {
[self initAmpliWithNewInstance:@"testSetGroup"];

NSString *groupType = @"test group type";
NSString *groupName = @"test group name";

AMPBlockMiddleware *testMiddleware = [[AMPBlockMiddleware alloc] initWithBlock: ^(AMPMiddlewarePayload * _Nonnull payload, AMPMiddlewareNext _Nonnull next) {
XCTAssertEqualObjects(payload.event[@"event_type"], @"@identify");
XCTAssertNil(payload.event[@"event_properties"]);
XCTAssertEqualObjects(payload.event[@"event_type"], @"$identify");
XCTAssertEqualObjects(payload.event[@"event_properties"], @{});
NSMutableDictionary *userPropertiesSet = payload.event[@"user_properties"][@"$set"];
XCTAssertEqualObjects(userPropertiesSet[groupType], groupName);
[self->_middlwareRun fulfill];
}];
[_ampli.client addEventMiddleware:testMiddleware];
[_ampli.client setGroup:groupType groupName:groupName];
[_ampli flush];

[self waitForExpectationsWithTimeout:2.0 handler:^(NSError *error) {
if (error) {
NSLog(@"timeout errored: %@", error);
XCTFail(@"test timed out");
}
}];
}

- (void)testGroupIdentify {
[self initAmpliWithNewInstance:@"testGroupIdentify"];

NSString *userId = @"test-user-id";
NSString *deviceId = @"test-device-id";
NSString *groupType = @"test-group-type";
NSString *groupName = @"test-group";
AMPBlockMiddleware *testMiddleware = [[AMPBlockMiddleware alloc] initWithBlock: ^(AMPMiddlewarePayload * _Nonnull payload, AMPMiddlewareNext _Nonnull next) {
XCTAssertEqualObjects(payload.event[@"event_type"], @"@groupidentify");
XCTAssertNil(payload.event[@"event_properties"]);
XCTAssertNil(payload.event[@"user_properties"]);
XCTAssertEqualObjects(payload.event[@"event_type"], @"$groupidentify");
XCTAssertEqualObjects(payload.event[@"event_properties"], @{});
XCTAssertEqualObjects(payload.event[@"user_properties"], @{});
NSMutableDictionary *groups = payload.event[@"groups"];
XCTAssertEqual(groups[groupType], groupName);
NSMutableDictionary *groupPropertiesSet = payload.event[@"group_properties"][@"$set"];
XCTAssertEqual(groupPropertiesSet[@"requiredBoolean"], @false);
XCTAssertEqual(groupPropertiesSet[@"optionalString"], @"optional string");
[self->_middlwareRun fulfill];
}];
[_ampli.client addEventMiddleware:testMiddleware];

AMPIdentify *identifyArgs = [AMPIdentify identify];
[identifyArgs set:@"requiredBoolean" value:@false];
[identifyArgs set:@"optionalString" value:@"optional string"];
[_ampli.client groupIdentifyWithGroupType:groupType groupName:groupName groupIdentify:identifyArgs];
[_ampli flush];

[self waitForExpectationsWithTimeout:2.0 handler:^(NSError *error) {
if (error) {
NSLog(@"timeout errored: %@", error);
XCTFail(@"test timed out");
}
}];
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
<EnvironmentVariables>
<EnvironmentVariable
key = "AMPLITUDE_API_KEY"
value = ""
value = "your-amplitude-api-key"
isEnabled = "YES">
</EnvironmentVariable>
</EnvironmentVariables>
Expand Down
Loading

0 comments on commit 085e473

Please sign in to comment.