Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 5343 Refactor Kit Filter Hash Functions #228

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
93 changes: 93 additions & 0 deletions UnitTests/HasherTests.mm
Expand Up @@ -4,6 +4,8 @@
#import "MPEvent.h"
#import "MPBaseTestCase.h"
#import "MPIHasher.h"
#import "mparticle.h"
#import "MPIConstants.h"

@interface HasherTests : MPBaseTestCase

Expand Down Expand Up @@ -293,4 +295,95 @@ - (void)testHashEventType {
XCTAssertEqualObjects(hashTestString, @"1600", @"Should have been equal.");
}

- (void)testHashEventName {
NSString *hashTestString = [MPIHasher hashEventName:MPEventTypeNavigation eventName:@"test" isLogScreen:false];
XCTAssertEqualObjects(hashTestString, @"48809027", @"Should have been equal.");

hashTestString = [MPIHasher hashEventName:MPEventTypeNavigation eventName:@"test" isLogScreen:true];
XCTAssertEqualObjects(hashTestString, @"47885506", @"Should have been equal.");

hashTestString = [MPIHasher hashEventName:MPEventTypeLocation eventName:@"test" isLogScreen:false];
XCTAssertEqualObjects(hashTestString, @"49732548", @"Should have been equal.");

hashTestString = [MPIHasher hashEventName:MPEventTypeLocation eventName:@"test" isLogScreen:true];
XCTAssertEqualObjects(hashTestString, @"47885506", @"Should have been equal.");
}

- (void)testHashEventAttributeKey {
NSString *hashTestString = [MPIHasher hashEventAttributeKey:MPEventTypeNavigation eventName:@"test" customAttributeName:@"testAtt" isLogScreen:false];
XCTAssertEqualObjects(hashTestString, @"-1449619668", @"Should have been equal.");

hashTestString = [MPIHasher hashEventAttributeKey:MPEventTypeNavigation eventName:@"test" customAttributeName:@"testAtt" isLogScreen:true];
XCTAssertEqualObjects(hashTestString, @"-1578702387", @"Should have been equal.");

hashTestString = [MPIHasher hashEventAttributeKey:MPEventTypeLocation eventName:@"test" customAttributeName:@"testAtt" isLogScreen:false];
XCTAssertEqualObjects(hashTestString, @"-1320536949", @"Should have been equal.");

hashTestString = [MPIHasher hashEventAttributeKey:MPEventTypeLocation eventName:@"test" customAttributeName:@"testAtt" isLogScreen:true];
XCTAssertEqualObjects(hashTestString, @"-1578702387", @"Should have been equal.");
}

- (void)testHashUserAttributeKeyAndValue {
NSString *hashTestString = [MPIHasher hashUserAttributeKey:@"key1"];
XCTAssertEqualObjects(hashTestString, @"3288498", @"Should have been equal.");

hashTestString = [MPIHasher hashUserAttributeKey:@"key2"];
XCTAssertEqualObjects(hashTestString, @"3288499", @"Should have been equal.");

hashTestString = [MPIHasher hashUserAttributeValue:@"value1"];
XCTAssertEqualObjects(hashTestString, @"-823812896", @"Should have been equal.");

hashTestString = [MPIHasher hashUserAttributeValue:@"value2"];
XCTAssertEqualObjects(hashTestString, @"-823812895", @"Should have been equal.");
}

- (void)testHashUserIdentity {
NSString *hashTestString = [MPIHasher hashUserIdentity:MPUserIdentityOther];
XCTAssertEqualObjects(hashTestString, @"0", @"Should have been equal.");

hashTestString = [MPIHasher hashUserIdentity:MPUserIdentityCustomerId];
XCTAssertEqualObjects(hashTestString, @"1", @"Should have been equal.");
}

- (void)testHashConsentPurpose {
NSString *hashTestString = [MPIHasher hashConsentPurpose:kMPConsentCCPARegulationType purpose:kMPConsentCCPAPurposeName];
XCTAssertEqualObjects(hashTestString, @"-575335347", @"Should have been equal.");

hashTestString = [MPIHasher hashConsentPurpose:kMPConsentGDPRRegulationType purpose:@""];
XCTAssertEqualObjects(hashTestString, @"49", @"Should have been equal.");

hashTestString = [MPIHasher hashConsentPurpose:kMPConsentGDPRRegulationType purpose:@"purpose1"];
XCTAssertEqualObjects(hashTestString, @"-910367228", @"Should have been equal.");
}

- (void)testHashCommerceEventAttribute {
NSString *hashTestString = [MPIHasher hashCommerceEventAttribute:MPEventTypePurchase key:@"price"];
XCTAssertEqualObjects(hashTestString, @"-2104051132", @"Should have been equal.");

hashTestString = [MPIHasher hashCommerceEventAttribute:MPEventTypeRefund key:@"price"];
XCTAssertEqualObjects(hashTestString, @"-2075421981", @"Should have been equal.");
}

- (void)testHashDifferences {
// Creates a product object
MPProduct *product = [[MPProduct alloc] initWithName:@"Awesome Book" sku:@"1234567890" quantity:@1 price:@9.99];
product.brand = @"A Publisher";
product.category = @"Fiction";
product.couponCode = @"XYZ123";
product.position = 1;
product[@"custom key"] = @"custom value"; // A product may contain custom key/value pairs

// Creates a commerce event object
MPCommerceEvent *commerceEvent = [[MPCommerceEvent alloc] initWithAction:MPCommerceEventActionPurchase product:product];
NSString *key = @"an_extra_key";
commerceEvent.customAttributes = @{key: @"an_extra_value"}; // A commerce event may contain custom key/value pairs

string attributeToHash = to_string([commerceEvent type]) + string([[key lowercaseString] cStringUsingEncoding:NSUTF8StringEncoding]);
int hashValueOldInt = mParticle::Hasher::hashFromString(attributeToHash);

NSString *hashValueNewString = [MPIHasher hashCommerceEventAttribute:commerceEvent.type key:key];
XCTAssertEqual(hashValueOldInt, [hashValueNewString intValue], @"Should have been equal.");

}

@end
1 change: 0 additions & 1 deletion UnitTests/MPKitContainerTests.m
Expand Up @@ -61,7 +61,6 @@ - (void)handleApplicationDidFinishLaunching:(NSNotification *)notification;
- (nullable NSString *)nameForKitCode:(nonnull NSNumber *)integrationId;
- (id<MPKitProtocol>)startKit:(NSNumber *)integrationId configuration:(MPKitConfiguration *)kitConfiguration;
- (void)flushSerializedKits;
- (NSDictionary *)methodMessageTypeMapping;
- (MPKitFilter *)filter:(id<MPExtensionKitProtocol>)kitRegister forEvent:(MPEvent *const)event selector:(SEL)selector;
- (MPKitFilter *)filter:(id<MPExtensionKitProtocol>)kitRegister forBaseEvent:(MPBaseEvent *const)event forSelector:(SEL)selector;
- (MPKitFilter *)filter:(id<MPExtensionKitProtocol>)kitRegister forUserAttributeKey:(NSString *)key value:(id)value;
Expand Down
8 changes: 7 additions & 1 deletion mParticle-Apple-SDK/Include/MPIHasher.h
Expand Up @@ -8,6 +8,12 @@
+ (NSString *)hashStringUTF16:(NSString *)stringToHash;
+ (NSString *)hashEventType:(MPEventType)eventType;
+ (MPEventType)eventTypeForHash:(NSString *)hashString;

+ (NSString *)hashEventName:(MPEventType)eventType eventName:(NSString *)eventName isLogScreen:(BOOL)isLogScreen;
+ (NSString *)hashEventAttributeKey:(MPEventType)eventType eventName:(NSString *)eventName customAttributeName:(NSString *)customAttributeName isLogScreen:(BOOL)isLogScreen;
+ (NSString *)hashUserAttributeKey:(NSString *)userAttributeKey;
+ (NSString *)hashUserAttributeValue:(NSString *)userAttributeValue;
+ (NSString *)hashUserIdentity:(MPUserIdentity)userIdentity;
+ (NSString *)hashConsentPurpose:(NSString *)regulationPrefix purpose:(NSString *)purpose;
+ (NSString *)hashCommerceEventAttribute:(MPEventType)commerceEventType key:(NSString *)key;

@end