FIRCountTests.mm 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. FIRCollectionReference* testCollection = [self collectionRefWithDocuments:@{
  50. @"a" : @{@"k" : @"a"},
  51. @"b" : @{@"k" : @"b"},
  52. @"c" : @{@"k" : @"c"}
  53. }];
  54. FIRAggregateQuerySnapshot* snapshot = [self readSnapshotForAggregate:[testCollection count]];
  55. XCTAssertEqual(snapshot.count, [NSNumber numberWithLong:3L]);
  56. }
  57. - (void)testCanRunCountWithFilters {
  58. FIRCollectionReference* testCollection = [self collectionRefWithDocuments:@{
  59. @"a" : @{@"k" : @"a"},
  60. @"b" : @{@"k" : @"b"},
  61. @"c" : @{@"k" : @"c"}
  62. }];
  63. FIRAggregateQuerySnapshot* snapshot =
  64. [self readSnapshotForAggregate:[[testCollection queryWhereField:@"k" isEqualTo:@"b"] count]];
  65. XCTAssertEqual(snapshot.count, [NSNumber numberWithLong:1L]);
  66. }
  67. - (void)testSnapshotEquals {
  68. FIRCollectionReference* testCollection = [self collectionRefWithDocuments:@{
  69. @"a" : @{@"k" : @"a"},
  70. @"b" : @{@"k" : @"b"},
  71. @"c" : @{@"k" : @"c"}
  72. }];
  73. FIRAggregateQuerySnapshot* snapshot1 =
  74. [self readSnapshotForAggregate:[[testCollection queryWhereField:@"k" isEqualTo:@"b"] count]];
  75. FIRAggregateQuerySnapshot* snapshot1Same =
  76. [self readSnapshotForAggregate:[[testCollection queryWhereField:@"k" isEqualTo:@"b"] count]];
  77. FIRAggregateQuerySnapshot* snapshot2 =
  78. [self readSnapshotForAggregate:[[testCollection queryWhereField:@"k" isEqualTo:@"a"] count]];
  79. [self writeDocumentRef:[testCollection documentWithPath:@"d"] data:@{@"k" : @"a"}];
  80. FIRAggregateQuerySnapshot* snapshot2Different =
  81. [self readSnapshotForAggregate:[[testCollection queryWhereField:@"k" isEqualTo:@"a"] count]];
  82. FIRAggregateQuerySnapshot* snapshot3 =
  83. [self readSnapshotForAggregate:[[testCollection queryWhereField:@"k" isEqualTo:@"b"] count]];
  84. FIRAggregateQuerySnapshot* snapshot3Different =
  85. [self readSnapshotForAggregate:[[testCollection queryWhereField:@"k" isEqualTo:@"c"] count]];
  86. XCTAssertEqualObjects(snapshot1, snapshot1Same);
  87. XCTAssertEqual([snapshot1 hash], [snapshot1Same hash]);
  88. XCTAssertEqualObjects([snapshot1 query], [[testCollection queryWhereField:@"k"
  89. isEqualTo:@"b"] count]);
  90. XCTAssertNotEqualObjects(snapshot1, nil);
  91. XCTAssertNotEqualObjects(snapshot1, @"string");
  92. XCTAssertNotEqualObjects(snapshot1, snapshot2);
  93. XCTAssertNotEqual([snapshot1 hash], [snapshot2 hash]);
  94. XCTAssertNotEqualObjects(snapshot2, snapshot2Different);
  95. XCTAssertNotEqual([snapshot2 hash], [snapshot2Different hash]);
  96. XCTAssertNotEqualObjects(snapshot3, snapshot3Different);
  97. XCTAssertNotEqual([snapshot3 hash], [snapshot3Different hash]);
  98. }
  99. - (void)testTerminateDoesNotCrashWithFlyingCountQuery {
  100. FIRCollectionReference* testCollection = [self collectionRefWithDocuments:@{
  101. @"a" : @{@"k" : @"a"},
  102. @"b" : @{@"k" : @"b"},
  103. @"c" : @{@"k" : @"c"},
  104. }];
  105. [[testCollection count]
  106. aggregationWithSource:FIRAggregateSourceServer
  107. completion:^(FIRAggregateQuerySnapshot* snapshot, NSError* error) {
  108. (void)snapshot;
  109. (void)error;
  110. }];
  111. [self terminateFirestore:testCollection.firestore];
  112. }
  113. - (void)testCanRunCollectionGroupCountQuery {
  114. NSString* collectionGroup =
  115. [NSString stringWithFormat:@"%@%@", @"b",
  116. [self.db collectionWithPath:@"foo"].documentWithAutoID.documentID];
  117. NSArray* docPathFormats = @[
  118. @"abc/123/%@/cg-doc1", @"abc/123/%@/cg-doc2", @"%@/cg-doc3", @"%@/cg-doc4",
  119. @"def/456/%@/cg-doc5", @"%@/virtual-doc/nested-coll/not-cg-doc", @"x%@/not-cg-doc",
  120. @"%@x/not-cg-doc", @"abc/123/%@x/not-cg-doc", @"abc/123/x%@/not-cg-doc", @"abc/%@"
  121. ];
  122. FIRWriteBatch* batch = self.db.batch;
  123. for (NSString* format in docPathFormats) {
  124. NSString* path = [NSString stringWithFormat:format, collectionGroup];
  125. [batch setData:@{@"x" : @"a"} forDocument:[self.db documentWithPath:path]];
  126. }
  127. XCTestExpectation* expectation = [self expectationWithDescription:@"commit"];
  128. [batch commitWithCompletion:^(NSError* error) {
  129. XCTAssertNil(error);
  130. [expectation fulfill];
  131. }];
  132. [self awaitExpectation:expectation];
  133. FIRAggregateQuerySnapshot* snapshot =
  134. [self readSnapshotForAggregate:[[self.db collectionGroupWithID:collectionGroup] count]];
  135. // "cg-doc1", "cg-doc2", "cg-doc3", "cg-doc4", "cg-doc5",
  136. XCTAssertEqual(snapshot.count, [NSNumber numberWithLong:5L]);
  137. }
  138. - (void)testCanRunCountWithFiltersAndLimits {
  139. FIRCollectionReference* testCollection = [self collectionRefWithDocuments:@{
  140. @"a" : @{@"k" : @"a"},
  141. @"b" : @{@"k" : @"a"},
  142. @"c" : @{@"k" : @"a"},
  143. @"d" : @{@"k" : @"d"},
  144. }];
  145. FIRAggregateQuerySnapshot* snapshot = [self
  146. readSnapshotForAggregate:[[[testCollection queryLimitedTo:2] queryWhereField:@"k"
  147. isEqualTo:@"a"] count]];
  148. XCTAssertEqual(snapshot.count, [NSNumber numberWithLong:2L]);
  149. snapshot = [self readSnapshotForAggregate:[[[testCollection queryLimitedToLast:2]
  150. queryWhereField:@"k"
  151. isEqualTo:@"a"] count]];
  152. XCTAssertEqual(snapshot.count, [NSNumber numberWithLong:2L]);
  153. snapshot = [self readSnapshotForAggregate:[[[testCollection queryLimitedToLast:1000]
  154. queryWhereField:@"k"
  155. isEqualTo:@"d"] count]];
  156. XCTAssertEqual(snapshot.count, [NSNumber numberWithLong:1L]);
  157. }
  158. - (void)testCanRunCountOnNonExistentCollection {
  159. FIRCollectionReference* testCollection = [self collectionRef];
  160. FIRAggregateQuerySnapshot* snapshot = [self readSnapshotForAggregate:[testCollection count]];
  161. XCTAssertEqual(snapshot.count, [NSNumber numberWithLong:0L]);
  162. snapshot = [self readSnapshotForAggregate:[[testCollection queryWhereField:@"k"
  163. isEqualTo:@"a"] count]];
  164. XCTAssertEqual(snapshot.count, [NSNumber numberWithLong:0L]);
  165. }
  166. - (void)testFailWithoutNetwork {
  167. FIRCollectionReference* testCollection = [self collectionRefWithDocuments:@{
  168. @"a" : @{@"k" : @"a"},
  169. @"b" : @{@"k" : @"b"},
  170. @"c" : @{@"k" : @"c"}
  171. }];
  172. [self disableNetwork];
  173. [[testCollection count]
  174. aggregationWithSource:FIRAggregateSourceServer
  175. completion:^(FIRAggregateQuerySnapshot* snapshot, NSError* error) {
  176. (void)snapshot;
  177. XCTAssertNotNil(error);
  178. }];
  179. [self enableNetwork];
  180. FIRAggregateQuerySnapshot* snapshot = [self readSnapshotForAggregate:[testCollection count]];
  181. XCTAssertEqual(snapshot.count, [NSNumber numberWithLong:3L]);
  182. }
  183. @end