/Tests/Unit/ObjectPinTests.m

https://gitlab.com/iranjith4/Parse-SDK-iOS-OSX · Objective C · 540 lines · 421 code · 107 blank · 12 comment · 0 complexity · d6769c002a5397638c9b52fa9f00a6ea MD5 · raw file

  1. /**
  2. * Copyright (c) 2015-present, Parse, LLC.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. */
  9. #import <OCMock/OCMock.h>
  10. #import "PFCoreManager.h"
  11. #import "PFObjectPrivate.h"
  12. #import "PFPinningObjectStore.h"
  13. #import "PFUnitTestCase.h"
  14. #import "Parse_Private.h"
  15. @interface ObjectPinTests : PFUnitTestCase
  16. @end
  17. @implementation ObjectPinTests
  18. ///--------------------------------------
  19. #pragma mark - Helpers
  20. ///--------------------------------------
  21. - (id)mockPinObjects:(NSArray *)objects withPinName:(NSString *)pinName error:(NSError *)error {
  22. PFPinningObjectStore *store = PFStrictClassMock([PFPinningObjectStore class]);
  23. [Parse _currentManager].coreManager.pinningObjectStore = store;
  24. BFTask *task = (error ? [BFTask taskWithError:error] : [BFTask taskWithResult:@YES]);
  25. OCMExpect([store pinObjectsAsync:objects
  26. withPinName:pinName
  27. includeChildren:YES]).andReturn(task);
  28. return store;
  29. }
  30. - (id)mockUnpinObjects:(NSArray *)objects withPinName:(NSString *)pinName error:(NSError *)error {
  31. PFPinningObjectStore *store = PFStrictClassMock([PFPinningObjectStore class]);
  32. [Parse _currentManager].coreManager.pinningObjectStore = store;
  33. BFTask *task = (error ? [BFTask taskWithError:error] : [BFTask taskWithResult:@YES]);
  34. OCMExpect([store unpinObjectsAsync:objects withPinName:pinName]).andReturn(task);
  35. return store;
  36. }
  37. - (id)mockUnpinAllObjectsWithPinName:(NSString *)pinName error:(NSError *)error {
  38. PFPinningObjectStore *store = PFStrictClassMock([PFPinningObjectStore class]);
  39. [Parse _currentManager].coreManager.pinningObjectStore = store;
  40. BFTask *task = (error ? [BFTask taskWithError:error] : [BFTask taskWithResult:@YES]);
  41. OCMExpect([store unpinAllObjectsAsyncWithPinName:pinName]).andReturn(task);
  42. return store;
  43. }
  44. ///--------------------------------------
  45. #pragma mark - Tests
  46. ///--------------------------------------
  47. #pragma mark Pinning
  48. - (void)testPinObject {
  49. PFObject *object = [PFObject objectWithClassName:@"Yarr"];
  50. id mock = [self mockPinObjects:@[ object ] withPinName:PFObjectDefaultPin error:nil];
  51. XCTAssertTrue([object pin]);
  52. OCMVerifyAll(mock);
  53. }
  54. - (void)testPinObjectWithError {
  55. NSError *expectedError = [NSError errorWithDomain:@"Yolo!" code:100500 userInfo:nil];
  56. PFObject *object = [PFObject objectWithClassName:@"Yarr"];
  57. id mock = [self mockPinObjects:@[ object ] withPinName:PFObjectDefaultPin error:expectedError];
  58. NSError *error = nil;
  59. XCTAssertFalse([object pin:&error]);
  60. XCTAssertEqualObjects(error, expectedError);
  61. OCMVerifyAll(mock);
  62. }
  63. - (void)testPinObjectViaTask {
  64. PFObject *object = [PFObject objectWithClassName:@"Yarr"];
  65. id mock = [self mockPinObjects:@[ object ] withPinName:PFObjectDefaultPin error:nil];
  66. XCTestExpectation *expectation = [self currentSelectorTestExpectation];
  67. [[object pinInBackground] continueWithSuccessBlock:^id(BFTask *task) {
  68. XCTAssertEqualObjects(task.result, @YES);
  69. [expectation fulfill];
  70. return nil;
  71. }];
  72. [self waitForTestExpectations];
  73. OCMVerifyAll(mock);
  74. }
  75. - (void)testPinObjectViaBlock {
  76. PFObject *object = [PFObject objectWithClassName:@"Yarr"];
  77. id mock = [self mockPinObjects:@[ object ] withPinName:PFObjectDefaultPin error:nil];
  78. XCTestExpectation *expectation = [self currentSelectorTestExpectation];
  79. [object pinInBackgroundWithBlock:^(BOOL success, NSError *error){
  80. XCTAssertTrue(success);
  81. XCTAssertNil(error);
  82. [expectation fulfill];
  83. }];
  84. [self waitForTestExpectations];
  85. OCMVerifyAll(mock);
  86. }
  87. - (void)testPinObjectWithName {
  88. PFObject *object = [PFObject objectWithClassName:@"Yarr"];
  89. id mock = [self mockPinObjects:@[ object ] withPinName:@"Pirates" error:nil];
  90. XCTAssertTrue([object pinWithName:@"Pirates"]);
  91. OCMVerifyAll(mock);
  92. }
  93. - (void)testPinObjectWithNameError {
  94. NSError *expectedError = [NSError errorWithDomain:@"Yolo!" code:100500 userInfo:nil];
  95. PFObject *object = [PFObject objectWithClassName:@"Yarr"];
  96. id mock = [self mockPinObjects:@[ object ] withPinName:@"Pirates" error:expectedError];
  97. NSError *error = nil;
  98. XCTAssertFalse([object pinWithName:@"Pirates" error:&error]);
  99. XCTAssertEqualObjects(error, expectedError);
  100. OCMVerifyAll(mock);
  101. }
  102. - (void)testPinObjectWithNameViaTask {
  103. PFObject *object = [PFObject objectWithClassName:@"Yarr"];
  104. id mock = [self mockPinObjects:@[ object ] withPinName:@"Pirates" error:nil];
  105. XCTestExpectation *expectation = [self currentSelectorTestExpectation];
  106. [[object pinInBackgroundWithName:@"Pirates"] continueWithSuccessBlock:^id(BFTask *task) {
  107. XCTAssertEqualObjects(task.result, @YES);
  108. [expectation fulfill];
  109. return nil;
  110. }];
  111. [self waitForTestExpectations];
  112. OCMVerifyAll(mock);
  113. }
  114. - (void)testPinObjectWithNameViaBlock {
  115. PFObject *object = [PFObject objectWithClassName:@"Yarr"];
  116. id mock = [self mockPinObjects:@[ object ] withPinName:@"Pirates" error:nil];
  117. XCTestExpectation *expectation = [self currentSelectorTestExpectation];
  118. [object pinInBackgroundWithName:@"Pirates" block:^(BOOL success, NSError *error){
  119. XCTAssertTrue(success);
  120. XCTAssertNil(error);
  121. [expectation fulfill];
  122. }];
  123. [self waitForTestExpectations];
  124. OCMVerifyAll(mock);
  125. }
  126. #pragma mark Pinning Many Objects
  127. - (void)testPinAll {
  128. NSArray *objects = @[ [PFObject objectWithClassName:@"Yarr"] ];
  129. id mock = [self mockPinObjects:objects withPinName:PFObjectDefaultPin error:nil];
  130. XCTAssertTrue([PFObject pinAll:objects]);
  131. OCMVerifyAll(mock);
  132. }
  133. - (void)testPinAllWithError {
  134. NSError *expectedError = [NSError errorWithDomain:@"Yolo!" code:100500 userInfo:nil];
  135. NSArray *objects = @[ [PFObject objectWithClassName:@"Yarr"] ];
  136. id mock = [self mockPinObjects:objects withPinName:PFObjectDefaultPin error:expectedError];
  137. NSError *error = nil;
  138. XCTAssertFalse([PFObject pinAll:objects error:&error]);
  139. XCTAssertEqualObjects(error, expectedError);
  140. OCMVerifyAll(mock);
  141. }
  142. - (void)testPinAllViaTask {
  143. NSArray *objects = @[ [PFObject objectWithClassName:@"Yarr"] ];
  144. id mock = [self mockPinObjects:objects withPinName:PFObjectDefaultPin error:nil];
  145. XCTestExpectation *expectation = [self currentSelectorTestExpectation];
  146. [[PFObject pinAllInBackground:objects] continueWithSuccessBlock:^id(BFTask *task) {
  147. XCTAssertEqualObjects(task.result, @YES);
  148. [expectation fulfill];
  149. return nil;
  150. }];
  151. [self waitForTestExpectations];
  152. OCMVerifyAll(mock);
  153. }
  154. - (void)testPinAllViaBlock {
  155. NSArray *objects = @[ [PFObject objectWithClassName:@"Yarr"] ];
  156. id mock = [self mockPinObjects:objects withPinName:PFObjectDefaultPin error:nil];
  157. XCTestExpectation *expectation = [self currentSelectorTestExpectation];
  158. [PFObject pinAllInBackground:objects block:^(BOOL success, NSError *error){
  159. XCTAssertTrue(success);
  160. XCTAssertNil(error);
  161. [expectation fulfill];
  162. }];
  163. [self waitForTestExpectations];
  164. OCMVerifyAll(mock);
  165. }
  166. - (void)testPinAllWithName {
  167. NSArray *objects = @[ [PFObject objectWithClassName:@"Yarr"] ];
  168. id mock = [self mockPinObjects:objects withPinName:@"Pirates" error:nil];
  169. XCTAssertTrue([PFObject pinAll:objects withName:@"Pirates"]);
  170. OCMVerifyAll(mock);
  171. }
  172. - (void)testPinAllWithNameError {
  173. NSError *expectedError = [NSError errorWithDomain:@"Yolo!" code:100500 userInfo:nil];
  174. NSArray *objects = @[ [PFObject objectWithClassName:@"Yarr"] ];
  175. id mock = [self mockPinObjects:objects withPinName:@"Pirates" error:expectedError];
  176. NSError *error = nil;
  177. XCTAssertFalse([PFObject pinAll:objects withName:@"Pirates" error:&error]);
  178. XCTAssertEqualObjects(error, expectedError);
  179. OCMVerifyAll(mock);
  180. }
  181. - (void)testPinAllWithNameViaTask {
  182. NSArray *objects = @[ [PFObject objectWithClassName:@"Yarr"] ];
  183. id mock = [self mockPinObjects:objects withPinName:@"Pirates" error:nil];
  184. XCTestExpectation *expectation = [self currentSelectorTestExpectation];
  185. [[PFObject pinAllInBackground:objects withName:@"Pirates"] continueWithSuccessBlock:^id(BFTask *task) {
  186. XCTAssertEqualObjects(task.result, @YES);
  187. [expectation fulfill];
  188. return nil;
  189. }];
  190. [self waitForTestExpectations];
  191. OCMVerifyAll(mock);
  192. }
  193. - (void)testPinAllWithNameViaBlock {
  194. NSArray *objects = @[ [PFObject objectWithClassName:@"Yarr"] ];
  195. id mock = [self mockPinObjects:objects withPinName:@"Pirates" error:nil];
  196. XCTestExpectation *expectation = [self currentSelectorTestExpectation];
  197. [PFObject pinAllInBackground:objects withName:@"Pirates" block:^(BOOL success, NSError *error){
  198. XCTAssertTrue(success);
  199. XCTAssertNil(error);
  200. [expectation fulfill];
  201. }];
  202. [self waitForTestExpectations];
  203. OCMVerifyAll(mock);
  204. }
  205. #pragma mark Unpinning
  206. - (void)testUnpinObject {
  207. PFObject *object = [PFObject objectWithClassName:@"Yarr"];
  208. id mock = [self mockUnpinObjects:@[ object ] withPinName:PFObjectDefaultPin error:nil];
  209. XCTAssertTrue([object unpin]);
  210. OCMVerifyAll(mock);
  211. }
  212. - (void)testUnpinObjectWithError {
  213. NSError *expectedError = [NSError errorWithDomain:@"Yolo!" code:100500 userInfo:nil];
  214. PFObject *object = [PFObject objectWithClassName:@"Yarr"];
  215. id mock = [self mockUnpinObjects:@[ object ] withPinName:PFObjectDefaultPin error:expectedError];
  216. NSError *error = nil;
  217. XCTAssertFalse([object unpin:&error]);
  218. XCTAssertEqualObjects(error, expectedError);
  219. OCMVerifyAll(mock);
  220. }
  221. - (void)testUnpinObjectViaTask {
  222. PFObject *object = [PFObject objectWithClassName:@"Yarr"];
  223. id mock = [self mockUnpinObjects:@[ object ] withPinName:PFObjectDefaultPin error:nil];
  224. XCTestExpectation *expectation = [self currentSelectorTestExpectation];
  225. [[object unpinInBackground] continueWithSuccessBlock:^id(BFTask *task) {
  226. XCTAssertEqualObjects(task.result, @YES);
  227. [expectation fulfill];
  228. return nil;
  229. }];
  230. [self waitForTestExpectations];
  231. OCMVerifyAll(mock);
  232. }
  233. - (void)testUnpinObjectViaBlock {
  234. PFObject *object = [PFObject objectWithClassName:@"Yarr"];
  235. id mock = [self mockUnpinObjects:@[ object ] withPinName:PFObjectDefaultPin error:nil];
  236. XCTestExpectation *expectation = [self currentSelectorTestExpectation];
  237. [object unpinInBackgroundWithBlock:^(BOOL success, NSError *error){
  238. XCTAssertTrue(success);
  239. XCTAssertNil(error);
  240. [expectation fulfill];
  241. }];
  242. [self waitForTestExpectations];
  243. OCMVerifyAll(mock);
  244. }
  245. - (void)testUnpinObjectWithName {
  246. PFObject *object = [PFObject objectWithClassName:@"Yarr"];
  247. id mock = [self mockUnpinObjects:@[ object ] withPinName:@"Pirates" error:nil];
  248. XCTAssertTrue([object unpinWithName:@"Pirates"]);
  249. OCMVerifyAll(mock);
  250. }
  251. - (void)testUnpinObjectWithNameError {
  252. NSError *expectedError = [NSError errorWithDomain:@"Yolo!" code:100500 userInfo:nil];
  253. PFObject *object = [PFObject objectWithClassName:@"Yarr"];
  254. id mock = [self mockUnpinObjects:@[ object ] withPinName:@"Pirates" error:expectedError];
  255. NSError *error = nil;
  256. XCTAssertFalse([object unpinWithName:@"Pirates" error:&error]);
  257. XCTAssertEqualObjects(error, expectedError);
  258. OCMVerifyAll(mock);
  259. }
  260. - (void)testUnpinObjectWithNameViaTask {
  261. PFObject *object = [PFObject objectWithClassName:@"Yarr"];
  262. id mock = [self mockUnpinObjects:@[ object ] withPinName:@"Pirates" error:nil];
  263. XCTestExpectation *expectation = [self currentSelectorTestExpectation];
  264. [[object unpinInBackgroundWithName:@"Pirates"] continueWithSuccessBlock:^id(BFTask *task) {
  265. XCTAssertEqualObjects(task.result, @YES);
  266. [expectation fulfill];
  267. return nil;
  268. }];
  269. [self waitForTestExpectations];
  270. OCMVerifyAll(mock);
  271. }
  272. - (void)testUnpinObjectWithNameViaBlock {
  273. PFObject *object = [PFObject objectWithClassName:@"Yarr"];
  274. id mock = [self mockUnpinObjects:@[ object ] withPinName:@"Pirates" error:nil];
  275. XCTestExpectation *expectation = [self currentSelectorTestExpectation];
  276. [object unpinInBackgroundWithName:@"Pirates" block:^(BOOL success, NSError *error){
  277. XCTAssertTrue(success);
  278. XCTAssertNil(error);
  279. [expectation fulfill];
  280. }];
  281. [self waitForTestExpectations];
  282. OCMVerifyAll(mock);
  283. }
  284. #pragma mark Unpinning Many Objects
  285. - (void)testUnpinAll {
  286. NSArray *objects = @[ [PFObject objectWithClassName:@"Yarr"] ];
  287. id mock = [self mockUnpinObjects:objects withPinName:PFObjectDefaultPin error:nil];
  288. XCTAssertTrue([PFObject unpinAll:objects]);
  289. OCMVerifyAll(mock);
  290. }
  291. - (void)testUnpinAllWithError {
  292. NSError *expectedError = [NSError errorWithDomain:@"Yolo!" code:100500 userInfo:nil];
  293. NSArray *objects = @[ [PFObject objectWithClassName:@"Yarr"] ];
  294. id mock = [self mockUnpinObjects:objects withPinName:PFObjectDefaultPin error:expectedError];
  295. NSError *error = nil;
  296. XCTAssertFalse([PFObject unpinAll:objects error:&error]);
  297. XCTAssertEqualObjects(error, expectedError);
  298. OCMVerifyAll(mock);
  299. }
  300. - (void)testUnpinAllViaTask {
  301. NSArray *objects = @[ [PFObject objectWithClassName:@"Yarr"] ];
  302. id mock = [self mockUnpinObjects:objects withPinName:PFObjectDefaultPin error:nil];
  303. XCTestExpectation *expectation = [self currentSelectorTestExpectation];
  304. [[PFObject unpinAllInBackground:objects] continueWithSuccessBlock:^id(BFTask *task) {
  305. XCTAssertEqualObjects(task.result, @YES);
  306. [expectation fulfill];
  307. return nil;
  308. }];
  309. [self waitForTestExpectations];
  310. OCMVerifyAll(mock);
  311. }
  312. - (void)testUnpinAllWithBlock {
  313. NSArray *objects = @[ [PFObject objectWithClassName:@"Yarr"] ];
  314. id mock = [self mockUnpinObjects:objects withPinName:PFObjectDefaultPin error:nil];
  315. XCTestExpectation *expectation = [self currentSelectorTestExpectation];
  316. [PFObject unpinAllInBackground:objects block:^(BOOL success, NSError *error){
  317. XCTAssertTrue(success);
  318. XCTAssertNil(error);
  319. [expectation fulfill];
  320. }];
  321. [self waitForTestExpectations];
  322. OCMVerifyAll(mock);
  323. }
  324. - (void)testUnpinAllWithName {
  325. NSArray *objects = @[ [PFObject objectWithClassName:@"Yarr"] ];
  326. id mock = [self mockUnpinObjects:objects withPinName:@"Pirates" error:nil];
  327. XCTAssertTrue([PFObject unpinAll:objects withName:@"Pirates"]);
  328. OCMVerifyAll(mock);
  329. }
  330. - (void)testUnpinAllWithNameError {
  331. NSError *expectedError = [NSError errorWithDomain:@"Yolo!" code:100500 userInfo:nil];
  332. NSArray *objects = @[ [PFObject objectWithClassName:@"Yarr"] ];
  333. id mock = [self mockUnpinObjects:objects withPinName:@"Pirates" error:expectedError];
  334. NSError *error = nil;
  335. XCTAssertFalse([PFObject unpinAll:objects withName:@"Pirates" error:&error]);
  336. XCTAssertEqualObjects(error, expectedError);
  337. OCMVerifyAll(mock);
  338. }
  339. - (void)testUnpinAllWithNameViaTask {
  340. NSArray *objects = @[ [PFObject objectWithClassName:@"Yarr"] ];
  341. id mock = [self mockUnpinObjects:objects withPinName:@"Pirates" error:nil];
  342. XCTestExpectation *expectation = [self currentSelectorTestExpectation];
  343. [[PFObject unpinAllInBackground:objects withName:@"Pirates"] continueWithSuccessBlock:^id(BFTask *task) {
  344. XCTAssertEqualObjects(task.result, @YES);
  345. [expectation fulfill];
  346. return nil;
  347. }];
  348. [self waitForTestExpectations];
  349. OCMVerifyAll(mock);
  350. }
  351. - (void)testUnpinAllWithNameViaBlock {
  352. NSArray *objects = @[ [PFObject objectWithClassName:@"Yarr"] ];
  353. id mock = [self mockUnpinObjects:objects withPinName:@"Pirates" error:nil];
  354. XCTestExpectation *expectation = [self currentSelectorTestExpectation];
  355. [PFObject unpinAllInBackground:objects withName:@"Pirates" block:^(BOOL success, NSError *error){
  356. XCTAssertTrue(success);
  357. XCTAssertNil(error);
  358. [expectation fulfill];
  359. }];
  360. [self waitForTestExpectations];
  361. OCMVerifyAll(mock);
  362. }
  363. - (void)testUnpinAllObjects {
  364. id mock = [self mockUnpinAllObjectsWithPinName:PFObjectDefaultPin error:nil];
  365. XCTAssertTrue([PFObject unpinAllObjects]);
  366. OCMVerifyAll(mock);
  367. }
  368. - (void)testUnpinAllObjectsWithError {
  369. NSError *expectedError = [NSError errorWithDomain:@"Yolo!" code:100500 userInfo:nil];
  370. id mock = [self mockUnpinAllObjectsWithPinName:PFObjectDefaultPin error:expectedError];
  371. NSError *error = nil;
  372. XCTAssertFalse([PFObject unpinAllObjects:&error]);
  373. XCTAssertEqualObjects(error, expectedError);
  374. OCMVerifyAll(mock);
  375. }
  376. - (void)testUnpinAllObjectsViaTask {
  377. id mock = [self mockUnpinAllObjectsWithPinName:PFObjectDefaultPin error:nil];
  378. XCTestExpectation *expectation = [self currentSelectorTestExpectation];
  379. [[PFObject unpinAllObjectsInBackground] continueWithSuccessBlock:^id(BFTask *task) {
  380. XCTAssertEqualObjects(task.result, @YES);
  381. [expectation fulfill];
  382. return nil;
  383. }];
  384. [self waitForTestExpectations];
  385. OCMVerifyAll(mock);
  386. }
  387. - (void)testUnpinAllObjectsViaBlock {
  388. id mock = [self mockUnpinAllObjectsWithPinName:PFObjectDefaultPin error:nil];
  389. XCTestExpectation *expectation = [self currentSelectorTestExpectation];
  390. [PFObject unpinAllObjectsInBackgroundWithBlock:^(BOOL success, NSError *error){
  391. XCTAssertTrue(success);
  392. XCTAssertNil(error);
  393. [expectation fulfill];
  394. }];
  395. [self waitForTestExpectations];
  396. OCMVerifyAll(mock);
  397. }
  398. - (void)testUnpinAllObjectsWithName {
  399. id mock = [self mockUnpinAllObjectsWithPinName:@"Pirates" error:nil];
  400. XCTAssertTrue([PFObject unpinAllObjectsWithName:@"Pirates"]);
  401. OCMVerifyAll(mock);
  402. }
  403. - (void)testUnpinAllObjectsWithNameError {
  404. NSError *expectedError = [NSError errorWithDomain:@"Yolo!" code:100500 userInfo:nil];
  405. id mock = [self mockUnpinAllObjectsWithPinName:@"Pirates" error:expectedError];
  406. NSError *error = nil;
  407. XCTAssertFalse([PFObject unpinAllObjectsWithName:@"Pirates" error:&error]);
  408. XCTAssertEqualObjects(error, expectedError);
  409. OCMVerifyAll(mock);
  410. }
  411. - (void)testUnpinAllObjectsWithNameViaTask {
  412. id mock = [self mockUnpinAllObjectsWithPinName:@"Pirates" error:nil];
  413. XCTestExpectation *expectation = [self currentSelectorTestExpectation];
  414. [[PFObject unpinAllObjectsInBackgroundWithName:@"Pirates"] continueWithSuccessBlock:^id(BFTask *task) {
  415. XCTAssertEqualObjects(task.result, @YES);
  416. [expectation fulfill];
  417. return nil;
  418. }];
  419. [self waitForTestExpectations];
  420. OCMVerifyAll(mock);
  421. }
  422. - (void)testUnpinAllObjectsWithNameViaBlock {
  423. id mock = [self mockUnpinAllObjectsWithPinName:@"Pirates" error:nil];
  424. XCTestExpectation *expectation = [self currentSelectorTestExpectation];
  425. [PFObject unpinAllObjectsInBackgroundWithName:@"Pirates" block:^(BOOL success, NSError *error){
  426. XCTAssertTrue(success);
  427. XCTAssertNil(error);
  428. [expectation fulfill];
  429. }];
  430. [self waitForTestExpectations];
  431. OCMVerifyAll(mock);
  432. }
  433. @end