Skip to content

Commit c2e2b1d

Browse files
committed
refactor: Port MPUserIdentityChange to Swift
1 parent 5743c8f commit c2e2b1d

File tree

12 files changed

+246
-189
lines changed

12 files changed

+246
-189
lines changed

UnitTests/MPBackendControllerTests.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,7 @@ - (void)testSetIdentityToNil {
773773
}];
774774
NSDictionary *identities = [MParticle sharedInstance].identity.currentUser.identities;
775775
XCTAssertEqualObjects(@"foo", [identities objectForKey:@(MPUserIdentityEmail)]);
776-
[[self backendController] setUserIdentity:(id)[NSNull null] identityType:MPUserIdentityEmail
776+
[[self backendController] setUserIdentity:nil identityType:MPUserIdentityEmail
777777
timestamp:[NSDate date]
778778
completionHandler:^(NSString * _Nullable identityString, MPUserIdentity identityType, MPExecStatus execStatus) {
779779

UnitTests/MPUserIdentityChangeTests.m

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#import <XCTest/XCTest.h>
2-
#import "MPUserIdentityChange.h"
32
#import "MPIdentityApiRequest.h"
43
#import "MParticleUser.h"
54
#import "mParticle.h"
65
#import "MPIUserDefaults.h"
76
#import "MPIConstants.h"
87
#import "MPBackendController.h"
8+
#import "MParticleSwift.h"
99
#import "MPBaseTestCase.h"
1010

1111
@interface MParticle ()
@@ -64,7 +64,7 @@ - (void)testUserIdentityInstance {
6464
NSDate *date = [NSDate date];
6565

6666
// New user identity
67-
MPUserIdentityInstance *userIdentity = [[MPUserIdentityInstance alloc] initWithType:MPUserIdentityCustomerId value:@"The Most Interesting Man in the World" dateFirstSet:date isFirstTimeSet:YES];
67+
MPUserIdentityInstance_PRIVATE *userIdentity = [[MPUserIdentityInstance_PRIVATE alloc] initWithType:MPUserIdentityCustomerId value:@"The Most Interesting Man in the World" dateFirstSet:date isFirstTimeSet:YES];
6868
XCTAssertNotNil(userIdentity);
6969
XCTAssertEqual(userIdentity.type, MPUserIdentityCustomerId);
7070
XCTAssertEqualObjects(userIdentity.value, @"The Most Interesting Man in the World");
@@ -79,7 +79,7 @@ - (void)testUserIdentityInstance {
7979
XCTAssertEqualObjects(dictionary[@"f"], @(YES));
8080

8181
// Delete user identity
82-
userIdentity = [[MPUserIdentityInstance alloc] initWithType:MPUserIdentityCustomerId value:nil dateFirstSet:date isFirstTimeSet:NO];
82+
userIdentity = [[MPUserIdentityInstance_PRIVATE alloc] initWithType:MPUserIdentityCustomerId value:nil dateFirstSet:date isFirstTimeSet:NO];
8383
XCTAssertNotNil(userIdentity);
8484
XCTAssertEqual(userIdentity.type, MPUserIdentityCustomerId);
8585
XCTAssertNil(userIdentity.value);
@@ -102,30 +102,43 @@ - (void)testUserIdentityInstanceWithDictionary {
102102
@"dfs":MPMilliseconds([date timeIntervalSince1970]),
103103
@"f":@YES};
104104

105-
MPUserIdentityInstance *userIdentity = [[MPUserIdentityInstance alloc] initWithUserIdentityDictionary:userIdentityDictionary];
105+
MPUserIdentityInstance_PRIVATE *userIdentity = [[MPUserIdentityInstance_PRIVATE alloc] initWithUserIdentityDictionary:userIdentityDictionary];
106106
XCTAssertNotNil(userIdentity);
107107
XCTAssertEqual(userIdentity.type, MPUserIdentityCustomerId);
108108
XCTAssertEqualObjects(userIdentity.value, @"The Most Interesting Man in the World");
109109
XCTAssertEqualWithAccuracy([userIdentity.dateFirstSet timeIntervalSince1970], [date timeIntervalSince1970], 0.01);
110110
XCTAssertTrue(userIdentity.isFirstTimeSet);
111+
112+
113+
userIdentityDictionary = @{@"n":@(MPUserIdentityOther2),
114+
@"i":@"34353234",
115+
@"dfs":MPMilliseconds([date timeIntervalSince1970]),
116+
@"f":@YES};
117+
118+
userIdentity = [[MPUserIdentityInstance_PRIVATE alloc] initWithUserIdentityDictionary:userIdentityDictionary];
119+
XCTAssertNotNil(userIdentity);
120+
XCTAssertEqual(userIdentity.type, MPUserIdentityOther2);
121+
XCTAssertEqualObjects(userIdentity.value, @"34353234");
122+
XCTAssertEqualWithAccuracy([userIdentity.dateFirstSet timeIntervalSince1970], [date timeIntervalSince1970], 0.01);
123+
XCTAssertTrue(userIdentity.isFirstTimeSet);
111124
}
112125

113126
- (void)testUserIdentityChange {
114127
NSDate *date = [NSDate date];
115-
MPUserIdentityInstance *userIdentityNew = [[MPUserIdentityInstance alloc] initWithType:MPUserIdentityCustomerId value:@"The Most Interesting Man in the World" dateFirstSet:date isFirstTimeSet:NO];
116-
MPUserIdentityInstance *userIdentityOld = [[MPUserIdentityInstance alloc] initWithType:MPUserIdentityCustomerId value:@"The Least Interesting Man in the World" dateFirstSet:[NSDate distantPast] isFirstTimeSet:YES];
128+
MPUserIdentityInstance_PRIVATE *newUserIdentity = [[MPUserIdentityInstance_PRIVATE alloc] initWithType:MPUserIdentityCustomerId value:@"The Most Interesting Man in the World" dateFirstSet:date isFirstTimeSet:NO];
129+
MPUserIdentityInstance_PRIVATE *oldUserIdentity = [[MPUserIdentityInstance_PRIVATE alloc] initWithType:MPUserIdentityCustomerId value:@"The Least Interesting Man in the World" dateFirstSet:[NSDate distantPast] isFirstTimeSet:YES];
117130

118-
MPUserIdentityChange *userIdentityChange = [[MPUserIdentityChange alloc] initWithNewUserIdentity:userIdentityNew oldUserIdentity:userIdentityOld timestamp:date userIdentities:nil];
131+
MPUserIdentityChange_PRIVATE *userIdentityChange = [[MPUserIdentityChange_PRIVATE alloc] initWithNewUserIdentity:newUserIdentity oldUserIdentity:oldUserIdentity timestamp:date userIdentities:nil];
119132
XCTAssertNotNil(userIdentityChange);
120-
XCTAssertNotNil(userIdentityChange.userIdentityNew);
121-
XCTAssertNotNil(userIdentityChange.userIdentityOld);
133+
XCTAssertNotNil(userIdentityChange.newUserIdentity);
134+
XCTAssertNotNil(userIdentityChange.oldUserIdentity);
122135
XCTAssertNotNil(userIdentityChange.timestamp);
123136
XCTAssertTrue(userIdentityChange.changed);
124137

125-
userIdentityChange = [[MPUserIdentityChange alloc] initWithNewUserIdentity:userIdentityNew oldUserIdentity:nil timestamp:nil userIdentities:nil];
138+
userIdentityChange = [[MPUserIdentityChange_PRIVATE alloc] initWithNewUserIdentity:newUserIdentity oldUserIdentity:nil timestamp:nil userIdentities:nil];
126139
XCTAssertNotNil(userIdentityChange);
127-
XCTAssertNotNil(userIdentityChange.userIdentityNew);
128-
XCTAssertNil(userIdentityChange.userIdentityOld);
140+
XCTAssertNotNil(userIdentityChange.newUserIdentity);
141+
XCTAssertNil(userIdentityChange.oldUserIdentity);
129142
XCTAssertNotNil(userIdentityChange.timestamp);
130143
XCTAssertTrue(userIdentityChange.changed);
131144
}
@@ -138,13 +151,30 @@ - (void)testIdenticalUserIdentityChange {
138151
}
139152
];
140153

141-
MPUserIdentityInstance *userIdentityNew = [[MPUserIdentityInstance alloc] initWithType:MPUserIdentityCustomerId value:@"The Most Interesting Man in the World" dateFirstSet:[NSDate date] isFirstTimeSet:NO];
142-
MPUserIdentityChange *userIdentityChange = [[MPUserIdentityChange alloc] initWithNewUserIdentity:userIdentityNew userIdentities:userIdentities];
154+
MPUserIdentityInstance_PRIVATE *newUserIdentity = [[MPUserIdentityInstance_PRIVATE alloc] initWithType:MPUserIdentityCustomerId value:@"The Most Interesting Man in the World" dateFirstSet:[NSDate date] isFirstTimeSet:NO];
155+
MPUserIdentityChange_PRIVATE *userIdentityChange = [[MPUserIdentityChange_PRIVATE alloc] initWithNewUserIdentity:newUserIdentity userIdentities:userIdentities];
143156
XCTAssertNotNil(userIdentityChange);
144-
XCTAssertNotNil(userIdentityChange.userIdentityNew);
145-
XCTAssertNil(userIdentityChange.userIdentityOld);
157+
XCTAssertNotNil(userIdentityChange.newUserIdentity);
158+
XCTAssertNil(userIdentityChange.oldUserIdentity);
146159
XCTAssertNotNil(userIdentityChange.timestamp);
147160
XCTAssertFalse(userIdentityChange.changed);
148161
}
149162

163+
- (void)testUserIdentityChangeTimestampBehavior {
164+
165+
MPUserIdentityInstance_PRIVATE *newUserIdentity = [[MPUserIdentityInstance_PRIVATE alloc] initWithType:MPUserIdentityCustomerId value:@"test1" dateFirstSet:[NSDate date] isFirstTimeSet:NO];
166+
MPUserIdentityInstance_PRIVATE *oldUserIdentity = [[MPUserIdentityInstance_PRIVATE alloc] initWithType:MPUserIdentityCustomerId value:@"test2" dateFirstSet:[NSDate distantPast] isFirstTimeSet:YES];
167+
168+
// Set date on init
169+
NSDate *date1 = [NSDate date];
170+
MPUserIdentityChange_PRIVATE *userIdentityChange1 = [[MPUserIdentityChange_PRIVATE alloc] initWithNewUserIdentity:newUserIdentity oldUserIdentity:oldUserIdentity timestamp:date1 userIdentities:nil];
171+
XCTAssertEqualObjects(userIdentityChange1.timestamp, date1);
172+
173+
// Date is created on get
174+
MPUserIdentityChange_PRIVATE *userIdentityChange2 = [[MPUserIdentityChange_PRIVATE alloc] initWithNewUserIdentity:newUserIdentity oldUserIdentity:oldUserIdentity timestamp:nil userIdentities:nil];
175+
NSTimeInterval timestamp = userIdentityChange2.timestamp.timeIntervalSince1970;
176+
[NSThread sleepForTimeInterval:0.5];
177+
XCTAssertEqual(timestamp, userIdentityChange2.timestamp.timeIntervalSince1970);
178+
}
179+
150180
@end

0 commit comments

Comments
 (0)