FSTAPIHelpers.mm 6.1 KB

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