FIRAggregateTests.mm 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445
  1. /*
  2. * Copyright 2023 Google LLC
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <FirebaseFirestore/FIRFieldPath.h>
  17. #import <FirebaseFirestore/FirebaseFirestore.h>
  18. #import <XCTest/XCTest.h>
  19. // TODO(sum/avg) update these imports with public imports when sum/avg is public
  20. #import "Firestore/Source/API/FIRAggregateField.h"
  21. #import "Firestore/Source/API/FIRAggregateQuerySnapshot+Internal.h"
  22. #import "Firestore/Source/API/FIRQuery+Internal.h"
  23. #import "Firestore/Example/Tests/Util/FSTIntegrationTestCase.h"
  24. @interface FIRAggregateTests : FSTIntegrationTestCase
  25. @end
  26. @implementation FIRAggregateTests
  27. - (void)testCountAggregateFieldQueryEquals {
  28. FIRCollectionReference* coll1 = [self collectionRefWithDocuments:@{}];
  29. FIRCollectionReference* coll1Same = [[coll1 firestore] collectionWithPath:[coll1 path]];
  30. FIRAggregateQuery* query1 = [coll1 aggregate:@[ [FIRAggregateField aggregateFieldForCount] ]];
  31. FIRAggregateQuery* query1Same =
  32. [coll1Same aggregate:@[ [FIRAggregateField aggregateFieldForCount] ]];
  33. FIRAggregateQuery* query1DiffAgg =
  34. [coll1Same aggregate:@[ [FIRAggregateField aggregateFieldForSumOfField:@"baz"] ]];
  35. FIRCollectionReference* sub = [[coll1 documentWithPath:@"bar"] collectionWithPath:@"baz"];
  36. FIRAggregateQuery* query2 = [[[sub queryWhereField:@"a" isEqualTo:@1] queryLimitedTo:100]
  37. aggregate:@[ [FIRAggregateField aggregateFieldForCount] ]];
  38. FIRAggregateQuery* query2Same = [[[sub queryWhereField:@"a"
  39. isEqualTo:@1] queryLimitedTo:100] count];
  40. FIRAggregateQuery* query3 = [[[sub queryWhereField:@"b" isEqualTo:@1] queryOrderedByField:@"c"]
  41. aggregate:@[ [FIRAggregateField aggregateFieldForCount] ]];
  42. FIRAggregateQuery* query3Same = [[[sub queryWhereField:@"b"
  43. isEqualTo:@1] queryOrderedByField:@"c"] count];
  44. XCTAssertEqualObjects(query1, query1Same);
  45. XCTAssertEqualObjects(query2, query2Same);
  46. XCTAssertEqualObjects(query3, query3Same);
  47. XCTAssertEqual([query1 hash], [query1Same hash]);
  48. XCTAssertEqual([query2 hash], [query2Same hash]);
  49. XCTAssertEqual([query3 hash], [query3Same hash]);
  50. XCTAssertFalse([query1 isEqual:nil]);
  51. XCTAssertFalse([query1 isEqual:@"string"]);
  52. XCTAssertFalse([query1 isEqual:query2]);
  53. XCTAssertFalse([query2 isEqual:query3]);
  54. XCTAssertNotEqual([query1 hash], [query1DiffAgg hash]);
  55. XCTAssertNotEqual([query1 hash], [query2 hash]);
  56. XCTAssertNotEqual([query2 hash], [query3 hash]);
  57. }
  58. - (void)testSumAggregateFieldQueryEquals {
  59. FIRCollectionReference* coll1 = [self collectionRefWithDocuments:@{}];
  60. FIRCollectionReference* coll1Same = [[coll1 firestore] collectionWithPath:[coll1 path]];
  61. FIRAggregateQuery* query1 =
  62. [coll1 aggregate:@[ [FIRAggregateField aggregateFieldForSumOfField:@"baz"] ]];
  63. FIRAggregateQuery* query1Same =
  64. [coll1Same aggregate:@[ [FIRAggregateField aggregateFieldForSumOfField:@"baz"] ]];
  65. FIRAggregateQuery* query1WithFieldPath =
  66. [coll1Same aggregate:@[ [FIRAggregateField
  67. aggregateFieldForSumOfFieldPath:[[FIRFieldPath alloc]
  68. initWithFields:@[ @"baz" ]]] ]];
  69. FIRAggregateQuery* query1DiffAgg =
  70. [coll1Same aggregate:@[ [FIRAggregateField aggregateFieldForCount] ]];
  71. FIRCollectionReference* sub = [[coll1 documentWithPath:@"bar"] collectionWithPath:@"baz"];
  72. FIRAggregateQuery* query2 = [[[sub queryWhereField:@"a" isEqualTo:@1] queryLimitedTo:100]
  73. aggregate:@[ [FIRAggregateField aggregateFieldForSumOfField:@"baz"] ]];
  74. FIRAggregateQuery* query2Same = [[[sub queryWhereField:@"a" isEqualTo:@1] queryLimitedTo:100]
  75. aggregate:@[ [FIRAggregateField aggregateFieldForSumOfField:@"baz"] ]];
  76. FIRAggregateQuery* query3 = [[[sub queryWhereField:@"b" isEqualTo:@1] queryOrderedByField:@"c"]
  77. aggregate:@[ [FIRAggregateField aggregateFieldForSumOfField:@"baz"] ]];
  78. FIRAggregateQuery* query3Same = [[[sub queryWhereField:@"b"
  79. isEqualTo:@1] queryOrderedByField:@"c"]
  80. aggregate:@[ [FIRAggregateField aggregateFieldForSumOfField:@"baz"] ]];
  81. XCTAssertEqualObjects(query1, query1Same);
  82. XCTAssertEqualObjects(query1, query1WithFieldPath);
  83. XCTAssertEqualObjects(query2, query2Same);
  84. XCTAssertEqualObjects(query3, query3Same);
  85. XCTAssertEqual([query1 hash], [query1Same hash]);
  86. XCTAssertEqual([query1 hash], [query1WithFieldPath hash]);
  87. XCTAssertEqual([query2 hash], [query2Same hash]);
  88. XCTAssertEqual([query3 hash], [query3Same hash]);
  89. XCTAssertFalse([query1 isEqual:nil]);
  90. XCTAssertFalse([query1 isEqual:@"string"]);
  91. XCTAssertFalse([query1 isEqual:query2]);
  92. XCTAssertFalse([query2 isEqual:query3]);
  93. XCTAssertNotEqual([query1 hash], [query1DiffAgg hash]);
  94. XCTAssertNotEqual([query1 hash], [query2 hash]);
  95. XCTAssertNotEqual([query2 hash], [query3 hash]);
  96. }
  97. - (void)testAverageAggregateFieldQueryEquals {
  98. FIRCollectionReference* coll1 = [self collectionRefWithDocuments:@{}];
  99. FIRCollectionReference* coll1Same = [[coll1 firestore] collectionWithPath:[coll1 path]];
  100. FIRAggregateQuery* query1 =
  101. [coll1 aggregate:@[ [FIRAggregateField aggregateFieldForAverageOfField:@"baz"] ]];
  102. FIRAggregateQuery* query1Same =
  103. [coll1Same aggregate:@[ [FIRAggregateField aggregateFieldForAverageOfField:@"baz"] ]];
  104. FIRAggregateQuery* query1WithFieldPath = [coll1Same aggregate:@[
  105. [FIRAggregateField
  106. aggregateFieldForAverageOfFieldPath:[[FIRFieldPath alloc] initWithFields:@[ @"baz" ]]]
  107. ]];
  108. FIRAggregateQuery* query1DiffAgg =
  109. [coll1Same aggregate:@[ [FIRAggregateField aggregateFieldForCount] ]];
  110. FIRCollectionReference* sub = [[coll1 documentWithPath:@"bar"] collectionWithPath:@"baz"];
  111. FIRAggregateQuery* query2 = [[[sub queryWhereField:@"a" isEqualTo:@1] queryLimitedTo:100]
  112. aggregate:@[ [FIRAggregateField aggregateFieldForAverageOfField:@"baz"] ]];
  113. FIRAggregateQuery* query2Same = [[[sub queryWhereField:@"a" isEqualTo:@1] queryLimitedTo:100]
  114. aggregate:@[ [FIRAggregateField aggregateFieldForAverageOfField:@"baz"] ]];
  115. FIRAggregateQuery* query3 = [[[sub queryWhereField:@"b" isEqualTo:@1] queryOrderedByField:@"c"]
  116. aggregate:@[ [FIRAggregateField aggregateFieldForAverageOfField:@"baz"] ]];
  117. FIRAggregateQuery* query3Same = [[[sub queryWhereField:@"b"
  118. isEqualTo:@1] queryOrderedByField:@"c"]
  119. aggregate:@[ [FIRAggregateField aggregateFieldForAverageOfField:@"baz"] ]];
  120. XCTAssertEqualObjects(query1, query1Same);
  121. XCTAssertEqualObjects(query1, query1WithFieldPath);
  122. XCTAssertEqualObjects(query2, query2Same);
  123. XCTAssertEqualObjects(query3, query3Same);
  124. XCTAssertEqual([query1 hash], [query1Same hash]);
  125. XCTAssertEqual([query1 hash], [query1WithFieldPath hash]);
  126. XCTAssertEqual([query2 hash], [query2Same hash]);
  127. XCTAssertEqual([query3 hash], [query3Same hash]);
  128. XCTAssertFalse([query1 isEqual:nil]);
  129. XCTAssertFalse([query1 isEqual:@"string"]);
  130. XCTAssertFalse([query1 isEqual:query2]);
  131. XCTAssertFalse([query2 isEqual:query3]);
  132. XCTAssertNotEqual([query1 hash], [query1DiffAgg hash]);
  133. XCTAssertNotEqual([query1 hash], [query2 hash]);
  134. XCTAssertNotEqual([query2 hash], [query3 hash]);
  135. }
  136. - (void)testAggregateFieldQueryNotEquals {
  137. FIRCollectionReference* coll = [self collectionRefWithDocuments:@{}];
  138. FIRAggregateQuery* query1 = [coll aggregate:@[ [FIRAggregateField aggregateFieldForCount] ]];
  139. FIRAggregateQuery* query2 =
  140. [coll aggregate:@[ [FIRAggregateField aggregateFieldForSumOfField:@"baz"] ]];
  141. FIRAggregateQuery* query3 =
  142. [coll aggregate:@[ [FIRAggregateField aggregateFieldForAverageOfField:@"baz"] ]];
  143. XCTAssertNotEqualObjects(query1, query2);
  144. XCTAssertNotEqualObjects(query2, query3);
  145. XCTAssertNotEqualObjects(query3, query1);
  146. XCTAssertNotEqual([query1 hash], [query2 hash]);
  147. XCTAssertNotEqual([query2 hash], [query3 hash]);
  148. XCTAssertNotEqual([query3 hash], [query1 hash]);
  149. FIRQuery* baseQuery = [[[[coll documentWithPath:@"bar"] collectionWithPath:@"baz"]
  150. queryWhereField:@"a"
  151. isEqualTo:@1] queryLimitedTo:100];
  152. FIRAggregateQuery* query4 = [baseQuery aggregate:@[ [FIRAggregateField aggregateFieldForCount] ]];
  153. FIRAggregateQuery* query5 =
  154. [baseQuery aggregate:@[ [FIRAggregateField aggregateFieldForSumOfField:@"baz"] ]];
  155. FIRAggregateQuery* query6 =
  156. [baseQuery aggregate:@[ [FIRAggregateField aggregateFieldForAverageOfField:@"baz"] ]];
  157. XCTAssertNotEqualObjects(query4, query5);
  158. XCTAssertNotEqualObjects(query5, query6);
  159. XCTAssertNotEqualObjects(query6, query4);
  160. XCTAssertNotEqual([query4 hash], [query5 hash]);
  161. XCTAssertNotEqual([query5 hash], [query6 hash]);
  162. XCTAssertNotEqual([query6 hash], [query4 hash]);
  163. }
  164. - (void)testCanRunAggregateQuery {
  165. // TODO(sum/avg) remove the check below when sum and avg are supported in production
  166. XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator], @"only tested against emulator");
  167. FIRCollectionReference* testCollection = [self collectionRefWithDocuments:@{
  168. @"a" : @{
  169. @"author" : @"authorA",
  170. @"title" : @"titleA",
  171. @"pages" : @100,
  172. @"height" : @24.5,
  173. @"weight" : @24.1,
  174. @"foo" : @1,
  175. @"bar" : @2,
  176. @"baz" : @3
  177. },
  178. @"b" : @{
  179. @"author" : @"authorB",
  180. @"title" : @"titleB",
  181. @"pages" : @50,
  182. @"height" : @25.5,
  183. @"weight" : @75.5,
  184. @"foo" : @1,
  185. @"bar" : @2,
  186. @"baz" : @3
  187. }
  188. }];
  189. FIRAggregateQuerySnapshot* snapshot =
  190. [self readSnapshotForAggregate:[testCollection aggregate:@[
  191. [FIRAggregateField aggregateFieldForCount],
  192. [FIRAggregateField aggregateFieldForSumOfField:@"pages"],
  193. [FIRAggregateField aggregateFieldForSumOfField:@"weight"],
  194. [FIRAggregateField aggregateFieldForAverageOfField:@"pages"],
  195. [FIRAggregateField aggregateFieldForAverageOfField:@"weight"]
  196. ]]];
  197. // Count
  198. XCTAssertEqual([snapshot valueForAggregation:[FIRAggregateField aggregateFieldForCount]],
  199. [NSNumber numberWithLong:2L]);
  200. XCTAssertEqual([snapshot count], [NSNumber numberWithLong:2L]);
  201. // Sum
  202. XCTAssertEqual(
  203. [snapshot valueForAggregation:[FIRAggregateField aggregateFieldForSumOfField:@"pages"]],
  204. [NSNumber numberWithLong:150L], );
  205. XCTAssertEqual(
  206. [[snapshot valueForAggregation:[FIRAggregateField aggregateFieldForSumOfField:@"weight"]]
  207. doubleValue],
  208. 99.6);
  209. // Average
  210. XCTAssertEqual(
  211. [snapshot valueForAggregation:[FIRAggregateField aggregateFieldForAverageOfField:@"pages"]],
  212. [NSNumber numberWithDouble:75.0]);
  213. XCTAssertEqual(
  214. [[snapshot valueForAggregation:[FIRAggregateField aggregateFieldForAverageOfField:@"weight"]]
  215. doubleValue],
  216. 49.8);
  217. }
  218. - (void)testCanRunEmptyAggregateQuery {
  219. // TODO(sum/avg) remove the check below when sum and avg are supported in production
  220. XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator], @"only tested against emulator");
  221. FIRCollectionReference* testCollection = [self collectionRefWithDocuments:@{
  222. @"a" : @{
  223. @"author" : @"authorA",
  224. @"title" : @"titleA",
  225. @"pages" : @100,
  226. @"height" : @24.5,
  227. @"weight" : @24.1,
  228. @"foo" : @1,
  229. @"bar" : @2,
  230. @"baz" : @3
  231. },
  232. @"b" : @{
  233. @"author" : @"authorB",
  234. @"title" : @"titleB",
  235. @"pages" : @50,
  236. @"height" : @25.5,
  237. @"weight" : @75.5,
  238. @"foo" : @1,
  239. @"bar" : @2,
  240. @"baz" : @3
  241. }
  242. }];
  243. FIRAggregateQuery* emptyQuery = [testCollection aggregate:@[]];
  244. __block NSError* result;
  245. XCTestExpectation* expectation = [self expectationWithDescription:@"aggregate result"];
  246. [emptyQuery aggregationWithSource:FIRAggregateSourceServer
  247. completion:^(FIRAggregateQuerySnapshot* snapshot, NSError* error) {
  248. XCTAssertNil(snapshot);
  249. result = error;
  250. [expectation fulfill];
  251. }];
  252. [self awaitExpectation:expectation];
  253. XCTAssertNotNil(result);
  254. XCTAssertTrue([[result localizedDescription] containsString:@"Aggregations can not be empty"]);
  255. }
  256. - (void)testAggregateFieldQuerySnapshotEquality {
  257. // TODO(sum/avg) remove the check below when sum and avg are supported in production
  258. XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator], @"only tested against emulator");
  259. FIRCollectionReference* testCollection = [self collectionRefWithDocuments:@{
  260. @"a" : @{
  261. @"author" : @"authorA",
  262. @"title" : @"titleA",
  263. @"pages" : @100,
  264. @"height" : @24.5,
  265. @"weight" : @24.1,
  266. @"foo" : @1,
  267. @"bar" : @2,
  268. @"baz" : @3
  269. },
  270. @"b" : @{
  271. @"author" : @"authorB",
  272. @"title" : @"titleB",
  273. @"pages" : @50,
  274. @"height" : @25.5,
  275. @"weight" : @75.5,
  276. @"foo" : @1,
  277. @"bar" : @2,
  278. @"baz" : @3
  279. }
  280. }];
  281. FIRAggregateQuerySnapshot* snapshot1 =
  282. [self readSnapshotForAggregate:[testCollection aggregate:@[
  283. [FIRAggregateField aggregateFieldForCount],
  284. [FIRAggregateField aggregateFieldForSumOfField:@"pages"],
  285. [FIRAggregateField aggregateFieldForSumOfField:@"weight"],
  286. [FIRAggregateField aggregateFieldForAverageOfField:@"pages"],
  287. [FIRAggregateField aggregateFieldForAverageOfField:@"weight"]
  288. ]]];
  289. FIRAggregateQuerySnapshot* snapshot2 =
  290. [self readSnapshotForAggregate:[testCollection aggregate:@[
  291. [FIRAggregateField aggregateFieldForCount],
  292. [FIRAggregateField aggregateFieldForSumOfField:@"pages"],
  293. [FIRAggregateField aggregateFieldForSumOfField:@"weight"],
  294. [FIRAggregateField aggregateFieldForAverageOfField:@"pages"],
  295. [FIRAggregateField aggregateFieldForAverageOfField:@"weight"]
  296. ]]];
  297. // different aggregates
  298. FIRAggregateQuerySnapshot* snapshot3 =
  299. [self readSnapshotForAggregate:[testCollection aggregate:@[
  300. [FIRAggregateField aggregateFieldForCount],
  301. [FIRAggregateField aggregateFieldForSumOfField:@"pages"],
  302. [FIRAggregateField aggregateFieldForSumOfField:@"weight"],
  303. [FIRAggregateField aggregateFieldForAverageOfField:@"pages"]
  304. ]]];
  305. // different data set
  306. FIRAggregateQuerySnapshot* snapshot4 = [self
  307. readSnapshotForAggregate:[[testCollection queryWhereField:@"pages" isGreaterThan:@50]
  308. aggregate:@[
  309. [FIRAggregateField aggregateFieldForCount],
  310. [FIRAggregateField aggregateFieldForSumOfField:@"pages"],
  311. [FIRAggregateField aggregateFieldForSumOfField:@"weight"],
  312. [FIRAggregateField aggregateFieldForAverageOfField:@"pages"],
  313. [FIRAggregateField
  314. aggregateFieldForAverageOfField:@"weight"]
  315. ]]];
  316. XCTAssertEqualObjects(snapshot1, snapshot2);
  317. XCTAssertNotEqualObjects(snapshot1, snapshot3);
  318. XCTAssertNotEqualObjects(snapshot1, snapshot4);
  319. XCTAssertNotEqualObjects(snapshot3, snapshot4);
  320. XCTAssertEqual([snapshot1 hash], [snapshot2 hash]);
  321. XCTAssertNotEqual([snapshot1 hash], [snapshot3 hash]);
  322. XCTAssertNotEqual([snapshot1 hash], [snapshot4 hash]);
  323. XCTAssertNotEqual([snapshot3 hash], [snapshot4 hash]);
  324. }
  325. - (void)testAllowsAliasesLongerThan1500Bytes {
  326. // TODO(sum/avg) remove the check below when sum and avg are supported in production
  327. XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator], @"only tested against emulator");
  328. // The longest field name allowed is 1500. The alias chosen by the client is <op>_<fieldName>.
  329. // If the field name is
  330. // 1500 bytes, the alias will be longer than 1500, which is the limit for aliases. This is to
  331. // make sure the client
  332. // can handle this corner case correctly.
  333. NSString* longField = [@"" stringByPaddingToLength:1499
  334. withString:@"0123456789"
  335. startingAtIndex:0];
  336. FIRCollectionReference* testCollection =
  337. [self collectionRefWithDocuments:@{@"a" : @{longField : @1}, @"b" : @{longField : @2}}];
  338. FIRAggregateQuerySnapshot* snapshot =
  339. [self readSnapshotForAggregate:[testCollection
  340. aggregate:@[ [FIRAggregateField
  341. aggregateFieldForSumOfField:longField] ]]];
  342. // Sum
  343. XCTAssertEqual(
  344. [snapshot valueForAggregation:[FIRAggregateField aggregateFieldForSumOfField:longField]],
  345. [NSNumber numberWithLong:3], );
  346. }
  347. - (void)testCanGetDuplicateAggregations {
  348. // TODO(sum/avg) remove the check below when sum and avg are supported in production
  349. XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator], @"only tested against emulator");
  350. FIRCollectionReference* testCollection = [self collectionRefWithDocuments:@{
  351. @"a" : @{
  352. @"author" : @"authorA",
  353. @"title" : @"titleA",
  354. @"pages" : @100,
  355. @"height" : @24.5,
  356. @"weight" : @24.1,
  357. @"foo" : @1,
  358. @"bar" : @2,
  359. @"baz" : @3
  360. },
  361. @"b" : @{
  362. @"author" : @"authorB",
  363. @"title" : @"titleB",
  364. @"pages" : @50,
  365. @"height" : @25.5,
  366. @"weight" : @75.5,
  367. @"foo" : @1,
  368. @"bar" : @2,
  369. @"baz" : @3
  370. }
  371. }];
  372. FIRAggregateQuerySnapshot* snapshot =
  373. [self readSnapshotForAggregate:[testCollection aggregate:@[
  374. [FIRAggregateField aggregateFieldForCount],
  375. [FIRAggregateField aggregateFieldForSumOfField:@"pages"],
  376. [FIRAggregateField aggregateFieldForSumOfField:@"pages"]
  377. ]]];
  378. // Count
  379. XCTAssertEqual([snapshot valueForAggregation:[FIRAggregateField aggregateFieldForCount]],
  380. [NSNumber numberWithLong:2L]);
  381. // Sum
  382. XCTAssertEqual(
  383. [snapshot valueForAggregation:[FIRAggregateField aggregateFieldForSumOfField:@"pages"]],
  384. [NSNumber numberWithLong:150L], );
  385. }
  386. - (void)testTerminateDoesNotCrashWithFlyingAggregateQuery {
  387. // TODO(sum/avg) remove the check below when sum and avg are supported in production
  388. XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator], @"only tested against emulator");
  389. FIRCollectionReference* testCollection = [self collectionRefWithDocuments:@{
  390. @"a" : @{
  391. @"author" : @"authorA",
  392. @"title" : @"titleA",
  393. @"pages" : @100,
  394. @"height" : @24.5,
  395. @"weight" : @24.1,
  396. @"foo" : @1,
  397. @"bar" : @2,
  398. @"baz" : @3
  399. },
  400. @"b" : @{
  401. @"author" : @"authorB",
  402. @"title" : @"titleB",
  403. @"pages" : @50,
  404. @"height" : @25.5,
  405. @"weight" : @75.5,
  406. @"foo" : @1,
  407. @"bar" : @2,
  408. @"baz" : @3
  409. }
  410. }];
  411. FIRAggregateQuery* aggQuery = [testCollection aggregate:@[
  412. [FIRAggregateField aggregateFieldForCount],
  413. [FIRAggregateField aggregateFieldForSumOfField:@"pages"],
  414. [FIRAggregateField aggregateFieldForAverageOfField:@"pages"]
  415. ]];
  416. __block FIRAggregateQuerySnapshot* result;
  417. XCTestExpectation* expectation = [self expectationWithDescription:@"aggregate result"];
  418. [aggQuery aggregationWithSource:FIRAggregateSourceServer
  419. completion:^(FIRAggregateQuerySnapshot* snapshot, NSError* error) {
  420. XCTAssertNil(error);
  421. result = snapshot;
  422. [expectation fulfill];
  423. }];
  424. [self awaitExpectation:expectation];
  425. // Count
  426. XCTAssertEqual([result valueForAggregation:[FIRAggregateField aggregateFieldForCount]],
  427. [NSNumber numberWithLong:2L]);
  428. // Sum
  429. XCTAssertEqual(
  430. [result valueForAggregation:[FIRAggregateField aggregateFieldForSumOfField:@"pages"]],
  431. [NSNumber numberWithLong:150L], );
  432. }
  433. - (void)testCanPerformMaxAggregations {
  434. // TODO(sum/avg) remove the check below when sum and avg are supported in production
  435. XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator], @"only tested against emulator");
  436. FIRCollectionReference* testCollection = [self collectionRefWithDocuments:@{
  437. @"a" : @{
  438. @"author" : @"authorA",
  439. @"title" : @"titleA",
  440. @"pages" : @100,
  441. @"height" : @24.5,
  442. @"weight" : @24.1,
  443. @"foo" : @1,
  444. @"bar" : @2,
  445. @"baz" : @3
  446. },
  447. @"b" : @{
  448. @"author" : @"authorB",
  449. @"title" : @"titleB",
  450. @"pages" : @50,
  451. @"height" : @25.5,
  452. @"weight" : @75.5,
  453. @"foo" : @1,
  454. @"bar" : @2,
  455. @"baz" : @3
  456. }
  457. }];
  458. // Max is 5, do not exceed
  459. FIRAggregateQuerySnapshot* snapshot =
  460. [self readSnapshotForAggregate:[testCollection aggregate:@[
  461. [FIRAggregateField aggregateFieldForCount],
  462. [FIRAggregateField aggregateFieldForSumOfField:@"pages"],
  463. [FIRAggregateField aggregateFieldForSumOfField:@"weight"],
  464. [FIRAggregateField aggregateFieldForAverageOfField:@"pages"],
  465. [FIRAggregateField aggregateFieldForAverageOfField:@"weight"]
  466. ]]];
  467. // Assert
  468. XCTAssertEqual([snapshot valueForAggregation:[FIRAggregateField aggregateFieldForCount]],
  469. [NSNumber numberWithLong:2L]);
  470. XCTAssertEqual(
  471. [snapshot valueForAggregation:[FIRAggregateField aggregateFieldForSumOfField:@"pages"]],
  472. [NSNumber numberWithLong:150L], );
  473. XCTAssertEqual(
  474. [[snapshot valueForAggregation:[FIRAggregateField aggregateFieldForSumOfField:@"weight"]]
  475. doubleValue],
  476. 99.6);
  477. XCTAssertEqual(
  478. [snapshot valueForAggregation:[FIRAggregateField aggregateFieldForAverageOfField:@"pages"]],
  479. [NSNumber numberWithDouble:75.0]);
  480. XCTAssertEqual(
  481. [[snapshot valueForAggregation:[FIRAggregateField aggregateFieldForAverageOfField:@"weight"]]
  482. doubleValue],
  483. 49.8);
  484. }
  485. - (void)testCannotPerformMoreThanMaxAggregations {
  486. // TODO(sum/avg) remove the check below when sum and avg are supported in production
  487. XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator], @"only tested against emulator");
  488. FIRCollectionReference* testCollection = [self collectionRefWithDocuments:@{
  489. @"a" : @{
  490. @"author" : @"authorA",
  491. @"title" : @"titleA",
  492. @"pages" : @100,
  493. @"height" : @24.5,
  494. @"weight" : @24.1,
  495. @"foo" : @1,
  496. @"bar" : @2,
  497. @"baz" : @3
  498. },
  499. @"b" : @{
  500. @"author" : @"authorB",
  501. @"title" : @"titleB",
  502. @"pages" : @50,
  503. @"height" : @25.5,
  504. @"weight" : @75.5,
  505. @"foo" : @1,
  506. @"bar" : @2,
  507. @"baz" : @3
  508. }
  509. }];
  510. // Max is 5, we're attempting 6. I also like to live dangerously.
  511. FIRAggregateQuery* query = [testCollection aggregate:@[
  512. [FIRAggregateField aggregateFieldForCount],
  513. [FIRAggregateField aggregateFieldForSumOfField:@"pages"],
  514. [FIRAggregateField aggregateFieldForSumOfField:@"weight"],
  515. [FIRAggregateField aggregateFieldForAverageOfField:@"pages"],
  516. [FIRAggregateField aggregateFieldForAverageOfField:@"weight"],
  517. [FIRAggregateField aggregateFieldForAverageOfField:@"foo"]
  518. ]];
  519. __block NSError* result;
  520. XCTestExpectation* expectation = [self expectationWithDescription:@"aggregate result"];
  521. [query aggregationWithSource:FIRAggregateSourceServer
  522. completion:^(FIRAggregateQuerySnapshot* snapshot, NSError* error) {
  523. XCTAssertNil(snapshot);
  524. result = error;
  525. [expectation fulfill];
  526. }];
  527. [self awaitExpectation:expectation];
  528. XCTAssertNotNil(result);
  529. XCTAssertTrue([[result localizedDescription] containsString:@"maximum number of aggregations"]);
  530. }
  531. - (void)testCanRunAggregateCollectionGroupQuery {
  532. // TODO(sum/avg) remove the check below when sum and avg are supported in production
  533. XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator], @"only tested against emulator");
  534. NSString* collectionGroup =
  535. [NSString stringWithFormat:@"%@%@", @"b",
  536. [self.db collectionWithPath:@"foo"].documentWithAutoID.documentID];
  537. NSArray* docPathFormats = @[
  538. @"abc/123/%@/cg-doc1", @"abc/123/%@/cg-doc2", @"%@/cg-doc3", @"%@/cg-doc4",
  539. @"def/456/%@/cg-doc5", @"%@/virtual-doc/nested-coll/not-cg-doc", @"x%@/not-cg-doc",
  540. @"%@x/not-cg-doc", @"abc/123/%@x/not-cg-doc", @"abc/123/x%@/not-cg-doc", @"abc/%@"
  541. ];
  542. FIRWriteBatch* batch = self.db.batch;
  543. for (NSString* format in docPathFormats) {
  544. NSString* path = [NSString stringWithFormat:format, collectionGroup];
  545. [batch setData:@{@"x" : @2} forDocument:[self.db documentWithPath:path]];
  546. }
  547. XCTestExpectation* expectation = [self expectationWithDescription:@"commit"];
  548. [batch commitWithCompletion:^(NSError* error) {
  549. XCTAssertNil(error);
  550. [expectation fulfill];
  551. }];
  552. [self awaitExpectation:expectation];
  553. FIRAggregateQuerySnapshot* snapshot =
  554. [self readSnapshotForAggregate:[[self.db collectionGroupWithID:collectionGroup] aggregate:@[
  555. [FIRAggregateField aggregateFieldForCount],
  556. [FIRAggregateField aggregateFieldForSumOfField:@"x"],
  557. [FIRAggregateField aggregateFieldForAverageOfField:@"x"]
  558. ]]];
  559. // "cg-doc1", "cg-doc2", "cg-doc3", "cg-doc4", "cg-doc5",
  560. XCTAssertEqual([snapshot valueForAggregation:[FIRAggregateField aggregateFieldForCount]],
  561. [NSNumber numberWithLong:5L]);
  562. XCTAssertEqual(
  563. [snapshot valueForAggregation:[FIRAggregateField aggregateFieldForSumOfField:@"x"]],
  564. [NSNumber numberWithLong:10L]);
  565. XCTAssertEqual(
  566. [snapshot valueForAggregation:[FIRAggregateField aggregateFieldForAverageOfField:@"x"]],
  567. [NSNumber numberWithDouble:2.0]);
  568. }
  569. - (void)testPerformsAggregationsWhenNaNExistsForSomeFieldValues {
  570. // TODO(sum/avg) remove the check below when sum and avg are supported in production
  571. XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator], @"only tested against emulator");
  572. FIRCollectionReference* testCollection = [self collectionRefWithDocuments:@{
  573. @"a" : @{
  574. @"author" : @"authorA",
  575. @"title" : @"titleA",
  576. @"pages" : @100,
  577. @"year" : @1980,
  578. @"rating" : @5
  579. },
  580. @"b" : @{
  581. @"author" : @"authorB",
  582. @"title" : @"titleB",
  583. @"pages" : @50,
  584. @"year" : @2020,
  585. @"rating" : @4
  586. },
  587. @"c" : @{
  588. @"author" : @"authorC",
  589. @"title" : @"titleC",
  590. @"pages" : @100,
  591. @"year" : @1980,
  592. @"rating" : [NSNumber numberWithFloat:NAN]
  593. },
  594. @"d" : @{
  595. @"author" : @"authorD",
  596. @"title" : @"titleD",
  597. @"pages" : @50,
  598. @"year" : @2020,
  599. @"rating" : @0
  600. }
  601. }];
  602. FIRAggregateQuerySnapshot* snapshot =
  603. [self readSnapshotForAggregate:[testCollection aggregate:@[
  604. [FIRAggregateField aggregateFieldForSumOfField:@"rating"],
  605. [FIRAggregateField aggregateFieldForSumOfField:@"pages"],
  606. [FIRAggregateField aggregateFieldForAverageOfField:@"rating"],
  607. [FIRAggregateField aggregateFieldForAverageOfField:@"year"]
  608. ]]];
  609. // Sum
  610. XCTAssertEqual(
  611. [snapshot valueForAggregation:[FIRAggregateField aggregateFieldForSumOfField:@"rating"]],
  612. [NSNumber numberWithDouble:NAN]);
  613. XCTAssertEqual(
  614. [[snapshot valueForAggregation:[FIRAggregateField aggregateFieldForSumOfField:@"pages"]]
  615. longValue],
  616. 300L);
  617. // Average
  618. XCTAssertEqual(
  619. [snapshot valueForAggregation:[FIRAggregateField aggregateFieldForAverageOfField:@"rating"]],
  620. [NSNumber numberWithDouble:NAN]);
  621. XCTAssertEqual(
  622. [[snapshot valueForAggregation:[FIRAggregateField aggregateFieldForAverageOfField:@"year"]]
  623. doubleValue],
  624. 2000.0);
  625. }
  626. - (void)testThrowsAnErrorWhenGettingTheResultOfAnUnrequestedAggregation {
  627. // TODO(sum/avg) remove the check below when sum and avg are supported in production
  628. XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator], @"only tested against emulator");
  629. FIRCollectionReference* testCollection = [self collectionRefWithDocuments:@{
  630. @"a" : @{
  631. @"author" : @"authorA",
  632. @"title" : @"titleA",
  633. @"pages" : @100,
  634. @"height" : @24.5,
  635. @"weight" : @24.1,
  636. @"foo" : @1,
  637. @"bar" : @2,
  638. @"baz" : @3
  639. },
  640. @"b" : @{
  641. @"author" : @"authorB",
  642. @"title" : @"titleB",
  643. @"pages" : @50,
  644. @"height" : @25.5,
  645. @"weight" : @75.5,
  646. @"foo" : @1,
  647. @"bar" : @2,
  648. @"baz" : @3
  649. }
  650. }];
  651. FIRAggregateQuerySnapshot* snapshot =
  652. [self readSnapshotForAggregate:[testCollection
  653. aggregate:@[ [FIRAggregateField
  654. aggregateFieldForSumOfField:@"pages"] ]]];
  655. @try {
  656. [snapshot count];
  657. XCTAssertTrue(false, "Exception expected");
  658. } @catch (NSException* exception) {
  659. XCTAssertEqualObjects(exception.name, @"FIRInvalidArgumentException");
  660. XCTAssertEqualObjects(exception.reason,
  661. @"'count()' was not requested in the aggregation query.");
  662. }
  663. @
  664. try {
  665. [snapshot valueForAggregation:[FIRAggregateField aggregateFieldForSumOfField:@"foo"]];
  666. XCTAssertTrue(false, "Exception expected");
  667. } @catch (NSException* exception) {
  668. XCTAssertEqualObjects(exception.name, @"FIRInvalidArgumentException");
  669. XCTAssertEqualObjects(exception.reason,
  670. @"'sum(foo)' was not requested in the aggregation query.");
  671. }
  672. @
  673. try {
  674. [snapshot valueForAggregation:[FIRAggregateField aggregateFieldForAverageOfField:@"pages"]];
  675. XCTAssertTrue(false, "Exception expected");
  676. } @catch (NSException* exception) {
  677. XCTAssertEqualObjects(exception.name, @"FIRInvalidArgumentException");
  678. XCTAssertEqualObjects(exception.reason,
  679. @"'avg(pages)' was not requested in the aggregation query.");
  680. }
  681. }
  682. - (void)testPerformsAggregationWhenUsingInOperator {
  683. // TODO(sum/avg) remove the check below when sum and avg are supported in production
  684. XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator], @"only tested against emulator");
  685. FIRCollectionReference* testCollection = [self collectionRefWithDocuments:@{
  686. @"a" : @{
  687. @"author" : @"authorA",
  688. @"title" : @"titleA",
  689. @"pages" : @100,
  690. @"year" : @1980,
  691. @"rating" : @5
  692. },
  693. @"b" : @{
  694. @"author" : @"authorB",
  695. @"title" : @"titleB",
  696. @"pages" : @50,
  697. @"year" : @2020,
  698. @"rating" : @4
  699. },
  700. @"c" : @{
  701. @"author" : @"authorC",
  702. @"title" : @"titleC",
  703. @"pages" : @100,
  704. @"year" : @1980,
  705. @"rating" : @3
  706. },
  707. @"d" : @{
  708. @"author" : @"authorD",
  709. @"title" : @"titleD",
  710. @"pages" : @50,
  711. @"year" : @2020,
  712. @"rating" : @0
  713. }
  714. }];
  715. FIRAggregateQuerySnapshot* snapshot = [self
  716. readSnapshotForAggregate:[[testCollection queryWhereField:@"rating" in:@[ @5, @3 ]]
  717. aggregate:@[
  718. [FIRAggregateField aggregateFieldForSumOfField:@"rating"],
  719. [FIRAggregateField aggregateFieldForSumOfField:@"pages"],
  720. [FIRAggregateField aggregateFieldForAverageOfField:@"rating"],
  721. [FIRAggregateField aggregateFieldForAverageOfField:@"pages"],
  722. [FIRAggregateField aggregateFieldForCount]
  723. ]]];
  724. // Count
  725. XCTAssertEqual(
  726. [[snapshot valueForAggregation:[FIRAggregateField aggregateFieldForCount]] longValue], 2L);
  727. // Sum
  728. XCTAssertEqual(
  729. [[snapshot valueForAggregation:[FIRAggregateField aggregateFieldForSumOfField:@"rating"]]
  730. longValue],
  731. 8L);
  732. XCTAssertEqual(
  733. [[snapshot valueForAggregation:[FIRAggregateField aggregateFieldForSumOfField:@"pages"]]
  734. longValue],
  735. 200L);
  736. // Average
  737. XCTAssertEqual(
  738. [[snapshot valueForAggregation:[FIRAggregateField aggregateFieldForAverageOfField:@"rating"]]
  739. doubleValue],
  740. 4.0);
  741. XCTAssertEqual(
  742. [[snapshot valueForAggregation:[FIRAggregateField aggregateFieldForAverageOfField:@"pages"]]
  743. doubleValue],
  744. 100.0);
  745. }
  746. - (void)testPerformsAggregationWhenUsingArrayContainsAnyOperator {
  747. // TODO(sum/avg) remove the check below when sum and avg are supported in production
  748. XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator], @"only tested against emulator");
  749. FIRCollectionReference* testCollection = [self collectionRefWithDocuments:@{
  750. @"a" : @{
  751. @"author" : @"authorA",
  752. @"title" : @"titleA",
  753. @"pages" : @100,
  754. @"year" : @1980,
  755. @"rating" : @[ @5, @1000 ]
  756. },
  757. @"b" : @{
  758. @"author" : @"authorB",
  759. @"title" : @"titleB",
  760. @"pages" : @50,
  761. @"year" : @2020,
  762. @"rating" : @[ @4 ]
  763. },
  764. @"c" : @{
  765. @"author" : @"authorC",
  766. @"title" : @"titleC",
  767. @"pages" : @100,
  768. @"year" : @1980,
  769. @"rating" : @[ @2222, @3 ]
  770. },
  771. @"d" : @{
  772. @"author" : @"authorD",
  773. @"title" : @"titleD",
  774. @"pages" : @50,
  775. @"year" : @2020,
  776. @"rating" : @[ @0 ]
  777. }
  778. }];
  779. FIRAggregateQuerySnapshot* snapshot = [self
  780. readSnapshotForAggregate:[[testCollection queryWhereField:@"rating"
  781. arrayContainsAny:@[ @5, @3 ]]
  782. aggregate:@[
  783. [FIRAggregateField aggregateFieldForSumOfField:@"rating"],
  784. [FIRAggregateField aggregateFieldForSumOfField:@"pages"],
  785. [FIRAggregateField aggregateFieldForAverageOfField:@"rating"],
  786. [FIRAggregateField aggregateFieldForAverageOfField:@"pages"],
  787. [FIRAggregateField aggregateFieldForCount]
  788. ]]];
  789. // Count
  790. XCTAssertEqual(
  791. [[snapshot valueForAggregation:[FIRAggregateField aggregateFieldForCount]] longValue], 2L);
  792. // Sum
  793. XCTAssertEqual(
  794. [[snapshot valueForAggregation:[FIRAggregateField aggregateFieldForSumOfField:@"rating"]]
  795. longValue],
  796. 0L);
  797. XCTAssertEqual(
  798. [[snapshot valueForAggregation:[FIRAggregateField aggregateFieldForSumOfField:@"pages"]]
  799. longValue],
  800. 200L);
  801. // Average
  802. XCTAssertEqualObjects(
  803. [snapshot valueForAggregation:[FIRAggregateField aggregateFieldForAverageOfField:@"rating"]],
  804. [NSNull null]);
  805. XCTAssertEqual(
  806. [[snapshot valueForAggregation:[FIRAggregateField aggregateFieldForAverageOfField:@"pages"]]
  807. doubleValue],
  808. 100.0);
  809. }
  810. - (void)testPerformsAggregationsOnNestedMapValues {
  811. // TODO(sum/avg) remove the check below when sum and avg are supported in production
  812. XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator], @"only tested against emulator");
  813. FIRCollectionReference* testCollection = [self collectionRefWithDocuments:@{
  814. @"a" : @{
  815. @"author" : @"authorA",
  816. @"title" : @"titleA",
  817. @"metadata" : @{@"pages" : @100, @"rating" : @{@"critic" : @2, @"user" : @5}}
  818. },
  819. @"b" : @{
  820. @"author" : @"authorB",
  821. @"title" : @"titleB",
  822. @"metadata" : @{@"pages" : @50, @"rating" : @{@"critic" : @4, @"user" : @4}}
  823. },
  824. }];
  825. FIRAggregateQuerySnapshot* snapshot =
  826. [self readSnapshotForAggregate:[testCollection aggregate:@[
  827. [FIRAggregateField aggregateFieldForSumOfField:@"metadata.pages"],
  828. [FIRAggregateField aggregateFieldForSumOfField:@"metadata.rating.user"],
  829. [FIRAggregateField aggregateFieldForAverageOfField:@"metadata.pages"],
  830. [FIRAggregateField aggregateFieldForAverageOfField:@"metadata.rating.critic"],
  831. [FIRAggregateField aggregateFieldForCount]
  832. ]]];
  833. // Count
  834. XCTAssertEqual(
  835. [[snapshot valueForAggregation:[FIRAggregateField aggregateFieldForCount]] longValue], 2L);
  836. // Sum
  837. XCTAssertEqual(
  838. [[snapshot valueForAggregation:[FIRAggregateField
  839. aggregateFieldForSumOfField:@"metadata.pages"]] longValue],
  840. 150L);
  841. XCTAssertEqual(
  842. [[snapshot valueForAggregation:[FIRAggregateField
  843. aggregateFieldForSumOfField:@"metadata.rating.user"]]
  844. longValue],
  845. 9);
  846. // Average
  847. XCTAssertEqual(
  848. [[snapshot
  849. valueForAggregation:[FIRAggregateField aggregateFieldForAverageOfField:@"metadata.pages"]]
  850. doubleValue],
  851. 75.0);
  852. XCTAssertEqual(
  853. [[snapshot valueForAggregation:[FIRAggregateField
  854. aggregateFieldForAverageOfField:@"metadata.rating.critic"]]
  855. doubleValue],
  856. 3.0);
  857. }
  858. - (void)testPerformsSumThatOverflowsMaxLong {
  859. // TODO(sum/avg) remove the check below when sum and avg are supported in production
  860. XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator], @"only tested against emulator");
  861. FIRCollectionReference* testCollection = [self collectionRefWithDocuments:@{
  862. @"a" : @{
  863. @"author" : @"authorA",
  864. @"title" : @"titleA",
  865. @"rating" : [NSNumber numberWithLong:LLONG_MAX]
  866. },
  867. @"b" : @{
  868. @"author" : @"authorB",
  869. @"title" : @"titleB",
  870. @"rating" : [NSNumber numberWithLong:LLONG_MAX]
  871. },
  872. }];
  873. FIRAggregateQuerySnapshot* snapshot =
  874. [self readSnapshotForAggregate:[testCollection
  875. aggregate:@[ [FIRAggregateField
  876. aggregateFieldForSumOfField:@"rating"] ]]];
  877. // Sum
  878. XCTAssertEqual(
  879. [[snapshot valueForAggregation:[FIRAggregateField aggregateFieldForSumOfField:@"rating"]]
  880. doubleValue],
  881. [[NSNumber numberWithLong:LLONG_MAX] doubleValue] +
  882. [[NSNumber numberWithLong:LLONG_MAX] doubleValue]);
  883. }
  884. - (void)testPerformsSumThatCanOverflowLongValuesDuringAccumulation {
  885. // TODO(sum/avg) remove the check below when sum and avg are supported in production
  886. XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator], @"only tested against emulator");
  887. FIRCollectionReference* testCollection = [self collectionRefWithDocuments:@{
  888. @"a" : @{
  889. @"author" : @"authorA",
  890. @"title" : @"titleA",
  891. @"rating" : [NSNumber numberWithLong:LLONG_MAX]
  892. },
  893. @"b" : @{@"author" : @"authorB", @"title" : @"titleB", @"rating" : [NSNumber numberWithLong:1]},
  894. @"c" :
  895. @{@"author" : @"authorC", @"title" : @"titleC", @"rating" : [NSNumber numberWithLong:-101]}
  896. }];
  897. FIRAggregateQuerySnapshot* snapshot =
  898. [self readSnapshotForAggregate:[testCollection
  899. aggregate:@[ [FIRAggregateField
  900. aggregateFieldForSumOfField:@"rating"] ]]];
  901. // Sum
  902. XCTAssertEqual(
  903. [[snapshot valueForAggregation:[FIRAggregateField aggregateFieldForSumOfField:@"rating"]]
  904. longLongValue],
  905. [[NSNumber numberWithLong:LLONG_MAX - 100] longLongValue]);
  906. }
  907. - (void)testPerformsSumThatIsNegative {
  908. // TODO(sum/avg) remove the check below when sum and avg are supported in production
  909. XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator], @"only tested against emulator");
  910. FIRCollectionReference* testCollection = [self collectionRefWithDocuments:@{
  911. @"a" : @{
  912. @"author" : @"authorA",
  913. @"title" : @"titleA",
  914. @"rating" : [NSNumber numberWithLong:LLONG_MAX]
  915. },
  916. @"b" : @{
  917. @"author" : @"authorB",
  918. @"title" : @"titleB",
  919. @"rating" : [NSNumber numberWithLong:-LLONG_MAX]
  920. },
  921. @"c" :
  922. @{@"author" : @"authorC", @"title" : @"titleC", @"rating" : [NSNumber numberWithLong:-101]},
  923. @"d" : @{
  924. @"author" : @"authorD",
  925. @"title" : @"titleD",
  926. @"rating" : [NSNumber numberWithLong:-10000]
  927. }
  928. }];
  929. FIRAggregateQuerySnapshot* snapshot =
  930. [self readSnapshotForAggregate:[testCollection
  931. aggregate:@[ [FIRAggregateField
  932. aggregateFieldForSumOfField:@"rating"] ]]];
  933. // Sum
  934. XCTAssertEqual(
  935. [[snapshot valueForAggregation:[FIRAggregateField aggregateFieldForSumOfField:@"rating"]]
  936. longLongValue],
  937. [[NSNumber numberWithLong:-10101] longLongValue]);
  938. }
  939. - (void)testPerformsSumThatIsPositiveInfinity {
  940. // TODO(sum/avg) remove the check below when sum and avg are supported in production
  941. XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator], @"only tested against emulator");
  942. FIRCollectionReference* testCollection = [self collectionRefWithDocuments:@{
  943. @"a" : @{
  944. @"author" : @"authorA",
  945. @"title" : @"titleA",
  946. @"rating" : [NSNumber numberWithDouble:DBL_MAX]
  947. },
  948. @"b" : @{
  949. @"author" : @"authorB",
  950. @"title" : @"titleB",
  951. @"rating" : [NSNumber numberWithDouble:DBL_MAX]
  952. }
  953. }];
  954. FIRAggregateQuerySnapshot* snapshot =
  955. [self readSnapshotForAggregate:[testCollection
  956. aggregate:@[ [FIRAggregateField
  957. aggregateFieldForSumOfField:@"rating"] ]]];
  958. // Sum
  959. XCTAssertEqual(
  960. [snapshot valueForAggregation:[FIRAggregateField aggregateFieldForSumOfField:@"rating"]],
  961. [NSNumber numberWithDouble:INFINITY]);
  962. }
  963. - (void)testPerformsSumThatIsNegativeInfinity {
  964. // TODO(sum/avg) remove the check below when sum and avg are supported in production
  965. XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator], @"only tested against emulator");
  966. FIRCollectionReference* testCollection = [self collectionRefWithDocuments:@{
  967. @"a" : @{
  968. @"author" : @"authorA",
  969. @"title" : @"titleA",
  970. @"rating" : [NSNumber numberWithDouble:-DBL_MAX]
  971. },
  972. @"b" : @{
  973. @"author" : @"authorB",
  974. @"title" : @"titleB",
  975. @"rating" : [NSNumber numberWithDouble:-DBL_MAX]
  976. }
  977. }];
  978. FIRAggregateQuerySnapshot* snapshot =
  979. [self readSnapshotForAggregate:[testCollection
  980. aggregate:@[ [FIRAggregateField
  981. aggregateFieldForSumOfField:@"rating"] ]]];
  982. // Sum
  983. XCTAssertEqual(
  984. [snapshot valueForAggregation:[FIRAggregateField aggregateFieldForSumOfField:@"rating"]],
  985. [NSNumber numberWithDouble:-INFINITY]);
  986. }
  987. - (void)testPerformsSumThatIsValidButCouldOverflowDuringAggregation {
  988. // TODO(sum/avg) remove the check below when sum and avg are supported in production
  989. XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator], @"only tested against emulator");
  990. FIRCollectionReference* testCollection = [self collectionRefWithDocuments:@{
  991. @"a" : @{@"rating" : [NSNumber numberWithDouble:DBL_MAX]},
  992. @"b" : @{@"rating" : [NSNumber numberWithDouble:DBL_MAX]},
  993. @"c" : @{@"rating" : [NSNumber numberWithDouble:-DBL_MAX]},
  994. @"d" : @{@"rating" : [NSNumber numberWithDouble:-DBL_MAX]},
  995. @"e" : @{@"rating" : [NSNumber numberWithDouble:DBL_MAX]},
  996. @"f" : @{@"rating" : [NSNumber numberWithDouble:-DBL_MAX]},
  997. @"g" : @{@"rating" : [NSNumber numberWithDouble:-DBL_MAX]},
  998. @"h" : @{@"rating" : [NSNumber numberWithDouble:DBL_MAX]}
  999. }];
  1000. FIRAggregateQuerySnapshot* snapshot =
  1001. [self readSnapshotForAggregate:[testCollection
  1002. aggregate:@[ [FIRAggregateField
  1003. aggregateFieldForSumOfField:@"rating"] ]]];
  1004. // Sum
  1005. XCTAssertEqual(
  1006. [[snapshot valueForAggregation:[FIRAggregateField aggregateFieldForSumOfField:@"rating"]]
  1007. longLongValue],
  1008. [[NSNumber numberWithLong:0] longLongValue]);
  1009. XCTAssertEqual(
  1010. [[snapshot valueForAggregation:[FIRAggregateField aggregateFieldForSumOfField:@"rating"]]
  1011. doubleValue],
  1012. [[NSNumber numberWithLong:0] doubleValue]);
  1013. }
  1014. - (void)testPerformsSumOverResultSetOfZeroDocuments {
  1015. // TODO(sum/avg) remove the check below when sum and avg are supported in production
  1016. XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator], @"only tested against emulator");
  1017. FIRCollectionReference* testCollection = [self collectionRefWithDocuments:@{
  1018. @"a" : @{
  1019. @"author" : @"authorA",
  1020. @"title" : @"titleA",
  1021. @"pages" : @100,
  1022. @"height" : @24.5,
  1023. @"weight" : @24.1,
  1024. @"foo" : @1,
  1025. @"bar" : @2,
  1026. @"baz" : @3
  1027. },
  1028. @"b" : @{
  1029. @"author" : @"authorB",
  1030. @"title" : @"titleB",
  1031. @"pages" : @50,
  1032. @"height" : @25.5,
  1033. @"weight" : @75.5,
  1034. @"foo" : @1,
  1035. @"bar" : @2,
  1036. @"baz" : @3
  1037. }
  1038. }];
  1039. FIRAggregateQuerySnapshot* snapshot =
  1040. [self readSnapshotForAggregate:[[testCollection queryWhereField:@"pages" isGreaterThan:@200]
  1041. aggregate:@[ [FIRAggregateField
  1042. aggregateFieldForSumOfField:@"pages"] ]]];
  1043. // Sum
  1044. XCTAssertEqual(
  1045. [snapshot valueForAggregation:[FIRAggregateField aggregateFieldForSumOfField:@"pages"]],
  1046. [NSNumber numberWithLong:0L]);
  1047. }
  1048. - (void)testPerformsSumOnlyOnNumericFields {
  1049. // TODO(sum/avg) remove the check below when sum and avg are supported in production
  1050. XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator], @"only tested against emulator");
  1051. FIRCollectionReference* testCollection = [self collectionRefWithDocuments:@{
  1052. @"a" : @{@"rating" : [NSNumber numberWithLong:5]},
  1053. @"b" : @{@"rating" : [NSNumber numberWithLong:4]},
  1054. @"c" : @{@"rating" : @"3"},
  1055. @"d" : @{@"rating" : [NSNumber numberWithLong:1]}
  1056. }];
  1057. FIRAggregateQuerySnapshot* snapshot =
  1058. [self readSnapshotForAggregate:[testCollection aggregate:@[
  1059. [FIRAggregateField aggregateFieldForCount],
  1060. [FIRAggregateField aggregateFieldForSumOfField:@"rating"]
  1061. ]]];
  1062. // Count
  1063. XCTAssertEqual(
  1064. [[snapshot valueForAggregation:[FIRAggregateField aggregateFieldForCount]] longValue], 4L);
  1065. // Sum
  1066. XCTAssertEqual(
  1067. [[snapshot valueForAggregation:[FIRAggregateField aggregateFieldForSumOfField:@"rating"]]
  1068. longLongValue],
  1069. [[NSNumber numberWithLong:10] longLongValue]);
  1070. }
  1071. - (void)testPerformsSumOfMinIEEE754 {
  1072. // TODO(sum/avg) remove the check below when sum and avg are supported in production
  1073. XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator], @"only tested against emulator");
  1074. FIRCollectionReference* testCollection = [self collectionRefWithDocuments:@{
  1075. @"a" : @{@"rating" : [NSNumber numberWithDouble:__DBL_DENORM_MIN__]}
  1076. }];
  1077. FIRAggregateQuerySnapshot* snapshot =
  1078. [self readSnapshotForAggregate:[testCollection
  1079. aggregate:@[ [FIRAggregateField
  1080. aggregateFieldForSumOfField:@"rating"] ]]];
  1081. // Sum
  1082. XCTAssertEqual(
  1083. [[snapshot valueForAggregation:[FIRAggregateField aggregateFieldForSumOfField:@"rating"]]
  1084. doubleValue],
  1085. [[NSNumber numberWithDouble:__DBL_DENORM_MIN__] doubleValue]);
  1086. }
  1087. - (void)testPerformsAverageOfVariousNumericTypes {
  1088. // TODO(sum/avg) remove the check below when sum and avg are supported in production
  1089. XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator], @"only tested against emulator");
  1090. FIRCollectionReference* testCollection = [self collectionRefWithDocuments:@{
  1091. @"a" : @{
  1092. @"x" : @1,
  1093. @"intToInt" : [NSNumber numberWithLong:10],
  1094. @"floatToInt" : [NSNumber numberWithDouble:10.5],
  1095. @"mixedToInt" : [NSNumber numberWithLong:10],
  1096. @"floatToFloat" : [NSNumber numberWithDouble:5.5],
  1097. @"mixedToFloat" : [NSNumber numberWithDouble:8.6],
  1098. @"intToFloat" : [NSNumber numberWithLong:10]
  1099. },
  1100. @"b" : @{
  1101. @"intToInt" : [NSNumber numberWithLong:5],
  1102. @"floatToInt" : [NSNumber numberWithDouble:9.5],
  1103. @"mixedToInt" : [NSNumber numberWithDouble:9.5],
  1104. @"floatToFloat" : [NSNumber numberWithDouble:4.5],
  1105. @"mixedToFloat" : [NSNumber numberWithLong:9],
  1106. @"intToFloat" : [NSNumber numberWithLong:9]
  1107. },
  1108. @"c" : @{
  1109. @"intToInt" : [NSNumber numberWithLong:0],
  1110. @"floatToInt" : @"ignore",
  1111. @"mixedToInt" : [NSNumber numberWithDouble:10.5],
  1112. @"floatToFloat" : [NSNumber numberWithDouble:3.5],
  1113. @"mixedToFloat" : [NSNumber numberWithLong:10],
  1114. @"intToFloat" : @"ignore"
  1115. }
  1116. }];
  1117. NSArray* testCases = @[
  1118. @{
  1119. @"agg" : [FIRAggregateField aggregateFieldForAverageOfField:@"intToInt"],
  1120. @"expected" : [NSNumber numberWithLong:5]
  1121. },
  1122. @{
  1123. @"agg" : [FIRAggregateField aggregateFieldForAverageOfField:@"floatToInt"],
  1124. @"expected" : [NSNumber numberWithLong:10]
  1125. },
  1126. @{
  1127. @"agg" : [FIRAggregateField aggregateFieldForAverageOfField:@"mixedToInt"],
  1128. @"expected" : [NSNumber numberWithLong:10]
  1129. },
  1130. @{
  1131. @"agg" : [FIRAggregateField aggregateFieldForAverageOfField:@"floatToFloat"],
  1132. @"expected" : [NSNumber numberWithDouble:4.5]
  1133. },
  1134. @{
  1135. @"agg" : [FIRAggregateField aggregateFieldForAverageOfField:@"mixedToFloat"],
  1136. @"expected" : [NSNumber numberWithDouble:9.2]
  1137. },
  1138. @{
  1139. @"agg" : [FIRAggregateField aggregateFieldForAverageOfField:@"intToFloat"],
  1140. @"expected" : [NSNumber numberWithDouble:9.5]
  1141. }
  1142. ];
  1143. for (NSDictionary* testCase in testCases) {
  1144. FIRAggregateQuerySnapshot* snapshot =
  1145. [self readSnapshotForAggregate:[testCollection aggregate:@[ testCase[@"agg"] ]]];
  1146. // Average
  1147. XCTAssertEqual([[snapshot valueForAggregation:testCase[@"agg"]] longValue],
  1148. [testCase[@"expected"] longLongValue]);
  1149. XCTAssertEqualWithAccuracy([[snapshot valueForAggregation:testCase[@"agg"]] doubleValue],
  1150. [testCase[@"expected"] doubleValue], 0.00000000000001);
  1151. }
  1152. }
  1153. - (void)testPerformsAverageCausingUnderflow {
  1154. // TODO(sum/avg) remove the check below when sum and avg are supported in production
  1155. XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator], @"only tested against emulator");
  1156. FIRCollectionReference* testCollection = [self collectionRefWithDocuments:@{
  1157. @"a" : @{@"rating" : [NSNumber numberWithDouble:__DBL_DENORM_MIN__]},
  1158. @"b" : @{@"rating" : [NSNumber numberWithDouble:0]}
  1159. }];
  1160. FIRAggregateQuerySnapshot* snapshot =
  1161. [self readSnapshotForAggregate:[testCollection aggregate:@[
  1162. [FIRAggregateField aggregateFieldForAverageOfField:@"rating"]
  1163. ]]];
  1164. // Average
  1165. XCTAssertEqual(
  1166. [[snapshot valueForAggregation:[FIRAggregateField aggregateFieldForAverageOfField:@"rating"]]
  1167. doubleValue],
  1168. [[NSNumber numberWithDouble:0] doubleValue]);
  1169. }
  1170. - (void)testPerformsAverageOfMinIEEE754 {
  1171. // TODO(sum/avg) remove the check below when sum and avg are supported in production
  1172. XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator], @"only tested against emulator");
  1173. FIRCollectionReference* testCollection = [self collectionRefWithDocuments:@{
  1174. @"a" : @{@"rating" : [NSNumber numberWithDouble:__DBL_DENORM_MIN__]}
  1175. }];
  1176. FIRAggregateQuerySnapshot* snapshot =
  1177. [self readSnapshotForAggregate:[testCollection aggregate:@[
  1178. [FIRAggregateField aggregateFieldForAverageOfField:@"rating"]
  1179. ]]];
  1180. // Average
  1181. XCTAssertEqual(
  1182. [[snapshot valueForAggregation:[FIRAggregateField aggregateFieldForAverageOfField:@"rating"]]
  1183. doubleValue],
  1184. [[NSNumber numberWithDouble:__DBL_DENORM_MIN__] doubleValue]);
  1185. }
  1186. - (void)testPerformsAverageOverflowIEEE754DuringAccumulation {
  1187. // TODO(sum/avg) remove the check below when sum and avg are supported in production
  1188. XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator], @"only tested against emulator");
  1189. FIRCollectionReference* testCollection = [self collectionRefWithDocuments:@{
  1190. @"a" : @{@"rating" : [NSNumber numberWithDouble:DBL_MAX]},
  1191. @"b" : @{@"rating" : [NSNumber numberWithDouble:DBL_MAX]}
  1192. }];
  1193. FIRAggregateQuerySnapshot* snapshot =
  1194. [self readSnapshotForAggregate:[testCollection aggregate:@[
  1195. [FIRAggregateField aggregateFieldForAverageOfField:@"rating"]
  1196. ]]];
  1197. // Average
  1198. XCTAssertEqual(
  1199. [[snapshot valueForAggregation:[FIRAggregateField aggregateFieldForAverageOfField:@"rating"]]
  1200. doubleValue],
  1201. [[NSNumber numberWithDouble:INFINITY] doubleValue]);
  1202. }
  1203. - (void)testPerformsAverageOverResultSetOfZeroDocuments {
  1204. // TODO(sum/avg) remove the check below when sum and avg are supported in production
  1205. XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator], @"only tested against emulator");
  1206. FIRCollectionReference* testCollection = [self collectionRefWithDocuments:@{
  1207. @"a" : @{
  1208. @"author" : @"authorA",
  1209. @"title" : @"titleA",
  1210. @"pages" : @100,
  1211. @"height" : @24.5,
  1212. @"weight" : @24.1,
  1213. @"foo" : @1,
  1214. @"bar" : @2,
  1215. @"baz" : @3
  1216. },
  1217. @"b" : @{
  1218. @"author" : @"authorB",
  1219. @"title" : @"titleB",
  1220. @"pages" : @50,
  1221. @"height" : @25.5,
  1222. @"weight" : @75.5,
  1223. @"foo" : @1,
  1224. @"bar" : @2,
  1225. @"baz" : @3
  1226. }
  1227. }];
  1228. FIRAggregateQuerySnapshot* snapshot = [self
  1229. readSnapshotForAggregate:[[testCollection queryWhereField:@"pages" isGreaterThan:@200]
  1230. aggregate:@[ [FIRAggregateField
  1231. aggregateFieldForAverageOfField:@"pages"] ]]];
  1232. // Average
  1233. XCTAssertEqual(
  1234. [snapshot valueForAggregation:[FIRAggregateField aggregateFieldForAverageOfField:@"pages"]],
  1235. [NSNull null]);
  1236. }
  1237. - (void)testPerformsAverageOnlyOnNumericFields {
  1238. // TODO(sum/avg) remove the check below when sum and avg are supported in production
  1239. XCTSkipIf(![FSTIntegrationTestCase isRunningAgainstEmulator], @"only tested against emulator");
  1240. FIRCollectionReference* testCollection = [self collectionRefWithDocuments:@{
  1241. @"a" : @{@"rating" : [NSNumber numberWithLong:5]},
  1242. @"b" : @{@"rating" : [NSNumber numberWithLong:4]},
  1243. @"c" : @{@"rating" : @"3"},
  1244. @"d" : @{@"rating" : [NSNumber numberWithLong:6]}
  1245. }];
  1246. FIRAggregateQuerySnapshot* snapshot =
  1247. [self readSnapshotForAggregate:[testCollection aggregate:@[
  1248. [FIRAggregateField aggregateFieldForCount],
  1249. [FIRAggregateField aggregateFieldForAverageOfField:@"rating"]
  1250. ]]];
  1251. // Count
  1252. XCTAssertEqual(
  1253. [[snapshot valueForAggregation:[FIRAggregateField aggregateFieldForCount]] longValue], 4L);
  1254. // Average
  1255. XCTAssertEqual(
  1256. [[snapshot valueForAggregation:[FIRAggregateField aggregateFieldForAverageOfField:@"rating"]]
  1257. doubleValue],
  1258. [[NSNumber numberWithDouble:5] doubleValue]);
  1259. }
  1260. @end