FSTRemoteDocumentCacheTests.mm 5.2 KB

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