Skip to content

Commit

Permalink
Move sum&avg composite index tests to FIRCompositeIndexQueryTests.mm (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
milaGGL authored Oct 25, 2023
1 parent 8872dbd commit 8881200
Show file tree
Hide file tree
Showing 5 changed files with 333 additions and 235 deletions.
225 changes: 0 additions & 225 deletions Firestore/Example/Tests/Integration/API/FIRAggregateTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -469,61 +469,6 @@ - (void)testTerminateDoesNotCrashWithFlyingAggregateQuery {
[NSNumber numberWithLong:150L], );
}

- (void)testCanPerformMaxAggregations {
XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator],
"Skip this test if running against production because it requires a composite index.");
FIRCollectionReference* testCollection = [self collectionRefWithDocuments:@{
@"a" : @{
@"author" : @"authorA",
@"title" : @"titleA",
@"pages" : @100,
@"height" : @24.5,
@"weight" : @24.1,
@"foo" : @1,
@"bar" : @2,
@"baz" : @3
},
@"b" : @{
@"author" : @"authorB",
@"title" : @"titleB",
@"pages" : @50,
@"height" : @25.5,
@"weight" : @75.5,
@"foo" : @1,
@"bar" : @2,
@"baz" : @3
}
}];

// Max is 5, do not exceed
FIRAggregateQuerySnapshot* snapshot =
[self readSnapshotForAggregate:[testCollection aggregate:@[
[FIRAggregateField aggregateFieldForCount],
[FIRAggregateField aggregateFieldForSumOfField:@"pages"],
[FIRAggregateField aggregateFieldForSumOfField:@"weight"],
[FIRAggregateField aggregateFieldForAverageOfField:@"pages"],
[FIRAggregateField aggregateFieldForAverageOfField:@"weight"]
]]];

// Assert
XCTAssertEqual([snapshot valueForAggregateField:[FIRAggregateField aggregateFieldForCount]],
[NSNumber numberWithLong:2L]);
XCTAssertEqual(
[snapshot valueForAggregateField:[FIRAggregateField aggregateFieldForSumOfField:@"pages"]],
[NSNumber numberWithLong:150L], );
XCTAssertEqual(
[[snapshot valueForAggregateField:[FIRAggregateField aggregateFieldForSumOfField:@"weight"]]
doubleValue],
99.6);
XCTAssertEqual([snapshot valueForAggregateField:[FIRAggregateField
aggregateFieldForAverageOfField:@"pages"]],
[NSNumber numberWithDouble:75.0]);
XCTAssertEqual([[snapshot valueForAggregateField:[FIRAggregateField
aggregateFieldForAverageOfField:@"weight"]]
doubleValue],
49.8);
}

- (void)testCannotPerformMoreThanMaxAggregations {
FIRCollectionReference* testCollection = [self collectionRefWithDocuments:@{
@"a" : @{
Expand Down Expand Up @@ -574,105 +519,6 @@ - (void)testCannotPerformMoreThanMaxAggregations {
XCTAssertTrue([[result localizedDescription] containsString:@"maximum number of aggregations"]);
}

- (void)testCanRunAggregateCollectionGroupQuery {
XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator],
"Skip this test if running against production because it requires a composite index.");

NSString* collectionGroup =
[NSString stringWithFormat:@"%@%@", @"b",
[self.db collectionWithPath:@"foo"].documentWithAutoID.documentID];
NSArray* docPathFormats = @[
@"abc/123/%@/cg-doc1", @"abc/123/%@/cg-doc2", @"%@/cg-doc3", @"%@/cg-doc4",
@"def/456/%@/cg-doc5", @"%@/virtual-doc/nested-coll/not-cg-doc", @"x%@/not-cg-doc",
@"%@x/not-cg-doc", @"abc/123/%@x/not-cg-doc", @"abc/123/x%@/not-cg-doc", @"abc/%@"
];

FIRWriteBatch* batch = self.db.batch;
for (NSString* format in docPathFormats) {
NSString* path = [NSString stringWithFormat:format, collectionGroup];
[batch setData:@{@"x" : @2} forDocument:[self.db documentWithPath:path]];
}
[self commitWriteBatch:batch];

FIRAggregateQuerySnapshot* snapshot =
[self readSnapshotForAggregate:[[self.db collectionGroupWithID:collectionGroup] aggregate:@[
[FIRAggregateField aggregateFieldForCount],
[FIRAggregateField aggregateFieldForSumOfField:@"x"],
[FIRAggregateField aggregateFieldForAverageOfField:@"x"]
]]];
// "cg-doc1", "cg-doc2", "cg-doc3", "cg-doc4", "cg-doc5",
XCTAssertEqual([snapshot valueForAggregateField:[FIRAggregateField aggregateFieldForCount]],
[NSNumber numberWithLong:5L]);
XCTAssertEqual(
[snapshot valueForAggregateField:[FIRAggregateField aggregateFieldForSumOfField:@"x"]],
[NSNumber numberWithLong:10L]);
XCTAssertEqual(
[snapshot valueForAggregateField:[FIRAggregateField aggregateFieldForAverageOfField:@"x"]],
[NSNumber numberWithDouble:2.0]);
}

- (void)testPerformsAggregationsWhenNaNExistsForSomeFieldValues {
XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator],
"Skip this test if running against production because it requires a composite index.");

FIRCollectionReference* testCollection = [self collectionRefWithDocuments:@{
@"a" : @{
@"author" : @"authorA",
@"title" : @"titleA",
@"pages" : @100,
@"year" : @1980,
@"rating" : @5
},
@"b" : @{
@"author" : @"authorB",
@"title" : @"titleB",
@"pages" : @50,
@"year" : @2020,
@"rating" : @4
},
@"c" : @{
@"author" : @"authorC",
@"title" : @"titleC",
@"pages" : @100,
@"year" : @1980,
@"rating" : [NSNumber numberWithFloat:NAN]
},
@"d" : @{
@"author" : @"authorD",
@"title" : @"titleD",
@"pages" : @50,
@"year" : @2020,
@"rating" : @0
}
}];

FIRAggregateQuerySnapshot* snapshot =
[self readSnapshotForAggregate:[testCollection aggregate:@[
[FIRAggregateField aggregateFieldForSumOfField:@"rating"],
[FIRAggregateField aggregateFieldForSumOfField:@"pages"],
[FIRAggregateField aggregateFieldForAverageOfField:@"rating"],
[FIRAggregateField aggregateFieldForAverageOfField:@"year"]
]]];

// Sum
XCTAssertEqual(
[snapshot valueForAggregateField:[FIRAggregateField aggregateFieldForSumOfField:@"rating"]],
[NSNumber numberWithDouble:NAN]);
XCTAssertEqual(
[[snapshot valueForAggregateField:[FIRAggregateField aggregateFieldForSumOfField:@"pages"]]
longValue],
300L);

// Average
XCTAssertEqual([snapshot valueForAggregateField:[FIRAggregateField
aggregateFieldForAverageOfField:@"rating"]],
[NSNumber numberWithDouble:NAN]);
XCTAssertEqual(
[[snapshot valueForAggregateField:[FIRAggregateField aggregateFieldForAverageOfField:@"year"]]
doubleValue],
2000.0);
}

- (void)testThrowsAnErrorWhenGettingTheResultOfAnUnrequestedAggregation {
FIRCollectionReference* testCollection = [self collectionRefWithDocuments:@{
@"a" : @{
Expand Down Expand Up @@ -789,77 +635,6 @@ - (void)testPerformsAggregationWhenUsingInOperator {
4.0);
}

- (void)testPerformsAggregationWhenUsingArrayContainsAnyOperator {
XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator],
"Skip this test if running against production because it requires a composite index.");

FIRCollectionReference* testCollection = [self collectionRefWithDocuments:@{
@"a" : @{
@"author" : @"authorA",
@"title" : @"titleA",
@"pages" : @100,
@"year" : @1980,
@"rating" : @[ @5, @1000 ]
},
@"b" : @{
@"author" : @"authorB",
@"title" : @"titleB",
@"pages" : @50,
@"year" : @2020,
@"rating" : @[ @4 ]
},
@"c" : @{
@"author" : @"authorC",
@"title" : @"titleC",
@"pages" : @100,
@"year" : @1980,
@"rating" : @[ @2222, @3 ]
},
@"d" : @{
@"author" : @"authorD",
@"title" : @"titleD",
@"pages" : @50,
@"year" : @2020,
@"rating" : @[ @0 ]
}
}];

FIRAggregateQuerySnapshot* snapshot = [self
readSnapshotForAggregate:[[testCollection queryWhereField:@"rating"
arrayContainsAny:@[ @5, @3 ]]
aggregate:@[
[FIRAggregateField aggregateFieldForSumOfField:@"rating"],
[FIRAggregateField aggregateFieldForSumOfField:@"pages"],
[FIRAggregateField aggregateFieldForAverageOfField:@"rating"],
[FIRAggregateField aggregateFieldForAverageOfField:@"pages"],
[FIRAggregateField aggregateFieldForCount]
]]];

// Count
XCTAssertEqual(
[[snapshot valueForAggregateField:[FIRAggregateField aggregateFieldForCount]] longValue], 2L);

// Sum
XCTAssertEqual(
[[snapshot valueForAggregateField:[FIRAggregateField aggregateFieldForSumOfField:@"rating"]]
longValue],
0L);
XCTAssertEqual(
[[snapshot valueForAggregateField:[FIRAggregateField aggregateFieldForSumOfField:@"pages"]]
longValue],
200L);

// Average
XCTAssertEqualObjects(
[snapshot
valueForAggregateField:[FIRAggregateField aggregateFieldForAverageOfField:@"rating"]],
[NSNull null]);
XCTAssertEqual(
[[snapshot valueForAggregateField:[FIRAggregateField
aggregateFieldForAverageOfField:@"pages"]] doubleValue],
100.0);
}

- (void)testPerformsAggregationsOnNestedMapValues {
FIRCollectionReference* testCollection = [self collectionRefWithDocuments:@{
@"a" : @{
Expand Down
Loading

0 comments on commit 8881200

Please sign in to comment.