forked from parse-community/Parse-SDK-iOS-OSX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CurrentConfigControllerTests.m
189 lines (144 loc) · 7.29 KB
/
CurrentConfigControllerTests.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/**
* Copyright (c) 2015-present, Parse, LLC.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#import <OCMock/OCMock.h>
@import Bolts.BFTask;
#import "BFTask+Private.h"
#import "PFCommandResult.h"
#import "PFConfig.h"
#import "PFConfig_Private.h"
#import "PFCurrentConfigController.h"
#import "PFPersistenceController.h"
#import "PFTestCase.h"
#import "PFJSONSerialization.h"
@interface CurrentConfigControllerTests : PFTestCase
@end
@implementation CurrentConfigControllerTests
///--------------------------------------
#pragma mark - Helpers
///--------------------------------------
- (NSDictionary *)testConfigDictionary {
return @{ @"params" : @{@"testKey" : @"testValue"} };
}
- (PFPersistenceController *)mockedPersistenceController {
id controller = PFStrictClassMock([PFPersistenceController class]);
id group = PFStrictProtocolMock(@protocol(PFPersistenceGroup));
OCMStub([controller getPersistenceGroupAsync]).andReturn([BFTask taskWithResult:group]);
return controller;
}
- (PFPersistenceController *)mockedPersistenceControllerWithConfigDictionary:(NSDictionary *)dictionary {
id controller = [self mockedPersistenceController];
id group = [[controller getPersistenceGroupAsync] waitForResult:nil];
NSData *jsonData = [PFJSONSerialization dataFromJSONObject:dictionary];
BFTask *task = [BFTask taskWithResult:jsonData];
OCMStub([group getDataAsyncForKey:@"config"]).andReturn(task);
return controller;
}
///--------------------------------------
#pragma mark - Tests
///--------------------------------------
- (void)testConstructor {
id dataSource = PFStrictProtocolMock(@protocol(PFPersistenceControllerProvider));
PFCurrentConfigController *controller = [[PFCurrentConfigController alloc] initWithDataSource:dataSource];
XCTAssertNotNil(controller);
XCTAssertEqual((id)controller.dataSource, dataSource);
controller = [PFCurrentConfigController controllerWithDataSource:dataSource];
XCTAssertNotNil(controller);
XCTAssertEqual((id)controller.dataSource, dataSource);
}
- (void)testGetCurrentConfig {
id dataSource = PFStrictProtocolMock(@protocol(PFPersistenceControllerProvider));
PFPersistenceController *controller = [self mockedPersistenceControllerWithConfigDictionary:[self testConfigDictionary]];
OCMStub([dataSource persistenceController]).andReturn(controller);
PFCurrentConfigController *currentController = [PFCurrentConfigController controllerWithDataSource:dataSource];
XCTestExpectation *expectation = [self currentSelectorTestExpectation];
[[currentController getCurrentConfigAsync] continueWithBlock:^id(BFTask *task) {
XCTAssertNotNil(task.result);
XCTAssertTrue([task.result isKindOfClass:[PFConfig class]]);
XCTAssertEqualObjects(task.result[@"testKey"], @"testValue");
[expectation fulfill];
return nil;
}];
[self waitForTestExpectations];
}
- (void)testSetCurrentConfig {
id dataSource = PFStrictProtocolMock(@protocol(PFPersistenceControllerProvider));
PFPersistenceController *controller = [self mockedPersistenceController];
OCMStub([dataSource persistenceController]).andReturn(controller);
PFConfig *testConfig = [[PFConfig alloc] initWithFetchedConfig:[self testConfigDictionary]];
PFCurrentConfigController *currentController = [PFCurrentConfigController controllerWithDataSource:dataSource];
id group = [[controller getPersistenceGroupAsync] waitForResult:nil];
OCMExpect([group setDataAsync:[OCMArg checkWithBlock:^BOOL(id obj) {
NSDictionary *dictionary = [PFJSONSerialization JSONObjectFromData:obj];
return [dictionary isEqual:[self testConfigDictionary]];
}] forKey:@"config"]).andReturn([BFTask taskWithResult:nil]);
XCTestExpectation *expectation = [self currentSelectorTestExpectation];
[[currentController setCurrentConfigAsync:testConfig] continueWithBlock:^id(BFTask *task) {
XCTAssertFalse(task.faulted);
[expectation fulfill];
return nil;
}];
[self waitForTestExpectations];
OCMVerifyAll(group);
}
- (void)testClearCurrentConfig {
id dataSource = PFStrictProtocolMock(@protocol(PFPersistenceControllerProvider));
PFPersistenceController *controller = [self mockedPersistenceController];
OCMStub([dataSource persistenceController]).andReturn(controller);
PFConfig *testConfig = [[PFConfig alloc] initWithFetchedConfig:[self testConfigDictionary]];
PFCurrentConfigController *currentController = [PFCurrentConfigController controllerWithDataSource:dataSource];
id group = [[controller getPersistenceGroupAsync] waitForResult:nil];
OCMExpect([group setDataAsync:[OCMArg checkWithBlock:^BOOL(id obj) {
NSDictionary *dictionary = [PFJSONSerialization JSONObjectFromData:obj];
return [dictionary isEqual:[self testConfigDictionary]];
}] forKey:@"config"]).andReturn([BFTask taskWithResult:nil]);
XCTestExpectation *saveExpectation = [self expectationWithDescription:@"Save"];
[[currentController setCurrentConfigAsync:testConfig] continueWithBlock:^id(BFTask *task) {
XCTAssertFalse(task.faulted);
[saveExpectation fulfill];
return nil;
}];
[self waitForTestExpectations];
OCMExpect([group removeDataAsyncForKey:@"config"]).andReturn([BFTask taskWithResult:nil]);
XCTestExpectation *clearExpectation = [self expectationWithDescription:@"Clear"];
[[currentController clearCurrentConfigAsync] continueWithBlock:^id(BFTask *task) {
XCTAssertFalse(task.faulted);
[clearExpectation fulfill];
return nil;
}];
[self waitForTestExpectations];
OCMVerifyAll(group);
}
- (void)testClearMemoryCachedCurrentConfig {
id dataSource = PFStrictProtocolMock(@protocol(PFPersistenceControllerProvider));
PFPersistenceController *controller = [self mockedPersistenceController];
OCMStub([dataSource persistenceController]).andReturn(controller);
PFConfig *testConfig = [[PFConfig alloc] initWithFetchedConfig:[self testConfigDictionary]];
PFCurrentConfigController *currentController = [PFCurrentConfigController controllerWithDataSource:dataSource];
id group = [[controller getPersistenceGroupAsync] waitForResult:nil];
OCMExpect([group setDataAsync:[OCMArg checkWithBlock:^BOOL(id obj) {
NSDictionary *dictionary = [PFJSONSerialization JSONObjectFromData:obj];
return [dictionary isEqual:[self testConfigDictionary]];
}] forKey:@"config"]).andReturn([BFTask taskWithResult:nil]);
XCTestExpectation *saveExpectation = [self expectationWithDescription:@"Save"];
[[currentController setCurrentConfigAsync:testConfig] continueWithBlock:^id(BFTask *task) {
XCTAssertFalse(task.faulted);
[saveExpectation fulfill];
return nil;
}];
[self waitForTestExpectations];
XCTestExpectation *clearExpectation = [self expectationWithDescription:@"Clear"];
[[currentController clearMemoryCachedCurrentConfigAsync] continueWithBlock:^id(BFTask *task) {
XCTAssertFalse(task.faulted);
[clearExpectation fulfill];
return nil;
}];
[self waitForTestExpectations];
OCMVerifyAll(group);
}
@end