FSTAPIHelpers.mm 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. #import "Firestore/Source/API/FIRCollectionReference+Internal.h"
  21. #import "Firestore/Source/API/FIRDocumentReference+Internal.h"
  22. #import "Firestore/Source/API/FIRDocumentSnapshot+Internal.h"
  23. #import "Firestore/Source/API/FIRFirestore+Internal.h"
  24. #import "Firestore/Source/API/FIRQuerySnapshot+Internal.h"
  25. #import "Firestore/Source/API/FIRSnapshotMetadata+Internal.h"
  26. #import "Firestore/Source/Core/FSTQuery.h"
  27. #import "Firestore/Source/Core/FSTViewSnapshot.h"
  28. #import "Firestore/Source/Model/FSTDocument.h"
  29. #import "Firestore/Source/Model/FSTDocumentKey.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. workerDispatchQueue: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 = data ? FSTTestDoc(path, version, data, hasMutations) : nil;
  58. return [FIRDocumentSnapshot snapshotWithFirestore:FSTTestFirestore()
  59. documentKey:testutil::Key(path)
  60. document:doc
  61. fromCache:fromCache];
  62. }
  63. FIRCollectionReference *FSTTestCollectionRef(const absl::string_view path) {
  64. return [FIRCollectionReference referenceWithPath:testutil::Resource(path)
  65. firestore:FSTTestFirestore()];
  66. }
  67. FIRDocumentReference *FSTTestDocRef(const absl::string_view path) {
  68. return [FIRDocumentReference referenceWithPath:testutil::Resource(path)
  69. firestore:FSTTestFirestore()];
  70. }
  71. /** A convenience method for creating a query snapshots for tests. */
  72. FIRQuerySnapshot *FSTTestQuerySnapshot(
  73. const absl::string_view path,
  74. NSDictionary<NSString *, NSDictionary<NSString *, id> *> *oldDocs,
  75. NSDictionary<NSString *, NSDictionary<NSString *, id> *> *docsToAdd,
  76. BOOL hasPendingWrites,
  77. BOOL fromCache) {
  78. FIRSnapshotMetadata *metadata =
  79. [FIRSnapshotMetadata snapshotMetadataWithPendingWrites:hasPendingWrites fromCache:fromCache];
  80. FSTDocumentSet *oldDocuments = FSTTestDocSet(FSTDocumentComparatorByKey, @[]);
  81. for (NSString *key in oldDocs) {
  82. oldDocuments = [oldDocuments
  83. documentSetByAddingDocument:FSTTestDoc(util::MakeStringView([NSString
  84. stringWithFormat:@"%s/%@", path.data(), key]),
  85. 1, oldDocs[key], hasPendingWrites)];
  86. }
  87. FSTDocumentSet *newDocuments = oldDocuments;
  88. NSArray<FSTDocumentViewChange *> *documentChanges = [NSArray array];
  89. for (NSString *key in docsToAdd) {
  90. FSTDocument *docToAdd =
  91. FSTTestDoc(util::MakeStringView([NSString stringWithFormat:@"%s/%@", path.data(), key]), 1,
  92. docsToAdd[key], hasPendingWrites);
  93. newDocuments = [newDocuments documentSetByAddingDocument:docToAdd];
  94. documentChanges = [documentChanges
  95. arrayByAddingObject:[FSTDocumentViewChange
  96. changeWithDocument:docToAdd
  97. type:FSTDocumentViewChangeTypeAdded]];
  98. }
  99. FSTViewSnapshot *viewSnapshot = [[FSTViewSnapshot alloc] initWithQuery:FSTTestQuery(path)
  100. documents:newDocuments
  101. oldDocuments:oldDocuments
  102. documentChanges:documentChanges
  103. fromCache:fromCache
  104. hasPendingWrites:hasPendingWrites
  105. syncStateChanged:YES];
  106. return [FIRQuerySnapshot snapshotWithFirestore:FSTTestFirestore()
  107. originalQuery:FSTTestQuery(path)
  108. snapshot:viewSnapshot
  109. metadata:metadata];
  110. }
  111. NS_ASSUME_NONNULL_END