Skip to content

Commit a272fc3

Browse files
authored
Counting on Client (#211)
* refactor sendAllCounts for easy testing * test sendAndClearCounts and makeParams * fix nonnull int
1 parent 9e13e61 commit a272fc3

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

Example/Tests/Classes/LPCountAggregatorTest.m

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#import <XCTest/XCTest.h>
2727
#import "LPCountAggregator.h"
28+
#import "Constants.h"
2829

2930
/**
3031
* Expose private class methods
@@ -33,6 +34,9 @@ @interface LPCountAggregator(UnitTest)
3334

3435
@property (nonatomic, strong) NSMutableDictionary *counts;
3536

37+
- (NSDictionary *)getAndClearCounts;
38+
- (NSMutableDictionary *)makeParams:(nonnull NSString *)name withCount:(int) count;
39+
3640
@end
3741

3842
@interface LPCountAggregatorTest : XCTestCase
@@ -96,4 +100,32 @@ - (void)test_incrementCountMultiple {
96100
XCTAssert([countAggregator.counts[testString] intValue] == 17);
97101
}
98102

103+
- (void)test_getAndClearCounts {
104+
LPCountAggregator *countAggregator = [[LPCountAggregator alloc] init];
105+
NSString *testString = @"test";
106+
NSString *testString2 = @"test2";
107+
countAggregator.enabledCounters = [NSSet setWithObjects:testString, testString2, nil];
108+
109+
[countAggregator incrementCount:testString by:2];
110+
[countAggregator incrementCount:testString2 by:15];
111+
112+
NSDictionary *previousCounts = [countAggregator getAndClearCounts];
113+
114+
//test counts is empty after clearing
115+
XCTAssert([countAggregator.counts count] == 0);
116+
//test counts transferred to previousCounts
117+
XCTAssert([previousCounts[testString] intValue] == 2);
118+
XCTAssert([previousCounts[testString2] intValue] == 15);
119+
}
120+
121+
- (void)test_makeParams {
122+
LPCountAggregator *countAggregator = [[LPCountAggregator alloc] init];
123+
NSString *testString = @"test";
124+
NSMutableDictionary *params = [countAggregator makeParams:testString withCount:2];
125+
126+
XCTAssert([params[LP_PARAM_TYPE] isEqualToString:@"SDK_COUNT"]);
127+
XCTAssert([params[LP_PARAM_MESSAGE] isEqualToString:testString]);
128+
XCTAssert([params[LP_PARAM_COUNT] intValue] == 2);
129+
}
130+
99131
@end

Leanplum-SDK/Classes/Managers/LPCountAggregator.m

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,19 @@ - (NSDictionary *)getAndClearCounts {
7575
return previousCounts;
7676
}
7777

78+
- (NSMutableDictionary *)makeParams:(nonnull NSString *)name withCount:(int) count {
79+
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
80+
params[LP_PARAM_TYPE] = @"SDK_COUNT";
81+
params[LP_PARAM_MESSAGE] = name;
82+
params[LP_PARAM_COUNT] = [NSNumber numberWithInt:count];
83+
return params;
84+
}
85+
7886
- (void)sendAllCounts {
7987
NSDictionary *counts = [self getAndClearCounts];
8088
for (NSString *name in counts) { // iterate over counts, creating one request per counter
81-
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
82-
params[LP_PARAM_TYPE] = @"SDK_COUNT";
83-
params[LP_PARAM_MESSAGE] = name;
84-
params[LP_PARAM_COUNT] = counts[name];
89+
int count = [counts[name] intValue];
90+
NSMutableDictionary *params = [self makeParams:name withCount:count];
8591
[[LeanplumRequest post:LP_METHOD_LOG params:params] sendEventually];
8692
}
8793
}

0 commit comments

Comments
 (0)