FSTRemoteDocumentCacheTests.m 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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/Example/Tests/Local/FSTRemoteDocumentCacheTests.h"
  17. #import "Firestore/Source/Core/FSTQuery.h"
  18. #import "Firestore/Source/Local/FSTPersistence.h"
  19. #import "Firestore/Source/Local/FSTWriteGroup.h"
  20. #import "Firestore/Source/Model/FSTDocument.h"
  21. #import "Firestore/Source/Model/FSTDocumentKey.h"
  22. #import "Firestore/Source/Model/FSTDocumentSet.h"
  23. #import "Firestore/Example/Tests/Util/FSTHelpers.h"
  24. NS_ASSUME_NONNULL_BEGIN
  25. static NSString *const kDocPath = @"a/b";
  26. static NSString *const kLongDocPath = @"a/b/c/d/e/f";
  27. static const int kVersion = 42;
  28. @implementation FSTRemoteDocumentCacheTests {
  29. NSDictionary<NSString *, id> *_kDocData;
  30. }
  31. - (void)setUp {
  32. [super setUp];
  33. // essentially a constant, but can't be a compile-time one.
  34. _kDocData = @{ @"a" : @1, @"b" : @2 };
  35. }
  36. - (void)testReadDocumentNotInCache {
  37. if (!self.remoteDocumentCache) return;
  38. XCTAssertNil([self readEntryAtPath:kDocPath]);
  39. }
  40. // Helper for next two tests.
  41. - (void)setAndReadADocumentAtPath:(NSString *)path {
  42. FSTDocument *written = [self setTestDocumentAtPath:path];
  43. FSTMaybeDocument *read = [self readEntryAtPath:path];
  44. XCTAssertEqualObjects(read, written);
  45. }
  46. - (void)testSetAndReadADocument {
  47. if (!self.remoteDocumentCache) return;
  48. [self setAndReadADocumentAtPath:kDocPath];
  49. }
  50. - (void)testSetAndReadADocumentAtDeepPath {
  51. if (!self.remoteDocumentCache) return;
  52. [self setAndReadADocumentAtPath:kLongDocPath];
  53. }
  54. - (void)testSetAndReadDeletedDocument {
  55. if (!self.remoteDocumentCache) return;
  56. FSTDeletedDocument *deletedDoc = FSTTestDeletedDoc(kDocPath, kVersion);
  57. [self addEntry:deletedDoc];
  58. XCTAssertEqualObjects([self readEntryAtPath:kDocPath], deletedDoc);
  59. }
  60. - (void)testSetDocumentToNewValue {
  61. if (!self.remoteDocumentCache) return;
  62. [self setTestDocumentAtPath:kDocPath];
  63. FSTDocument *newDoc = FSTTestDoc(kDocPath, kVersion, @{ @"data" : @2 }, NO);
  64. [self addEntry:newDoc];
  65. XCTAssertEqualObjects([self readEntryAtPath:kDocPath], newDoc);
  66. }
  67. - (void)testRemoveDocument {
  68. if (!self.remoteDocumentCache) return;
  69. [self setTestDocumentAtPath:kDocPath];
  70. [self removeEntryAtPath:kDocPath];
  71. XCTAssertNil([self readEntryAtPath:kDocPath]);
  72. }
  73. - (void)testRemoveNonExistentDocument {
  74. if (!self.remoteDocumentCache) return;
  75. // no-op, but make sure it doesn't throw.
  76. XCTAssertNoThrow([self removeEntryAtPath:kDocPath]);
  77. }
  78. // TODO(mikelehen): Write more elaborate tests once we have more elaborate implementations.
  79. - (void)testDocumentsMatchingQuery {
  80. if (!self.remoteDocumentCache) return;
  81. // TODO(rsgowman): This just verifies that we do a prefix scan against the
  82. // query path. We'll need more tests once we add index support.
  83. [self setTestDocumentAtPath:@"a/1"];
  84. [self setTestDocumentAtPath:@"b/1"];
  85. [self setTestDocumentAtPath:@"b/2"];
  86. [self setTestDocumentAtPath:@"c/1"];
  87. FSTQuery *query = [FSTQuery queryWithPath:FSTTestPath(@"b")];
  88. FSTDocumentDictionary *results = [self.remoteDocumentCache documentsMatchingQuery:query];
  89. NSArray *expected =
  90. @[ FSTTestDoc(@"b/1", kVersion, _kDocData, NO), FSTTestDoc(@"b/2", kVersion, _kDocData, NO) ];
  91. XCTAssertEqual([results count], [expected count]);
  92. for (FSTDocument *doc in expected) {
  93. XCTAssertEqualObjects([results objectForKey:doc.key], doc);
  94. }
  95. }
  96. #pragma mark - Helpers
  97. - (FSTDocument *)setTestDocumentAtPath:(NSString *)path {
  98. FSTDocument *doc = FSTTestDoc(path, kVersion, _kDocData, NO);
  99. [self addEntry:doc];
  100. return doc;
  101. }
  102. - (void)addEntry:(FSTMaybeDocument *)maybeDoc {
  103. FSTWriteGroup *group = [self.persistence startGroupWithAction:@"addEntry"];
  104. [self.remoteDocumentCache addEntry:maybeDoc group:group];
  105. [self.persistence commitGroup:group];
  106. }
  107. - (FSTMaybeDocument *_Nullable)readEntryAtPath:(NSString *)path {
  108. return [self.remoteDocumentCache entryForKey:FSTTestDocKey(path)];
  109. }
  110. - (void)removeEntryAtPath:(NSString *)path {
  111. FSTWriteGroup *group = [self.persistence startGroupWithAction:@"removeEntryAtPath"];
  112. [self.remoteDocumentCache removeEntryForKey:FSTTestDocKey(path) group:group];
  113. [self.persistence commitGroup:group];
  114. }
  115. @end
  116. NS_ASSUME_NONNULL_END