FSTAPIHelpers.m 5.3 KB

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