FSTAPIHelpers.mm 6.7 KB

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