FSTRemoteDocumentCacheTests.mm 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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)testReadDocumentNotInCache {
  41. if (!self.remoteDocumentCache) return;
  42. self.persistence.run("testReadDocumentNotInCache", [&]() {
  43. XCTAssertNil([self.remoteDocumentCache entryForKey:testutil::Key(kDocPath)]);
  44. });
  45. }
  46. // Helper for next two tests.
  47. - (void)setAndReadADocumentAtPath:(const absl::string_view)path {
  48. self.persistence.run("setAndReadADocumentAtPath", [&]() {
  49. FSTDocument *written = [self setTestDocumentAtPath:path];
  50. FSTMaybeDocument *read = [self.remoteDocumentCache entryForKey:testutil::Key(path)];
  51. XCTAssertEqualObjects(read, written);
  52. });
  53. }
  54. - (void)testSetAndReadADocument {
  55. if (!self.remoteDocumentCache) return;
  56. [self setAndReadADocumentAtPath:kDocPath];
  57. }
  58. - (void)testSetAndReadADocumentAtDeepPath {
  59. if (!self.remoteDocumentCache) return;
  60. [self setAndReadADocumentAtPath:kLongDocPath];
  61. }
  62. - (void)testSetAndReadDeletedDocument {
  63. if (!self.remoteDocumentCache) return;
  64. self.persistence.run("testSetAndReadDeletedDocument", [&]() {
  65. FSTDeletedDocument *deletedDoc = FSTTestDeletedDoc(kDocPath, kVersion);
  66. [self.remoteDocumentCache addEntry:deletedDoc];
  67. XCTAssertEqualObjects([self.remoteDocumentCache entryForKey:testutil::Key(kDocPath)],
  68. deletedDoc);
  69. });
  70. }
  71. - (void)testSetDocumentToNewValue {
  72. if (!self.remoteDocumentCache) return;
  73. self.persistence.run("testSetDocumentToNewValue", [&]() {
  74. [self setTestDocumentAtPath:kDocPath];
  75. FSTDocument *newDoc = FSTTestDoc(kDocPath, kVersion, @{ @"data" : @2 }, NO);
  76. [self.remoteDocumentCache addEntry:newDoc];
  77. XCTAssertEqualObjects([self.remoteDocumentCache entryForKey:testutil::Key(kDocPath)], newDoc);
  78. });
  79. }
  80. - (void)testRemoveDocument {
  81. if (!self.remoteDocumentCache) return;
  82. self.persistence.run("testRemoveDocument", [&]() {
  83. [self setTestDocumentAtPath:kDocPath];
  84. [self.remoteDocumentCache removeEntryForKey:testutil::Key(kDocPath)];
  85. XCTAssertNil([self.remoteDocumentCache entryForKey:testutil::Key(kDocPath)]);
  86. });
  87. }
  88. - (void)testRemoveNonExistentDocument {
  89. if (!self.remoteDocumentCache) return;
  90. self.persistence.run("testRemoveNonExistentDocument", [&]() {
  91. // no-op, but make sure it doesn't throw.
  92. XCTAssertNoThrow([self.remoteDocumentCache removeEntryForKey:testutil::Key(kDocPath)]);
  93. });
  94. }
  95. // TODO(mikelehen): Write more elaborate tests once we have more elaborate implementations.
  96. - (void)testDocumentsMatchingQuery {
  97. if (!self.remoteDocumentCache) return;
  98. self.persistence.run("testDocumentsMatchingQuery", [&]() {
  99. // TODO(rsgowman): This just verifies that we do a prefix scan against the
  100. // query path. We'll need more tests once we add index support.
  101. [self setTestDocumentAtPath:"a/1"];
  102. [self setTestDocumentAtPath:"b/1"];
  103. [self setTestDocumentAtPath:"b/2"];
  104. [self setTestDocumentAtPath:"c/1"];
  105. FSTQuery *query = FSTTestQuery("b");
  106. FSTDocumentDictionary *results = [self.remoteDocumentCache documentsMatchingQuery:query];
  107. NSArray *expected =
  108. @[ FSTTestDoc("b/1", kVersion, _kDocData, NO), FSTTestDoc("b/2", kVersion, _kDocData, NO) ];
  109. XCTAssertEqual([results count], [expected count]);
  110. for (FSTDocument *doc in expected) {
  111. XCTAssertEqualObjects([results objectForKey:doc.key], doc);
  112. }
  113. });
  114. }
  115. #pragma mark - Helpers
  116. // TODO(gsoltis): reevaluate if any of these helpers are still needed
  117. - (FSTDocument *)setTestDocumentAtPath:(const absl::string_view)path {
  118. FSTDocument *doc = FSTTestDoc(path, kVersion, _kDocData, NO);
  119. [self.remoteDocumentCache addEntry:doc];
  120. return doc;
  121. }
  122. @end
  123. NS_ASSUME_NONNULL_END