FSTRemoteDocumentCacheTests.mm 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. #include <vector>
  19. #import "Firestore/Source/Local/FSTPersistence.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::Document;
  34. using firebase::firestore::model::DocumentKey;
  35. using firebase::firestore::model::DocumentKeySet;
  36. using firebase::firestore::model::DocumentMap;
  37. using firebase::firestore::model::DocumentState;
  38. using firebase::firestore::model::FieldValue;
  39. using firebase::firestore::model::MaybeDocument;
  40. using firebase::firestore::model::MaybeDocumentMap;
  41. using firebase::firestore::model::NoDocument;
  42. using firebase::firestore::model::OptionalMaybeDocumentMap;
  43. using testutil::DeletedDoc;
  44. using testutil::Doc;
  45. using testutil::Map;
  46. using testutil::Query;
  47. NS_ASSUME_NONNULL_BEGIN
  48. static const char *kDocPath = "a/b";
  49. static const char *kLongDocPath = "a/b/c/d/e/f";
  50. static const int kVersion = 42;
  51. void ExpectMapHasDocs(XCTestCase *self,
  52. const MaybeDocumentMap &map,
  53. const std::vector<Document> &expected,
  54. bool exactly) {
  55. if (exactly) {
  56. XCTAssertEqual(map.size(), expected.size());
  57. }
  58. for (const Document &doc : expected) {
  59. absl::optional<MaybeDocument> actual = map.get(doc.key());
  60. XCTAssertTrue(actual.has_value());
  61. XCTAssertEqual(*actual, doc);
  62. }
  63. }
  64. void ExpectMapHasDocs(XCTestCase *self,
  65. const OptionalMaybeDocumentMap &map,
  66. const std::vector<Document> &expected,
  67. bool exactly) {
  68. if (exactly) {
  69. XCTAssertEqual(map.size(), expected.size());
  70. }
  71. for (const Document &doc : expected) {
  72. absl::optional<absl::optional<MaybeDocument>> actual = map.get(doc.key());
  73. XCTAssertTrue(actual.has_value());
  74. XCTAssertEqual(**actual, doc);
  75. }
  76. }
  77. @implementation FSTRemoteDocumentCacheTests {
  78. FieldValue::Map _kDocData;
  79. }
  80. - (void)setUp {
  81. [super setUp];
  82. // essentially a constant, but can't be a compile-time one.
  83. _kDocData = Map("a", 1, "b", 2);
  84. }
  85. - (void)tearDown {
  86. [self.persistence shutdown];
  87. }
  88. - (void)testReadDocumentNotInCache {
  89. if (!self.remoteDocumentCache) return;
  90. self.persistence.run("testReadDocumentNotInCache", [&] {
  91. XCTAssertEqual(absl::nullopt, self.remoteDocumentCache->Get(testutil::Key(kDocPath)));
  92. });
  93. }
  94. // Helper for next two tests.
  95. - (void)setAndReadADocumentAtPath:(const absl::string_view)path {
  96. self.persistence.run("setAndReadADocumentAtPath", [&] {
  97. Document written = [self setTestDocumentAtPath:path];
  98. absl::optional<MaybeDocument> read = self.remoteDocumentCache->Get(testutil::Key(path));
  99. XCTAssertEqual(*read, written);
  100. });
  101. }
  102. - (void)testSetAndReadADocument {
  103. if (!self.remoteDocumentCache) return;
  104. [self setAndReadADocumentAtPath:kDocPath];
  105. }
  106. - (void)testSetAndReadSeveralDocuments {
  107. if (!self.remoteDocumentCache) return;
  108. self.persistence.run("testSetAndReadSeveralDocuments", [=] {
  109. std::vector<Document> written = {
  110. [self setTestDocumentAtPath:kDocPath],
  111. [self setTestDocumentAtPath:kLongDocPath],
  112. };
  113. OptionalMaybeDocumentMap read = self.remoteDocumentCache->GetAll(
  114. DocumentKeySet{testutil::Key(kDocPath), testutil::Key(kLongDocPath)});
  115. ExpectMapHasDocs(self, read, written, /* exactly= */ true);
  116. });
  117. }
  118. - (void)testSetAndReadSeveralDocumentsIncludingMissingDocument {
  119. if (!self.remoteDocumentCache) return;
  120. self.persistence.run("testSetAndReadSeveralDocumentsIncludingMissingDocument", [=] {
  121. std::vector<Document> written = {
  122. [self setTestDocumentAtPath:kDocPath],
  123. [self setTestDocumentAtPath:kLongDocPath],
  124. };
  125. OptionalMaybeDocumentMap read = self.remoteDocumentCache->GetAll(DocumentKeySet{
  126. testutil::Key(kDocPath),
  127. testutil::Key(kLongDocPath),
  128. testutil::Key("foo/nonexistent"),
  129. });
  130. ExpectMapHasDocs(self, read, written, /* exactly= */ false);
  131. auto found = read.find(DocumentKey::FromPathString("foo/nonexistent"));
  132. XCTAssertTrue(found != read.end());
  133. XCTAssertEqual(absl::nullopt, found->second);
  134. });
  135. }
  136. - (void)testSetAndReadADocumentAtDeepPath {
  137. if (!self.remoteDocumentCache) return;
  138. [self setAndReadADocumentAtPath:kLongDocPath];
  139. }
  140. - (void)testSetAndReadDeletedDocument {
  141. if (!self.remoteDocumentCache) return;
  142. self.persistence.run("testSetAndReadDeletedDocument", [&] {
  143. absl::optional<MaybeDocument> deletedDoc = DeletedDoc(kDocPath, kVersion);
  144. self.remoteDocumentCache->Add(*deletedDoc);
  145. XCTAssertEqual(self.remoteDocumentCache->Get(testutil::Key(kDocPath)), deletedDoc);
  146. });
  147. }
  148. - (void)testSetDocumentToNewValue {
  149. if (!self.remoteDocumentCache) return;
  150. self.persistence.run("testSetDocumentToNewValue", [&] {
  151. [self setTestDocumentAtPath:kDocPath];
  152. absl::optional<MaybeDocument> newDoc = Doc(kDocPath, kVersion, Map("data", 2));
  153. self.remoteDocumentCache->Add(*newDoc);
  154. XCTAssertEqual(self.remoteDocumentCache->Get(testutil::Key(kDocPath)), newDoc);
  155. });
  156. }
  157. - (void)testRemoveDocument {
  158. if (!self.remoteDocumentCache) return;
  159. self.persistence.run("testRemoveDocument", [&] {
  160. [self setTestDocumentAtPath:kDocPath];
  161. self.remoteDocumentCache->Remove(testutil::Key(kDocPath));
  162. XCTAssertEqual(self.remoteDocumentCache->Get(testutil::Key(kDocPath)), absl::nullopt);
  163. });
  164. }
  165. - (void)testRemoveNonExistentDocument {
  166. if (!self.remoteDocumentCache) return;
  167. self.persistence.run("testRemoveNonExistentDocument", [&] {
  168. // no-op, but make sure it doesn't throw.
  169. XCTAssertNoThrow(self.remoteDocumentCache->Remove(testutil::Key(kDocPath)));
  170. });
  171. }
  172. // TODO(mikelehen): Write more elaborate tests once we have more elaborate implementations.
  173. - (void)testDocumentsMatchingQuery {
  174. if (!self.remoteDocumentCache) return;
  175. self.persistence.run("testDocumentsMatchingQuery", [&] {
  176. // TODO(rsgowman): This just verifies that we do a prefix scan against the
  177. // query path. We'll need more tests once we add index support.
  178. [self setTestDocumentAtPath:"a/1"];
  179. [self setTestDocumentAtPath:"b/1"];
  180. [self setTestDocumentAtPath:"b/1/z/1"];
  181. [self setTestDocumentAtPath:"b/2"];
  182. [self setTestDocumentAtPath:"c/1"];
  183. core::Query query = Query("b");
  184. DocumentMap results = self.remoteDocumentCache->GetMatching(query);
  185. std::vector<Document> docs = {
  186. Doc("b/1", kVersion, _kDocData),
  187. Doc("b/2", kVersion, _kDocData),
  188. };
  189. ExpectMapHasDocs(self, results.underlying_map(), docs, /* exactly= */ true);
  190. });
  191. }
  192. #pragma mark - Helpers
  193. - (Document)setTestDocumentAtPath:(const absl::string_view)path {
  194. Document doc = Doc(path, kVersion, _kDocData);
  195. self.remoteDocumentCache->Add(doc);
  196. return doc;
  197. }
  198. @end
  199. NS_ASSUME_NONNULL_END