FSTAPIHelpers.mm 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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/API/FSTAPIHelpers.h"
  17. #import <FirebaseFirestore/FIRDocumentChange.h>
  18. #import <FirebaseFirestore/FIRDocumentReference.h>
  19. #import <FirebaseFirestore/FIRSnapshotMetadata.h>
  20. #include <string>
  21. #include <utility>
  22. #include <vector>
  23. #import "Firestore/Example/Tests/Util/FSTHelpers.h"
  24. #import "Firestore/Source/API/FIRCollectionReference+Internal.h"
  25. #import "Firestore/Source/API/FIRDocumentReference+Internal.h"
  26. #import "Firestore/Source/API/FIRDocumentSnapshot+Internal.h"
  27. #import "Firestore/Source/API/FIRFirestore+Internal.h"
  28. #import "Firestore/Source/API/FIRQuerySnapshot+Internal.h"
  29. #import "Firestore/Source/API/FIRSnapshotMetadata+Internal.h"
  30. #import "Firestore/Source/Core/FSTQuery.h"
  31. #import "Firestore/Source/Model/FSTDocument.h"
  32. #include "Firestore/core/src/firebase/firestore/core/view_snapshot.h"
  33. #include "Firestore/core/src/firebase/firestore/model/document.h"
  34. #include "Firestore/core/src/firebase/firestore/model/document_set.h"
  35. #include "Firestore/core/src/firebase/firestore/util/string_apple.h"
  36. #include "Firestore/core/test/firebase/firestore/testutil/testutil.h"
  37. namespace testutil = firebase::firestore::testutil;
  38. namespace util = firebase::firestore::util;
  39. using firebase::firestore::api::SnapshotMetadata;
  40. using firebase::firestore::core::DocumentViewChange;
  41. using firebase::firestore::core::ViewSnapshot;
  42. using firebase::firestore::model::DatabaseId;
  43. using firebase::firestore::model::DocumentComparator;
  44. using firebase::firestore::model::DocumentKeySet;
  45. using firebase::firestore::model::DocumentSet;
  46. using firebase::firestore::model::DocumentState;
  47. NS_ASSUME_NONNULL_BEGIN
  48. FIRFirestore *FSTTestFirestore() {
  49. static FIRFirestore *sharedInstance = nil;
  50. static dispatch_once_t onceToken;
  51. #pragma clang diagnostic push
  52. #pragma clang diagnostic ignored "-Wnonnull"
  53. dispatch_once(&onceToken, ^{
  54. sharedInstance = [[FIRFirestore alloc] initWithDatabaseID:DatabaseId("abc", "abc")
  55. persistenceKey:"db123"
  56. credentialsProvider:nullptr
  57. workerQueue:nullptr
  58. firebaseApp:nil
  59. instanceRegistry:nil];
  60. });
  61. #pragma clang diagnostic pop
  62. return sharedInstance;
  63. }
  64. FIRDocumentSnapshot *FSTTestDocSnapshot(const char *path,
  65. FSTTestSnapshotVersion version,
  66. NSDictionary<NSString *, id> *_Nullable data,
  67. BOOL hasMutations,
  68. BOOL fromCache) {
  69. FSTDocument *doc =
  70. data ? FSTTestDoc(path, version, data,
  71. hasMutations ? DocumentState::kLocalMutations : DocumentState::kSynced)
  72. : nil;
  73. return [[FIRDocumentSnapshot alloc] initWithFirestore:FSTTestFirestore().wrapped
  74. documentKey:testutil::Key(path)
  75. document:doc
  76. fromCache:fromCache
  77. hasPendingWrites:hasMutations];
  78. }
  79. FIRCollectionReference *FSTTestCollectionRef(const char *path) {
  80. return [[FIRCollectionReference alloc] initWithPath:testutil::Resource(path)
  81. firestore:FSTTestFirestore().wrapped];
  82. }
  83. FIRDocumentReference *FSTTestDocRef(const char *path) {
  84. return [[FIRDocumentReference alloc] initWithPath:testutil::Resource(path)
  85. firestore:FSTTestFirestore().wrapped];
  86. }
  87. /** A convenience method for creating a query snapshots for tests. */
  88. FIRQuerySnapshot *FSTTestQuerySnapshot(
  89. const char *path,
  90. NSDictionary<NSString *, NSDictionary<NSString *, id> *> *oldDocs,
  91. NSDictionary<NSString *, NSDictionary<NSString *, id> *> *docsToAdd,
  92. BOOL hasPendingWrites,
  93. BOOL fromCache) {
  94. SnapshotMetadata metadata(hasPendingWrites, fromCache);
  95. DocumentSet oldDocuments = FSTTestDocSet(DocumentComparator::ByKey(), @[]);
  96. DocumentKeySet mutatedKeys;
  97. for (NSString *key in oldDocs) {
  98. oldDocuments = oldDocuments.insert(
  99. FSTTestDoc(util::StringFormat("%s/%s", path, key), 1, oldDocs[key],
  100. hasPendingWrites ? DocumentState::kLocalMutations : DocumentState::kSynced));
  101. if (hasPendingWrites) {
  102. const std::string documentKey = util::StringFormat("%s/%s", path, key);
  103. mutatedKeys = mutatedKeys.insert(testutil::Key(documentKey));
  104. }
  105. }
  106. DocumentSet newDocuments = oldDocuments;
  107. std::vector<DocumentViewChange> documentChanges;
  108. for (NSString *key in docsToAdd) {
  109. FSTDocument *docToAdd =
  110. FSTTestDoc(util::StringFormat("%s/%s", path, key), 1, docsToAdd[key],
  111. hasPendingWrites ? DocumentState::kLocalMutations : DocumentState::kSynced);
  112. newDocuments = newDocuments.insert(docToAdd);
  113. documentChanges.emplace_back(docToAdd, DocumentViewChange::Type::kAdded);
  114. if (hasPendingWrites) {
  115. const std::string documentKey = util::StringFormat("%s/%s", path, key);
  116. mutatedKeys = mutatedKeys.insert(testutil::Key(documentKey));
  117. }
  118. }
  119. ViewSnapshot viewSnapshot{FSTTestQuery(path),
  120. newDocuments,
  121. oldDocuments,
  122. std::move(documentChanges),
  123. mutatedKeys,
  124. static_cast<bool>(fromCache),
  125. /*sync_state_changed=*/true,
  126. /*excludes_metadata_changes=*/false};
  127. return [[FIRQuerySnapshot alloc] initWithFirestore:FSTTestFirestore().wrapped
  128. originalQuery:FSTTestQuery(path)
  129. snapshot:std::move(viewSnapshot)
  130. metadata:std::move(metadata)];
  131. }
  132. NS_ASSUME_NONNULL_END