FIRCountTests.mm 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * Copyright 2022 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/FirebaseFirestore.h>
  17. #import <XCTest/XCTest.h>
  18. #import "Firestore/Example/Tests/Util/FSTIntegrationTestCase.h"
  19. @interface FIRCountTests : FSTIntegrationTestCase
  20. @end
  21. @implementation FIRCountTests
  22. - (void)testAggregateQueryEquals {
  23. FIRCollectionReference* coll1 = [self collectionRefWithDocuments:@{}];
  24. FIRCollectionReference* coll1Same = [[coll1 firestore] collectionWithPath:[coll1 path]];
  25. FIRAggregateQuery* query1 = [coll1 count];
  26. FIRAggregateQuery* query1Same = [coll1Same count];
  27. FIRCollectionReference* sub = [[coll1 documentWithPath:@"bar"] collectionWithPath:@"baz"];
  28. FIRAggregateQuery* query2 = [[[sub queryWhereField:@"a" isEqualTo:@1] queryLimitedTo:100] count];
  29. FIRAggregateQuery* query2Same = [[[sub queryWhereField:@"a"
  30. isEqualTo:@1] queryLimitedTo:100] count];
  31. FIRAggregateQuery* query3 = [[[sub queryWhereField:@"b"
  32. isEqualTo:@1] queryOrderedByField:@"c"] count];
  33. FIRAggregateQuery* query3Same = [[[sub queryWhereField:@"b"
  34. isEqualTo:@1] queryOrderedByField:@"c"] count];
  35. XCTAssertEqualObjects(query1, query1Same);
  36. XCTAssertEqualObjects(query2, query2Same);
  37. XCTAssertEqualObjects(query3, query3Same);
  38. XCTAssertEqual([query1 hash], [query1Same hash]);
  39. XCTAssertEqual([query2 hash], [query2Same hash]);
  40. XCTAssertEqual([query3 hash], [query3Same hash]);
  41. XCTAssertFalse([query1 isEqual:nil]);
  42. XCTAssertFalse([query1 isEqual:@"string"]);
  43. XCTAssertFalse([query1 isEqual:query2]);
  44. XCTAssertFalse([query2 isEqual:query3]);
  45. XCTAssertNotEqual([query1 hash], [query2 hash]);
  46. XCTAssertNotEqual([query2 hash], [query3 hash]);
  47. }
  48. - (void)testCanRunCountQuery {
  49. // TODO(b/246758022): Remove this (and below) once COUNT is release for the backend.
  50. if (![FSTIntegrationTestCase isRunningAgainstEmulator]) {
  51. return;
  52. }
  53. FIRCollectionReference* testCollection = [self collectionRefWithDocuments:@{
  54. @"a" : @{@"k" : @"a"},
  55. @"b" : @{@"k" : @"b"},
  56. @"c" : @{@"k" : @"c"}
  57. }];
  58. FIRAggregateQuerySnapshot* snapshot = [self readSnapshotForAggregate:[testCollection count]];
  59. XCTAssertEqual(snapshot.count, [NSNumber numberWithLong:3L]);
  60. }
  61. - (void)testCanRunCountWithFilters {
  62. if (![FSTIntegrationTestCase isRunningAgainstEmulator]) {
  63. return;
  64. }
  65. FIRCollectionReference* testCollection = [self collectionRefWithDocuments:@{
  66. @"a" : @{@"k" : @"a"},
  67. @"b" : @{@"k" : @"b"},
  68. @"c" : @{@"k" : @"c"}
  69. }];
  70. FIRAggregateQuerySnapshot* snapshot =
  71. [self readSnapshotForAggregate:[[testCollection queryWhereField:@"k" isEqualTo:@"b"] count]];
  72. XCTAssertEqual(snapshot.count, [NSNumber numberWithLong:1L]);
  73. }
  74. - (void)testCanRunCountWithOrderBys {
  75. if (![FSTIntegrationTestCase isRunningAgainstEmulator]) {
  76. return;
  77. }
  78. FIRCollectionReference* testCollection = [self collectionRefWithDocuments:@{
  79. @"a" : @{@"k" : @"a"},
  80. @"b" : @{@"k" : @"b"},
  81. @"c" : @{@"k" : @"c"},
  82. @"d" : @{@"absent" : @"d"},
  83. }];
  84. FIRAggregateQuerySnapshot* snapshot =
  85. [self readSnapshotForAggregate:[[testCollection queryOrderedByField:@"k"] count]];
  86. XCTAssertEqual(snapshot.count, [NSNumber numberWithLong:3L]);
  87. }
  88. - (void)testSnapshotEquals {
  89. if (![FSTIntegrationTestCase isRunningAgainstEmulator]) {
  90. return;
  91. }
  92. FIRCollectionReference* testCollection = [self collectionRefWithDocuments:@{
  93. @"a" : @{@"k" : @"a"},
  94. @"b" : @{@"k" : @"b"},
  95. @"c" : @{@"k" : @"c"}
  96. }];
  97. FIRAggregateQuerySnapshot* snapshot1 =
  98. [self readSnapshotForAggregate:[[testCollection queryWhereField:@"k" isEqualTo:@"b"] count]];
  99. FIRAggregateQuerySnapshot* snapshot1Same =
  100. [self readSnapshotForAggregate:[[testCollection queryWhereField:@"k" isEqualTo:@"b"] count]];
  101. FIRAggregateQuerySnapshot* snapshot2 =
  102. [self readSnapshotForAggregate:[[testCollection queryWhereField:@"k" isEqualTo:@"a"] count]];
  103. [self writeDocumentRef:[testCollection documentWithPath:@"d"] data:@{@"k" : @"a"}];
  104. FIRAggregateQuerySnapshot* snapshot2Different =
  105. [self readSnapshotForAggregate:[[testCollection queryWhereField:@"k" isEqualTo:@"a"] count]];
  106. FIRAggregateQuerySnapshot* snapshot3 =
  107. [self readSnapshotForAggregate:[[testCollection queryWhereField:@"k" isEqualTo:@"b"] count]];
  108. FIRAggregateQuerySnapshot* snapshot3Different =
  109. [self readSnapshotForAggregate:[[testCollection queryWhereField:@"k" isEqualTo:@"c"] count]];
  110. XCTAssertEqualObjects(snapshot1, snapshot1Same);
  111. XCTAssertEqual([snapshot1 hash], [snapshot1Same hash]);
  112. XCTAssertEqualObjects([snapshot1 query], [[testCollection queryWhereField:@"k"
  113. isEqualTo:@"b"] count]);
  114. XCTAssertNotEqualObjects(snapshot1, nil);
  115. XCTAssertNotEqualObjects(snapshot1, @"string");
  116. XCTAssertNotEqualObjects(snapshot1, snapshot2);
  117. XCTAssertNotEqual([snapshot1 hash], [snapshot2 hash]);
  118. XCTAssertNotEqualObjects(snapshot2, snapshot2Different);
  119. XCTAssertNotEqual([snapshot2 hash], [snapshot2Different hash]);
  120. XCTAssertNotEqualObjects(snapshot3, snapshot3Different);
  121. XCTAssertNotEqual([snapshot3 hash], [snapshot3Different hash]);
  122. }
  123. - (void)testTerminateDoesNotCrashWithFlyingCountQuery {
  124. if (![FSTIntegrationTestCase isRunningAgainstEmulator]) {
  125. return;
  126. }
  127. FIRCollectionReference* testCollection = [self collectionRefWithDocuments:@{
  128. @"a" : @{@"k" : @"a"},
  129. @"b" : @{@"k" : @"b"},
  130. @"c" : @{@"k" : @"c"},
  131. }];
  132. [[testCollection count]
  133. aggregationWithSource:FIRAggregateSourceServer
  134. completion:^(FIRAggregateQuerySnapshot* snapshot, NSError* error) {
  135. (void)snapshot;
  136. (void)error;
  137. }];
  138. [self terminateFirestore:testCollection.firestore];
  139. }
  140. - (void)testCanRunCollectionGroupCountQuery {
  141. if (![FSTIntegrationTestCase isRunningAgainstEmulator]) {
  142. return;
  143. }
  144. NSString* collectionGroup =
  145. [NSString stringWithFormat:@"%@%@", @"b",
  146. [self.db collectionWithPath:@"foo"].documentWithAutoID.documentID];
  147. NSArray* docPathFormats = @[
  148. @"abc/123/%@/cg-doc1", @"abc/123/%@/cg-doc2", @"%@/cg-doc3", @"%@/cg-doc4",
  149. @"def/456/%@/cg-doc5", @"%@/virtual-doc/nested-coll/not-cg-doc", @"x%@/not-cg-doc",
  150. @"%@x/not-cg-doc", @"abc/123/%@x/not-cg-doc", @"abc/123/x%@/not-cg-doc", @"abc/%@"
  151. ];
  152. FIRWriteBatch* batch = self.db.batch;
  153. for (NSString* format in docPathFormats) {
  154. NSString* path = [NSString stringWithFormat:format, collectionGroup];
  155. [batch setData:@{@"x" : @"a"} forDocument:[self.db documentWithPath:path]];
  156. }
  157. XCTestExpectation* expectation = [self expectationWithDescription:@"commit"];
  158. [batch commitWithCompletion:^(NSError* error) {
  159. XCTAssertNil(error);
  160. [expectation fulfill];
  161. }];
  162. [self awaitExpectation:expectation];
  163. FIRAggregateQuerySnapshot* snapshot =
  164. [self readSnapshotForAggregate:[[self.db collectionGroupWithID:collectionGroup] count]];
  165. // "cg-doc1", "cg-doc2", "cg-doc3", "cg-doc4", "cg-doc5",
  166. XCTAssertEqual(snapshot.count, [NSNumber numberWithLong:5L]);
  167. }
  168. - (void)testCanRunCountWithFiltersAndLimits {
  169. if (![FSTIntegrationTestCase isRunningAgainstEmulator]) {
  170. return;
  171. }
  172. FIRCollectionReference* testCollection = [self collectionRefWithDocuments:@{
  173. @"a" : @{@"k" : @"a"},
  174. @"b" : @{@"k" : @"a"},
  175. @"c" : @{@"k" : @"a"},
  176. @"d" : @{@"k" : @"d"},
  177. }];
  178. FIRAggregateQuerySnapshot* snapshot = [self
  179. readSnapshotForAggregate:[[[testCollection queryLimitedTo:2] queryWhereField:@"k"
  180. isEqualTo:@"a"] count]];
  181. XCTAssertEqual(snapshot.count, [NSNumber numberWithLong:2L]);
  182. snapshot = [self readSnapshotForAggregate:[[[testCollection queryLimitedToLast:2]
  183. queryWhereField:@"k"
  184. isEqualTo:@"a"] count]];
  185. XCTAssertEqual(snapshot.count, [NSNumber numberWithLong:2L]);
  186. snapshot = [self readSnapshotForAggregate:[[[testCollection queryLimitedToLast:1000]
  187. queryWhereField:@"k"
  188. isEqualTo:@"d"] count]];
  189. XCTAssertEqual(snapshot.count, [NSNumber numberWithLong:1L]);
  190. }
  191. - (void)testCanRunCountOnNonExistentCollection {
  192. if (![FSTIntegrationTestCase isRunningAgainstEmulator]) {
  193. return;
  194. }
  195. FIRCollectionReference* testCollection = [self collectionRef];
  196. FIRAggregateQuerySnapshot* snapshot = [self readSnapshotForAggregate:[testCollection count]];
  197. XCTAssertEqual(snapshot.count, [NSNumber numberWithLong:0L]);
  198. snapshot = [self readSnapshotForAggregate:[[testCollection queryWhereField:@"k"
  199. isEqualTo:@"a"] count]];
  200. XCTAssertEqual(snapshot.count, [NSNumber numberWithLong:0L]);
  201. }
  202. - (void)testFailWithoutNetwork {
  203. if (![FSTIntegrationTestCase isRunningAgainstEmulator]) {
  204. return;
  205. }
  206. FIRCollectionReference* testCollection = [self collectionRefWithDocuments:@{
  207. @"a" : @{@"k" : @"a"},
  208. @"b" : @{@"k" : @"b"},
  209. @"c" : @{@"k" : @"c"}
  210. }];
  211. [self disableNetwork];
  212. [[testCollection count]
  213. aggregationWithSource:FIRAggregateSourceServer
  214. completion:^(FIRAggregateQuerySnapshot* snapshot, NSError* error) {
  215. (void)snapshot;
  216. XCTAssertNotNil(error);
  217. }];
  218. [self enableNetwork];
  219. FIRAggregateQuerySnapshot* snapshot = [self readSnapshotForAggregate:[testCollection count]];
  220. XCTAssertEqual(snapshot.count, [NSNumber numberWithLong:3L]);
  221. }
  222. @end