FSTAPIHelpers.mm 6.8 KB

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