Skip to content

Firestore: FIRQueryTests.mm: Use a different Firestore instance to delete documents instead of a transaction #11615

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions Firestore/Example/Tests/Integration/API/FIRAggregateTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -639,13 +639,7 @@ - (void)testCanRunAggregateCollectionGroupQuery {
NSString* path = [NSString stringWithFormat:format, collectionGroup];
[batch setData:@{@"x" : @2} forDocument:[self.db documentWithPath:path]];
}

XCTestExpectation* expectation = [self expectationWithDescription:@"commit"];
[batch commitWithCompletion:^(NSError* error) {
XCTAssertNil(error);
[expectation fulfill];
}];
[self awaitExpectation:expectation];
[self commitWriteBatch:batch];

FIRAggregateQuerySnapshot* snapshot =
[self readSnapshotForAggregate:[[self.db collectionGroupWithID:collectionGroup] aggregate:@[
Expand Down
7 changes: 1 addition & 6 deletions Firestore/Example/Tests/Integration/API/FIRCountTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,7 @@ - (void)testCanRunCollectionGroupCountQuery {
[batch setData:@{@"x" : @"a"} forDocument:[self.db documentWithPath:path]];
}

XCTestExpectation* expectation = [self expectationWithDescription:@"commit"];
[batch commitWithCompletion:^(NSError* error) {
XCTAssertNil(error);
[expectation fulfill];
}];
[self awaitExpectation:expectation];
[self commitWriteBatch:batch];

FIRAggregateQuerySnapshot* snapshot =
[self readSnapshotForAggregate:[[self.db collectionGroupWithID:collectionGroup] count]];
Expand Down
50 changes: 16 additions & 34 deletions Firestore/Example/Tests/Integration/API/FIRQueryTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -790,12 +790,7 @@ - (void)testCollectionGroupQueries {
withString:collectionGroup];
[batch setData:@{@"x" : @1} forDocument:[self.db documentWithPath:path]];
}
XCTestExpectation *expectation = [self expectationWithDescription:@"batch written"];
[batch commitWithCompletion:^(NSError *error) {
XCTAssertNil(error);
[expectation fulfill];
}];
[self awaitExpectations];
[self commitWriteBatch:batch];

FIRQuerySnapshot *querySnapshot =
[self readDocumentSetForRef:[self.db collectionGroupWithID:collectionGroup]];
Expand All @@ -821,12 +816,7 @@ - (void)testCollectionGroupQueriesWithStartAtEndAtWithArbitraryDocumentIDs {
withString:collectionGroup];
[batch setData:@{@"x" : @1} forDocument:[self.db documentWithPath:path]];
}
XCTestExpectation *expectation = [self expectationWithDescription:@"batch written"];
[batch commitWithCompletion:^(NSError *error) {
XCTAssertNil(error);
[expectation fulfill];
}];
[self awaitExpectations];
[self commitWriteBatch:batch];

FIRQuerySnapshot *querySnapshot = [self
readDocumentSetForRef:[[[[self.db collectionGroupWithID:collectionGroup]
Expand Down Expand Up @@ -858,12 +848,7 @@ - (void)testCollectionGroupQueriesWithWhereFiltersOnArbitraryDocumentIDs {
withString:collectionGroup];
[batch setData:@{@"x" : @1} forDocument:[self.db documentWithPath:path]];
}
XCTestExpectation *expectation = [self expectationWithDescription:@"batch written"];
[batch commitWithCompletion:^(NSError *error) {
XCTAssertNil(error);
[expectation fulfill];
}];
[self awaitExpectations];
[self commitWriteBatch:batch];

FIRQuerySnapshot *querySnapshot = [self
readDocumentSetForRef:[[[self.db collectionGroupWithID:collectionGroup]
Expand Down Expand Up @@ -1231,25 +1216,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
3 changes: 3 additions & 0 deletions Firestore/Example/Tests/Util/FSTIntegrationTestCase.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
@class FIRFirestore;
@class FIRFirestoreSettings;
@class FIRQuery;
@class FIRWriteBatch;
@class FSTEventAccumulator;

NS_ASSUME_NONNULL_BEGIN
Expand Down Expand Up @@ -115,6 +116,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