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

SDK-3831 - Adding unit test cases #346

Merged
merged 5 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CleverTapSDK.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@
07D8C08B21DDEC54006F5A1B /* CTCarouselImageView.h in Headers */ = {isa = PBXBuildFile; fileRef = 07D8C08A21DDEC54006F5A1B /* CTCarouselImageView.h */; settings = {ATTRIBUTES = (Private, ); }; };
07FD65A2223BC26300A845B7 /* CTCoverViewController~iphoneland.xib in Resources */ = {isa = PBXBuildFile; fileRef = 07FD65A1223BC26300A845B7 /* CTCoverViewController~iphoneland.xib */; };
07FD65A4223BCB8200A845B7 /* CTCoverViewController~ipadland.xib in Resources */ = {isa = PBXBuildFile; fileRef = 07FD65A3223BCB8200A845B7 /* CTCoverViewController~ipadland.xib */; };
0B5564562C25946C00B87284 /* CTUserInfoMigratorTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 0B5564552C25946C00B87284 /* CTUserInfoMigratorTest.m */; };
0B995A4A2C36AEDC00AF6006 /* CTLocalDataStoreTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 0B995A492C36AEDC00AF6006 /* CTLocalDataStoreTests.m */; };
1F1C18806B7F29B3374F2448 /* libPods-shared-CleverTapSDKTestsApp.a in Frameworks */ = {isa = PBXBuildFile; fileRef = E303560B5EE1D154C1E3D9EF /* libPods-shared-CleverTapSDKTestsApp.a */; };
32394C1F29FA251E00956058 /* CTEventBuilderTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 32394C1E29FA251E00956058 /* CTEventBuilderTest.m */; };
32394C2129FA264B00956058 /* CTPreferencesTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 32394C2029FA264B00956058 /* CTPreferencesTest.m */; };
Expand Down Expand Up @@ -700,6 +702,8 @@
07D8C08A21DDEC54006F5A1B /* CTCarouselImageView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CTCarouselImageView.h; sourceTree = "<group>"; };
07FD65A1223BC26300A845B7 /* CTCoverViewController~iphoneland.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = "CTCoverViewController~iphoneland.xib"; sourceTree = "<group>"; };
07FD65A3223BCB8200A845B7 /* CTCoverViewController~ipadland.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = "CTCoverViewController~ipadland.xib"; sourceTree = "<group>"; };
0B5564552C25946C00B87284 /* CTUserInfoMigratorTest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CTUserInfoMigratorTest.m; sourceTree = "<group>"; };
0B995A492C36AEDC00AF6006 /* CTLocalDataStoreTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CTLocalDataStoreTests.m; sourceTree = "<group>"; };
0CA46771B6F202E37DAC9F70 /* Pods-shared-CleverTapSDKTestsApp.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-shared-CleverTapSDKTestsApp.debug.xcconfig"; path = "Target Support Files/Pods-shared-CleverTapSDKTestsApp/Pods-shared-CleverTapSDKTestsApp.debug.xcconfig"; sourceTree = "<group>"; };
129AEC403AFA828F591B756E /* Pods-shared-CleverTapSDKTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-shared-CleverTapSDKTests.release.xcconfig"; path = "Target Support Files/Pods-shared-CleverTapSDKTests/Pods-shared-CleverTapSDKTests.release.xcconfig"; sourceTree = "<group>"; };
32394C1E29FA251E00956058 /* CTEventBuilderTest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CTEventBuilderTest.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1405,6 +1409,8 @@
6A59D20E2A3351A800531F9D /* LeanplumCTTest.m */,
4ECD88302ADC8A05003885CE /* CTSessionManagerTests.m */,
6BD851C82B45CD1800FA5298 /* CTMultiDelegateManager+Tests.h */,
0B5564552C25946C00B87284 /* CTUserInfoMigratorTest.m */,
0B995A492C36AEDC00AF6006 /* CTLocalDataStoreTests.m */,
);
path = CleverTapSDKTests;
sourceTree = "<group>";
Expand Down Expand Up @@ -2303,7 +2309,9 @@
32790959299F4B29001FE140 /* CTDeviceInfoTest.m in Sources */,
48C31A822B1DC5CF00CA2A90 /* CTInAppImagePrefetchManagerTest.m in Sources */,
4ECD88312ADC8A05003885CE /* CTSessionManagerTests.m in Sources */,
0B995A4A2C36AEDC00AF6006 /* CTLocalDataStoreTests.m in Sources */,
6A4427C52AA6515A0098866F /* CTTriggersMatcherTest.m in Sources */,
0B5564562C25946C00B87284 /* CTUserInfoMigratorTest.m in Sources */,
4E2CF1442AC56D8F00441E8B /* CTEncryptionTests.m in Sources */,
32394C2729FA278C00956058 /* CTUriHelperTest.m in Sources */,
6A59D20D2A334B8500531F9D /* NSDictionaryExtensionsTest.m in Sources */,
Expand Down
118 changes: 118 additions & 0 deletions CleverTapSDKTests/CTLocalDataStoreTests.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
//
// CTLocalDataStoreTests.m
// CleverTapSDKTests
//
// Created by Kushagra Mishra on 04/07/24.
// Copyright © 2024 CleverTap. All rights reserved.
//
#import <XCTest/XCTest.h>
#import <OCMock/OCMock.h>
#import "CTLocalDataStore.h"
#import "CTProfileBuilder.h"
#import "CTConstants.h"

@interface CTLocalDataStoreTests : XCTestCase
@property (nonatomic, strong) CTLocalDataStore *dataStore;
@property (nonatomic, strong) id dataStoreMock;
@property (nonatomic, strong) id profileBuilderMock;
@end

@implementation CTLocalDataStoreTests

- (void)setUp {
[super setUp];
CleverTapInstanceConfig *config = [[CleverTapInstanceConfig alloc] initWithAccountId:@"testAccount" accountToken:@"testToken" accountRegion:@"testRegion"];
CTDeviceInfo *deviceInfo = [[CTDeviceInfo alloc] initWithConfig:config andCleverTapID:@"testDeviceInfo"];
CTDispatchQueueManager *queueManager =[[CTDispatchQueueManager alloc]initWithConfig:config];
self.dataStore = [[CTLocalDataStore alloc] initWithConfig:config profileValues:[NSMutableDictionary new] andDeviceInfo:deviceInfo dispatchQueueManager:queueManager];
}

- (void)tearDown {
self.dataStore = nil;
[super tearDown];
}

- (void)testGetUserAttributeChangePropertiesWithEmptyEvent {
NSDictionary *event = @{};
NSDictionary *result = [self.dataStore getUserAttributeChangeProperties:event];
XCTAssertEqual(result.count, 0);
}

- (void)testGetUserAttributeChangePropertiesWithNoProfile {
NSDictionary *event = @{@"someKey": @"someValue"};
NSDictionary *result = [self.dataStore getUserAttributeChangeProperties:event];
XCTAssertEqual(result.count, 0);
}

- (void)testGetUserAttributeChangePropertiesWithProfileUpdate {
NSDictionary *profile = @{
@"name": @"John",
@"age": @"30",
@"cc": @"1234", // Should be skipped
@"tz": @"GMT", // Should be skipped
@"Carrier": @"Jio" // Should be skipped
};
NSDictionary *event = @{CLTAP_PROFILE: profile};

// Mock old values for the keys
id mockOldValueForName = @"Jane";
id mockOldValueForAge = @"25";

// Stub the method to return mock old values
CTLocalDataStore *dataStoreMock = OCMPartialMock(self.dataStore);
id mockGetProfileFieldForKeyName = OCMStub([dataStoreMock getProfileFieldForKey:@"name"]).andReturn(mockOldValueForName);
id mockGetProfileFieldForKeyAge = OCMStub([dataStoreMock getProfileFieldForKey:@"age"]).andReturn(mockOldValueForAge);

// Call the method and get the result
NSDictionary *result = [dataStoreMock getUserAttributeChangeProperties:event];

// Print debug information
NSLog(@"Result: %@", result);

// Verify the result dictionary
XCTAssertEqual(result.count, 2);
XCTAssertEqual(result[@"name"][CLTAP_KEY_OLD_VALUE], mockOldValueForName);
XCTAssertEqual(result[@"name"][CLTAP_KEY_NEW_VALUE], @"John");
XCTAssertEqual(result[@"age"][CLTAP_KEY_OLD_VALUE], mockOldValueForAge);
XCTAssertEqual(result[@"age"][CLTAP_KEY_NEW_VALUE], @"30");

// Ensure skipped keys are not present in the result
XCTAssertNil(result[@"cc"]);
XCTAssertNil(result[@"tz"]);
XCTAssertNil(result[@"Carrier"]);

// Verify the mock methods were called
OCMVerify(mockGetProfileFieldForKeyName);
OCMVerify(mockGetProfileFieldForKeyAge);
}

- (void)testGetUserAttributeChangePropertiesWithIncrementCommand {
NSDictionary *profile = @{
@"points": @{kCLTAP_COMMAND_INCREMENT: @10}
};
NSDictionary *event = @{CLTAP_PROFILE: profile};

// Mock old and new values for the key
id mockOldValue = @20;
id mockNewValue = @30;

// Stub the method to return mock old values and handle increment command
CTLocalDataStore *dataStoreMock = OCMPartialMock(self.dataStore);
id mockGetProfileFieldForKey = OCMStub([dataStoreMock getProfileFieldForKey:@"points"]).andReturn(mockOldValue);

// Call the method and get the result
NSDictionary *result = [dataStoreMock getUserAttributeChangeProperties:event];

// Print debug information
NSLog(@"Result: %@", result);

// Verify the result dictionary
XCTAssertEqual(result.count, 1);
XCTAssertEqual(result[@"points"][CLTAP_KEY_OLD_VALUE], mockOldValue);
XCTAssertEqual(result[@"points"][CLTAP_KEY_NEW_VALUE], mockNewValue);

// Verify the mock methods were called
OCMVerify(mockGetProfileFieldForKey);
}

@end
107 changes: 107 additions & 0 deletions CleverTapSDKTests/CTUserInfoMigratorTest.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
//
// CTUserInfoMigratorTests.m
// CleverTapSDKTests
//
// Created by Kushagra Mishra on 21/06/24.
// Copyright © 2024 CleverTap. All rights reserved.
//

#import <XCTest/XCTest.h>
#import "CTUserInfoMigrator.h"
#import "XCTestCase+XCTestCase_Tests.h"

@interface CTUserInfoMigratorTest : XCTestCase

@property (nonatomic, strong) NSFileManager *fileManager;
@property (nonatomic, strong) NSString *libraryPath;

@end

@implementation CTUserInfoMigratorTest


- (void)setUp {
[super setUp];
self.fileManager = [NSFileManager defaultManager];

// Get the path to the Library directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
self.libraryPath = [paths objectAtIndex:0];
}

- (void)tearDown {
// Clean up any files created during the test
NSError *error = nil;
NSArray *contents = [self.fileManager contentsOfDirectoryAtPath:self.libraryPath error:&error];
for (NSString *file in contents) {
if ([file containsString:@"clevertap-"]) {
NSString *filePath = [self.libraryPath stringByAppendingPathComponent:file];
[self.fileManager removeItemAtPath:filePath error:&error];
}
}
[super tearDown];
}

- (void)testMigrateUserInfoFileForAccountID_WhenOldFileExists_ShouldCopyToNewLocation {
NSString *acc_id = @"testAccID";
NSString *device_id = @"testDeviceID";

// Create the old plist file
NSString *oldFileName = [NSString stringWithFormat:@"clevertap-%@-userprofile.plist", acc_id];
NSString *oldFilePath = [self.libraryPath stringByAppendingPathComponent:oldFileName];
[self.fileManager createFileAtPath:oldFilePath contents:[@"old content" dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];

// Call the method to migrate the user info file
[CTUserInfoMigrator migrateUserInfoFileForAccountID:acc_id deviceID:device_id];

// Check that the old file has been copied to the new location
NSString *newFileName = [NSString stringWithFormat:@"clevertap-%@-%@-userprofile.plist", acc_id, device_id];
NSString *newFilePath = [self.libraryPath stringByAppendingPathComponent:newFileName];
XCTAssertTrue([self.fileManager fileExistsAtPath:newFilePath], @"New plist file should exist");

// Check that the old file has been deleted
XCTAssertFalse([self.fileManager fileExistsAtPath:oldFilePath], @"Old plist file should be deleted");
}

- (void)testMigrateUserInfoFileForAccountID_WhenNewFileExists_ShouldNotCopyAndDeleteOldFile {
NSString *acc_id = @"testAccID";
NSString *device_id = @"testDeviceID";

// Create both old and new plist files
NSString *oldFileName = [NSString stringWithFormat:@"clevertap-%@-userprofile.plist", acc_id];
NSString *oldFilePath = [self.libraryPath stringByAppendingPathComponent:oldFileName];
[self.fileManager createFileAtPath:oldFilePath contents:[@"old content" dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];

NSString *newFileName = [NSString stringWithFormat:@"clevertap-%@-%@-userprofile.plist", acc_id, device_id];
NSString *newFilePath = [self.libraryPath stringByAppendingPathComponent:newFileName];
[self.fileManager createFileAtPath:newFilePath contents:[@"new content" dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];

// Call the method to migrate the user info file
[CTUserInfoMigrator migrateUserInfoFileForAccountID:acc_id deviceID:device_id];

// Check that the new file still exists
XCTAssertTrue([self.fileManager fileExistsAtPath:newFilePath], @"New plist file should exist");

// Check that the old file has been deleted
XCTAssertFalse([self.fileManager fileExistsAtPath:oldFilePath], @"Old plist file should be deleted");
}

- (void)testMigrateUserInfoFileForAccountID_WhenOldFileDoesNotExist_ShouldNotCreateNewFile {
NSString *acc_id = @"testAccID";
NSString *device_id = @"testDeviceID";

// Ensure the old plist file does not exist
NSString *oldFileName = [NSString stringWithFormat:@"clevertap-%@-userprofile.plist", acc_id];
NSString *oldFilePath = [self.libraryPath stringByAppendingPathComponent:oldFileName];
[self.fileManager removeItemAtPath:oldFilePath error:nil];

// Call the method to migrate the user info file
[CTUserInfoMigrator migrateUserInfoFileForAccountID:acc_id deviceID:device_id];

// Check that the new file does not exist
NSString *newFileName = [NSString stringWithFormat:@"clevertap-%@-%@-userprofile.plist", acc_id, device_id];
NSString *newFilePath = [self.libraryPath stringByAppendingPathComponent:newFileName];
XCTAssertFalse([self.fileManager fileExistsAtPath:newFilePath], @"New plist file should not be created");
}

@end
37 changes: 37 additions & 0 deletions CleverTapSDKTests/InApps/CTInAppEvaluationManagerTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,43 @@ - (void)testEvaluateWithInApps {
XCTAssertEqual([self.evaluationManager.triggerManager getTriggers:@"4"], 1);
}

- (void)testEvaluateUserAttribute {

self.helper.inAppStore.serverSideInApps = @[
@{
@"ti": @1,
@"whenTriggers": @[@{
@"eventProperties": @[@{
@"propertyName": @"newValue",
@"propertyValue": @"Gold",
}],
@"profileAttrName": @"Customer Type",
}]
},
@{
@"ti": @2,
@"whenTriggers": @[@{
@"eventProperties": @[@{
@"propertyName": @"newValue",
@"propertyValue": @"Premium",
}],
@"profileAttrName": @"Customer Type",
}]
},
];
NSDictionary *profile = @{
@"Customer Type": @{
@"newValue": @"Gold",
@"oldValue": @"Premium"
}
};


[self.evaluationManager evaluateOnUserAttributeChange:profile];
XCTAssertEqualObjects((@[@1]), self.evaluationManager.evaluatedServerSideInAppIds);
XCTAssertNotEqualObjects((@[@2]), self.evaluationManager.evaluatedServerSideInAppIds);
}

- (void)testEvaluateCharged {
self.helper.inAppStore.serverSideInApps = @[
@{
Expand Down
Loading