25
25
static NSString *const kDatamodelDatafileName = @" datafile_6372300739" ;
26
26
static NSTimeInterval kDatafileDownloadInteval = 5 ; // in seconds
27
27
static NSString *const kLastModifiedDate = @" Mon, 28 Nov 2016 06:10:59 GMT" ;
28
+ static NSData *kDatafileData ;
28
29
29
30
@interface OPTLYDatafileManager (test)
30
31
@property (nonatomic , strong ) NSTimer *datafileDownloadTimer;
@@ -43,6 +44,8 @@ @implementation OPTLYDatafileManagerTest
43
44
+ (void )setUp {
44
45
[super setUp ];
45
46
47
+ kDatafileData = [OPTLYTestHelper loadJSONDatafileIntoDataObject: kDatamodelDatafileName ];
48
+
46
49
// stub all requests
47
50
[OHHTTPStubs stubRequestsPassingTest: ^BOOL (NSURLRequest * _Nonnull request) {
48
51
// every requests passes this test
@@ -108,11 +111,8 @@ - (void)testSaveDatafileMethod {
108
111
XCTAssertNotNil (self.datafileManager );
109
112
XCTAssertFalse ([self .dataStore fileExists: kProjectId type: OPTLYDataStoreDataTypeDatafile]);
110
113
111
- // get the datafile
112
- NSData *datafile = [OPTLYTestHelper loadJSONDatafileIntoDataObject: kDatamodelDatafileName ];
113
-
114
114
// save the datafile
115
- [self .datafileManager saveDatafile: datafile ];
115
+ [self .datafileManager saveDatafile: kDatafileData ];
116
116
117
117
// test the datafile was saved correctly
118
118
bool fileExists = [self .dataStore fileExists: kProjectId type: OPTLYDataStoreDataTypeDatafile];
@@ -123,8 +123,8 @@ - (void)testSaveDatafileMethod {
123
123
error: &error];
124
124
XCTAssertNil (error);
125
125
XCTAssertNotNil (savedData);
126
- XCTAssertNotEqual (datafile , savedData, @" we should not be referencing the same object. Saved data should be a new NSData object created from disk." );
127
- XCTAssertEqualObjects (datafile , savedData, @" retrieved saved data from disk should be equivalent to the datafile we wanted to save to disk" );
126
+ XCTAssertNotEqual (kDatafileData , savedData, @" we should not be referencing the same object. Saved data should be a new NSData object created from disk." );
127
+ XCTAssertEqualObjects (kDatafileData , savedData, @" retrieved saved data from disk should be equivalent to the datafile we wanted to save to disk" );
128
128
}
129
129
130
130
// if 200 response, save the {projectID : lastModifiedDate} and datafile
@@ -191,11 +191,8 @@ - (void)testIsDatafileCachedFlag
191
191
{
192
192
XCTAssertFalse (self.datafileManager .isDatafileCached , @" Datafile cached flag should be false." );
193
193
194
- // get the datafile
195
- NSData *datafile = [OPTLYTestHelper loadJSONDatafileIntoDataObject: kDatamodelDatafileName ];
196
-
197
194
// save the datafile
198
- [self .datafileManager saveDatafile: datafile ];
195
+ [self .datafileManager saveDatafile: kDatafileData ];
199
196
200
197
XCTAssertTrue (self.datafileManager .isDatafileCached , @" Datafile cached flag should be true." );
201
198
}
@@ -225,7 +222,7 @@ - (void)test304Response
225
222
[self .datafileManager downloadDatafile: kProjectId completionHandler: ^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
226
223
XCTAssertEqual (((NSHTTPURLResponse *)response).statusCode , 304 );
227
224
XCTAssertNotNil (data);
228
- XCTAssertEqualObjects (data, [OPTLYTestHelper loadJSONDatafileIntoDataObject: kDatamodelDatafileName ] );
225
+ XCTAssertEqualObjects (data, kDatafileData );
229
226
[expect304 fulfill ];
230
227
}];
231
228
@@ -243,7 +240,7 @@ - (void)test304Response
243
240
return [OHHTTPStubs stubRequestsPassingTest: ^BOOL (NSURLRequest *request) {
244
241
return [request.URL.host isEqualToString: hostName];
245
242
} withStubResponse: ^OHHTTPStubsResponse *(NSURLRequest *request) {
246
- return [OHHTTPStubsResponse responseWithData: [OPTLYTestHelper loadJSONDatafileIntoDataObject: kDatamodelDatafileName ]
243
+ return [OHHTTPStubsResponse responseWithData: kDatafileData
247
244
statusCode: 200
248
245
headers: @{@" Content-Type" :@" application/json" ,
249
246
@" Last-Modified" :kLastModifiedDate }];
@@ -265,7 +262,7 @@ - (void)test304Response
265
262
@" Last-Modified" :kLastModifiedDate }];
266
263
}
267
264
else {
268
- return [OHHTTPStubsResponse responseWithData: [OPTLYTestHelper loadJSONDatafileIntoDataObject: kDatamodelDatafileName ]
265
+ return [OHHTTPStubsResponse responseWithData: kDatafileData
269
266
statusCode: 200
270
267
headers: @{@" Content-Type" :@" application/json" ,
271
268
@" Last-Modified" :kLastModifiedDate }];
0 commit comments