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