diff --git a/CleverTapSDK/CTLocalDataStore.m b/CleverTapSDK/CTLocalDataStore.m index e073d446..0b46253e 100644 --- a/CleverTapSDK/CTLocalDataStore.m +++ b/CleverTapSDK/CTLocalDataStore.m @@ -48,6 +48,7 @@ - (instancetype)initWithConfig:(CleverTapInstanceConfig *)config profileValues:( _deviceInfo = deviceInfo; self.dispatchQueueManager = dispatchQueueManager; self.userEventLogs = [NSMutableSet set]; + self.dbHelper = [CTEventDatabase sharedInstanceWithConfig:self.config]; localProfileUpdateExpiryStore = [NSMutableDictionary new]; _backgroundQueue = dispatch_queue_create([[NSString stringWithFormat:@"com.clevertap.profileBackgroundQueue:%@", _config.accountId] UTF8String], DISPATCH_QUEUE_SERIAL); dispatch_queue_set_specific(_backgroundQueue, kProfileBackgroundQueueKey, (__bridge void *)self, NULL); @@ -191,24 +192,8 @@ - (void)persistEvent:(NSDictionary *)event { if (!event || !event[CLTAP_EVENT_NAME]) return; [self runOnBackgroundQueue:^{ NSString *eventName = event[CLTAP_EVENT_NAME]; - NSDictionary *storedEvents = [self getStoredEvents]; - if (!storedEvents) storedEvents = @{}; - NSTimeInterval now = [[[NSDate alloc] init] timeIntervalSince1970]; - NSArray *eventData = storedEvents[eventName]; - if (!eventData || eventData.count < 3) { - // This event has been recorded for the very first time - // Set the count to 0, first and last to now - // Count will be incremented soon after this block - eventData = @[@0.0f, @(now), @(now)]; - } - NSMutableArray *eventDataCopy = [eventData mutableCopy]; - double currentCount = ((NSNumber *) eventDataCopy[0]).doubleValue; - currentCount++; - eventDataCopy[0] = @(currentCount); - eventDataCopy[2] = @(now); - NSMutableDictionary *store = [storedEvents mutableCopy]; - store[eventName] = eventDataCopy; - [self setStoredEvents:store]; + // TODO: add normalisation + [self.dbHelper upsertEvent:eventName normalizedEventName:[CTUtils getNormalizedName:eventName] deviceID:self.deviceInfo.deviceId]; }]; } @@ -624,11 +609,8 @@ - (BOOL)isEventLoggedFirstTime:(NSString*)eventName { return NO; } } - if (!self.dbHelper) { - self.dbHelper = [CTEventDatabase sharedInstanceWithConfig:self.config]; - } // TODO: Add normalized name here - NSInteger count = [self.dbHelper getEventCount:eventName deviceID:self.deviceInfo.deviceId]; + NSInteger count = [self.dbHelper getEventCount:[CTUtils getNormalizedName:eventName] deviceID:self.deviceInfo.deviceId]; if (count > 1) { @synchronized (self.userEventLogs) { [self.userEventLogs addObject:eventName]; @@ -706,7 +688,6 @@ - (void)_setProfileValue:(id)value forKey:(NSString *)key fromUpstream:(BOOL)fro NSArray *systemProfileKeys = @[CLTAP_SYS_CARRIER, CLTAP_SYS_CC, CLTAP_SYS_TZ]; if (![systemProfileKeys containsObject:key]) { NSDictionary *profileEvent = @{CLTAP_EVENT_NAME: key}; - // TODO: Call appropriate persist method from the new db/localDataStore class [self persistEvent:profileEvent]; } } diff --git a/CleverTapSDK/CleverTap.m b/CleverTapSDK/CleverTap.m index 1f8a6422..c643e05b 100644 --- a/CleverTapSDK/CleverTap.m +++ b/CleverTapSDK/CleverTap.m @@ -537,7 +537,7 @@ - (void)initializeInAppSupport { templatesManager:templatesManager fileDownloader:self.fileDownloader]; - CTInAppEvaluationManager *evaluationManager = [[CTInAppEvaluationManager alloc] initWithAccountId:self.config.accountId deviceId:self.deviceInfo.deviceId delegateManager:self.delegateManager impressionManager:impressionManager inAppDisplayManager:displayManager inAppStore:inAppStore inAppTriggerManager:triggerManager]; + CTInAppEvaluationManager *evaluationManager = [[CTInAppEvaluationManager alloc] initWithAccountId:self.config.accountId deviceId:self.deviceInfo.deviceId delegateManager:self.delegateManager impressionManager:impressionManager inAppDisplayManager:displayManager inAppStore:inAppStore inAppTriggerManager:triggerManager localDataStore:self.localDataStore]; self.customTemplatesManager = templatesManager; self.inAppFCManager = inAppFCManager; diff --git a/CleverTapSDK/EventDatabase/CTEventDatabase.m b/CleverTapSDK/EventDatabase/CTEventDatabase.m index ce56dd03..9967e81e 100644 --- a/CleverTapSDK/EventDatabase/CTEventDatabase.m +++ b/CleverTapSDK/EventDatabase/CTEventDatabase.m @@ -477,7 +477,7 @@ - (BOOL)deleteLeastRecentlyUsedRows:(NSInteger)maxRowLimit - (BOOL)openDatabase { NSString *databasePath = [self databasePath]; - if (sqlite3_open([databasePath UTF8String], &_eventDatabase) == SQLITE_OK) { + if (sqlite3_open_v2([databasePath UTF8String], &_eventDatabase, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX, NULL) == SQLITE_OK) { // Create table, check and update the version if needed [self createTable]; [self checkAndUpdateDatabaseVersion]; diff --git a/CleverTapSDK/InApps/CTInAppEvaluationManager.h b/CleverTapSDK/InApps/CTInAppEvaluationManager.h index 2dbea791..847f6469 100644 --- a/CleverTapSDK/InApps/CTInAppEvaluationManager.h +++ b/CleverTapSDK/InApps/CTInAppEvaluationManager.h @@ -10,6 +10,7 @@ #import #import "CTBatchSentDelegate.h" #import "CTAttachToBatchHeaderDelegate.h" +#import "CTLocalDataStore.h" @class CTMultiDelegateManager; @class CTImpressionManager; @@ -30,7 +31,8 @@ NS_ASSUME_NONNULL_BEGIN impressionManager:(CTImpressionManager *)impressionManager inAppDisplayManager:(CTInAppDisplayManager *)inAppDisplayManager inAppStore:(CTInAppStore *)inAppStore - inAppTriggerManager:(CTInAppTriggerManager *)inAppTriggerManager; + inAppTriggerManager:(CTInAppTriggerManager *)inAppTriggerManager + localDataStore:(CTLocalDataStore *)dataStore; - (void)evaluateOnEvent:(NSString *)eventName withProps:(NSDictionary *)properties; - (void)evaluateOnChargedEvent:(NSDictionary *)chargeDetails andItems:(NSArray *)items; diff --git a/CleverTapSDK/InApps/CTInAppEvaluationManager.m b/CleverTapSDK/InApps/CTInAppEvaluationManager.m index f2896252..7f4427db 100644 --- a/CleverTapSDK/InApps/CTInAppEvaluationManager.m +++ b/CleverTapSDK/InApps/CTInAppEvaluationManager.m @@ -52,7 +52,8 @@ - (instancetype)initWithAccountId:(NSString *)accountId impressionManager:(CTImpressionManager *)impressionManager inAppDisplayManager:(CTInAppDisplayManager *)inAppDisplayManager inAppStore:(CTInAppStore *)inAppStore - inAppTriggerManager:(CTInAppTriggerManager *)inAppTriggerManager { + inAppTriggerManager:(CTInAppTriggerManager *)inAppTriggerManager + localDataStore:(CTLocalDataStore *)dataStore { if (self = [super init]) { self.accountId = accountId; self.deviceId = deviceId; @@ -84,7 +85,7 @@ - (instancetype)initWithAccountId:(NSString *)accountId } self.inAppStore = inAppStore; - self.triggersMatcher = [CTTriggersMatcher new]; + self.triggersMatcher = [[CTTriggersMatcher alloc]initWithDataStore:dataStore]; self.limitsMatcher = [CTLimitsMatcher new]; self.triggerManager = inAppTriggerManager; diff --git a/CleverTapSDK/InApps/Matchers/CTTriggersMatcher.h b/CleverTapSDK/InApps/Matchers/CTTriggersMatcher.h index 404c465a..9c08333e 100644 --- a/CleverTapSDK/InApps/Matchers/CTTriggersMatcher.h +++ b/CleverTapSDK/InApps/Matchers/CTTriggersMatcher.h @@ -8,11 +8,13 @@ #import #import "CTEventAdapter.h" +#import "CTLocalDataStore.h" NS_ASSUME_NONNULL_BEGIN @interface CTTriggersMatcher : NSObject +- (instancetype)initWithDataStore:(CTLocalDataStore *)dataStore; - (BOOL)matchEventWhenTriggers:(NSArray *)whenTriggers event:(CTEventAdapter *)event; @end diff --git a/CleverTapSDK/InApps/Matchers/CTTriggersMatcher.m b/CleverTapSDK/InApps/Matchers/CTTriggersMatcher.m index 785afb21..b006a81e 100644 --- a/CleverTapSDK/InApps/Matchers/CTTriggersMatcher.m +++ b/CleverTapSDK/InApps/Matchers/CTTriggersMatcher.m @@ -13,8 +13,19 @@ #import "CTTriggerEvaluator.h" #import "CTUtils.h" +@interface CTTriggersMatcher () {} +@property (nonatomic, strong) CTLocalDataStore *dataStore; +@end + @implementation CTTriggersMatcher +- (instancetype)initWithDataStore:(CTLocalDataStore *)dataStore { + if (self = [super init]) { + self.dataStore = dataStore; + } + return self; +} + - (BOOL)matchEventWhenTriggers:(NSArray *)whenTriggers event:(CTEventAdapter *)event { // Events in the array are OR-ed for (NSDictionary *triggerObject in whenTriggers) { @@ -41,7 +52,7 @@ - (BOOL)match:(CTTriggerAdapter *)trigger event:(CTEventAdapter *)event { return NO; } - if (![self matchFirstTimeOnly:event trigger:trigger]) { + if (![self matchFirstTimeOnlyForTrigger:trigger]) { return NO; } @@ -120,15 +131,12 @@ - (BOOL)matchCharged:(CTTriggerAdapter *)trigger event:(CTEventAdapter *)event { return YES; } -- (BOOL)matchFirstTimeOnly:(CTEventAdapter *)event trigger:(CTTriggerAdapter *)trigger { +- (BOOL)matchFirstTimeOnlyForTrigger:(CTTriggerAdapter *)trigger { if (!trigger.firstTimeOnly) { return YES; } - // TODO: Call IsEventFirstTime from the new db/localDataStore class -// NSString *nameToCheck = trigger.profileAttrName ?: trigger.eventName; -// return [CTLocalDataStore IsEventFirstTime:nameToCheck]; - - return YES; + NSString *nameToCheck = trigger.profileAttrName ?: trigger.eventName; + return [self.dataStore isEventLoggedFirstTime:nameToCheck]; } @end diff --git a/CleverTapSDKTests/CTLocalDataStoreTests.m b/CleverTapSDKTests/CTLocalDataStoreTests.m index 0def94fd..22b854b8 100644 --- a/CleverTapSDKTests/CTLocalDataStoreTests.m +++ b/CleverTapSDKTests/CTLocalDataStoreTests.m @@ -128,7 +128,7 @@ - (void)testPersistEventAndGetEventDetail { NSDictionary *event = @{CLTAP_EVENT_NAME: eventName}; [self.dataStore persistEvent:event]; sleep(1); - CleverTapEventDetail *eventDetails = [self.dataStore getEventDetail:eventName]; + CleverTapEventDetail *eventDetails = [self.dataStore readUserEventLog:eventName]; XCTAssertEqual(eventDetails.count, 1); XCTAssertGreaterThan(eventDetails.firstTime, 0); XCTAssertGreaterThan(eventDetails.lastTime, 0); diff --git a/CleverTapSDKTests/InApps/CTInAppEvaluationManagerTest.m b/CleverTapSDKTests/InApps/CTInAppEvaluationManagerTest.m index 87718b0e..328d4551 100644 --- a/CleverTapSDKTests/InApps/CTInAppEvaluationManagerTest.m +++ b/CleverTapSDKTests/InApps/CTInAppEvaluationManagerTest.m @@ -778,7 +778,7 @@ - (void)testDelegatesAdded { NSUInteger batchHeaderDelegatesCount = [[delegateManager attachToHeaderDelegates] count]; NSUInteger batchSentDelegatesCount = [[delegateManager batchSentDelegates] count]; - __unused CTInAppEvaluationManager *manager = [[CTInAppEvaluationManager alloc] initWithAccountId:self.helper.accountId deviceId:self.helper.deviceId delegateManager:delegateManager impressionManager:self.helper.impressionManager inAppDisplayManager:self.helper.inAppDisplayManager inAppStore:self.helper.inAppStore inAppTriggerManager:self.helper.inAppTriggerManager]; + __unused CTInAppEvaluationManager *manager = [[CTInAppEvaluationManager alloc] initWithAccountId:self.helper.accountId deviceId:self.helper.deviceId delegateManager:delegateManager impressionManager:self.helper.impressionManager inAppDisplayManager:self.helper.inAppDisplayManager inAppStore:self.helper.inAppStore inAppTriggerManager:self.helper.inAppTriggerManager localDataStore:self.helper.dataStore]; XCTAssertEqual([[delegateManager attachToHeaderDelegates] count], batchHeaderDelegatesCount + 1); XCTAssertEqual([[delegateManager batchSentDelegates] count], batchSentDelegatesCount + 1); diff --git a/CleverTapSDKTests/InApps/CTTriggersMatcherTest.m b/CleverTapSDKTests/InApps/CTTriggersMatcherTest.m index d3adc751..bef7652e 100644 --- a/CleverTapSDKTests/InApps/CTTriggersMatcherTest.m +++ b/CleverTapSDKTests/InApps/CTTriggersMatcherTest.m @@ -8,6 +8,7 @@ #import #import +#import #import "CTTriggersMatcher.h" #import "CTEventAdapter.h" #import "CTTriggerEvaluator.h" @@ -15,11 +16,19 @@ #import "CTConstants.h" @interface CTTriggersMatcherTest : XCTestCase - +@property (nonatomic, strong) CTLocalDataStore *dataStore; @end @implementation CTTriggersMatcherTest +- (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]; +} + #pragma mark Event - (void)testMatchEventAllOperators { NSArray *whenTriggers = @[ @@ -73,7 +82,7 @@ - (void)testMatchEventAllOperators { } ]; - CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] init]; + CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] initWithDataStore:self.dataStore]; BOOL match = [triggerMatcher matchEventWhenTriggers:whenTriggers eventName:@"event1" eventProperties:@{ @"prop1": @160, @@ -96,7 +105,7 @@ - (void)testMatchEventWithoutTriggerProps { } ]; - CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] init]; + CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] initWithDataStore:self.dataStore]; BOOL match = [triggerMatcher matchEventWhenTriggers:whenTriggers eventName:@"event1" eventProperties:@{ @"prop1": @"clevertap" @@ -116,7 +125,7 @@ - (void)testMatchEventWithEmptyTriggerProps { } ]; - CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] init]; + CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] initWithDataStore:self.dataStore]; BOOL match = [triggerMatcher matchEventWhenTriggers:whenTriggers eventName:@"event1" eventProperties:@{ @"prop1": @"clevertap" @@ -140,7 +149,7 @@ - (void)testMatchEventWithoutProps { } ]; - CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] init]; + CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] initWithDataStore:self.dataStore]; BOOL matchNoProps = [triggerMatcher matchEventWhenTriggers:whenTriggers eventName:@"event1" eventProperties:@{}]; XCTAssertFalse(matchNoProps); @@ -233,7 +242,7 @@ - (void)testMatchChargedEvent { } ]; - CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] init]; + CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] initWithDataStore:self.dataStore]; BOOL match = [triggerMatcher matchChargedEventWhenTriggers:whenTriggers details:@{ @"prop1": @150, @@ -277,7 +286,7 @@ - (void)testChargedWithoutItems { } ]; - CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] init]; + CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] initWithDataStore:self.dataStore]; BOOL match = [triggerMatcher matchChargedEventWhenTriggers:whenTriggers details:@{ @"prop1": @150, @@ -337,7 +346,7 @@ - (void)testMatchChargedEventItemArrayEquals { } ]; - CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] init]; + CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] initWithDataStore:self.dataStore]; BOOL match = [triggerMatcher matchChargedEventWhenTriggers:whenTriggers details:@{ @"prop1": @150, @@ -412,7 +421,7 @@ - (void)testMatchChargedEventItemArrayContains { } ]; - CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] init]; + CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] initWithDataStore:self.dataStore]; BOOL match = [triggerMatcher matchChargedEventWhenTriggers:whenTriggers details:@{ @"prop1": @150, @@ -487,7 +496,7 @@ - (void)testMatchEqualsPrimitives { } ]; - CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] init]; + CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] initWithDataStore:self.dataStore]; BOOL match = [triggerMatcher matchEventWhenTriggers:whenTriggers eventName:@"event1" eventProperties:@{ @"prop1": @150, @@ -518,7 +527,7 @@ - (void)testMatchEqualsBoolean { } ]; - CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] init]; + CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] initWithDataStore:self.dataStore]; BOOL match = [triggerMatcher matchEventWhenTriggers:whenTriggers eventName:@"event1" eventProperties:@{ @"prop1": @(YES), @@ -564,7 +573,7 @@ - (void)testMatchEqualsBooleanString { } ]; - CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] init]; + CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] initWithDataStore:self.dataStore]; BOOL match = [triggerMatcher matchEventWhenTriggers:whenTriggers eventName:@"event1" eventProperties:@{ @"prop1": @"true", @@ -593,7 +602,7 @@ - (void)testMatchEqualsBooleanCaseInsensitive { } ]; - CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] init]; + CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] initWithDataStore:self.dataStore]; BOOL match = [triggerMatcher matchEventWhenTriggers:whenTriggers eventName:@"event1" eventProperties:@{ @"prop1": @(YES), @@ -627,7 +636,7 @@ - (void)testMatchEqualsNumbers { } ]; - CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] init]; + CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] initWithDataStore:self.dataStore]; BOOL match = [triggerMatcher matchEventWhenTriggers:whenTriggers eventName:@"event1" eventProperties:@{ @"prop1": @150, @@ -696,7 +705,7 @@ - (void)testMatchEqualsNumbersCharged { } ]; - CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] init]; + CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] initWithDataStore:self.dataStore]; BOOL match = [triggerMatcher matchChargedEventWhenTriggers:whenTriggers details:@{} items:@[@{ @"prop1": @"150", @@ -743,7 +752,7 @@ - (void)testMatchEqualsDouble { } ]; - CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] init]; + CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] initWithDataStore:self.dataStore]; BOOL match = [triggerMatcher matchEventWhenTriggers:whenTriggers eventName:@"event1" eventProperties:@{ @"prop1": @150.950 @@ -775,7 +784,7 @@ - (void)testMatchEqualsExtectedStringWithActualArray { } ]; - CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] init]; + CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] initWithDataStore:self.dataStore]; BOOL match = [triggerMatcher matchEventWhenTriggers:whenTriggers eventName:@"event1" eventProperties:@{ @"prop1": @[@"test", @"test2"] @@ -804,7 +813,7 @@ - (void)testMatchEqualsExtectedArrayWithActualString { } ]; - CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] init]; + CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] initWithDataStore:self.dataStore]; BOOL match = [triggerMatcher matchEventWhenTriggers:whenTriggers eventName:@"event1" eventProperties:@{ @"prop1": @[@"test"] @@ -836,7 +845,7 @@ - (void)testMatchEqualsExtectedNumberWithActualArray { } ]; - CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] init]; + CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] initWithDataStore:self.dataStore]; BOOL match = [triggerMatcher matchEventWhenTriggers:whenTriggers eventName:@"event1" eventProperties:@{ @"prop1": @[@"test", @150] @@ -859,7 +868,7 @@ - (void)testMatchEqualsExtectedStringWithActualString { } ]; - CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] init]; + CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] initWithDataStore:self.dataStore]; BOOL match = [triggerMatcher matchEventWhenTriggers:whenTriggers eventName:@"event1" eventProperties:@{ @"prop1": @"test" @@ -951,7 +960,7 @@ - (void)testMatchEqualsExtectedNumberWithActualString { } ]; - CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] init]; + CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] initWithDataStore:self.dataStore]; BOOL match = [triggerMatcher matchEventWhenTriggers:whenTriggers eventName:@"event1" eventProperties:@{ @"prop1": @"test" @@ -973,7 +982,7 @@ - (void)testMatchEqualsExtectedDoubleWithActualDouble { } ]; - CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] init]; + CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] initWithDataStore:self.dataStore]; BOOL match = [triggerMatcher matchEventWhenTriggers:whenTriggers eventName:@"event1" eventProperties:@{ @"prop1": @150.99 @@ -996,7 +1005,7 @@ - (void)testMatchEqualsExtectedDoubleWithActualDoubleString { } ]; - CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] init]; + CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] initWithDataStore:self.dataStore]; BOOL match = [triggerMatcher matchEventWhenTriggers:whenTriggers eventName:@"event1" eventProperties:@{ @"prop1": @"150.99" @@ -1019,7 +1028,7 @@ - (void)testMatchEqualsExtectedArrayWithActualArray { } ]; - CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] init]; + CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] initWithDataStore:self.dataStore]; BOOL match = [triggerMatcher matchEventWhenTriggers:whenTriggers eventName:@"event1" eventProperties:@{ @"prop1": @[@"test2", @"test3", @"test"] @@ -1042,7 +1051,7 @@ - (void)testMatchEqualsExtectedArrayWithActualArrayNumber { } ]; - CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] init]; + CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] initWithDataStore:self.dataStore]; BOOL match = [triggerMatcher matchEventWhenTriggers:whenTriggers eventName:@"event1" eventProperties:@{ @"prop1": @[@3, @1, @2] @@ -1065,7 +1074,7 @@ - (void)testMatchSet { } ]; - CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] init]; + CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] initWithDataStore:self.dataStore]; BOOL match = [triggerMatcher matchEventWhenTriggers:whenTriggers eventName:@"event1" eventProperties:@{ @"prop1": @150 @@ -1086,7 +1095,7 @@ - (void)testMatchSetCharged { } ]; - CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] init]; + CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] initWithDataStore:self.dataStore]; BOOL match = [triggerMatcher matchChargedEventWhenTriggers:whenTriggers details:@{} items:@[ @{ @@ -1120,7 +1129,7 @@ - (void)testMatchNotSet { } ]; - CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] init]; + CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] initWithDataStore:self.dataStore]; BOOL match = [triggerMatcher matchEventWhenTriggers:whenTriggers eventName:@"event1" eventProperties:@{ @"prop2": @150 @@ -1142,7 +1151,7 @@ - (void)testMatchNotSetCharged { } ]; - CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] init]; + CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] initWithDataStore:self.dataStore]; BOOL match = [triggerMatcher matchChargedEventWhenTriggers:whenTriggers details:@{} items:@[ @{ @@ -1177,7 +1186,7 @@ - (void)testMatchNotEquals { } ]; - CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] init]; + CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] initWithDataStore:self.dataStore]; BOOL match = [triggerMatcher matchEventWhenTriggers:whenTriggers eventName:@"event1" eventProperties:@{ @"prop1": @240 @@ -1204,7 +1213,7 @@ - (void)testMatchNotEqualsArrays { } ]; - CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] init]; + CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] initWithDataStore:self.dataStore]; BOOL match = [triggerMatcher matchEventWhenTriggers:whenTriggers eventName:@"event1" eventProperties:@{ @"prop1": @[@241] @@ -1233,7 +1242,7 @@ - (void)testMatchLessThan { } ]; - CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] init]; + CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] initWithDataStore:self.dataStore]; BOOL match = [triggerMatcher matchEventWhenTriggers:whenTriggers eventName:@"event1" eventProperties:@{ @"prop1": @150 @@ -1261,7 +1270,7 @@ - (void)testMatchLessThanWithString { } ]; - CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] init]; + CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] initWithDataStore:self.dataStore]; BOOL match = [triggerMatcher matchEventWhenTriggers:whenTriggers eventName:@"event1" eventProperties:@{ @"prop1": @"-120" @@ -1290,7 +1299,7 @@ - (void)testMatchLessThanWithArrays { } ]; - CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] init]; + CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] initWithDataStore:self.dataStore]; BOOL match = [triggerMatcher matchEventWhenTriggers:whenTriggers eventName:@"event1" eventProperties:@{ @"prop1": @"-120" @@ -1336,7 +1345,7 @@ - (void)testMatchGreaterThan { } ]; - CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] init]; + CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] initWithDataStore:self.dataStore]; BOOL match = [triggerMatcher matchEventWhenTriggers:whenTriggers eventName:@"event1" eventProperties:@{ @"prop1": @240 @@ -1364,7 +1373,7 @@ - (void)testMatchGreaterThanWithString { } ]; - CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] init]; + CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] initWithDataStore:self.dataStore]; BOOL match = [triggerMatcher matchEventWhenTriggers:whenTriggers eventName:@"event1" eventProperties:@{ @"prop1": @"240" @@ -1393,7 +1402,7 @@ - (void)testMatchGreaterThanWithArrays { } ]; - CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] init]; + CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] initWithDataStore:self.dataStore]; BOOL match = [triggerMatcher matchEventWhenTriggers:whenTriggers eventName:@"event1" eventProperties:@{ @"prop1": @600 @@ -1439,7 +1448,7 @@ - (void)testMatchBetween { } ]; - CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] init]; + CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] initWithDataStore:self.dataStore]; BOOL match = [triggerMatcher matchEventWhenTriggers:whenTriggers eventName:@"event1" eventProperties:@{ @"prop1": @150 @@ -1492,7 +1501,7 @@ - (void)testMatchBetweenCharged { } ]; - CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] init]; + CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] initWithDataStore:self.dataStore]; BOOL match = [triggerMatcher matchChargedEventWhenTriggers:whenTriggers details:@{} items:@[ @{ @@ -1529,7 +1538,7 @@ - (void)testMatchBetweenArrayMoreThan2 { } ]; - CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] init]; + CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] initWithDataStore:self.dataStore]; BOOL match = [triggerMatcher matchEventWhenTriggers:whenTriggers eventName:@"event1" eventProperties:@{ @"prop1": @150 @@ -1552,7 +1561,7 @@ - (void)testMatchBetweenEmptyArray { } ]; - CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] init]; + CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] initWithDataStore:self.dataStore]; BOOL match = [triggerMatcher matchEventWhenTriggers:whenTriggers eventName:@"event1" eventProperties:@{ @"prop1": @150 @@ -1576,7 +1585,7 @@ - (void)testMatchContainsString { } ]; - CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] init]; + CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] initWithDataStore:self.dataStore]; BOOL match = [triggerMatcher matchEventWhenTriggers:whenTriggers eventName:@"event1" eventProperties:@{ @"prop1": @"clevertap" @@ -1603,7 +1612,7 @@ - (void)testMatchContainsStringBool { } ]; - CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] init]; + CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] initWithDataStore:self.dataStore]; BOOL match = [triggerMatcher matchEventWhenTriggers:whenTriggers eventName:@"event1" eventProperties:@{ @"prop1": @"this is true" @@ -1630,7 +1639,7 @@ - (void)testMatchContainsNumber { } ]; - CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] init]; + CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] initWithDataStore:self.dataStore]; BOOL match = [triggerMatcher matchEventWhenTriggers:whenTriggers eventName:@"event1" eventProperties:@{ @"prop1": @"1234567" @@ -1662,7 +1671,7 @@ - (void)testMatchContainsArray { } ]; - CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] init]; + CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] initWithDataStore:self.dataStore]; BOOL match = [triggerMatcher matchEventWhenTriggers:whenTriggers eventName:@"event1" eventProperties:@{ @"prop1": @"clevertap" @@ -1685,7 +1694,7 @@ - (void)testMatchContainsArrayNumber { } ]; - CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] init]; + CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] initWithDataStore:self.dataStore]; BOOL match = [triggerMatcher matchEventWhenTriggers:whenTriggers eventName:@"event1" eventProperties:@{ @"prop1": @"123456" @@ -1712,7 +1721,7 @@ - (void)testMatchContainsArrayEmpty { } ]; - CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] init]; + CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] initWithDataStore:self.dataStore]; BOOL match = [triggerMatcher matchEventWhenTriggers:whenTriggers eventName:@"event1" eventProperties:@{ @"prop1": @"clevertap" @@ -1735,7 +1744,7 @@ - (void)testMatchContainsStringWithPropertyArray { } ]; - CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] init]; + CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] initWithDataStore:self.dataStore]; BOOL match = [triggerMatcher matchEventWhenTriggers:whenTriggers eventName:@"event1" eventProperties:@{ @"prop1": @[@"clevertap",@"test"] @@ -1759,7 +1768,7 @@ - (void)testMatchNotContainsArray { } ]; - CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] init]; + CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] initWithDataStore:self.dataStore]; BOOL match = [triggerMatcher matchEventWhenTriggers:whenTriggers eventName:@"event1" eventProperties:@{ @"prop1": @"clevertap" @@ -1792,7 +1801,7 @@ - (void)testMatchNotContainsArrayFromTriggerArray { } ]; - CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] init]; + CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] initWithDataStore:self.dataStore]; BOOL match = [triggerMatcher matchEventWhenTriggers:whenTriggers eventName:@"event1" eventProperties:@{ @"prop1": @[@"clevertap", @"yes"] @@ -1815,7 +1824,7 @@ - (void)testMatchNotContainsString { } ]; - CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] init]; + CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] initWithDataStore:self.dataStore]; BOOL match = [triggerMatcher matchEventWhenTriggers:whenTriggers eventName:@"event1" eventProperties:@{ @"prop1": @"clevertap" @@ -1838,7 +1847,7 @@ - (void)testMatchSystemProperties { } ]; - CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] init]; + CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] initWithDataStore:self.dataStore]; BOOL match = [triggerMatcher matchEventWhenTriggers:whenTriggers eventName:@"event1" eventProperties:@{ CLTAP_SDK_VERSION: @60000 @@ -1889,7 +1898,7 @@ - (void)testMatchSystemPropertiesCurrentAndLegacy { } ]; - CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] init]; + CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] initWithDataStore:self.dataStore]; BOOL match = [triggerMatcher matchEventWhenTriggers:whenTriggers eventName:@"event1" eventProperties:@{ CLTAP_SDK_VERSION: @60000, CLTAP_APP_VERSION: @"6.0.0", @@ -1927,7 +1936,7 @@ - (void)testMatchNotificationProperties { } ]; - CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] init]; + CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] initWithDataStore:self.dataStore]; BOOL match = [triggerMatcher matchEventWhenTriggers:whenTriggers eventName:@"Notification Viewed" eventProperties:@{ CLTAP_PROP_WZRK_ID: @"1701172437_20231128", @@ -1959,7 +1968,7 @@ - (void)testMatchEventWithGeoRadius { CTEventAdapter *event = [[CTEventAdapter alloc] initWithEventName:@"event1" eventProperties:@{} andLocation:location1km]; - CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] init]; + CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] initWithDataStore:self.dataStore]; BOOL match = [triggerMatcher matchEventWhenTriggers:whenTriggers event:event]; XCTAssertTrue(match); @@ -1995,7 +2004,7 @@ - (void)testMatchEventWithGeoRadiusButNotParams { CTEventAdapter *event = [[CTEventAdapter alloc] initWithEventName:@"event1" eventProperties:@{@"prop1": @151} andLocation:location1km]; - CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] init]; + CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] initWithDataStore:self.dataStore]; BOOL match = [triggerMatcher matchEventWhenTriggers:whenTriggers event:event]; XCTAssertFalse(match); @@ -2012,10 +2021,14 @@ - (void)testMatchEventWithFirstTimeOnly { ]; // TODO: Update tests after db/localdatastore is updated with db methods - CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] init]; + CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] initWithDataStore:self.dataStore]; + CTLocalDataStore *dataStoreMock = OCMPartialMock(self.dataStore); + id mockIsEventLoggedFirstTime = OCMStub([dataStoreMock isEventLoggedFirstTime:@"event1"]).andReturn(YES); BOOL match = [triggerMatcher matchEventWhenTriggers:whenTriggers eventName:@"event1" eventProperties:@{}]; + XCTAssertTrue(match); + OCMVerify(mockIsEventLoggedFirstTime); } - (void)testMatchChargedEventWithFirstTimeOnly { @@ -2039,7 +2052,10 @@ - (void)testMatchChargedEventWithFirstTimeOnly { ]; // TODO: Update tests after db/localdatastore is updated with db methods - CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] init]; + CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] initWithDataStore:self.dataStore]; + + CTLocalDataStore *dataStoreMock = OCMPartialMock(self.dataStore); + id mockIsEventLoggedFirstTime = OCMStub([dataStoreMock isEventLoggedFirstTime:@"Charged"]).andReturn(YES); BOOL match = [triggerMatcher matchChargedEventWhenTriggers:whenTriggers details:@{ @"prop1": @150, } items:@[ @@ -2053,6 +2069,7 @@ - (void)testMatchChargedEventWithFirstTimeOnly { } ]]; XCTAssertTrue(match); + OCMVerify(mockIsEventLoggedFirstTime); } - (void)testMatchEventFirstTimeOnlyWithGeoRadius { @@ -2074,7 +2091,9 @@ - (void)testMatchEventFirstTimeOnlyWithGeoRadius { CTEventAdapter *event = [[CTEventAdapter alloc] initWithEventName:@"event1" eventProperties:@{} andLocation:location1km]; - CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] init]; + CTTriggersMatcher *triggerMatcher = [[CTTriggersMatcher alloc] initWithDataStore:self.dataStore]; + CTLocalDataStore *dataStoreMock = OCMPartialMock(self.dataStore); + id mockIsEventLoggedFirstTime = OCMStub([dataStoreMock isEventLoggedFirstTime:@"event1"]).andReturn(YES); BOOL match = [triggerMatcher matchEventWhenTriggers:whenTriggers event:event]; XCTAssertTrue(match); diff --git a/CleverTapSDKTests/InApps/InAppHelper.h b/CleverTapSDKTests/InApps/InAppHelper.h index 0a782204..a97d44c8 100644 --- a/CleverTapSDKTests/InApps/InAppHelper.h +++ b/CleverTapSDKTests/InApps/InAppHelper.h @@ -16,6 +16,7 @@ @class CTMultiDelegateManager; @class CTInAppTriggerManager; @class CTFileDownloader; +@class CTLocalDataStore; NS_ASSUME_NONNULL_BEGIN @@ -35,6 +36,7 @@ extern NSString *const CLTAP_TEST_CAMPAIGN_ID; @property (nonatomic, strong) CTInAppStore *inAppStore; @property (nonatomic, strong) CTInAppTriggerManager *inAppTriggerManager; @property (nonatomic, strong) CTFileDownloader *fileDownloader; +@property (nonatomic, strong) CTLocalDataStore *dataStore; - (NSString *)accountId; - (NSString *)accountToken; diff --git a/CleverTapSDKTests/InApps/InAppHelper.m b/CleverTapSDKTests/InApps/InAppHelper.m index c523ad61..6d105f66 100644 --- a/CleverTapSDKTests/InApps/InAppHelper.m +++ b/CleverTapSDKTests/InApps/InAppHelper.m @@ -67,6 +67,10 @@ - (instancetype)init { impressionManager:self.impressionManager inAppTriggerManager:self.inAppTriggerManager]; + CTDeviceInfo *deviceInfo = [[CTDeviceInfo alloc] initWithConfig:self.config andCleverTapID:CLTAP_TEST_DEVICE_ID]; + CTDispatchQueueManager *queueManager = [[CTDispatchQueueManager alloc] initWithConfig:self.config]; + self.dataStore = [[CTLocalDataStore alloc] initWithConfig:self.config profileValues:[NSMutableDictionary new] andDeviceInfo:deviceInfo dispatchQueueManager:queueManager]; + #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wnonnull" // Initialize when needed, requires CleverTap instance @@ -79,7 +83,7 @@ - (instancetype)init { impressionManager:self.impressionManager inAppDisplayManager:self.inAppDisplayManager inAppStore:self.inAppStore - inAppTriggerManager:self.inAppTriggerManager]; + inAppTriggerManager:self.inAppTriggerManager localDataStore:self.dataStore]; } return self; }