FSTAPIHelpers.mm 6.5 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/Core/FSTViewSnapshot.h"
  31. #import "Firestore/Source/Model/FSTDocument.h"
  32. #import "Firestore/Source/Model/FSTDocumentSet.h"
  33. #include "Firestore/core/src/firebase/firestore/core/view_snapshot.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::core::DocumentViewChange;
  39. using firebase::firestore::model::DocumentKeySet;
  40. NS_ASSUME_NONNULL_BEGIN
  41. FIRFirestore *FSTTestFirestore() {
  42. static FIRFirestore *sharedInstance = nil;
  43. static dispatch_once_t onceToken;
  44. #pragma clang diagnostic push
  45. #pragma clang diagnostic ignored "-Wnonnull"
  46. dispatch_once(&onceToken, ^{
  47. sharedInstance = [[FIRFirestore alloc] initWithProjectID:"abc"
  48. database:"abc"
  49. persistenceKey:@"db123"
  50. credentialsProvider:nil
  51. workerQueue:nil
  52. firebaseApp:nil];
  53. });
  54. #pragma clang diagnostic pop
  55. return sharedInstance;
  56. }
  57. FIRDocumentSnapshot *FSTTestDocSnapshot(const absl::string_view path,
  58. FSTTestSnapshotVersion version,
  59. NSDictionary<NSString *, id> *_Nullable data,
  60. BOOL hasMutations,
  61. BOOL fromCache) {
  62. FSTDocument *doc =
  63. data ? FSTTestDoc(path, version, data,
  64. hasMutations ? FSTDocumentStateLocalMutations : FSTDocumentStateSynced)
  65. : nil;
  66. return [FIRDocumentSnapshot snapshotWithFirestore:FSTTestFirestore()
  67. documentKey:testutil::Key(path)
  68. document:doc
  69. fromCache:fromCache
  70. hasPendingWrites:hasMutations];
  71. }
  72. FIRCollectionReference *FSTTestCollectionRef(const absl::string_view path) {
  73. return [FIRCollectionReference referenceWithPath:testutil::Resource(path)
  74. firestore:FSTTestFirestore()];
  75. }
  76. FIRDocumentReference *FSTTestDocRef(const absl::string_view path) {
  77. return [FIRDocumentReference referenceWithPath:testutil::Resource(path)
  78. firestore:FSTTestFirestore()];
  79. }
  80. /** A convenience method for creating a query snapshots for tests. */
  81. FIRQuerySnapshot *FSTTestQuerySnapshot(
  82. const absl::string_view path,
  83. NSDictionary<NSString *, NSDictionary<NSString *, id> *> *oldDocs,
  84. NSDictionary<NSString *, NSDictionary<NSString *, id> *> *docsToAdd,
  85. BOOL hasPendingWrites,
  86. BOOL fromCache) {
  87. FIRSnapshotMetadata *metadata =
  88. [FIRSnapshotMetadata snapshotMetadataWithPendingWrites:hasPendingWrites fromCache:fromCache];
  89. FSTDocumentSet *oldDocuments = FSTTestDocSet(FSTDocumentComparatorByKey, @[]);
  90. DocumentKeySet mutatedKeys;
  91. for (NSString *key in oldDocs) {
  92. oldDocuments = [oldDocuments
  93. documentSetByAddingDocument:FSTTestDoc(util::StringFormat("%s/%s", path, key), 1,
  94. oldDocs[key],
  95. hasPendingWrites ? FSTDocumentStateLocalMutations
  96. : 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. FSTDocumentSet *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 documentSetByAddingDocument: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. FSTViewSnapshot *viewSnapshot = [[FSTViewSnapshot alloc] initWithQuery:FSTTestQuery(path)
  116. documents:newDocuments
  117. oldDocuments:oldDocuments
  118. documentChanges:std::move(documentChanges)
  119. fromCache:fromCache
  120. mutatedKeys:mutatedKeys
  121. syncStateChanged:YES
  122. excludesMetadataChanges:NO];
  123. return [FIRQuerySnapshot snapshotWithFirestore:FSTTestFirestore()
  124. originalQuery:FSTTestQuery(path)
  125. snapshot:viewSnapshot
  126. metadata:metadata];
  127. }
  128. NS_ASSUME_NONNULL_END