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