FIRQueryTests.mm 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*
  2. * Copyright 2017 Google
  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/FSTEventAccumulator.h"
  19. #import "Firestore/Example/Tests/Util/FSTIntegrationTestCase.h"
  20. // TODO(b/116617988): Remove Internal include once CG queries are public.
  21. #import "Firestore/Source/API/FIRFirestore+Internal.h"
  22. @interface FIRQueryTests : FSTIntegrationTestCase
  23. @end
  24. @implementation FIRQueryTests
  25. - (void)testLimitQueries {
  26. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  27. @"a" : @{@"k" : @"a"},
  28. @"b" : @{@"k" : @"b"},
  29. @"c" : @{@"k" : @"c"}
  30. }];
  31. FIRQuerySnapshot *snapshot = [self readDocumentSetForRef:[collRef queryLimitedTo:2]];
  32. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[ @{@"k" : @"a"}, @{@"k" : @"b"} ]));
  33. }
  34. - (void)testLimitQueriesWithDescendingSortOrder {
  35. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  36. @"a" : @{@"k" : @"a", @"sort" : @0},
  37. @"b" : @{@"k" : @"b", @"sort" : @1},
  38. @"c" : @{@"k" : @"c", @"sort" : @1},
  39. @"d" : @{@"k" : @"d", @"sort" : @2},
  40. }];
  41. FIRQuerySnapshot *snapshot =
  42. [self readDocumentSetForRef:[[collRef queryOrderedByField:@"sort"
  43. descending:YES] queryLimitedTo:2]];
  44. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot),
  45. (@[ @{@"k" : @"d", @"sort" : @2}, @{@"k" : @"c", @"sort" : @1} ]));
  46. }
  47. - (void)testKeyOrderIsDescendingForDescendingInequality {
  48. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  49. @"a" : @{@"foo" : @42},
  50. @"b" : @{@"foo" : @42.0},
  51. @"c" : @{@"foo" : @42},
  52. @"d" : @{@"foo" : @21},
  53. @"e" : @{@"foo" : @21.0},
  54. @"f" : @{@"foo" : @66},
  55. @"g" : @{@"foo" : @66.0},
  56. }];
  57. FIRQuerySnapshot *snapshot =
  58. [self readDocumentSetForRef:[[collRef queryWhereField:@"foo"
  59. isGreaterThan:@21] queryOrderedByField:@"foo"
  60. descending:YES]];
  61. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(snapshot), (@[ @"g", @"f", @"c", @"b", @"a" ]));
  62. }
  63. - (void)testUnaryFilterQueries {
  64. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  65. @"a" : @{@"null" : [NSNull null], @"nan" : @(NAN)},
  66. @"b" : @{@"null" : [NSNull null], @"nan" : @0},
  67. @"c" : @{@"null" : @NO, @"nan" : @(NAN)}
  68. }];
  69. FIRQuerySnapshot *results =
  70. [self readDocumentSetForRef:[[collRef queryWhereField:@"null"
  71. isEqualTo:[NSNull null]] queryWhereField:@"nan"
  72. isEqualTo:@(NAN)]];
  73. XCTAssertEqualObjects(FIRQuerySnapshotGetData(results),
  74. (@[ @{@"null" : [NSNull null], @"nan" : @(NAN)} ]));
  75. }
  76. - (void)testQueryWithFieldPaths {
  77. FIRCollectionReference *collRef = [self
  78. collectionRefWithDocuments:@{@"a" : @{@"a" : @1}, @"b" : @{@"a" : @2}, @"c" : @{@"a" : @3}}];
  79. FIRQuery *query = [collRef queryWhereFieldPath:[[FIRFieldPath alloc] initWithFields:@[ @"a" ]]
  80. isLessThan:@3];
  81. query = [query queryOrderedByFieldPath:[[FIRFieldPath alloc] initWithFields:@[ @"a" ]]
  82. descending:YES];
  83. FIRQuerySnapshot *snapshot = [self readDocumentSetForRef:query];
  84. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(snapshot), (@[ @"b", @"a" ]));
  85. }
  86. - (void)testQueryWithPredicate {
  87. FIRCollectionReference *collRef = [self
  88. collectionRefWithDocuments:@{@"a" : @{@"a" : @1}, @"b" : @{@"a" : @2}, @"c" : @{@"a" : @3}}];
  89. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"a < 3"];
  90. FIRQuery *query = [collRef queryFilteredUsingPredicate:predicate];
  91. query = [query queryOrderedByFieldPath:[[FIRFieldPath alloc] initWithFields:@[ @"a" ]]
  92. descending:YES];
  93. FIRQuerySnapshot *snapshot = [self readDocumentSetForRef:query];
  94. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(snapshot), (@[ @"b", @"a" ]));
  95. }
  96. - (void)testFilterOnInfinity {
  97. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  98. @"a" : @{@"inf" : @(INFINITY)},
  99. @"b" : @{@"inf" : @(-INFINITY)}
  100. }];
  101. FIRQuerySnapshot *results = [self readDocumentSetForRef:[collRef queryWhereField:@"inf"
  102. isEqualTo:@(INFINITY)]];
  103. XCTAssertEqualObjects(FIRQuerySnapshotGetData(results), (@[ @{@"inf" : @(INFINITY)} ]));
  104. }
  105. - (void)testCanExplicitlySortByDocumentID {
  106. NSDictionary *testDocs = @{
  107. @"a" : @{@"key" : @"a"},
  108. @"b" : @{@"key" : @"b"},
  109. @"c" : @{@"key" : @"c"},
  110. };
  111. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  112. // Ideally this would be descending to validate it's different than
  113. // the default, but that requires an extra index
  114. FIRQuerySnapshot *docs =
  115. [self readDocumentSetForRef:[collection queryOrderedByFieldPath:[FIRFieldPath documentID]]];
  116. XCTAssertEqualObjects(FIRQuerySnapshotGetData(docs),
  117. (@[ testDocs[@"a"], testDocs[@"b"], testDocs[@"c"] ]));
  118. }
  119. - (void)testCanQueryByDocumentID {
  120. NSDictionary *testDocs = @{
  121. @"aa" : @{@"key" : @"aa"},
  122. @"ab" : @{@"key" : @"ab"},
  123. @"ba" : @{@"key" : @"ba"},
  124. @"bb" : @{@"key" : @"bb"},
  125. };
  126. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  127. FIRQuerySnapshot *docs =
  128. [self readDocumentSetForRef:[collection queryWhereFieldPath:[FIRFieldPath documentID]
  129. isEqualTo:@"ab"]];
  130. XCTAssertEqualObjects(FIRQuerySnapshotGetData(docs), (@[ testDocs[@"ab"] ]));
  131. }
  132. - (void)testCanQueryByDocumentIDs {
  133. NSDictionary *testDocs = @{
  134. @"aa" : @{@"key" : @"aa"},
  135. @"ab" : @{@"key" : @"ab"},
  136. @"ba" : @{@"key" : @"ba"},
  137. @"bb" : @{@"key" : @"bb"},
  138. };
  139. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  140. FIRQuerySnapshot *docs =
  141. [self readDocumentSetForRef:[collection queryWhereFieldPath:[FIRFieldPath documentID]
  142. isEqualTo:@"ab"]];
  143. XCTAssertEqualObjects(FIRQuerySnapshotGetData(docs), (@[ testDocs[@"ab"] ]));
  144. docs = [self readDocumentSetForRef:[[collection queryWhereFieldPath:[FIRFieldPath documentID]
  145. isGreaterThan:@"aa"]
  146. queryWhereFieldPath:[FIRFieldPath documentID]
  147. isLessThanOrEqualTo:@"ba"]];
  148. XCTAssertEqualObjects(FIRQuerySnapshotGetData(docs), (@[ testDocs[@"ab"], testDocs[@"ba"] ]));
  149. }
  150. - (void)testCanQueryByDocumentIDsUsingRefs {
  151. NSDictionary *testDocs = @{
  152. @"aa" : @{@"key" : @"aa"},
  153. @"ab" : @{@"key" : @"ab"},
  154. @"ba" : @{@"key" : @"ba"},
  155. @"bb" : @{@"key" : @"bb"},
  156. };
  157. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  158. FIRQuerySnapshot *docs = [self
  159. readDocumentSetForRef:[collection queryWhereFieldPath:[FIRFieldPath documentID]
  160. isEqualTo:[collection documentWithPath:@"ab"]]];
  161. XCTAssertEqualObjects(FIRQuerySnapshotGetData(docs), (@[ testDocs[@"ab"] ]));
  162. docs = [self
  163. readDocumentSetForRef:[[collection queryWhereFieldPath:[FIRFieldPath documentID]
  164. isGreaterThan:[collection documentWithPath:@"aa"]]
  165. queryWhereFieldPath:[FIRFieldPath documentID]
  166. isLessThanOrEqualTo:[collection documentWithPath:@"ba"]]];
  167. XCTAssertEqualObjects(FIRQuerySnapshotGetData(docs), (@[ testDocs[@"ab"], testDocs[@"ba"] ]));
  168. }
  169. - (void)testWatchSurvivesNetworkDisconnect {
  170. XCTestExpectation *testExpectiation =
  171. [self expectationWithDescription:@"testWatchSurvivesNetworkDisconnect"];
  172. FIRCollectionReference *collectionRef = [self collectionRef];
  173. FIRDocumentReference *docRef = [collectionRef documentWithAutoID];
  174. FIRFirestore *firestore = collectionRef.firestore;
  175. [collectionRef
  176. addSnapshotListenerWithIncludeMetadataChanges:YES
  177. listener:^(FIRQuerySnapshot *snapshot, NSError *error) {
  178. XCTAssertNil(error);
  179. if (!snapshot.empty && !snapshot.metadata.fromCache) {
  180. [testExpectiation fulfill];
  181. }
  182. }];
  183. [firestore disableNetworkWithCompletion:^(NSError *error) {
  184. XCTAssertNil(error);
  185. [docRef setData:@{@"foo" : @"bar"}];
  186. [firestore enableNetworkWithCompletion:^(NSError *error) {
  187. XCTAssertNil(error);
  188. }];
  189. }];
  190. [self awaitExpectations];
  191. }
  192. - (void)testQueriesFireFromCacheWhenOffline {
  193. NSDictionary *testDocs = @{
  194. @"a" : @{@"foo" : @1},
  195. };
  196. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  197. id<FIRListenerRegistration> registration = [collection
  198. addSnapshotListenerWithIncludeMetadataChanges:YES
  199. listener:self.eventAccumulator.valueEventHandler];
  200. FIRQuerySnapshot *querySnap = [self.eventAccumulator awaitEventWithName:@"initial event"];
  201. XCTAssertEqualObjects(FIRQuerySnapshotGetData(querySnap), @[ @{@"foo" : @1} ]);
  202. XCTAssertEqual(querySnap.metadata.isFromCache, NO);
  203. [self disableNetwork];
  204. querySnap = [self.eventAccumulator awaitEventWithName:@"offline event with isFromCache=YES"];
  205. XCTAssertEqual(querySnap.metadata.isFromCache, YES);
  206. [self enableNetwork];
  207. querySnap = [self.eventAccumulator awaitEventWithName:@"back online event with isFromCache=NO"];
  208. XCTAssertEqual(querySnap.metadata.isFromCache, NO);
  209. [registration remove];
  210. }
  211. - (void)testCanHaveMultipleMutationsWhileOffline {
  212. FIRCollectionReference *col = [self collectionRef];
  213. // set a few docs to known values
  214. NSDictionary *initialDocs = @{@"doc1" : @{@"key1" : @"value1"}, @"doc2" : @{@"key2" : @"value2"}};
  215. [self writeAllDocuments:initialDocs toCollection:col];
  216. // go offline for the rest of this test
  217. [self disableNetwork];
  218. // apply *multiple* mutations while offline
  219. [[col documentWithPath:@"doc1"] setData:@{@"key1b" : @"value1b"}];
  220. [[col documentWithPath:@"doc2"] setData:@{@"key2b" : @"value2b"}];
  221. FIRQuerySnapshot *result = [self readDocumentSetForRef:col];
  222. XCTAssertEqualObjects(FIRQuerySnapshotGetData(result), (@[
  223. @{@"key1b" : @"value1b"},
  224. @{@"key2b" : @"value2b"},
  225. ]));
  226. }
  227. - (void)testArrayContainsQueries {
  228. NSDictionary *testDocs = @{
  229. @"a" : @{@"array" : @[ @42 ]},
  230. @"b" : @{@"array" : @[ @"a", @42, @"c" ]},
  231. @"c" : @{@"array" : @[ @41.999, @"42", @{@"a" : @[ @42 ]} ]},
  232. @"d" : @{@"array" : @[ @42 ], @"array2" : @[ @"bingo" ]}
  233. };
  234. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  235. // Search for 42
  236. FIRQuerySnapshot *snapshot = [self readDocumentSetForRef:[collection queryWhereField:@"array"
  237. arrayContains:@42]];
  238. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[
  239. @{@"array" : @[ @42 ]}, @{@"array" : @[ @"a", @42, @"c" ]},
  240. @{@"array" : @[ @42 ], @"array2" : @[ @"bingo" ]}
  241. ]));
  242. // NOTE: The backend doesn't currently support null, NaN, objects, or arrays, so there isn't much
  243. // of anything else interesting to test.
  244. }
  245. - (void)testCollectionGroupQueries {
  246. // Use .document() to get a random collection group name to use but ensure it starts with 'b'
  247. // for predictable ordering.
  248. NSString *collectionGroup = [NSString
  249. stringWithFormat:@"b%@", [[self.db collectionWithPath:@"foo"] documentWithAutoID].documentID];
  250. NSArray *docPaths = @[
  251. @"abc/123/${collectionGroup}/cg-doc1", @"abc/123/${collectionGroup}/cg-doc2",
  252. @"${collectionGroup}/cg-doc3", @"${collectionGroup}/cg-doc4",
  253. @"def/456/${collectionGroup}/cg-doc5", @"${collectionGroup}/virtual-doc/nested-coll/not-cg-doc",
  254. @"x${collectionGroup}/not-cg-doc", @"${collectionGroup}x/not-cg-doc",
  255. @"abc/123/${collectionGroup}x/not-cg-doc", @"abc/123/x${collectionGroup}/not-cg-doc",
  256. @"abc/${collectionGroup}"
  257. ];
  258. FIRWriteBatch *batch = [self.db batch];
  259. for (NSString *docPath in docPaths) {
  260. NSString *path = [docPath stringByReplacingOccurrencesOfString:@"${collectionGroup}"
  261. withString:collectionGroup];
  262. [batch setData:@{@"x" : @1} forDocument:[self.db documentWithPath:path]];
  263. }
  264. XCTestExpectation *expectation = [self expectationWithDescription:@"batch written"];
  265. [batch commitWithCompletion:^(NSError *error) {
  266. XCTAssertNil(error);
  267. [expectation fulfill];
  268. }];
  269. [self awaitExpectations];
  270. FIRQuerySnapshot *querySnapshot =
  271. [self readDocumentSetForRef:[self.db collectionGroupWithID:collectionGroup]];
  272. NSArray<NSString *> *ids = FIRQuerySnapshotGetIDs(querySnapshot);
  273. XCTAssertEqualObjects(ids, (@[ @"cg-doc1", @"cg-doc2", @"cg-doc3", @"cg-doc4", @"cg-doc5" ]));
  274. }
  275. - (void)testCollectionGroupQueriesWithStartAtEndAtWithArbitraryDocumentIDs {
  276. // Use .document() to get a random collection group name to use but ensure it starts with 'b'
  277. // for predictable ordering.
  278. NSString *collectionGroup = [NSString
  279. stringWithFormat:@"b%@", [[self.db collectionWithPath:@"foo"] documentWithAutoID].documentID];
  280. NSArray *docPaths = @[
  281. @"a/a/${collectionGroup}/cg-doc1", @"a/b/a/b/${collectionGroup}/cg-doc2",
  282. @"a/b/${collectionGroup}/cg-doc3", @"a/b/c/d/${collectionGroup}/cg-doc4",
  283. @"a/c/${collectionGroup}/cg-doc5", @"${collectionGroup}/cg-doc6", @"a/b/nope/nope"
  284. ];
  285. FIRWriteBatch *batch = [self.db batch];
  286. for (NSString *docPath in docPaths) {
  287. NSString *path = [docPath stringByReplacingOccurrencesOfString:@"${collectionGroup}"
  288. withString:collectionGroup];
  289. [batch setData:@{@"x" : @1} forDocument:[self.db documentWithPath:path]];
  290. }
  291. XCTestExpectation *expectation = [self expectationWithDescription:@"batch written"];
  292. [batch commitWithCompletion:^(NSError *error) {
  293. XCTAssertNil(error);
  294. [expectation fulfill];
  295. }];
  296. [self awaitExpectations];
  297. FIRQuerySnapshot *querySnapshot = [self
  298. readDocumentSetForRef:[[[[self.db collectionGroupWithID:collectionGroup]
  299. queryOrderedByFieldPath:[FIRFieldPath documentID]]
  300. queryStartingAfterValues:@[ @"a/b" ]]
  301. queryEndingBeforeValues:@[
  302. [NSString stringWithFormat:@"a/b/%@/cg-doc3", collectionGroup]
  303. ]]];
  304. NSArray<NSString *> *ids = FIRQuerySnapshotGetIDs(querySnapshot);
  305. XCTAssertEqualObjects(ids, (@[ @"cg-doc2" ]));
  306. }
  307. - (void)testCollectionGroupQueriesWithWhereFiltersOnArbitraryDocumentIDs {
  308. // Use .document() to get a random collection group name to use but ensure it starts with 'b'
  309. // for predictable ordering.
  310. NSString *collectionGroup = [NSString
  311. stringWithFormat:@"b%@", [[self.db collectionWithPath:@"foo"] documentWithAutoID].documentID];
  312. NSArray *docPaths = @[
  313. @"a/a/${collectionGroup}/cg-doc1", @"a/b/a/b/${collectionGroup}/cg-doc2",
  314. @"a/b/${collectionGroup}/cg-doc3", @"a/b/c/d/${collectionGroup}/cg-doc4",
  315. @"a/c/${collectionGroup}/cg-doc5", @"${collectionGroup}/cg-doc6", @"a/b/nope/nope"
  316. ];
  317. FIRWriteBatch *batch = [self.db batch];
  318. for (NSString *docPath in docPaths) {
  319. NSString *path = [docPath stringByReplacingOccurrencesOfString:@"${collectionGroup}"
  320. withString:collectionGroup];
  321. [batch setData:@{@"x" : @1} forDocument:[self.db documentWithPath:path]];
  322. }
  323. XCTestExpectation *expectation = [self expectationWithDescription:@"batch written"];
  324. [batch commitWithCompletion:^(NSError *error) {
  325. XCTAssertNil(error);
  326. [expectation fulfill];
  327. }];
  328. [self awaitExpectations];
  329. FIRQuerySnapshot *querySnapshot = [self
  330. readDocumentSetForRef:[[[self.db collectionGroupWithID:collectionGroup]
  331. queryWhereFieldPath:[FIRFieldPath documentID]
  332. isGreaterThanOrEqualTo:@"a/b"]
  333. queryWhereFieldPath:[FIRFieldPath documentID]
  334. isLessThan:[NSString stringWithFormat:@"a/b/%@/cg-doc3",
  335. collectionGroup]]];
  336. NSArray<NSString *> *ids = FIRQuerySnapshotGetIDs(querySnapshot);
  337. XCTAssertEqualObjects(ids, (@[ @"cg-doc2" ]));
  338. }
  339. @end