FSTRemoteDocumentCacheTests.mm 7.2 KB

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