FSTRemoteDocumentCacheTests.mm 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. #include <memory>
  18. #import "Firestore/Source/Local/FSTPersistence.h"
  19. #import "Firestore/Source/Model/FSTDocument.h"
  20. #import "Firestore/Example/Tests/Util/FSTHelpers.h"
  21. #include "Firestore/core/src/firebase/firestore/local/memory_remote_document_cache.h"
  22. #include "Firestore/core/src/firebase/firestore/local/remote_document_cache.h"
  23. #include "Firestore/core/src/firebase/firestore/model/document_key.h"
  24. #include "Firestore/core/src/firebase/firestore/model/document_key_set.h"
  25. #include "Firestore/core/src/firebase/firestore/model/document_map.h"
  26. #include "Firestore/core/src/firebase/firestore/util/string_apple.h"
  27. #include "Firestore/core/test/firebase/firestore/testutil/testutil.h"
  28. #include "absl/strings/string_view.h"
  29. namespace testutil = firebase::firestore::testutil;
  30. namespace core = firebase::firestore::core;
  31. namespace util = firebase::firestore::util;
  32. using firebase::firestore::local::RemoteDocumentCache;
  33. using firebase::firestore::model::DocumentKey;
  34. using firebase::firestore::model::DocumentKeySet;
  35. using firebase::firestore::model::DocumentMap;
  36. using firebase::firestore::model::DocumentState;
  37. using firebase::firestore::model::MaybeDocumentMap;
  38. using testutil::Query;
  39. NS_ASSUME_NONNULL_BEGIN
  40. static const char *kDocPath = "a/b";
  41. static const char *kLongDocPath = "a/b/c/d/e/f";
  42. static const int kVersion = 42;
  43. @implementation FSTRemoteDocumentCacheTests {
  44. NSDictionary<NSString *, id> *_kDocData;
  45. }
  46. - (void)setUp {
  47. [super setUp];
  48. // essentially a constant, but can't be a compile-time one.
  49. _kDocData = @{@"a" : @1, @"b" : @2};
  50. }
  51. - (void)tearDown {
  52. [self.persistence shutdown];
  53. }
  54. - (void)testReadDocumentNotInCache {
  55. if (!self.remoteDocumentCache) return;
  56. self.persistence.run("testReadDocumentNotInCache", [&]() {
  57. XCTAssertNil(self.remoteDocumentCache->Get(testutil::Key(kDocPath)));
  58. });
  59. }
  60. // Helper for next two tests.
  61. - (void)setAndReadADocumentAtPath:(const absl::string_view)path {
  62. self.persistence.run("setAndReadADocumentAtPath", [&]() {
  63. FSTDocument *written = [self setTestDocumentAtPath:path];
  64. FSTMaybeDocument *read = self.remoteDocumentCache->Get(testutil::Key(path));
  65. XCTAssertEqualObjects(read, written);
  66. });
  67. }
  68. - (void)testSetAndReadADocument {
  69. if (!self.remoteDocumentCache) return;
  70. [self setAndReadADocumentAtPath:kDocPath];
  71. }
  72. - (void)testSetAndReadSeveralDocuments {
  73. if (!self.remoteDocumentCache) return;
  74. self.persistence.run("testSetAndReadSeveralDocuments", [=]() {
  75. NSArray<FSTDocument *> *written =
  76. @[ [self setTestDocumentAtPath:kDocPath], [self setTestDocumentAtPath:kLongDocPath] ];
  77. MaybeDocumentMap read = self.remoteDocumentCache->GetAll(
  78. DocumentKeySet{testutil::Key(kDocPath), testutil::Key(kLongDocPath)});
  79. [self expectMap:read hasDocsInArray:written exactly:YES];
  80. });
  81. }
  82. - (void)testSetAndReadSeveralDocumentsIncludingMissingDocument {
  83. if (!self.remoteDocumentCache) return;
  84. self.persistence.run("testSetAndReadSeveralDocumentsIncludingMissingDocument", [=]() {
  85. NSArray<FSTDocument *> *written =
  86. @[ [self setTestDocumentAtPath:kDocPath], [self setTestDocumentAtPath:kLongDocPath] ];
  87. MaybeDocumentMap read = self.remoteDocumentCache->GetAll(DocumentKeySet{
  88. testutil::Key(kDocPath),
  89. testutil::Key(kLongDocPath),
  90. testutil::Key("foo/nonexistent"),
  91. });
  92. [self expectMap:read hasDocsInArray:written exactly:NO];
  93. auto found = read.find(DocumentKey::FromPathString("foo/nonexistent"));
  94. XCTAssertTrue(found != read.end());
  95. XCTAssertNil(found->second);
  96. });
  97. }
  98. - (void)testSetAndReadADocumentAtDeepPath {
  99. if (!self.remoteDocumentCache) return;
  100. [self setAndReadADocumentAtPath:kLongDocPath];
  101. }
  102. - (void)testSetAndReadDeletedDocument {
  103. if (!self.remoteDocumentCache) return;
  104. self.persistence.run("testSetAndReadDeletedDocument", [&]() {
  105. FSTDeletedDocument *deletedDoc = FSTTestDeletedDoc(kDocPath, kVersion, NO);
  106. self.remoteDocumentCache->Add(deletedDoc);
  107. XCTAssertEqualObjects(self.remoteDocumentCache->Get(testutil::Key(kDocPath)), deletedDoc);
  108. });
  109. }
  110. - (void)testSetDocumentToNewValue {
  111. if (!self.remoteDocumentCache) return;
  112. self.persistence.run("testSetDocumentToNewValue", [&]() {
  113. [self setTestDocumentAtPath:kDocPath];
  114. FSTDocument *newDoc = FSTTestDoc(kDocPath, kVersion, @{@"data" : @2}, DocumentState::kSynced);
  115. self.remoteDocumentCache->Add(newDoc);
  116. XCTAssertEqualObjects(self.remoteDocumentCache->Get(testutil::Key(kDocPath)), newDoc);
  117. });
  118. }
  119. - (void)testRemoveDocument {
  120. if (!self.remoteDocumentCache) return;
  121. self.persistence.run("testRemoveDocument", [&]() {
  122. [self setTestDocumentAtPath:kDocPath];
  123. self.remoteDocumentCache->Remove(testutil::Key(kDocPath));
  124. XCTAssertNil(self.remoteDocumentCache->Get(testutil::Key(kDocPath)));
  125. });
  126. }
  127. - (void)testRemoveNonExistentDocument {
  128. if (!self.remoteDocumentCache) return;
  129. self.persistence.run("testRemoveNonExistentDocument", [&]() {
  130. // no-op, but make sure it doesn't throw.
  131. XCTAssertNoThrow(self.remoteDocumentCache->Remove(testutil::Key(kDocPath)));
  132. });
  133. }
  134. // TODO(mikelehen): Write more elaborate tests once we have more elaborate implementations.
  135. - (void)testDocumentsMatchingQuery {
  136. if (!self.remoteDocumentCache) return;
  137. self.persistence.run("testDocumentsMatchingQuery", [&]() {
  138. // TODO(rsgowman): This just verifies that we do a prefix scan against the
  139. // query path. We'll need more tests once we add index support.
  140. [self setTestDocumentAtPath:"a/1"];
  141. [self setTestDocumentAtPath:"b/1"];
  142. [self setTestDocumentAtPath:"b/1/z/1"];
  143. [self setTestDocumentAtPath:"b/2"];
  144. [self setTestDocumentAtPath:"c/1"];
  145. core::Query query = Query("b");
  146. DocumentMap results = self.remoteDocumentCache->GetMatching(query);
  147. [self expectMap:results.underlying_map()
  148. hasDocsInArray:@[
  149. FSTTestDoc("b/1", kVersion, _kDocData, DocumentState::kSynced),
  150. FSTTestDoc("b/2", kVersion, _kDocData, DocumentState::kSynced)
  151. ]
  152. exactly:YES];
  153. });
  154. }
  155. #pragma mark - Helpers
  156. - (FSTDocument *)setTestDocumentAtPath:(const absl::string_view)path {
  157. FSTDocument *doc = FSTTestDoc(path, kVersion, _kDocData, DocumentState::kSynced);
  158. self.remoteDocumentCache->Add(doc);
  159. return doc;
  160. }
  161. - (void)expectMap:(const MaybeDocumentMap &)map
  162. hasDocsInArray:(NSArray<FSTDocument *> *)expected
  163. exactly:(BOOL)exactly {
  164. if (exactly) {
  165. XCTAssertEqual(map.size(), [expected count]);
  166. }
  167. for (FSTDocument *doc in expected) {
  168. FSTDocument *actual = nil;
  169. auto found = map.find(doc.key);
  170. if (found != map.end()) {
  171. actual = static_cast<FSTDocument *>(found->second);
  172. }
  173. XCTAssertEqualObjects(actual, doc);
  174. }
  175. }
  176. @end
  177. NS_ASSUME_NONNULL_END