forked from parse-community/Parse-SDK-iOS-OSX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObjectFilePersistenceControllerTests.m
143 lines (112 loc) · 5.79 KB
/
ObjectFilePersistenceControllerTests.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
/**
* 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 Bolts.BFTask;
#import "BFTask+Private.h"
#import "PFObject.h"
#import "PFObjectFilePersistenceController.h"
#import "PFTestCase.h"
#import "PFPersistenceController.h"
@interface ObjectFilePersistenceControllerTests : PFTestCase
@end
@implementation ObjectFilePersistenceControllerTests
///--------------------------------------
#pragma mark - Helpers
///--------------------------------------
- (id)mockedDataSource {
id dataSource = PFStrictProtocolMock(@protocol(PFPersistenceControllerProvider));
OCMStub([dataSource persistenceController]).andReturn([self mockedPersistenceController]);
return dataSource;
}
- (PFPersistenceController *)mockedPersistenceController {
id controller = PFStrictClassMock([PFPersistenceController class]);
id group = PFStrictProtocolMock(@protocol(PFPersistenceGroup));
OCMStub([controller getPersistenceGroupAsync]).andReturn([BFTask taskWithResult:group]);
return controller;
}
- (NSString *)testFilePathForSelector:(SEL)cmd {
NSString *configPath = [NSTemporaryDirectory() stringByAppendingPathComponent:NSStringFromSelector(cmd)];
[[NSFileManager defaultManager] removeItemAtPath:configPath error:NULL];
return configPath;
}
///--------------------------------------
#pragma mark - Tests
///--------------------------------------
- (void)testConstructors {
id dataSource = [self mockedDataSource];
PFObjectFilePersistenceController *controller = [[PFObjectFilePersistenceController alloc] initWithDataSource:dataSource];
XCTAssertNotNil(controller);
XCTAssertEqual((id)controller.dataSource, dataSource);
controller = [PFObjectFilePersistenceController controllerWithDataSource:dataSource];
XCTAssertNotNil(controller);
XCTAssertEqual((id)controller.dataSource, dataSource);
}
- (void)testLoadPersistentObject {
id dataSource = [self mockedDataSource];
id group = [[[dataSource persistenceController] getPersistenceGroupAsync] waitForResult:nil];
PFObjectFilePersistenceController *controller = [PFObjectFilePersistenceController controllerWithDataSource:dataSource];
NSDictionary *dictionary = @{ @"classname" : @"Yolo",
@"data" : @{@"objectId" : @"100500", @"yarr" : @"pff"} };
NSData *data = [NSJSONSerialization dataWithJSONObject:dictionary options:0 error:nil];
OCMExpect([group beginLockedContentAccessAsyncToDataForKey:@"object"]).andReturn([BFTask taskWithResult:nil]);
OCMExpect([group getDataAsyncForKey:@"object"]).andReturn([BFTask taskWithResult:data]);
OCMExpect([group endLockedContentAccessAsyncToDataForKey:@"object"]).andReturn([BFTask taskWithResult:nil]);
XCTestExpectation *expectation = [self currentSelectorTestExpectation];
[[controller loadPersistentObjectAsyncForKey:@"object"] continueWithSuccessBlock:^id(BFTask *task) {
PFObject *object = task.result;
XCTAssertNotNil(object);
XCTAssertEqualObjects(object.parseClassName, @"Yolo");
XCTAssertEqualObjects(object.objectId, @"100500");
XCTAssertEqualObjects(object[@"yarr"], @"pff");
[expectation fulfill];
return nil;
}];
[self waitForTestExpectations];
OCMVerifyAll(group);
}
- (void)testPersistObjectForKey {
id dataSource = [self mockedDataSource];
id group = [[[dataSource persistenceController] getPersistenceGroupAsync] waitForResult:nil];
OCMExpect([group beginLockedContentAccessAsyncToDataForKey:@"object"]).andReturn([BFTask taskWithResult:nil]);
OCMExpect([group setDataAsync:[OCMArg checkWithBlock:^BOOL(id obj) {
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:obj options:0 error:nil];
XCTAssertNotNil(dictionary);
XCTAssertEqualObjects(dictionary[@"classname"], @"Yolo");
XCTAssertEqualObjects(dictionary[@"data"][@"objectId"], @"100500");
return YES;
}] forKey:@"object"]).andReturn([BFTask taskWithResult:nil]);
OCMExpect([group endLockedContentAccessAsyncToDataForKey:@"object"]).andReturn([BFTask taskWithResult:nil]);
PFObjectFilePersistenceController *controller = [PFObjectFilePersistenceController controllerWithDataSource:dataSource];
PFObject *object = [PFObject objectWithClassName:@"Yolo"];
object.objectId = @"100500";
XCTestExpectation *expectation = [self currentSelectorTestExpectation];
[[controller persistObjectAsync:object forKey:@"object"] continueWithSuccessBlock:^id(BFTask *task) {
XCTAssertNil(task.result);
[expectation fulfill];
return nil;
}];
[self waitForTestExpectations];
OCMVerifyAll(group);
}
- (void)testRemovePersistenceObjectForKey {
id dataSource = [self mockedDataSource];
id group = [[[dataSource persistenceController] getPersistenceGroupAsync] waitForResult:nil];
OCMExpect([group beginLockedContentAccessAsyncToDataForKey:@"object"]).andReturn([BFTask taskWithResult:nil]);
OCMExpect([group removeDataAsyncForKey:@"object"]).andReturn([BFTask taskWithResult:nil]);
OCMExpect([group endLockedContentAccessAsyncToDataForKey:@"object"]).andReturn([BFTask taskWithResult:nil]);
PFObjectFilePersistenceController *controller = [PFObjectFilePersistenceController controllerWithDataSource:dataSource];
XCTestExpectation *expectation = [self currentSelectorTestExpectation];
[[controller removePersistentObjectAsyncForKey:@"object"] continueWithSuccessBlock:^id(BFTask *task) {
XCTAssertNil(task.result);
[expectation fulfill];
return nil;
}];
[self waitForTestExpectations];
OCMVerifyAll(group);
}
@end