FSTAPIHelpers.mm 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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/API/FSTUserDataReader.h"
  31. #include "Firestore/core/src/core/view_snapshot.h"
  32. #include "Firestore/core/src/model/document.h"
  33. #include "Firestore/core/src/model/document_set.h"
  34. #include "Firestore/core/src/model/mutable_document.h"
  35. #include "Firestore/core/src/nanopb/message.h"
  36. #include "Firestore/core/src/remote/firebase_metadata_provider.h"
  37. #include "Firestore/core/src/util/string_apple.h"
  38. #include "Firestore/core/test/unit/testutil/testutil.h"
  39. namespace testutil = firebase::firestore::testutil;
  40. namespace util = firebase::firestore::util;
  41. using firebase::firestore::api::SnapshotMetadata;
  42. using firebase::firestore::core::DocumentViewChange;
  43. using firebase::firestore::core::ViewSnapshot;
  44. using firebase::firestore::google_firestore_v1_Value;
  45. using firebase::firestore::model::DatabaseId;
  46. using firebase::firestore::model::Document;
  47. using firebase::firestore::model::DocumentComparator;
  48. using firebase::firestore::model::DocumentKeySet;
  49. using firebase::firestore::model::DocumentSet;
  50. using firebase::firestore::model::MutableDocument;
  51. using firebase::firestore::nanopb::Message;
  52. using testutil::Doc;
  53. using testutil::Query;
  54. NS_ASSUME_NONNULL_BEGIN
  55. FIRFirestore *FSTTestFirestore() {
  56. static FIRFirestore *sharedInstance = nil;
  57. static dispatch_once_t onceToken;
  58. #pragma clang diagnostic push
  59. #pragma clang diagnostic ignored "-Wnonnull"
  60. dispatch_once(&onceToken, ^{
  61. sharedInstance = [[FIRFirestore alloc] initWithDatabaseID:DatabaseId("abc", "abc")
  62. persistenceKey:"db123"
  63. authCredentialsProvider:nullptr
  64. appCheckCredentialsProvider:nullptr
  65. workerQueue:nullptr
  66. firebaseMetadataProvider:nullptr
  67. firebaseApp:nil
  68. instanceRegistry:nil];
  69. });
  70. #pragma clang diagnostic pop
  71. return sharedInstance;
  72. }
  73. FIRDocumentSnapshot *FSTTestDocSnapshot(const char *path,
  74. FSTTestSnapshotVersion version,
  75. NSDictionary<NSString *, id> *_Nullable data,
  76. BOOL hasMutations,
  77. BOOL fromCache) {
  78. absl::optional<MutableDocument> doc;
  79. if (data) {
  80. FSTUserDataReader *reader = FSTTestUserDataReader();
  81. Message<google_firestore_v1_Value> parsed = [reader parsedQueryValue:data];
  82. doc = Doc(path, version, std::move(parsed));
  83. if (hasMutations) doc->SetHasLocalMutations();
  84. }
  85. return [[FIRDocumentSnapshot alloc] initWithFirestore:FSTTestFirestore()
  86. documentKey:testutil::Key(path)
  87. document:doc
  88. fromCache:fromCache
  89. hasPendingWrites:hasMutations];
  90. }
  91. FIRCollectionReference *FSTTestCollectionRef(const char *path) {
  92. return [[FIRCollectionReference alloc] initWithPath:testutil::Resource(path)
  93. firestore:FSTTestFirestore().wrapped];
  94. }
  95. FIRDocumentReference *FSTTestDocRef(const char *path) {
  96. return [[FIRDocumentReference alloc] initWithPath:testutil::Resource(path)
  97. firestore:FSTTestFirestore().wrapped];
  98. }
  99. /** A convenience method for creating a query snapshots for tests. */
  100. FIRQuerySnapshot *FSTTestQuerySnapshot(
  101. const char *path,
  102. NSDictionary<NSString *, NSDictionary<NSString *, id> *> *oldDocs,
  103. NSDictionary<NSString *, NSDictionary<NSString *, id> *> *docsToAdd,
  104. BOOL hasPendingWrites,
  105. BOOL fromCache) {
  106. FSTUserDataReader *reader = FSTTestUserDataReader();
  107. SnapshotMetadata metadata(hasPendingWrites, fromCache);
  108. DocumentSet oldDocuments(DocumentComparator::ByKey());
  109. DocumentKeySet mutatedKeys;
  110. for (NSString *key in oldDocs) {
  111. Message<google_firestore_v1_Value> value = [reader parsedQueryValue:oldDocs[key]];
  112. std::string documentKey = util::StringFormat("%s/%s", path, key);
  113. MutableDocument doc = Doc(documentKey, 1, std::move(value));
  114. if (hasPendingWrites) {
  115. mutatedKeys = mutatedKeys.insert(testutil::Key(documentKey));
  116. doc.SetHasLocalMutations();
  117. }
  118. oldDocuments = oldDocuments.insert(doc);
  119. }
  120. DocumentSet newDocuments = oldDocuments;
  121. std::vector<DocumentViewChange> documentChanges;
  122. for (NSString *key in docsToAdd) {
  123. Message<google_firestore_v1_Value> value = [reader parsedQueryValue:docsToAdd[key]];
  124. std::string documentKey = util::StringFormat("%s/%s", path, key);
  125. MutableDocument doc = Doc(documentKey, 1, std::move(value));
  126. documentChanges.emplace_back(doc, DocumentViewChange::Type::Added);
  127. if (hasPendingWrites) {
  128. mutatedKeys = mutatedKeys.insert(testutil::Key(documentKey));
  129. doc.SetHasLocalMutations();
  130. }
  131. newDocuments = newDocuments.insert(doc);
  132. }
  133. ViewSnapshot viewSnapshot{Query(path),
  134. newDocuments,
  135. oldDocuments,
  136. std::move(documentChanges),
  137. mutatedKeys,
  138. static_cast<bool>(fromCache),
  139. /*sync_state_changed=*/true,
  140. /*excludes_metadata_changes=*/false};
  141. return [[FIRQuerySnapshot alloc] initWithFirestore:FSTTestFirestore().wrapped
  142. originalQuery:Query(path)
  143. snapshot:std::move(viewSnapshot)
  144. metadata:std::move(metadata)];
  145. }
  146. NS_ASSUME_NONNULL_END