Skip to content

Commit 961769b

Browse files
authored
E2-1629 Tests for Request Classes (#221)
* sendEventually test * sendEventually test mock with args * add logic instead of exception catching * Revert "add logic instead of exception catching" This reverts commit 69bb12c. * tests for requestSender * revert to old xcscheme * revert to old xcscheme * revert to old plist * revert to old plist * revert to old plist * comment entire new file * investigate build fail * investigate build fail * add stopMocking
1 parent 951905e commit 961769b

File tree

4 files changed

+193
-2
lines changed

4 files changed

+193
-2
lines changed

Example/Leanplum-SDK.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
6003F5B2195388D20070C39A /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6003F591195388D20070C39A /* UIKit.framework */; };
8989
6003F5BA195388D20070C39A /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 6003F5B8195388D20070C39A /* InfoPlist.strings */; };
9090
653192A121517D4F00D7DDDC /* LPFeatureFlagManagerTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 653192A021517D4F00D7DDDC /* LPFeatureFlagManagerTest.m */; };
91+
65E189C421643631005E6B93 /* LPRequestSenderTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 65E189C321643631005E6B93 /* LPRequestSenderTest.m */; };
9192
65E53E982148786C00A05040 /* LPCountAggregatorTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 65E53E962148786C00A05040 /* LPCountAggregatorTest.m */; };
9293
873B8AEB1B1F5CCA007FD442 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 873B8AEA1B1F5CCA007FD442 /* Main.storyboard */; };
9394
9CB691DE1F15FFF5002D83D8 /* batch_response.json in Resources */ = {isa = PBXBuildFile; fileRef = 9CB691DD1F15FFF5002D83D8 /* batch_response.json */; };
@@ -190,6 +191,7 @@
190191
6003F5B9195388D20070C39A /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = "<group>"; };
191192
606FC2411953D9B200FFA9A0 /* Tests-Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Tests-Prefix.pch"; sourceTree = "<group>"; };
192193
653192A021517D4F00D7DDDC /* LPFeatureFlagManagerTest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = LPFeatureFlagManagerTest.m; sourceTree = "<group>"; };
194+
65E189C321643631005E6B93 /* LPRequestSenderTest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = LPRequestSenderTest.m; sourceTree = "<group>"; };
193195
65E53E962148786C00A05040 /* LPCountAggregatorTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LPCountAggregatorTest.m; sourceTree = "<group>"; };
194196
73B565AA61D6B6A4E92607C4 /* Pods-Leanplum-SDK_Tests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Leanplum-SDK_Tests.release.xcconfig"; path = "Pods/Target Support Files/Pods-Leanplum-SDK_Tests/Pods-Leanplum-SDK_Tests.release.xcconfig"; sourceTree = "<group>"; };
195197
74C2EA3F2D53CAE77A03BFFA /* Pods-Leanplum-tvOS-SDK_Example.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Leanplum-tvOS-SDK_Example.release.xcconfig"; path = "Pods/Target Support Files/Pods-Leanplum-tvOS-SDK_Example/Pods-Leanplum-tvOS-SDK_Example.release.xcconfig"; sourceTree = "<group>"; };
@@ -256,6 +258,7 @@
256258
children = (
257259
07E5C86B1F052B7800A4B092 /* LPActionManagerTest.m */,
258260
A84527FC215C3035001FA20D /* LPRequestTest.m */,
261+
65E189C321643631005E6B93 /* LPRequestSenderTest.m */,
259262
07E5C86C1F052B7800A4B092 /* LPAppIconManagerTest.m */,
260263
07E5C86D1F052B7800A4B092 /* LPEventDataManagerTest.m */,
261264
07E5C86E1F052B7800A4B092 /* ExceptionsTest.m */,
@@ -828,6 +831,7 @@
828831
07E5C8B11F052B7800A4B092 /* LPNetworkEngine+Category.m in Sources */,
829832
07E5C8B61F052B7800A4B092 /* LeanplumTest.m in Sources */,
830833
07E5C8AD1F052B7800A4B092 /* LPEventDataManagerTest.m in Sources */,
834+
65E189C421643631005E6B93 /* LPRequestSenderTest.m in Sources */,
831835
07E5C8AE1F052B7800A4B092 /* ExceptionsTest.m in Sources */,
832836
A84527FD215C3035001FA20D /* LPRequestTest.m in Sources */,
833837
07E5C8AC1F052B7800A4B092 /* LPAppIconManagerTest.m in Sources */,

Example/Leanplum-SDK.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist

Whitespace-only changes.

Example/Leanplum-SDK.xcodeproj/xcshareddata/xcschemes/Leanplum-SDK-Example.xcscheme

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
buildConfiguration = "Debug"
2727
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
2828
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
29-
shouldUseLaunchSchemeArgsEnv = "YES"
30-
codeCoverageEnabled = "YES">
29+
codeCoverageEnabled = "YES"
30+
shouldUseLaunchSchemeArgsEnv = "YES">
3131
<Testables>
3232
<TestableReference
3333
skipped = "NO">
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
//
2+
// LPRequestSenderTest.m
3+
// Leanplum-SDK_Tests
4+
//
5+
// Created by Grace Gu on 10/01/18.
6+
// Copyright © 2018 Leanplum. All rights reserved.
7+
//
8+
9+
#import <XCTest/XCTest.h>
10+
#import <OCMock/OCMock.h>
11+
#import <Leanplum/LPAPIConfig.h>
12+
#import <Leanplum/LPEventDataManager.h>
13+
#import <Leanplum/LPNetworkProtocol.h>
14+
#import <Leanplum/LPRequestSender.h>
15+
#import <Leanplum/LPRequestFactory.h>
16+
#import <Leanplum/LPRequest.h>
17+
#import <Leanplum/LPCountAggregator.h>
18+
19+
@interface LPRequestSender(UnitTest)
20+
21+
@property (nonatomic, strong) id<LPNetworkEngineProtocol> engine;
22+
23+
- (void)sendNow:(id<LPRequesting>)request sync:(BOOL)sync;
24+
- (void)sendRequests:(BOOL)sync;
25+
- (NSOperationQueue *)sendNowQueue;
26+
27+
@end
28+
29+
@interface LPRequestSenderTest : XCTestCase
30+
31+
@end
32+
33+
@implementation LPRequestSenderTest
34+
35+
- (void)setUp {
36+
// Put setup code here. This method is called before the invocation of each test method in the class.
37+
}
38+
39+
- (void)tearDown {
40+
// Put teardown code here. This method is called after the invocation of each test method in the class.
41+
}
42+
43+
- (void)testCreateArgsDictionaryForRequest {
44+
LPRequest *request = [LPRequest post:@"test" params:@{}];
45+
LPRequestSender *requestSender = [[LPRequestSender alloc] init];
46+
LPConstantsState *constants = [LPConstantsState sharedState];
47+
NSString *timestamp = [NSString stringWithFormat:@"%f", [[NSDate date] timeIntervalSince1970]];
48+
NSMutableDictionary *testArgs = [@{
49+
LP_PARAM_ACTION: @"test",
50+
LP_PARAM_DEVICE_ID: @"",
51+
LP_PARAM_USER_ID: @"",
52+
LP_PARAM_SDK_VERSION: constants.sdkVersion,
53+
LP_PARAM_CLIENT: constants.client,
54+
LP_PARAM_DEV_MODE: @(constants.isDevelopmentModeEnabled),
55+
LP_PARAM_TIME: timestamp,
56+
LP_PARAM_UUID: @"uuid",
57+
} mutableCopy];
58+
NSMutableDictionary *args = [requestSender createArgsDictionaryForRequest:request];
59+
args[LP_PARAM_UUID] = @"uuid";
60+
args[LP_PARAM_TIME] = timestamp;
61+
62+
XCTAssertEqualObjects(testArgs, args);
63+
}
64+
65+
- (void)testSendNow {
66+
LPRequest *request = [LPRequest post:@"test" params:@{}];
67+
LPRequestSender *requestSender = [[LPRequestSender alloc] init];
68+
id requestSenderMock = OCMPartialMock(requestSender);
69+
id configMock = OCMClassMock([LPAPIConfig class]);
70+
OCMStub([configMock sharedConfig]).andReturn(configMock);
71+
OCMStub([configMock appId]).andReturn(@"appID");
72+
OCMStub([configMock accessKey]).andReturn(@"accessKey");
73+
[requestSender sendNow:request];
74+
75+
OCMVerify([requestSenderMock sendNow:request sync:false]);
76+
OCMVerify([requestSenderMock sendEventually:request]);
77+
OCMVerify([requestSenderMock sendRequests:false]);
78+
79+
[requestSenderMock stopMocking];
80+
[configMock stopMocking];
81+
}
82+
83+
- (void)testSendEventually {
84+
LPRequest *request = [LPRequest post:@"test" params:@{}];
85+
LPRequestSender *requestSender = [[LPRequestSender alloc] init];
86+
id eventDataManagerMock = OCMClassMock([LPEventDataManager class]);
87+
[requestSender sendEventually:request];
88+
89+
OCMVerify([eventDataManagerMock addEvent:[OCMArg isNotNil]]);
90+
}
91+
92+
- (void)testSendIfConnected {
93+
LPRequest *request = [LPRequest post:@"test" params:@{}];
94+
LPRequestSender *requestSender = [[LPRequestSender alloc] init];
95+
id requestSenderMock = OCMPartialMock(requestSender);
96+
id reachabilityMock = OCMClassMock([Leanplum_Reachability class]);
97+
OCMStub([reachabilityMock reachabilityForInternetConnection]).andReturn(reachabilityMock);
98+
OCMStub([reachabilityMock isReachable]).andReturn(true);
99+
[requestSender sendIfConnected:request];
100+
101+
OCMVerify([requestSenderMock sendNow:request]);
102+
}
103+
104+
- (void)testSendIfNotConnected {
105+
LPRequest *request = [LPRequest post:@"test" params:@{}];
106+
LPRequestSender *requestSender = [[LPRequestSender alloc] init];
107+
id requestSenderMock = OCMPartialMock(requestSender);
108+
id reachabilityMock = OCMClassMock([Leanplum_Reachability class]);
109+
OCMStub([reachabilityMock reachabilityForInternetConnection]).andReturn(reachabilityMock);
110+
OCMStub([reachabilityMock isReachable]).andReturn(false);
111+
[requestSender sendIfConnected:request];
112+
113+
OCMVerify([requestSenderMock sendEventually:request]);
114+
115+
[requestSenderMock stopMocking];
116+
[reachabilityMock stopMocking];
117+
}
118+
119+
- (void)testSendNowWithData {
120+
LPRequest *request = [LPRequest post:@"test" params:@{}];
121+
LPRequestSender *requestSender = [[LPRequestSender alloc] init];
122+
id requestSenderMock = OCMPartialMock(requestSender);
123+
124+
NSMutableDictionary *data = [[NSMutableDictionary alloc] init];
125+
data [@"key"] = [@"value" dataUsingEncoding:NSUTF8StringEncoding];
126+
[requestSender sendNow:request withData:data[@"key"] forKey:@"key"];
127+
128+
OCMVerify([requestSenderMock sendNow:request withDatas:data]);
129+
}
130+
131+
- (void)testSendNowWithDatas {
132+
LPRequest *request = [LPRequest post:@"test" params:@{}];
133+
LPRequestSender *requestSender = [[LPRequestSender alloc] init];
134+
requestSender.engine = OCMProtocolMock(@protocol(LPNetworkEngineProtocol));
135+
id opMock = OCMProtocolMock(@protocol(LPNetworkOperationProtocol));
136+
OCMStub([requestSender.engine operationWithPath:[OCMArg any] params:[OCMArg any] httpMethod:[OCMArg any] ssl:[OCMArg any] timeoutSeconds:60]).andReturn(opMock);
137+
138+
NSMutableDictionary *datas = [[NSMutableDictionary alloc] init];
139+
datas[@"key"] = [@"value" dataUsingEncoding:NSUTF8StringEncoding];
140+
[requestSender sendNow:request withDatas:datas];
141+
142+
OCMVerify([requestSender.engine enqueueOperation:opMock]);
143+
OCMVerify([opMock addData:datas[@"key"] forKey:@"key"]);
144+
OCMVerify([opMock addCompletionHandler:[OCMArg any] errorHandler:[OCMArg any]]);
145+
}
146+
147+
- (void) testSendRequestsSync {
148+
LPRequestSender *requestSender = [[LPRequestSender alloc] init];
149+
id requestOperationMock = OCMClassMock([NSBlockOperation class]);
150+
OCMStub([NSBlockOperation new]).andReturn(requestOperationMock);
151+
id countAggregatorMock = OCMClassMock([LPCountAggregator class]);
152+
OCMStub([countAggregatorMock sharedAggregator]).andReturn(countAggregatorMock);
153+
id eventDataManagerMock = OCMClassMock([LPEventDataManager class]);
154+
155+
NSMutableArray *requestsToSend = [[NSMutableArray alloc] init];
156+
[requestsToSend addObject:[[NSMutableDictionary alloc] init]];
157+
158+
OCMStub([eventDataManagerMock eventsWithLimit:MAX_EVENTS_PER_API_CALL]).andReturn(requestsToSend);
159+
requestSender.engine = OCMProtocolMock(@protocol(LPNetworkEngineProtocol));
160+
161+
LPConstantsState *constants = [LPConstantsState sharedState];
162+
int timeout = 5 * constants.syncNetworkTimeoutSeconds;
163+
164+
id opMock = OCMProtocolMock(@protocol(LPNetworkOperationProtocol));
165+
OCMStub([requestSender.engine operationWithPath:[OCMArg any] params:[OCMArg any] httpMethod:[OCMArg any] ssl:[OCMArg any] timeoutSeconds:timeout]).andReturn(opMock);
166+
[requestSender sendRequests:true];
167+
168+
OCMVerify([countAggregatorMock sendAllCounts]);
169+
OCMVerify([eventDataManagerMock eventsWithLimit:MAX_EVENTS_PER_API_CALL]);
170+
OCMVerify([requestSender.engine operationWithPath:[OCMArg any] params:[OCMArg any] httpMethod:[OCMArg any] ssl:[OCMArg any] timeoutSeconds:timeout]);
171+
OCMVerify([opMock addCompletionHandler:[OCMArg any] errorHandler:[OCMArg any]]);
172+
OCMVerify([requestSender.engine enqueueOperation:opMock]);
173+
[countAggregatorMock stopMocking];
174+
}
175+
176+
- (void) testSendRequestsAsync {
177+
LPRequestSender *requestSender = OCMPartialMock([[LPRequestSender alloc] init]);
178+
id requestOperationMock = OCMClassMock([NSBlockOperation class]);
179+
OCMStub([requestOperationMock new]).andReturn(requestOperationMock);
180+
id mockQueue = OCMClassMock([NSOperationQueue class]);
181+
OCMStub([requestSender sendNowQueue]).andReturn(mockQueue);
182+
[requestSender sendRequests:false];
183+
184+
OCMVerify([requestOperationMock addExecutionBlock:[OCMArg any]]);
185+
OCMVerify([mockQueue addOperation:requestOperationMock]);
186+
}
187+
@end

0 commit comments

Comments
 (0)