FSTRemoteDocumentChangeBufferTests.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "Firestore/Source/Local/FSTRemoteDocumentChangeBuffer.h"
  17. #import <XCTest/XCTest.h>
  18. #import "Firestore/Source/Local/FSTLevelDB.h"
  19. #import "Firestore/Source/Local/FSTRemoteDocumentCache.h"
  20. #import "Firestore/Source/Model/FSTDocument.h"
  21. #import "Firestore/Example/Tests/Local/FSTPersistenceTestHelpers.h"
  22. #import "Firestore/Example/Tests/Util/FSTHelpers.h"
  23. NS_ASSUME_NONNULL_BEGIN
  24. @interface FSTRemoteDocumentChangeBufferTests : XCTestCase
  25. @end
  26. @implementation FSTRemoteDocumentChangeBufferTests {
  27. FSTLevelDB *_db;
  28. id<FSTRemoteDocumentCache> _remoteDocumentCache;
  29. FSTRemoteDocumentChangeBuffer *_remoteDocumentBuffer;
  30. FSTMaybeDocument *_kInitialADoc;
  31. FSTMaybeDocument *_kInitialBDoc;
  32. }
  33. - (void)setUp {
  34. [super setUp];
  35. _db = [FSTPersistenceTestHelpers levelDBPersistence];
  36. _remoteDocumentCache = [_db remoteDocumentCache];
  37. // Add a couple initial items to the cache.
  38. FSTWriteGroup *group = [_db startGroupWithAction:@"Add initial docs."];
  39. _kInitialADoc = FSTTestDoc(@"coll/a", 42, @{@"test" : @"data"}, NO);
  40. [_remoteDocumentCache addEntry:_kInitialADoc group:group];
  41. _kInitialBDoc =
  42. [FSTDeletedDocument documentWithKey:FSTTestDocKey(@"coll/b") version:FSTTestVersion(314)];
  43. [_remoteDocumentCache addEntry:_kInitialBDoc group:group];
  44. [_db commitGroup:group];
  45. _remoteDocumentBuffer =
  46. [FSTRemoteDocumentChangeBuffer changeBufferWithCache:_remoteDocumentCache];
  47. }
  48. - (void)tearDown {
  49. _remoteDocumentBuffer = nil;
  50. _remoteDocumentCache = nil;
  51. _db = nil;
  52. [super tearDown];
  53. }
  54. - (void)testReadUnchangedEntry {
  55. XCTAssertEqualObjects([_remoteDocumentBuffer entryForKey:FSTTestDocKey(@"coll/a")],
  56. _kInitialADoc);
  57. }
  58. - (void)testAddEntryAndReadItBack {
  59. FSTMaybeDocument *newADoc = FSTTestDoc(@"coll/a", 43, @{@"new" : @"data"}, NO);
  60. [_remoteDocumentBuffer addEntry:newADoc];
  61. XCTAssertEqualObjects([_remoteDocumentBuffer entryForKey:FSTTestDocKey(@"coll/a")], newADoc);
  62. // B should still be unchanged.
  63. XCTAssertEqualObjects([_remoteDocumentBuffer entryForKey:FSTTestDocKey(@"coll/b")],
  64. _kInitialBDoc);
  65. }
  66. - (void)testApplyChanges {
  67. FSTMaybeDocument *newADoc = FSTTestDoc(@"coll/a", 43, @{@"new" : @"data"}, NO);
  68. [_remoteDocumentBuffer addEntry:newADoc];
  69. XCTAssertEqualObjects([_remoteDocumentBuffer entryForKey:FSTTestDocKey(@"coll/a")], newADoc);
  70. // Reading directly against the cache should still yield the old result.
  71. XCTAssertEqualObjects([_remoteDocumentCache entryForKey:FSTTestDocKey(@"coll/a")], _kInitialADoc);
  72. FSTWriteGroup *group = [_db startGroupWithAction:@"Apply changes"];
  73. [_remoteDocumentBuffer applyToWriteGroup:group];
  74. [_db commitGroup:group];
  75. // Reading against the cache should now yield the new result.
  76. XCTAssertEqualObjects([_remoteDocumentCache entryForKey:FSTTestDocKey(@"coll/a")], newADoc);
  77. }
  78. - (void)testMethodsThrowAfterApply {
  79. FSTWriteGroup *group = [_db startGroupWithAction:@"Apply changes"];
  80. [_remoteDocumentBuffer applyToWriteGroup:group];
  81. [_db commitGroup:group];
  82. XCTAssertThrows([_remoteDocumentBuffer entryForKey:FSTTestDocKey(@"coll/a")]);
  83. XCTAssertThrows([_remoteDocumentBuffer addEntry:_kInitialADoc]);
  84. XCTAssertThrows([_remoteDocumentBuffer applyToWriteGroup:group]);
  85. }
  86. @end
  87. NS_ASSUME_NONNULL_END