| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445 |
- /*
- * Copyright 2023 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- #import <FirebaseFirestore/FIRFieldPath.h>
- #import <FirebaseFirestore/FirebaseFirestore.h>
- #import <XCTest/XCTest.h>
- // TODO(sum/avg) update these imports with public imports when sum/avg is public
- #import "Firestore/Source/API/FIRAggregateField.h"
- #import "Firestore/Source/API/FIRAggregateQuerySnapshot+Internal.h"
- #import "Firestore/Source/API/FIRQuery+Internal.h"
- #import "Firestore/Example/Tests/Util/FSTIntegrationTestCase.h"
- @interface FIRAggregateTests : FSTIntegrationTestCase
- @end
- @implementation FIRAggregateTests
- - (void)testCountAggregateFieldQueryEquals {
- FIRCollectionReference* coll1 = [self collectionRefWithDocuments:@{}];
- FIRCollectionReference* coll1Same = [[coll1 firestore] collectionWithPath:[coll1 path]];
- FIRAggregateQuery* query1 = [coll1 aggregate:@[ [FIRAggregateField aggregateFieldForCount] ]];
- FIRAggregateQuery* query1Same =
- [coll1Same aggregate:@[ [FIRAggregateField aggregateFieldForCount] ]];
- FIRAggregateQuery* query1DiffAgg =
- [coll1Same aggregate:@[ [FIRAggregateField aggregateFieldForSumOfField:@"baz"] ]];
- FIRCollectionReference* sub = [[coll1 documentWithPath:@"bar"] collectionWithPath:@"baz"];
- FIRAggregateQuery* query2 = [[[sub queryWhereField:@"a" isEqualTo:@1] queryLimitedTo:100]
- aggregate:@[ [FIRAggregateField aggregateFieldForCount] ]];
- FIRAggregateQuery* query2Same = [[[sub queryWhereField:@"a"
- isEqualTo:@1] queryLimitedTo:100] count];
- FIRAggregateQuery* query3 = [[[sub queryWhereField:@"b" isEqualTo:@1] queryOrderedByField:@"c"]
- aggregate:@[ [FIRAggregateField aggregateFieldForCount] ]];
- FIRAggregateQuery* query3Same = [[[sub queryWhereField:@"b"
- isEqualTo:@1] queryOrderedByField:@"c"] count];
- XCTAssertEqualObjects(query1, query1Same);
- XCTAssertEqualObjects(query2, query2Same);
- XCTAssertEqualObjects(query3, query3Same);
- XCTAssertEqual([query1 hash], [query1Same hash]);
- XCTAssertEqual([query2 hash], [query2Same hash]);
- XCTAssertEqual([query3 hash], [query3Same hash]);
- XCTAssertFalse([query1 isEqual:nil]);
- XCTAssertFalse([query1 isEqual:@"string"]);
- XCTAssertFalse([query1 isEqual:query2]);
- XCTAssertFalse([query2 isEqual:query3]);
- XCTAssertNotEqual([query1 hash], [query1DiffAgg hash]);
- XCTAssertNotEqual([query1 hash], [query2 hash]);
- XCTAssertNotEqual([query2 hash], [query3 hash]);
- }
- - (void)testSumAggregateFieldQueryEquals {
- FIRCollectionReference* coll1 = [self collectionRefWithDocuments:@{}];
- FIRCollectionReference* coll1Same = [[coll1 firestore] collectionWithPath:[coll1 path]];
- FIRAggregateQuery* query1 =
- [coll1 aggregate:@[ [FIRAggregateField aggregateFieldForSumOfField:@"baz"] ]];
- FIRAggregateQuery* query1Same =
- [coll1Same aggregate:@[ [FIRAggregateField aggregateFieldForSumOfField:@"baz"] ]];
- FIRAggregateQuery* query1WithFieldPath =
- [coll1Same aggregate:@[ [FIRAggregateField
- aggregateFieldForSumOfFieldPath:[[FIRFieldPath alloc]
- initWithFields:@[ @"baz" ]]] ]];
- FIRAggregateQuery* query1DiffAgg =
- [coll1Same aggregate:@[ [FIRAggregateField aggregateFieldForCount] ]];
- FIRCollectionReference* sub = [[coll1 documentWithPath:@"bar"] collectionWithPath:@"baz"];
- FIRAggregateQuery* query2 = [[[sub queryWhereField:@"a" isEqualTo:@1] queryLimitedTo:100]
- aggregate:@[ [FIRAggregateField aggregateFieldForSumOfField:@"baz"] ]];
- FIRAggregateQuery* query2Same = [[[sub queryWhereField:@"a" isEqualTo:@1] queryLimitedTo:100]
- aggregate:@[ [FIRAggregateField aggregateFieldForSumOfField:@"baz"] ]];
- FIRAggregateQuery* query3 = [[[sub queryWhereField:@"b" isEqualTo:@1] queryOrderedByField:@"c"]
- aggregate:@[ [FIRAggregateField aggregateFieldForSumOfField:@"baz"] ]];
- FIRAggregateQuery* query3Same = [[[sub queryWhereField:@"b"
- isEqualTo:@1] queryOrderedByField:@"c"]
- aggregate:@[ [FIRAggregateField aggregateFieldForSumOfField:@"baz"] ]];
- XCTAssertEqualObjects(query1, query1Same);
- XCTAssertEqualObjects(query1, query1WithFieldPath);
- XCTAssertEqualObjects(query2, query2Same);
- XCTAssertEqualObjects(query3, query3Same);
- XCTAssertEqual([query1 hash], [query1Same hash]);
- XCTAssertEqual([query1 hash], [query1WithFieldPath hash]);
- XCTAssertEqual([query2 hash], [query2Same hash]);
- XCTAssertEqual([query3 hash], [query3Same hash]);
- XCTAssertFalse([query1 isEqual:nil]);
- XCTAssertFalse([query1 isEqual:@"string"]);
- XCTAssertFalse([query1 isEqual:query2]);
- XCTAssertFalse([query2 isEqual:query3]);
- XCTAssertNotEqual([query1 hash], [query1DiffAgg hash]);
- XCTAssertNotEqual([query1 hash], [query2 hash]);
- XCTAssertNotEqual([query2 hash], [query3 hash]);
- }
- - (void)testAverageAggregateFieldQueryEquals {
- FIRCollectionReference* coll1 = [self collectionRefWithDocuments:@{}];
- FIRCollectionReference* coll1Same = [[coll1 firestore] collectionWithPath:[coll1 path]];
- FIRAggregateQuery* query1 =
- [coll1 aggregate:@[ [FIRAggregateField aggregateFieldForAverageOfField:@"baz"] ]];
- FIRAggregateQuery* query1Same =
- [coll1Same aggregate:@[ [FIRAggregateField aggregateFieldForAverageOfField:@"baz"] ]];
- FIRAggregateQuery* query1WithFieldPath = [coll1Same aggregate:@[
- [FIRAggregateField
- aggregateFieldForAverageOfFieldPath:[[FIRFieldPath alloc] initWithFields:@[ @"baz" ]]]
- ]];
- FIRAggregateQuery* query1DiffAgg =
- [coll1Same aggregate:@[ [FIRAggregateField aggregateFieldForCount] ]];
- FIRCollectionReference* sub = [[coll1 documentWithPath:@"bar"] collectionWithPath:@"baz"];
- FIRAggregateQuery* query2 = [[[sub queryWhereField:@"a" isEqualTo:@1] queryLimitedTo:100]
- aggregate:@[ [FIRAggregateField aggregateFieldForAverageOfField:@"baz"] ]];
- FIRAggregateQuery* query2Same = [[[sub queryWhereField:@"a" isEqualTo:@1] queryLimitedTo:100]
- aggregate:@[ [FIRAggregateField aggregateFieldForAverageOfField:@"baz"] ]];
- FIRAggregateQuery* query3 = [[[sub queryWhereField:@"b" isEqualTo:@1] queryOrderedByField:@"c"]
- aggregate:@[ [FIRAggregateField aggregateFieldForAverageOfField:@"baz"] ]];
- FIRAggregateQuery* query3Same = [[[sub queryWhereField:@"b"
- isEqualTo:@1] queryOrderedByField:@"c"]
- aggregate:@[ [FIRAggregateField aggregateFieldForAverageOfField:@"baz"] ]];
- XCTAssertEqualObjects(query1, query1Same);
- XCTAssertEqualObjects(query1, query1WithFieldPath);
- XCTAssertEqualObjects(query2, query2Same);
- XCTAssertEqualObjects(query3, query3Same);
- XCTAssertEqual([query1 hash], [query1Same hash]);
- XCTAssertEqual([query1 hash], [query1WithFieldPath hash]);
- XCTAssertEqual([query2 hash], [query2Same hash]);
- XCTAssertEqual([query3 hash], [query3Same hash]);
- XCTAssertFalse([query1 isEqual:nil]);
- XCTAssertFalse([query1 isEqual:@"string"]);
- XCTAssertFalse([query1 isEqual:query2]);
- XCTAssertFalse([query2 isEqual:query3]);
- XCTAssertNotEqual([query1 hash], [query1DiffAgg hash]);
- XCTAssertNotEqual([query1 hash], [query2 hash]);
- XCTAssertNotEqual([query2 hash], [query3 hash]);
- }
- - (void)testAggregateFieldQueryNotEquals {
- FIRCollectionReference* coll = [self collectionRefWithDocuments:@{}];
- FIRAggregateQuery* query1 = [coll aggregate:@[ [FIRAggregateField aggregateFieldForCount] ]];
- FIRAggregateQuery* query2 =
- [coll aggregate:@[ [FIRAggregateField aggregateFieldForSumOfField:@"baz"] ]];
- FIRAggregateQuery* query3 =
- [coll aggregate:@[ [FIRAggregateField aggregateFieldForAverageOfField:@"baz"] ]];
- XCTAssertNotEqualObjects(query1, query2);
- XCTAssertNotEqualObjects(query2, query3);
- XCTAssertNotEqualObjects(query3, query1);
- XCTAssertNotEqual([query1 hash], [query2 hash]);
- XCTAssertNotEqual([query2 hash], [query3 hash]);
- XCTAssertNotEqual([query3 hash], [query1 hash]);
- FIRQuery* baseQuery = [[[[coll documentWithPath:@"bar"] collectionWithPath:@"baz"]
- queryWhereField:@"a"
- isEqualTo:@1] queryLimitedTo:100];
- FIRAggregateQuery* query4 = [baseQuery aggregate:@[ [FIRAggregateField aggregateFieldForCount] ]];
- FIRAggregateQuery* query5 =
- [baseQuery aggregate:@[ [FIRAggregateField aggregateFieldForSumOfField:@"baz"] ]];
- FIRAggregateQuery* query6 =
- [baseQuery aggregate:@[ [FIRAggregateField aggregateFieldForAverageOfField:@"baz"] ]];
- XCTAssertNotEqualObjects(query4, query5);
- XCTAssertNotEqualObjects(query5, query6);
- XCTAssertNotEqualObjects(query6, query4);
- XCTAssertNotEqual([query4 hash], [query5 hash]);
- XCTAssertNotEqual([query5 hash], [query6 hash]);
- XCTAssertNotEqual([query6 hash], [query4 hash]);
- }
- - (void)testCanRunAggregateQuery {
- // TODO(sum/avg) remove the check below when sum and avg are supported in production
- XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator], @"only tested against emulator");
- 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
- }
- }];
- FIRAggregateQuerySnapshot* snapshot =
- [self readSnapshotForAggregate:[testCollection aggregate:@[
- [FIRAggregateField aggregateFieldForCount],
- [FIRAggregateField aggregateFieldForSumOfField:@"pages"],
- [FIRAggregateField aggregateFieldForSumOfField:@"weight"],
- [FIRAggregateField aggregateFieldForAverageOfField:@"pages"],
- [FIRAggregateField aggregateFieldForAverageOfField:@"weight"]
- ]]];
- // Count
- XCTAssertEqual([snapshot valueForAggregation:[FIRAggregateField aggregateFieldForCount]],
- [NSNumber numberWithLong:2L]);
- XCTAssertEqual([snapshot count], [NSNumber numberWithLong:2L]);
- // Sum
- XCTAssertEqual(
- [snapshot valueForAggregation:[FIRAggregateField aggregateFieldForSumOfField:@"pages"]],
- [NSNumber numberWithLong:150L], );
- XCTAssertEqual(
- [[snapshot valueForAggregation:[FIRAggregateField aggregateFieldForSumOfField:@"weight"]]
- doubleValue],
- 99.6);
- // Average
- XCTAssertEqual(
- [snapshot valueForAggregation:[FIRAggregateField aggregateFieldForAverageOfField:@"pages"]],
- [NSNumber numberWithDouble:75.0]);
- XCTAssertEqual(
- [[snapshot valueForAggregation:[FIRAggregateField aggregateFieldForAverageOfField:@"weight"]]
- doubleValue],
- 49.8);
- }
- - (void)testCanRunEmptyAggregateQuery {
- // TODO(sum/avg) remove the check below when sum and avg are supported in production
- XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator], @"only tested against emulator");
- 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
- }
- }];
- FIRAggregateQuery* emptyQuery = [testCollection aggregate:@[]];
- __block NSError* result;
- XCTestExpectation* expectation = [self expectationWithDescription:@"aggregate result"];
- [emptyQuery aggregationWithSource:FIRAggregateSourceServer
- completion:^(FIRAggregateQuerySnapshot* snapshot, NSError* error) {
- XCTAssertNil(snapshot);
- result = error;
- [expectation fulfill];
- }];
- [self awaitExpectation:expectation];
- XCTAssertNotNil(result);
- XCTAssertTrue([[result localizedDescription] containsString:@"Aggregations can not be empty"]);
- }
- - (void)testAggregateFieldQuerySnapshotEquality {
- // TODO(sum/avg) remove the check below when sum and avg are supported in production
- XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator], @"only tested against emulator");
- 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
- }
- }];
- FIRAggregateQuerySnapshot* snapshot1 =
- [self readSnapshotForAggregate:[testCollection aggregate:@[
- [FIRAggregateField aggregateFieldForCount],
- [FIRAggregateField aggregateFieldForSumOfField:@"pages"],
- [FIRAggregateField aggregateFieldForSumOfField:@"weight"],
- [FIRAggregateField aggregateFieldForAverageOfField:@"pages"],
- [FIRAggregateField aggregateFieldForAverageOfField:@"weight"]
- ]]];
- FIRAggregateQuerySnapshot* snapshot2 =
- [self readSnapshotForAggregate:[testCollection aggregate:@[
- [FIRAggregateField aggregateFieldForCount],
- [FIRAggregateField aggregateFieldForSumOfField:@"pages"],
- [FIRAggregateField aggregateFieldForSumOfField:@"weight"],
- [FIRAggregateField aggregateFieldForAverageOfField:@"pages"],
- [FIRAggregateField aggregateFieldForAverageOfField:@"weight"]
- ]]];
- // different aggregates
- FIRAggregateQuerySnapshot* snapshot3 =
- [self readSnapshotForAggregate:[testCollection aggregate:@[
- [FIRAggregateField aggregateFieldForCount],
- [FIRAggregateField aggregateFieldForSumOfField:@"pages"],
- [FIRAggregateField aggregateFieldForSumOfField:@"weight"],
- [FIRAggregateField aggregateFieldForAverageOfField:@"pages"]
- ]]];
- // different data set
- FIRAggregateQuerySnapshot* snapshot4 = [self
- readSnapshotForAggregate:[[testCollection queryWhereField:@"pages" isGreaterThan:@50]
- aggregate:@[
- [FIRAggregateField aggregateFieldForCount],
- [FIRAggregateField aggregateFieldForSumOfField:@"pages"],
- [FIRAggregateField aggregateFieldForSumOfField:@"weight"],
- [FIRAggregateField aggregateFieldForAverageOfField:@"pages"],
- [FIRAggregateField
- aggregateFieldForAverageOfField:@"weight"]
- ]]];
- XCTAssertEqualObjects(snapshot1, snapshot2);
- XCTAssertNotEqualObjects(snapshot1, snapshot3);
- XCTAssertNotEqualObjects(snapshot1, snapshot4);
- XCTAssertNotEqualObjects(snapshot3, snapshot4);
- XCTAssertEqual([snapshot1 hash], [snapshot2 hash]);
- XCTAssertNotEqual([snapshot1 hash], [snapshot3 hash]);
- XCTAssertNotEqual([snapshot1 hash], [snapshot4 hash]);
- XCTAssertNotEqual([snapshot3 hash], [snapshot4 hash]);
- }
- - (void)testAllowsAliasesLongerThan1500Bytes {
- // TODO(sum/avg) remove the check below when sum and avg are supported in production
- XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator], @"only tested against emulator");
- // The longest field name allowed is 1500. The alias chosen by the client is <op>_<fieldName>.
- // If the field name is
- // 1500 bytes, the alias will be longer than 1500, which is the limit for aliases. This is to
- // make sure the client
- // can handle this corner case correctly.
- NSString* longField = [@"" stringByPaddingToLength:1499
- withString:@"0123456789"
- startingAtIndex:0];
- FIRCollectionReference* testCollection =
- [self collectionRefWithDocuments:@{@"a" : @{longField : @1}, @"b" : @{longField : @2}}];
- FIRAggregateQuerySnapshot* snapshot =
- [self readSnapshotForAggregate:[testCollection
- aggregate:@[ [FIRAggregateField
- aggregateFieldForSumOfField:longField] ]]];
- // Sum
- XCTAssertEqual(
- [snapshot valueForAggregation:[FIRAggregateField aggregateFieldForSumOfField:longField]],
- [NSNumber numberWithLong:3], );
- }
- - (void)testCanGetDuplicateAggregations {
- // TODO(sum/avg) remove the check below when sum and avg are supported in production
- XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator], @"only tested against emulator");
- 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
- }
- }];
- FIRAggregateQuerySnapshot* snapshot =
- [self readSnapshotForAggregate:[testCollection aggregate:@[
- [FIRAggregateField aggregateFieldForCount],
- [FIRAggregateField aggregateFieldForSumOfField:@"pages"],
- [FIRAggregateField aggregateFieldForSumOfField:@"pages"]
- ]]];
- // Count
- XCTAssertEqual([snapshot valueForAggregation:[FIRAggregateField aggregateFieldForCount]],
- [NSNumber numberWithLong:2L]);
- // Sum
- XCTAssertEqual(
- [snapshot valueForAggregation:[FIRAggregateField aggregateFieldForSumOfField:@"pages"]],
- [NSNumber numberWithLong:150L], );
- }
- - (void)testTerminateDoesNotCrashWithFlyingAggregateQuery {
- // TODO(sum/avg) remove the check below when sum and avg are supported in production
- XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator], @"only tested against emulator");
- 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
- }
- }];
- FIRAggregateQuery* aggQuery = [testCollection aggregate:@[
- [FIRAggregateField aggregateFieldForCount],
- [FIRAggregateField aggregateFieldForSumOfField:@"pages"],
- [FIRAggregateField aggregateFieldForAverageOfField:@"pages"]
- ]];
- __block FIRAggregateQuerySnapshot* result;
- XCTestExpectation* expectation = [self expectationWithDescription:@"aggregate result"];
- [aggQuery aggregationWithSource:FIRAggregateSourceServer
- completion:^(FIRAggregateQuerySnapshot* snapshot, NSError* error) {
- XCTAssertNil(error);
- result = snapshot;
- [expectation fulfill];
- }];
- [self awaitExpectation:expectation];
- // Count
- XCTAssertEqual([result valueForAggregation:[FIRAggregateField aggregateFieldForCount]],
- [NSNumber numberWithLong:2L]);
- // Sum
- XCTAssertEqual(
- [result valueForAggregation:[FIRAggregateField aggregateFieldForSumOfField:@"pages"]],
- [NSNumber numberWithLong:150L], );
- }
- - (void)testCanPerformMaxAggregations {
- // TODO(sum/avg) remove the check below when sum and avg are supported in production
- XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator], @"only tested against emulator");
- 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 valueForAggregation:[FIRAggregateField aggregateFieldForCount]],
- [NSNumber numberWithLong:2L]);
- XCTAssertEqual(
- [snapshot valueForAggregation:[FIRAggregateField aggregateFieldForSumOfField:@"pages"]],
- [NSNumber numberWithLong:150L], );
- XCTAssertEqual(
- [[snapshot valueForAggregation:[FIRAggregateField aggregateFieldForSumOfField:@"weight"]]
- doubleValue],
- 99.6);
- XCTAssertEqual(
- [snapshot valueForAggregation:[FIRAggregateField aggregateFieldForAverageOfField:@"pages"]],
- [NSNumber numberWithDouble:75.0]);
- XCTAssertEqual(
- [[snapshot valueForAggregation:[FIRAggregateField aggregateFieldForAverageOfField:@"weight"]]
- doubleValue],
- 49.8);
- }
- - (void)testCannotPerformMoreThanMaxAggregations {
- // TODO(sum/avg) remove the check below when sum and avg are supported in production
- XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator], @"only tested against emulator");
- 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, we're attempting 6. I also like to live dangerously.
- FIRAggregateQuery* query = [testCollection aggregate:@[
- [FIRAggregateField aggregateFieldForCount],
- [FIRAggregateField aggregateFieldForSumOfField:@"pages"],
- [FIRAggregateField aggregateFieldForSumOfField:@"weight"],
- [FIRAggregateField aggregateFieldForAverageOfField:@"pages"],
- [FIRAggregateField aggregateFieldForAverageOfField:@"weight"],
- [FIRAggregateField aggregateFieldForAverageOfField:@"foo"]
- ]];
- __block NSError* result;
- XCTestExpectation* expectation = [self expectationWithDescription:@"aggregate result"];
- [query aggregationWithSource:FIRAggregateSourceServer
- completion:^(FIRAggregateQuerySnapshot* snapshot, NSError* error) {
- XCTAssertNil(snapshot);
- result = error;
- [expectation fulfill];
- }];
- [self awaitExpectation:expectation];
- XCTAssertNotNil(result);
- XCTAssertTrue([[result localizedDescription] containsString:@"maximum number of aggregations"]);
- }
- - (void)testCanRunAggregateCollectionGroupQuery {
- // TODO(sum/avg) remove the check below when sum and avg are supported in production
- XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator], @"only tested against emulator");
- 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]];
- }
- XCTestExpectation* expectation = [self expectationWithDescription:@"commit"];
- [batch commitWithCompletion:^(NSError* error) {
- XCTAssertNil(error);
- [expectation fulfill];
- }];
- [self awaitExpectation:expectation];
- 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 valueForAggregation:[FIRAggregateField aggregateFieldForCount]],
- [NSNumber numberWithLong:5L]);
- XCTAssertEqual(
- [snapshot valueForAggregation:[FIRAggregateField aggregateFieldForSumOfField:@"x"]],
- [NSNumber numberWithLong:10L]);
- XCTAssertEqual(
- [snapshot valueForAggregation:[FIRAggregateField aggregateFieldForAverageOfField:@"x"]],
- [NSNumber numberWithDouble:2.0]);
- }
- - (void)testPerformsAggregationsWhenNaNExistsForSomeFieldValues {
- // TODO(sum/avg) remove the check below when sum and avg are supported in production
- XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator], @"only tested against emulator");
- 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 valueForAggregation:[FIRAggregateField aggregateFieldForSumOfField:@"rating"]],
- [NSNumber numberWithDouble:NAN]);
- XCTAssertEqual(
- [[snapshot valueForAggregation:[FIRAggregateField aggregateFieldForSumOfField:@"pages"]]
- longValue],
- 300L);
- // Average
- XCTAssertEqual(
- [snapshot valueForAggregation:[FIRAggregateField aggregateFieldForAverageOfField:@"rating"]],
- [NSNumber numberWithDouble:NAN]);
- XCTAssertEqual(
- [[snapshot valueForAggregation:[FIRAggregateField aggregateFieldForAverageOfField:@"year"]]
- doubleValue],
- 2000.0);
- }
- - (void)testThrowsAnErrorWhenGettingTheResultOfAnUnrequestedAggregation {
- // TODO(sum/avg) remove the check below when sum and avg are supported in production
- XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator], @"only tested against emulator");
- 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
- }
- }];
- FIRAggregateQuerySnapshot* snapshot =
- [self readSnapshotForAggregate:[testCollection
- aggregate:@[ [FIRAggregateField
- aggregateFieldForSumOfField:@"pages"] ]]];
- @try {
- [snapshot count];
- XCTAssertTrue(false, "Exception expected");
- } @catch (NSException* exception) {
- XCTAssertEqualObjects(exception.name, @"FIRInvalidArgumentException");
- XCTAssertEqualObjects(exception.reason,
- @"'count()' was not requested in the aggregation query.");
- }
- @
- try {
- [snapshot valueForAggregation:[FIRAggregateField aggregateFieldForSumOfField:@"foo"]];
- XCTAssertTrue(false, "Exception expected");
- } @catch (NSException* exception) {
- XCTAssertEqualObjects(exception.name, @"FIRInvalidArgumentException");
- XCTAssertEqualObjects(exception.reason,
- @"'sum(foo)' was not requested in the aggregation query.");
- }
- @
- try {
- [snapshot valueForAggregation:[FIRAggregateField aggregateFieldForAverageOfField:@"pages"]];
- XCTAssertTrue(false, "Exception expected");
- } @catch (NSException* exception) {
- XCTAssertEqualObjects(exception.name, @"FIRInvalidArgumentException");
- XCTAssertEqualObjects(exception.reason,
- @"'avg(pages)' was not requested in the aggregation query.");
- }
- }
- - (void)testPerformsAggregationWhenUsingInOperator {
- // TODO(sum/avg) remove the check below when sum and avg are supported in production
- XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator], @"only tested against emulator");
- 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" : @3
- },
- @"d" : @{
- @"author" : @"authorD",
- @"title" : @"titleD",
- @"pages" : @50,
- @"year" : @2020,
- @"rating" : @0
- }
- }];
- FIRAggregateQuerySnapshot* snapshot = [self
- readSnapshotForAggregate:[[testCollection queryWhereField:@"rating" in:@[ @5, @3 ]]
- aggregate:@[
- [FIRAggregateField aggregateFieldForSumOfField:@"rating"],
- [FIRAggregateField aggregateFieldForSumOfField:@"pages"],
- [FIRAggregateField aggregateFieldForAverageOfField:@"rating"],
- [FIRAggregateField aggregateFieldForAverageOfField:@"pages"],
- [FIRAggregateField aggregateFieldForCount]
- ]]];
- // Count
- XCTAssertEqual(
- [[snapshot valueForAggregation:[FIRAggregateField aggregateFieldForCount]] longValue], 2L);
- // Sum
- XCTAssertEqual(
- [[snapshot valueForAggregation:[FIRAggregateField aggregateFieldForSumOfField:@"rating"]]
- longValue],
- 8L);
- XCTAssertEqual(
- [[snapshot valueForAggregation:[FIRAggregateField aggregateFieldForSumOfField:@"pages"]]
- longValue],
- 200L);
- // Average
- XCTAssertEqual(
- [[snapshot valueForAggregation:[FIRAggregateField aggregateFieldForAverageOfField:@"rating"]]
- doubleValue],
- 4.0);
- XCTAssertEqual(
- [[snapshot valueForAggregation:[FIRAggregateField aggregateFieldForAverageOfField:@"pages"]]
- doubleValue],
- 100.0);
- }
- - (void)testPerformsAggregationWhenUsingArrayContainsAnyOperator {
- // TODO(sum/avg) remove the check below when sum and avg are supported in production
- XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator], @"only tested against emulator");
- 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 valueForAggregation:[FIRAggregateField aggregateFieldForCount]] longValue], 2L);
- // Sum
- XCTAssertEqual(
- [[snapshot valueForAggregation:[FIRAggregateField aggregateFieldForSumOfField:@"rating"]]
- longValue],
- 0L);
- XCTAssertEqual(
- [[snapshot valueForAggregation:[FIRAggregateField aggregateFieldForSumOfField:@"pages"]]
- longValue],
- 200L);
- // Average
- XCTAssertEqualObjects(
- [snapshot valueForAggregation:[FIRAggregateField aggregateFieldForAverageOfField:@"rating"]],
- [NSNull null]);
- XCTAssertEqual(
- [[snapshot valueForAggregation:[FIRAggregateField aggregateFieldForAverageOfField:@"pages"]]
- doubleValue],
- 100.0);
- }
- - (void)testPerformsAggregationsOnNestedMapValues {
- // TODO(sum/avg) remove the check below when sum and avg are supported in production
- XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator], @"only tested against emulator");
- FIRCollectionReference* testCollection = [self collectionRefWithDocuments:@{
- @"a" : @{
- @"author" : @"authorA",
- @"title" : @"titleA",
- @"metadata" : @{@"pages" : @100, @"rating" : @{@"critic" : @2, @"user" : @5}}
- },
- @"b" : @{
- @"author" : @"authorB",
- @"title" : @"titleB",
- @"metadata" : @{@"pages" : @50, @"rating" : @{@"critic" : @4, @"user" : @4}}
- },
- }];
- FIRAggregateQuerySnapshot* snapshot =
- [self readSnapshotForAggregate:[testCollection aggregate:@[
- [FIRAggregateField aggregateFieldForSumOfField:@"metadata.pages"],
- [FIRAggregateField aggregateFieldForSumOfField:@"metadata.rating.user"],
- [FIRAggregateField aggregateFieldForAverageOfField:@"metadata.pages"],
- [FIRAggregateField aggregateFieldForAverageOfField:@"metadata.rating.critic"],
- [FIRAggregateField aggregateFieldForCount]
- ]]];
- // Count
- XCTAssertEqual(
- [[snapshot valueForAggregation:[FIRAggregateField aggregateFieldForCount]] longValue], 2L);
- // Sum
- XCTAssertEqual(
- [[snapshot valueForAggregation:[FIRAggregateField
- aggregateFieldForSumOfField:@"metadata.pages"]] longValue],
- 150L);
- XCTAssertEqual(
- [[snapshot valueForAggregation:[FIRAggregateField
- aggregateFieldForSumOfField:@"metadata.rating.user"]]
- longValue],
- 9);
- // Average
- XCTAssertEqual(
- [[snapshot
- valueForAggregation:[FIRAggregateField aggregateFieldForAverageOfField:@"metadata.pages"]]
- doubleValue],
- 75.0);
- XCTAssertEqual(
- [[snapshot valueForAggregation:[FIRAggregateField
- aggregateFieldForAverageOfField:@"metadata.rating.critic"]]
- doubleValue],
- 3.0);
- }
- - (void)testPerformsSumThatOverflowsMaxLong {
- // TODO(sum/avg) remove the check below when sum and avg are supported in production
- XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator], @"only tested against emulator");
- FIRCollectionReference* testCollection = [self collectionRefWithDocuments:@{
- @"a" : @{
- @"author" : @"authorA",
- @"title" : @"titleA",
- @"rating" : [NSNumber numberWithLong:LLONG_MAX]
- },
- @"b" : @{
- @"author" : @"authorB",
- @"title" : @"titleB",
- @"rating" : [NSNumber numberWithLong:LLONG_MAX]
- },
- }];
- FIRAggregateQuerySnapshot* snapshot =
- [self readSnapshotForAggregate:[testCollection
- aggregate:@[ [FIRAggregateField
- aggregateFieldForSumOfField:@"rating"] ]]];
- // Sum
- XCTAssertEqual(
- [[snapshot valueForAggregation:[FIRAggregateField aggregateFieldForSumOfField:@"rating"]]
- doubleValue],
- [[NSNumber numberWithLong:LLONG_MAX] doubleValue] +
- [[NSNumber numberWithLong:LLONG_MAX] doubleValue]);
- }
- - (void)testPerformsSumThatCanOverflowLongValuesDuringAccumulation {
- // TODO(sum/avg) remove the check below when sum and avg are supported in production
- XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator], @"only tested against emulator");
- FIRCollectionReference* testCollection = [self collectionRefWithDocuments:@{
- @"a" : @{
- @"author" : @"authorA",
- @"title" : @"titleA",
- @"rating" : [NSNumber numberWithLong:LLONG_MAX]
- },
- @"b" : @{@"author" : @"authorB", @"title" : @"titleB", @"rating" : [NSNumber numberWithLong:1]},
- @"c" :
- @{@"author" : @"authorC", @"title" : @"titleC", @"rating" : [NSNumber numberWithLong:-101]}
- }];
- FIRAggregateQuerySnapshot* snapshot =
- [self readSnapshotForAggregate:[testCollection
- aggregate:@[ [FIRAggregateField
- aggregateFieldForSumOfField:@"rating"] ]]];
- // Sum
- XCTAssertEqual(
- [[snapshot valueForAggregation:[FIRAggregateField aggregateFieldForSumOfField:@"rating"]]
- longLongValue],
- [[NSNumber numberWithLong:LLONG_MAX - 100] longLongValue]);
- }
- - (void)testPerformsSumThatIsNegative {
- // TODO(sum/avg) remove the check below when sum and avg are supported in production
- XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator], @"only tested against emulator");
- FIRCollectionReference* testCollection = [self collectionRefWithDocuments:@{
- @"a" : @{
- @"author" : @"authorA",
- @"title" : @"titleA",
- @"rating" : [NSNumber numberWithLong:LLONG_MAX]
- },
- @"b" : @{
- @"author" : @"authorB",
- @"title" : @"titleB",
- @"rating" : [NSNumber numberWithLong:-LLONG_MAX]
- },
- @"c" :
- @{@"author" : @"authorC", @"title" : @"titleC", @"rating" : [NSNumber numberWithLong:-101]},
- @"d" : @{
- @"author" : @"authorD",
- @"title" : @"titleD",
- @"rating" : [NSNumber numberWithLong:-10000]
- }
- }];
- FIRAggregateQuerySnapshot* snapshot =
- [self readSnapshotForAggregate:[testCollection
- aggregate:@[ [FIRAggregateField
- aggregateFieldForSumOfField:@"rating"] ]]];
- // Sum
- XCTAssertEqual(
- [[snapshot valueForAggregation:[FIRAggregateField aggregateFieldForSumOfField:@"rating"]]
- longLongValue],
- [[NSNumber numberWithLong:-10101] longLongValue]);
- }
- - (void)testPerformsSumThatIsPositiveInfinity {
- // TODO(sum/avg) remove the check below when sum and avg are supported in production
- XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator], @"only tested against emulator");
- FIRCollectionReference* testCollection = [self collectionRefWithDocuments:@{
- @"a" : @{
- @"author" : @"authorA",
- @"title" : @"titleA",
- @"rating" : [NSNumber numberWithDouble:DBL_MAX]
- },
- @"b" : @{
- @"author" : @"authorB",
- @"title" : @"titleB",
- @"rating" : [NSNumber numberWithDouble:DBL_MAX]
- }
- }];
- FIRAggregateQuerySnapshot* snapshot =
- [self readSnapshotForAggregate:[testCollection
- aggregate:@[ [FIRAggregateField
- aggregateFieldForSumOfField:@"rating"] ]]];
- // Sum
- XCTAssertEqual(
- [snapshot valueForAggregation:[FIRAggregateField aggregateFieldForSumOfField:@"rating"]],
- [NSNumber numberWithDouble:INFINITY]);
- }
- - (void)testPerformsSumThatIsNegativeInfinity {
- // TODO(sum/avg) remove the check below when sum and avg are supported in production
- XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator], @"only tested against emulator");
- FIRCollectionReference* testCollection = [self collectionRefWithDocuments:@{
- @"a" : @{
- @"author" : @"authorA",
- @"title" : @"titleA",
- @"rating" : [NSNumber numberWithDouble:-DBL_MAX]
- },
- @"b" : @{
- @"author" : @"authorB",
- @"title" : @"titleB",
- @"rating" : [NSNumber numberWithDouble:-DBL_MAX]
- }
- }];
- FIRAggregateQuerySnapshot* snapshot =
- [self readSnapshotForAggregate:[testCollection
- aggregate:@[ [FIRAggregateField
- aggregateFieldForSumOfField:@"rating"] ]]];
- // Sum
- XCTAssertEqual(
- [snapshot valueForAggregation:[FIRAggregateField aggregateFieldForSumOfField:@"rating"]],
- [NSNumber numberWithDouble:-INFINITY]);
- }
- - (void)testPerformsSumThatIsValidButCouldOverflowDuringAggregation {
- // TODO(sum/avg) remove the check below when sum and avg are supported in production
- XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator], @"only tested against emulator");
- FIRCollectionReference* testCollection = [self collectionRefWithDocuments:@{
- @"a" : @{@"rating" : [NSNumber numberWithDouble:DBL_MAX]},
- @"b" : @{@"rating" : [NSNumber numberWithDouble:DBL_MAX]},
- @"c" : @{@"rating" : [NSNumber numberWithDouble:-DBL_MAX]},
- @"d" : @{@"rating" : [NSNumber numberWithDouble:-DBL_MAX]},
- @"e" : @{@"rating" : [NSNumber numberWithDouble:DBL_MAX]},
- @"f" : @{@"rating" : [NSNumber numberWithDouble:-DBL_MAX]},
- @"g" : @{@"rating" : [NSNumber numberWithDouble:-DBL_MAX]},
- @"h" : @{@"rating" : [NSNumber numberWithDouble:DBL_MAX]}
- }];
- FIRAggregateQuerySnapshot* snapshot =
- [self readSnapshotForAggregate:[testCollection
- aggregate:@[ [FIRAggregateField
- aggregateFieldForSumOfField:@"rating"] ]]];
- // Sum
- XCTAssertEqual(
- [[snapshot valueForAggregation:[FIRAggregateField aggregateFieldForSumOfField:@"rating"]]
- longLongValue],
- [[NSNumber numberWithLong:0] longLongValue]);
- XCTAssertEqual(
- [[snapshot valueForAggregation:[FIRAggregateField aggregateFieldForSumOfField:@"rating"]]
- doubleValue],
- [[NSNumber numberWithLong:0] doubleValue]);
- }
- - (void)testPerformsSumOverResultSetOfZeroDocuments {
- // TODO(sum/avg) remove the check below when sum and avg are supported in production
- XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator], @"only tested against emulator");
- 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
- }
- }];
- FIRAggregateQuerySnapshot* snapshot =
- [self readSnapshotForAggregate:[[testCollection queryWhereField:@"pages" isGreaterThan:@200]
- aggregate:@[ [FIRAggregateField
- aggregateFieldForSumOfField:@"pages"] ]]];
- // Sum
- XCTAssertEqual(
- [snapshot valueForAggregation:[FIRAggregateField aggregateFieldForSumOfField:@"pages"]],
- [NSNumber numberWithLong:0L]);
- }
- - (void)testPerformsSumOnlyOnNumericFields {
- // TODO(sum/avg) remove the check below when sum and avg are supported in production
- XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator], @"only tested against emulator");
- FIRCollectionReference* testCollection = [self collectionRefWithDocuments:@{
- @"a" : @{@"rating" : [NSNumber numberWithLong:5]},
- @"b" : @{@"rating" : [NSNumber numberWithLong:4]},
- @"c" : @{@"rating" : @"3"},
- @"d" : @{@"rating" : [NSNumber numberWithLong:1]}
- }];
- FIRAggregateQuerySnapshot* snapshot =
- [self readSnapshotForAggregate:[testCollection aggregate:@[
- [FIRAggregateField aggregateFieldForCount],
- [FIRAggregateField aggregateFieldForSumOfField:@"rating"]
- ]]];
- // Count
- XCTAssertEqual(
- [[snapshot valueForAggregation:[FIRAggregateField aggregateFieldForCount]] longValue], 4L);
- // Sum
- XCTAssertEqual(
- [[snapshot valueForAggregation:[FIRAggregateField aggregateFieldForSumOfField:@"rating"]]
- longLongValue],
- [[NSNumber numberWithLong:10] longLongValue]);
- }
- - (void)testPerformsSumOfMinIEEE754 {
- // TODO(sum/avg) remove the check below when sum and avg are supported in production
- XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator], @"only tested against emulator");
- FIRCollectionReference* testCollection = [self collectionRefWithDocuments:@{
- @"a" : @{@"rating" : [NSNumber numberWithDouble:__DBL_DENORM_MIN__]}
- }];
- FIRAggregateQuerySnapshot* snapshot =
- [self readSnapshotForAggregate:[testCollection
- aggregate:@[ [FIRAggregateField
- aggregateFieldForSumOfField:@"rating"] ]]];
- // Sum
- XCTAssertEqual(
- [[snapshot valueForAggregation:[FIRAggregateField aggregateFieldForSumOfField:@"rating"]]
- doubleValue],
- [[NSNumber numberWithDouble:__DBL_DENORM_MIN__] doubleValue]);
- }
- - (void)testPerformsAverageOfVariousNumericTypes {
- // TODO(sum/avg) remove the check below when sum and avg are supported in production
- XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator], @"only tested against emulator");
- FIRCollectionReference* testCollection = [self collectionRefWithDocuments:@{
- @"a" : @{
- @"x" : @1,
- @"intToInt" : [NSNumber numberWithLong:10],
- @"floatToInt" : [NSNumber numberWithDouble:10.5],
- @"mixedToInt" : [NSNumber numberWithLong:10],
- @"floatToFloat" : [NSNumber numberWithDouble:5.5],
- @"mixedToFloat" : [NSNumber numberWithDouble:8.6],
- @"intToFloat" : [NSNumber numberWithLong:10]
- },
- @"b" : @{
- @"intToInt" : [NSNumber numberWithLong:5],
- @"floatToInt" : [NSNumber numberWithDouble:9.5],
- @"mixedToInt" : [NSNumber numberWithDouble:9.5],
- @"floatToFloat" : [NSNumber numberWithDouble:4.5],
- @"mixedToFloat" : [NSNumber numberWithLong:9],
- @"intToFloat" : [NSNumber numberWithLong:9]
- },
- @"c" : @{
- @"intToInt" : [NSNumber numberWithLong:0],
- @"floatToInt" : @"ignore",
- @"mixedToInt" : [NSNumber numberWithDouble:10.5],
- @"floatToFloat" : [NSNumber numberWithDouble:3.5],
- @"mixedToFloat" : [NSNumber numberWithLong:10],
- @"intToFloat" : @"ignore"
- }
- }];
- NSArray* testCases = @[
- @{
- @"agg" : [FIRAggregateField aggregateFieldForAverageOfField:@"intToInt"],
- @"expected" : [NSNumber numberWithLong:5]
- },
- @{
- @"agg" : [FIRAggregateField aggregateFieldForAverageOfField:@"floatToInt"],
- @"expected" : [NSNumber numberWithLong:10]
- },
- @{
- @"agg" : [FIRAggregateField aggregateFieldForAverageOfField:@"mixedToInt"],
- @"expected" : [NSNumber numberWithLong:10]
- },
- @{
- @"agg" : [FIRAggregateField aggregateFieldForAverageOfField:@"floatToFloat"],
- @"expected" : [NSNumber numberWithDouble:4.5]
- },
- @{
- @"agg" : [FIRAggregateField aggregateFieldForAverageOfField:@"mixedToFloat"],
- @"expected" : [NSNumber numberWithDouble:9.2]
- },
- @{
- @"agg" : [FIRAggregateField aggregateFieldForAverageOfField:@"intToFloat"],
- @"expected" : [NSNumber numberWithDouble:9.5]
- }
- ];
- for (NSDictionary* testCase in testCases) {
- FIRAggregateQuerySnapshot* snapshot =
- [self readSnapshotForAggregate:[testCollection aggregate:@[ testCase[@"agg"] ]]];
- // Average
- XCTAssertEqual([[snapshot valueForAggregation:testCase[@"agg"]] longValue],
- [testCase[@"expected"] longLongValue]);
- XCTAssertEqualWithAccuracy([[snapshot valueForAggregation:testCase[@"agg"]] doubleValue],
- [testCase[@"expected"] doubleValue], 0.00000000000001);
- }
- }
- - (void)testPerformsAverageCausingUnderflow {
- // TODO(sum/avg) remove the check below when sum and avg are supported in production
- XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator], @"only tested against emulator");
- FIRCollectionReference* testCollection = [self collectionRefWithDocuments:@{
- @"a" : @{@"rating" : [NSNumber numberWithDouble:__DBL_DENORM_MIN__]},
- @"b" : @{@"rating" : [NSNumber numberWithDouble:0]}
- }];
- FIRAggregateQuerySnapshot* snapshot =
- [self readSnapshotForAggregate:[testCollection aggregate:@[
- [FIRAggregateField aggregateFieldForAverageOfField:@"rating"]
- ]]];
- // Average
- XCTAssertEqual(
- [[snapshot valueForAggregation:[FIRAggregateField aggregateFieldForAverageOfField:@"rating"]]
- doubleValue],
- [[NSNumber numberWithDouble:0] doubleValue]);
- }
- - (void)testPerformsAverageOfMinIEEE754 {
- // TODO(sum/avg) remove the check below when sum and avg are supported in production
- XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator], @"only tested against emulator");
- FIRCollectionReference* testCollection = [self collectionRefWithDocuments:@{
- @"a" : @{@"rating" : [NSNumber numberWithDouble:__DBL_DENORM_MIN__]}
- }];
- FIRAggregateQuerySnapshot* snapshot =
- [self readSnapshotForAggregate:[testCollection aggregate:@[
- [FIRAggregateField aggregateFieldForAverageOfField:@"rating"]
- ]]];
- // Average
- XCTAssertEqual(
- [[snapshot valueForAggregation:[FIRAggregateField aggregateFieldForAverageOfField:@"rating"]]
- doubleValue],
- [[NSNumber numberWithDouble:__DBL_DENORM_MIN__] doubleValue]);
- }
- - (void)testPerformsAverageOverflowIEEE754DuringAccumulation {
- // TODO(sum/avg) remove the check below when sum and avg are supported in production
- XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator], @"only tested against emulator");
- FIRCollectionReference* testCollection = [self collectionRefWithDocuments:@{
- @"a" : @{@"rating" : [NSNumber numberWithDouble:DBL_MAX]},
- @"b" : @{@"rating" : [NSNumber numberWithDouble:DBL_MAX]}
- }];
- FIRAggregateQuerySnapshot* snapshot =
- [self readSnapshotForAggregate:[testCollection aggregate:@[
- [FIRAggregateField aggregateFieldForAverageOfField:@"rating"]
- ]]];
- // Average
- XCTAssertEqual(
- [[snapshot valueForAggregation:[FIRAggregateField aggregateFieldForAverageOfField:@"rating"]]
- doubleValue],
- [[NSNumber numberWithDouble:INFINITY] doubleValue]);
- }
- - (void)testPerformsAverageOverResultSetOfZeroDocuments {
- // TODO(sum/avg) remove the check below when sum and avg are supported in production
- XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator], @"only tested against emulator");
- 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
- }
- }];
- FIRAggregateQuerySnapshot* snapshot = [self
- readSnapshotForAggregate:[[testCollection queryWhereField:@"pages" isGreaterThan:@200]
- aggregate:@[ [FIRAggregateField
- aggregateFieldForAverageOfField:@"pages"] ]]];
- // Average
- XCTAssertEqual(
- [snapshot valueForAggregation:[FIRAggregateField aggregateFieldForAverageOfField:@"pages"]],
- [NSNull null]);
- }
- - (void)testPerformsAverageOnlyOnNumericFields {
- // TODO(sum/avg) remove the check below when sum and avg are supported in production
- XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator], @"only tested against emulator");
- FIRCollectionReference* testCollection = [self collectionRefWithDocuments:@{
- @"a" : @{@"rating" : [NSNumber numberWithLong:5]},
- @"b" : @{@"rating" : [NSNumber numberWithLong:4]},
- @"c" : @{@"rating" : @"3"},
- @"d" : @{@"rating" : [NSNumber numberWithLong:6]}
- }];
- FIRAggregateQuerySnapshot* snapshot =
- [self readSnapshotForAggregate:[testCollection aggregate:@[
- [FIRAggregateField aggregateFieldForCount],
- [FIRAggregateField aggregateFieldForAverageOfField:@"rating"]
- ]]];
- // Count
- XCTAssertEqual(
- [[snapshot valueForAggregation:[FIRAggregateField aggregateFieldForCount]] longValue], 4L);
- // Average
- XCTAssertEqual(
- [[snapshot valueForAggregation:[FIRAggregateField aggregateFieldForAverageOfField:@"rating"]]
- doubleValue],
- [[NSNumber numberWithDouble:5] doubleValue]);
- }
- @end
|