FSTAPIHelpers.mm 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 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. firebaseMetadataProvider:nullptr
  63. firebaseApp:nil
  64. instanceRegistry:nil];
  65. });
  66. #pragma clang diagnostic pop
  67. return sharedInstance;
  68. }
  69. FIRDocumentSnapshot *FSTTestDocSnapshot(const char *path,
  70. FSTTestSnapshotVersion version,
  71. NSDictionary<NSString *, id> *_Nullable data,
  72. BOOL hasMutations,
  73. BOOL fromCache) {
  74. absl::optional<Document> doc;
  75. if (data) {
  76. FSTUserDataConverter *converter = FSTTestUserDataConverter();
  77. FieldValue parsed = [converter parsedQueryValue:data];
  78. doc = Doc(path, version, parsed,
  79. hasMutations ? DocumentState::kLocalMutations : DocumentState::kSynced);
  80. }
  81. return [[FIRDocumentSnapshot alloc] initWithFirestore:FSTTestFirestore().wrapped
  82. documentKey:testutil::Key(path)
  83. document:doc
  84. fromCache:fromCache
  85. hasPendingWrites:hasMutations];
  86. }
  87. FIRCollectionReference *FSTTestCollectionRef(const char *path) {
  88. return [[FIRCollectionReference alloc] initWithPath:testutil::Resource(path)
  89. firestore:FSTTestFirestore().wrapped];
  90. }
  91. FIRDocumentReference *FSTTestDocRef(const char *path) {
  92. return [[FIRDocumentReference alloc] initWithPath:testutil::Resource(path)
  93. firestore:FSTTestFirestore().wrapped];
  94. }
  95. /** A convenience method for creating a query snapshots for tests. */
  96. FIRQuerySnapshot *FSTTestQuerySnapshot(
  97. const char *path,
  98. NSDictionary<NSString *, NSDictionary<NSString *, id> *> *oldDocs,
  99. NSDictionary<NSString *, NSDictionary<NSString *, id> *> *docsToAdd,
  100. BOOL hasPendingWrites,
  101. BOOL fromCache) {
  102. FSTUserDataConverter *converter = FSTTestUserDataConverter();
  103. SnapshotMetadata metadata(hasPendingWrites, fromCache);
  104. DocumentSet oldDocuments(DocumentComparator::ByKey());
  105. DocumentKeySet mutatedKeys;
  106. for (NSString *key in oldDocs) {
  107. FieldValue doc = [converter parsedQueryValue:oldDocs[key]];
  108. std::string documentKey = util::StringFormat("%s/%s", path, key);
  109. oldDocuments = oldDocuments.insert(
  110. Doc(documentKey, 1, doc,
  111. hasPendingWrites ? DocumentState::kLocalMutations : DocumentState::kSynced));
  112. if (hasPendingWrites) {
  113. mutatedKeys = mutatedKeys.insert(testutil::Key(documentKey));
  114. }
  115. }
  116. DocumentSet newDocuments = oldDocuments;
  117. std::vector<DocumentViewChange> documentChanges;
  118. for (NSString *key in docsToAdd) {
  119. FieldValue doc = [converter parsedQueryValue:docsToAdd[key]];
  120. std::string documentKey = util::StringFormat("%s/%s", path, key);
  121. Document docToAdd =
  122. Doc(documentKey, 1, doc,
  123. hasPendingWrites ? DocumentState::kLocalMutations : DocumentState::kSynced);
  124. newDocuments = newDocuments.insert(docToAdd);
  125. documentChanges.emplace_back(docToAdd, DocumentViewChange::Type::Added);
  126. if (hasPendingWrites) {
  127. mutatedKeys = mutatedKeys.insert(testutil::Key(documentKey));
  128. }
  129. }
  130. ViewSnapshot viewSnapshot{Query(path),
  131. newDocuments,
  132. oldDocuments,
  133. std::move(documentChanges),
  134. mutatedKeys,
  135. static_cast<bool>(fromCache),
  136. /*sync_state_changed=*/true,
  137. /*excludes_metadata_changes=*/false};
  138. return [[FIRQuerySnapshot alloc] initWithFirestore:FSTTestFirestore().wrapped
  139. originalQuery:Query(path)
  140. snapshot:std::move(viewSnapshot)
  141. metadata:std::move(metadata)];
  142. }
  143. NS_ASSUME_NONNULL_END