Skip to content

Commit 6980be9

Browse files
committed
test download and save datafile when we initialize optimizely datafile manager
1 parent 4e63827 commit 6980be9

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

OptimizelySDKDatafileManager/OptimizelySDKDatafileManagerTests/OPTLYDatafileManagerTest.m

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,11 @@ - (void)testRequestDatafileHandlesCompletionEvenWithBadRequest {
116116

117117
- (void)testSaveDatafileMethod {
118118
// setup datafile manager and datastore
119-
OPTLYDataStore *dataStore = [OPTLYDataStore new];
120119
OPTLYDatafileManager *datafileManager = [OPTLYDatafileManager initWithBuilderBlock:^(OPTLYDatafileManagerBuilder * _Nullable builder) {
121120
builder.projectId = kProjectId;
122121
}];
123122
XCTAssertNotNil(datafileManager);
124-
XCTAssertFalse([dataStore fileExists:kProjectId type:OPTLYDataStoreDataTypeDatafile]);
123+
XCTAssertFalse([self.dataStore fileExists:kProjectId type:OPTLYDataStoreDataTypeDatafile]);
125124

126125
// get the datafile
127126
NSData *datafile = [OPTLYTestHelper loadJSONDatafileIntoDataObject:kDatamodelDatafileName];
@@ -130,10 +129,10 @@ - (void)testSaveDatafileMethod {
130129
[datafileManager saveDatafile:datafile];
131130

132131
// test the datafile was saved correctly
133-
bool fileExists = [dataStore fileExists:kProjectId type:OPTLYDataStoreDataTypeDatafile];
132+
bool fileExists = [self.dataStore fileExists:kProjectId type:OPTLYDataStoreDataTypeDatafile];
134133
XCTAssertTrue(fileExists, @"save Datafile did not save the datafile to disk");
135134
NSError *error;
136-
NSData *savedData = [dataStore getFile:kProjectId
135+
NSData *savedData = [self.dataStore getFile:kProjectId
137136
type:OPTLYDataStoreDataTypeDatafile
138137
error:&error];
139138
XCTAssertNil(error);
@@ -143,11 +142,32 @@ - (void)testSaveDatafileMethod {
143142
}
144143

145144
- (void)testDatafileManagerPullsDatafileOnInitialization {
146-
// setup stubbing
145+
// setup stubbing and listener expectation
146+
__weak XCTestExpectation *expectation = [self expectationWithDescription:@"testInitializeClientAsync"];
147+
id<OHHTTPStubsDescriptor> stub = [OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
148+
return [request.URL.host isEqualToString:@"cdn.optimizely.com"];
149+
} withStubResponse:^OHHTTPStubsResponse*(NSURLRequest *request) {
150+
// Stub it with our "wsresponse.json" stub file (which is in same bundle as self)
151+
[expectation fulfill];
152+
return [OHHTTPStubsResponse responseWithData:[OPTLYTestHelper loadJSONDatafileIntoDataObject:kDatamodelDatafileName]
153+
statusCode:200
154+
headers:@{@"Content-Type":@"application/json"}];
155+
}];
147156

148157
// instantiate datafile manager (it should fire off a request)
158+
XCTAssertFalse([self.dataStore fileExists:kProjectId type:OPTLYDataStoreDataTypeDatafile], @"no datafile sould exist yet.");
159+
OPTLYDatafileManager *datafileManager = [OPTLYDatafileManager initWithBuilderBlock:^(OPTLYDatafileManagerBuilder * _Nullable builder) {
160+
builder.projectId = kProjectId;
161+
}];
162+
XCTAssertNotNil(datafileManager);
149163

164+
// make sure we were able to save the datafile
165+
[self waitForExpectationsWithTimeout:2 handler:nil];
166+
sleep(2); // not sure if there is a better way for to wait for disk write other than to sleep this thread
167+
XCTAssertTrue([self.dataStore fileExists:kProjectId type:OPTLYDataStoreDataTypeDatafile], @"we should have stored the datafile");
150168

169+
// cleanup stubs
170+
[OHHTTPStubs removeStub:stub];
151171
}
152172

153173
@end

0 commit comments

Comments
 (0)