FSTAPIHelpers.mm 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. #import "Firestore/Source/Model/FSTPath.h"
  32. NS_ASSUME_NONNULL_BEGIN
  33. FIRFirestore *FSTTestFirestore() {
  34. static FIRFirestore *sharedInstance = nil;
  35. static dispatch_once_t onceToken;
  36. #pragma clang diagnostic push
  37. #pragma clang diagnostic ignored "-Wnonnull"
  38. dispatch_once(&onceToken, ^{
  39. sharedInstance = [[FIRFirestore alloc] initWithProjectID:"abc"
  40. database:"abc"
  41. persistenceKey:@"db123"
  42. credentialsProvider:nil
  43. workerDispatchQueue:nil
  44. firebaseApp:nil];
  45. });
  46. #pragma clang diagnostic pop
  47. return sharedInstance;
  48. }
  49. FIRDocumentSnapshot *FSTTestDocSnapshot(NSString *path,
  50. FSTTestSnapshotVersion version,
  51. NSDictionary<NSString *, id> *_Nullable data,
  52. BOOL hasMutations,
  53. BOOL fromCache) {
  54. FSTDocument *doc = data ? FSTTestDoc(path, version, data, hasMutations) : nil;
  55. return [FIRDocumentSnapshot snapshotWithFirestore:FSTTestFirestore()
  56. documentKey:FSTTestDocKey(path)
  57. document:doc
  58. fromCache:fromCache];
  59. }
  60. FIRCollectionReference *FSTTestCollectionRef(NSString *path) {
  61. return [FIRCollectionReference referenceWithPath:FSTTestPath(path) firestore:FSTTestFirestore()];
  62. }
  63. FIRDocumentReference *FSTTestDocRef(NSString *path) {
  64. return [FIRDocumentReference referenceWithPath:FSTTestPath(path) firestore:FSTTestFirestore()];
  65. }
  66. /** A convenience method for creating a query snapshots for tests. */
  67. FIRQuerySnapshot *FSTTestQuerySnapshot(
  68. NSString *path,
  69. NSDictionary<NSString *, NSDictionary<NSString *, id> *> *oldDocs,
  70. NSDictionary<NSString *, NSDictionary<NSString *, id> *> *docsToAdd,
  71. BOOL hasPendingWrites,
  72. BOOL fromCache) {
  73. FIRSnapshotMetadata *metadata =
  74. [FIRSnapshotMetadata snapshotMetadataWithPendingWrites:hasPendingWrites fromCache:fromCache];
  75. FSTDocumentSet *oldDocuments = FSTTestDocSet(FSTDocumentComparatorByKey, @[]);
  76. for (NSString *key in oldDocs) {
  77. oldDocuments = [oldDocuments
  78. documentSetByAddingDocument:FSTTestDoc([NSString stringWithFormat:@"%@/%@", path, key], 1,
  79. oldDocs[key], hasPendingWrites)];
  80. }
  81. FSTDocumentSet *newDocuments = oldDocuments;
  82. NSArray<FSTDocumentViewChange *> *documentChanges = [NSArray array];
  83. for (NSString *key in docsToAdd) {
  84. FSTDocument *docToAdd = FSTTestDoc([NSString stringWithFormat:@"%@/%@", path, key], 1,
  85. docsToAdd[key], hasPendingWrites);
  86. newDocuments = [newDocuments documentSetByAddingDocument:docToAdd];
  87. documentChanges = [documentChanges
  88. arrayByAddingObject:[FSTDocumentViewChange
  89. changeWithDocument:docToAdd
  90. type:FSTDocumentViewChangeTypeAdded]];
  91. }
  92. FSTViewSnapshot *viewSnapshot = [[FSTViewSnapshot alloc] initWithQuery:FSTTestQuery(path)
  93. documents:newDocuments
  94. oldDocuments:oldDocuments
  95. documentChanges:documentChanges
  96. fromCache:fromCache
  97. hasPendingWrites:hasPendingWrites
  98. syncStateChanged:YES];
  99. return [FIRQuerySnapshot snapshotWithFirestore:FSTTestFirestore()
  100. originalQuery:FSTTestQuery(path)
  101. snapshot:viewSnapshot
  102. metadata:metadata];
  103. }
  104. NS_ASSUME_NONNULL_END