Skip to content

Commit

Permalink
Firestore: FIRQueryTests.mm: Use a different Firestore instance to de…
Browse files Browse the repository at this point in the history
…lete documents instead of a transaction

This is a port of firebase/firebase-js-sdk#7415 and firebase/firebase-js-sdk#7486
  • Loading branch information
dconeybe committed Jul 26, 2023
1 parent 2cdbbb7 commit fdecbb0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 22 deletions.
29 changes: 13 additions & 16 deletions Firestore/Example/Tests/Integration/API/FIRQueryTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1231,25 +1231,22 @@ - (void)testResumingAQueryShouldUseBloomFilterToAvoidFullRequery {
NSArray<FIRDocumentReference *> *createdDocuments =
FIRDocumentReferenceArrayFromQuerySnapshot(querySnapshot1);

// Delete 50 of the 100 documents. Do this in a transaction, rather than
// [FIRDocumentReference deleteDocument], to avoid affecting the local cache.
// Delete 50 of the 100 documents. Use a different Firestore instance to avoid affecting the
// local cache.
NSSet<NSString *> *deletedDocumentIds;
{
FIRFirestore *db2 = [self firestore];
FIRWriteBatch *batch = [db2 batch];

NSMutableArray<NSString *> *deletedDocumentIdsAccumulator = [[NSMutableArray alloc] init];
XCTestExpectation *expectation = [self expectationWithDescription:@"DeleteTransaction"];
[collRef.firestore
runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **) {
for (decltype(createdDocuments.count) i = 0; i < createdDocuments.count; i += 2) {
FIRDocumentReference *documentToDelete = createdDocuments[i];
[transaction deleteDocument:documentToDelete];
[deletedDocumentIdsAccumulator addObject:documentToDelete.documentID];
}
return @"document deletion successful";
}
completion:^(id, NSError *) {
[expectation fulfill];
}];
[self awaitExpectation:expectation];
for (decltype(createdDocuments.count) i = 0; i < createdDocuments.count; i += 2) {
FIRDocumentReference *documentToDelete = [db2 documentWithPath:createdDocuments[i].path];
[batch deleteDocument:documentToDelete];
[deletedDocumentIdsAccumulator addObject:documentToDelete.documentID];
}

[self commitWriteBatch:batch];

deletedDocumentIds = [NSSet setWithArray:deletedDocumentIdsAccumulator];
}
XCTAssertEqual(deletedDocumentIds.count, 50u, @"deletedDocumentIds has the wrong size");
Expand Down
2 changes: 2 additions & 0 deletions Firestore/Example/Tests/Util/FSTIntegrationTestCase.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ extern "C" {
data:(NSDictionary<NSString *, id> *)data
fields:(NSArray<id> *)fields;

- (void)commitWriteBatch:(FIRWriteBatch *)batch;

- (void)disableNetwork;

- (void)enableNetwork;
Expand Down
17 changes: 11 additions & 6 deletions Firestore/Example/Tests/Util/FSTIntegrationTestCase.mm
Original file line number Diff line number Diff line change
Expand Up @@ -412,9 +412,7 @@ - (void)writeAllDocuments:(NSDictionary<NSString *, NSDictionary<NSString *, id>
XCTestExpectation *commitExpectation = [self expectationWithDescription:@"WriteBatch commit"];
[writeBatch commitWithCompletion:^(NSError *_Nullable error) {
[commitExpectation fulfill];
if (error != nil) {
XCTFail(@"WriteBatch commit failed: %@", error);
}
XCTAssertNil(error, @"WriteBatch commit failed: %@", error);
}];
[commits addObject:commitExpectation];
writeBatch = nil;
Expand All @@ -426,9 +424,7 @@ - (void)writeAllDocuments:(NSDictionary<NSString *, NSDictionary<NSString *, id>
XCTestExpectation *commitExpectation = [self expectationWithDescription:@"WriteBatch commit"];
[writeBatch commitWithCompletion:^(NSError *_Nullable error) {
[commitExpectation fulfill];
if (error != nil) {
XCTFail(@"WriteBatch commit failed: %@", error);
}
XCTAssertNil(error, @"WriteBatch commit failed: %@", error);
}];
[commits addObject:commitExpectation];
}
Expand Down Expand Up @@ -570,6 +566,15 @@ - (void)mergeDocumentRef:(FIRDocumentReference *)ref
[self awaitExpectation:expectation];
}

- (void)commitWriteBatch:(FIRWriteBatch *)batch {
XCTestExpectation *expectation = [self expectationWithDescription:@"WriteBatch commit"];
[batch commitWithCompletion:^(NSError *_Nullable error) {
[expectation fulfill];
XCTAssertNil(error, @"WriteBatch commit should have succeeded, but it failed: %@", error);
}];
[self awaitExpectation:expectation];
}

- (void)disableNetwork {
XCTestExpectation *expectation = [self expectationWithDescription:@"disableNetwork"];
[self.db disableNetworkWithCompletion:[self completionForExpectation:expectation]];
Expand Down

0 comments on commit fdecbb0

Please sign in to comment.