Skip to content

Commit 78b2fea

Browse files
authored
Firestore: FIRQueryTests.mm: Use a different Firestore instance to delete documents (#11615)
1 parent f4cf208 commit 78b2fea

File tree

5 files changed

+32
-53
lines changed

5 files changed

+32
-53
lines changed

Firestore/Example/Tests/Integration/API/FIRAggregateTests.mm

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -639,13 +639,7 @@ - (void)testCanRunAggregateCollectionGroupQuery {
639639
NSString* path = [NSString stringWithFormat:format, collectionGroup];
640640
[batch setData:@{@"x" : @2} forDocument:[self.db documentWithPath:path]];
641641
}
642-
643-
XCTestExpectation* expectation = [self expectationWithDescription:@"commit"];
644-
[batch commitWithCompletion:^(NSError* error) {
645-
XCTAssertNil(error);
646-
[expectation fulfill];
647-
}];
648-
[self awaitExpectation:expectation];
642+
[self commitWriteBatch:batch];
649643

650644
FIRAggregateQuerySnapshot* snapshot =
651645
[self readSnapshotForAggregate:[[self.db collectionGroupWithID:collectionGroup] aggregate:@[

Firestore/Example/Tests/Integration/API/FIRCountTests.mm

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,7 @@ - (void)testCanRunCollectionGroupCountQuery {
163163
[batch setData:@{@"x" : @"a"} forDocument:[self.db documentWithPath:path]];
164164
}
165165

166-
XCTestExpectation* expectation = [self expectationWithDescription:@"commit"];
167-
[batch commitWithCompletion:^(NSError* error) {
168-
XCTAssertNil(error);
169-
[expectation fulfill];
170-
}];
171-
[self awaitExpectation:expectation];
166+
[self commitWriteBatch:batch];
172167

173168
FIRAggregateQuerySnapshot* snapshot =
174169
[self readSnapshotForAggregate:[[self.db collectionGroupWithID:collectionGroup] count]];

Firestore/Example/Tests/Integration/API/FIRQueryTests.mm

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -790,12 +790,7 @@ - (void)testCollectionGroupQueries {
790790
withString:collectionGroup];
791791
[batch setData:@{@"x" : @1} forDocument:[self.db documentWithPath:path]];
792792
}
793-
XCTestExpectation *expectation = [self expectationWithDescription:@"batch written"];
794-
[batch commitWithCompletion:^(NSError *error) {
795-
XCTAssertNil(error);
796-
[expectation fulfill];
797-
}];
798-
[self awaitExpectations];
793+
[self commitWriteBatch:batch];
799794

800795
FIRQuerySnapshot *querySnapshot =
801796
[self readDocumentSetForRef:[self.db collectionGroupWithID:collectionGroup]];
@@ -821,12 +816,7 @@ - (void)testCollectionGroupQueriesWithStartAtEndAtWithArbitraryDocumentIDs {
821816
withString:collectionGroup];
822817
[batch setData:@{@"x" : @1} forDocument:[self.db documentWithPath:path]];
823818
}
824-
XCTestExpectation *expectation = [self expectationWithDescription:@"batch written"];
825-
[batch commitWithCompletion:^(NSError *error) {
826-
XCTAssertNil(error);
827-
[expectation fulfill];
828-
}];
829-
[self awaitExpectations];
819+
[self commitWriteBatch:batch];
830820

831821
FIRQuerySnapshot *querySnapshot = [self
832822
readDocumentSetForRef:[[[[self.db collectionGroupWithID:collectionGroup]
@@ -858,12 +848,7 @@ - (void)testCollectionGroupQueriesWithWhereFiltersOnArbitraryDocumentIDs {
858848
withString:collectionGroup];
859849
[batch setData:@{@"x" : @1} forDocument:[self.db documentWithPath:path]];
860850
}
861-
XCTestExpectation *expectation = [self expectationWithDescription:@"batch written"];
862-
[batch commitWithCompletion:^(NSError *error) {
863-
XCTAssertNil(error);
864-
[expectation fulfill];
865-
}];
866-
[self awaitExpectations];
851+
[self commitWriteBatch:batch];
867852

868853
FIRQuerySnapshot *querySnapshot = [self
869854
readDocumentSetForRef:[[[self.db collectionGroupWithID:collectionGroup]
@@ -1231,25 +1216,22 @@ - (void)testResumingAQueryShouldUseBloomFilterToAvoidFullRequery {
12311216
NSArray<FIRDocumentReference *> *createdDocuments =
12321217
FIRDocumentReferenceArrayFromQuerySnapshot(querySnapshot1);
12331218

1234-
// Delete 50 of the 100 documents. Do this in a transaction, rather than
1235-
// [FIRDocumentReference deleteDocument], to avoid affecting the local cache.
1219+
// Delete 50 of the 100 documents. Use a different Firestore instance to avoid affecting the
1220+
// local cache.
12361221
NSSet<NSString *> *deletedDocumentIds;
12371222
{
1223+
FIRFirestore *db2 = [self firestore];
1224+
FIRWriteBatch *batch = [db2 batch];
1225+
12381226
NSMutableArray<NSString *> *deletedDocumentIdsAccumulator = [[NSMutableArray alloc] init];
1239-
XCTestExpectation *expectation = [self expectationWithDescription:@"DeleteTransaction"];
1240-
[collRef.firestore
1241-
runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **) {
1242-
for (decltype(createdDocuments.count) i = 0; i < createdDocuments.count; i += 2) {
1243-
FIRDocumentReference *documentToDelete = createdDocuments[i];
1244-
[transaction deleteDocument:documentToDelete];
1245-
[deletedDocumentIdsAccumulator addObject:documentToDelete.documentID];
1246-
}
1247-
return @"document deletion successful";
1248-
}
1249-
completion:^(id, NSError *) {
1250-
[expectation fulfill];
1251-
}];
1252-
[self awaitExpectation:expectation];
1227+
for (decltype(createdDocuments.count) i = 0; i < createdDocuments.count; i += 2) {
1228+
FIRDocumentReference *documentToDelete = [db2 documentWithPath:createdDocuments[i].path];
1229+
[batch deleteDocument:documentToDelete];
1230+
[deletedDocumentIdsAccumulator addObject:documentToDelete.documentID];
1231+
}
1232+
1233+
[self commitWriteBatch:batch];
1234+
12531235
deletedDocumentIds = [NSSet setWithArray:deletedDocumentIdsAccumulator];
12541236
}
12551237
XCTAssertEqual(deletedDocumentIds.count, 50u, @"deletedDocumentIds has the wrong size");

Firestore/Example/Tests/Util/FSTIntegrationTestCase.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
@class FIRFirestore;
3434
@class FIRFirestoreSettings;
3535
@class FIRQuery;
36+
@class FIRWriteBatch;
3637
@class FSTEventAccumulator;
3738

3839
NS_ASSUME_NONNULL_BEGIN
@@ -115,6 +116,8 @@ extern "C" {
115116
data:(NSDictionary<NSString *, id> *)data
116117
fields:(NSArray<id> *)fields;
117118

119+
- (void)commitWriteBatch:(FIRWriteBatch *)batch;
120+
118121
- (void)disableNetwork;
119122

120123
- (void)enableNetwork;

Firestore/Example/Tests/Util/FSTIntegrationTestCase.mm

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -412,9 +412,7 @@ - (void)writeAllDocuments:(NSDictionary<NSString *, NSDictionary<NSString *, id>
412412
XCTestExpectation *commitExpectation = [self expectationWithDescription:@"WriteBatch commit"];
413413
[writeBatch commitWithCompletion:^(NSError *_Nullable error) {
414414
[commitExpectation fulfill];
415-
if (error != nil) {
416-
XCTFail(@"WriteBatch commit failed: %@", error);
417-
}
415+
XCTAssertNil(error, @"WriteBatch commit failed: %@", error);
418416
}];
419417
[commits addObject:commitExpectation];
420418
writeBatch = nil;
@@ -426,9 +424,7 @@ - (void)writeAllDocuments:(NSDictionary<NSString *, NSDictionary<NSString *, id>
426424
XCTestExpectation *commitExpectation = [self expectationWithDescription:@"WriteBatch commit"];
427425
[writeBatch commitWithCompletion:^(NSError *_Nullable error) {
428426
[commitExpectation fulfill];
429-
if (error != nil) {
430-
XCTFail(@"WriteBatch commit failed: %@", error);
431-
}
427+
XCTAssertNil(error, @"WriteBatch commit failed: %@", error);
432428
}];
433429
[commits addObject:commitExpectation];
434430
}
@@ -570,6 +566,15 @@ - (void)mergeDocumentRef:(FIRDocumentReference *)ref
570566
[self awaitExpectation:expectation];
571567
}
572568

569+
- (void)commitWriteBatch:(FIRWriteBatch *)batch {
570+
XCTestExpectation *expectation = [self expectationWithDescription:@"WriteBatch commit"];
571+
[batch commitWithCompletion:^(NSError *_Nullable error) {
572+
[expectation fulfill];
573+
XCTAssertNil(error, @"WriteBatch commit should have succeeded, but it failed: %@", error);
574+
}];
575+
[self awaitExpectation:expectation];
576+
}
577+
573578
- (void)disableNetwork {
574579
XCTestExpectation *expectation = [self expectationWithDescription:@"disableNetwork"];
575580
[self.db disableNetworkWithCompletion:[self completionForExpectation:expectation]];

0 commit comments

Comments
 (0)