FSTAPIHelpers.mm 7.4 KB

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