FSTAPIHelpers.mm 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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/Source/API/FIRCollectionReference+Internal.h"
  24. #import "Firestore/Source/API/FIRDocumentReference+Internal.h"
  25. #import "Firestore/Source/API/FIRDocumentSnapshot+Internal.h"
  26. #import "Firestore/Source/API/FIRFirestore+Internal.h"
  27. #import "Firestore/Source/API/FIRQuerySnapshot+Internal.h"
  28. #import "Firestore/Source/API/FIRSnapshotMetadata+Internal.h"
  29. #import "Firestore/Source/Core/FSTQuery.h"
  30. #import "Firestore/Source/Model/FSTDocument.h"
  31. #include "Firestore/core/src/firebase/firestore/core/view_snapshot.h"
  32. #include "Firestore/core/src/firebase/firestore/model/document_set.h"
  33. #include "Firestore/core/src/firebase/firestore/util/string_apple.h"
  34. #include "Firestore/core/test/firebase/firestore/testutil/testutil.h"
  35. namespace testutil = firebase::firestore::testutil;
  36. namespace util = firebase::firestore::util;
  37. using firebase::firestore::api::SnapshotMetadata;
  38. using firebase::firestore::core::DocumentViewChange;
  39. using firebase::firestore::core::ViewSnapshot;
  40. using firebase::firestore::model::DocumentComparator;
  41. using firebase::firestore::model::DocumentKeySet;
  42. using firebase::firestore::model::DocumentSet;
  43. NS_ASSUME_NONNULL_BEGIN
  44. FIRFirestore *FSTTestFirestore() {
  45. static FIRFirestore *sharedInstance = nil;
  46. static dispatch_once_t onceToken;
  47. #pragma clang diagnostic push
  48. #pragma clang diagnostic ignored "-Wnonnull"
  49. dispatch_once(&onceToken, ^{
  50. sharedInstance = [[FIRFirestore alloc] initWithProjectID:"abc"
  51. database:"abc"
  52. persistenceKey:"db123"
  53. credentialsProvider:nullptr
  54. workerQueue:nullptr
  55. firebaseApp:nil];
  56. });
  57. #pragma clang diagnostic pop
  58. return sharedInstance;
  59. }
  60. FIRDocumentSnapshot *FSTTestDocSnapshot(const absl::string_view path,
  61. FSTTestSnapshotVersion version,
  62. NSDictionary<NSString *, id> *_Nullable data,
  63. BOOL hasMutations,
  64. BOOL fromCache) {
  65. FSTDocument *doc =
  66. data ? FSTTestDoc(path, version, data,
  67. hasMutations ? FSTDocumentStateLocalMutations : FSTDocumentStateSynced)
  68. : nil;
  69. return [[FIRDocumentSnapshot alloc] initWithFirestore:FSTTestFirestore().wrapped
  70. documentKey:testutil::Key(path)
  71. document:doc
  72. fromCache:fromCache
  73. hasPendingWrites:hasMutations];
  74. }
  75. FIRCollectionReference *FSTTestCollectionRef(const absl::string_view path) {
  76. return [FIRCollectionReference referenceWithPath:testutil::Resource(path)
  77. firestore:FSTTestFirestore()];
  78. }
  79. FIRDocumentReference *FSTTestDocRef(const absl::string_view path) {
  80. return [[FIRDocumentReference alloc] initWithPath:testutil::Resource(path)
  81. firestore:FSTTestFirestore().wrapped];
  82. }
  83. /** A convenience method for creating a query snapshots for tests. */
  84. FIRQuerySnapshot *FSTTestQuerySnapshot(
  85. const absl::string_view path,
  86. NSDictionary<NSString *, NSDictionary<NSString *, id> *> *oldDocs,
  87. NSDictionary<NSString *, NSDictionary<NSString *, id> *> *docsToAdd,
  88. bool hasPendingWrites,
  89. bool fromCache) {
  90. SnapshotMetadata metadata(hasPendingWrites, fromCache);
  91. DocumentSet oldDocuments = FSTTestDocSet(DocumentComparator::ByKey(), @[]);
  92. DocumentKeySet mutatedKeys;
  93. for (NSString *key in oldDocs) {
  94. oldDocuments = oldDocuments.insert(
  95. FSTTestDoc(util::StringFormat("%s/%s", path, key), 1, oldDocs[key],
  96. hasPendingWrites ? FSTDocumentStateLocalMutations : FSTDocumentStateSynced));
  97. if (hasPendingWrites) {
  98. const std::string documentKey = util::StringFormat("%s/%s", path, key);
  99. mutatedKeys = mutatedKeys.insert(testutil::Key(documentKey));
  100. }
  101. }
  102. DocumentSet newDocuments = oldDocuments;
  103. std::vector<DocumentViewChange> documentChanges;
  104. for (NSString *key in docsToAdd) {
  105. FSTDocument *docToAdd =
  106. FSTTestDoc(util::StringFormat("%s/%s", path, key), 1, docsToAdd[key],
  107. hasPendingWrites ? FSTDocumentStateLocalMutations : FSTDocumentStateSynced);
  108. newDocuments = newDocuments.insert(docToAdd);
  109. documentChanges.emplace_back(docToAdd, DocumentViewChange::Type::kAdded);
  110. if (hasPendingWrites) {
  111. const std::string documentKey = util::StringFormat("%s/%s", path, key);
  112. mutatedKeys = mutatedKeys.insert(testutil::Key(documentKey));
  113. }
  114. }
  115. ViewSnapshot viewSnapshot{FSTTestQuery(path),
  116. newDocuments,
  117. oldDocuments,
  118. std::move(documentChanges),
  119. mutatedKeys,
  120. fromCache,
  121. /*sync_state_changed=*/true,
  122. /*excludes_metadata_changes=*/false};
  123. return [[FIRQuerySnapshot alloc] initWithFirestore:FSTTestFirestore().wrapped
  124. originalQuery:FSTTestQuery(path)
  125. snapshot:std::move(viewSnapshot)
  126. metadata:std::move(metadata)];
  127. }
  128. NS_ASSUME_NONNULL_END