FSTRemoteDocumentCacheTests.mm 7.1 KB

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