FIRQueryTests.mm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. @interface FIRQueryTests : FSTIntegrationTestCase
  21. @end
  22. @implementation FIRQueryTests
  23. - (void)testLimitQueries {
  24. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  25. @"a" : @{@"k" : @"a"},
  26. @"b" : @{@"k" : @"b"},
  27. @"c" : @{@"k" : @"c"}
  28. }];
  29. FIRQuerySnapshot *snapshot = [self readDocumentSetForRef:[collRef queryLimitedTo:2]];
  30. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[ @{@"k" : @"a"}, @{@"k" : @"b"} ]));
  31. }
  32. - (void)testLimitQueriesWithDescendingSortOrder {
  33. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  34. @"a" : @{@"k" : @"a", @"sort" : @0},
  35. @"b" : @{@"k" : @"b", @"sort" : @1},
  36. @"c" : @{@"k" : @"c", @"sort" : @1},
  37. @"d" : @{@"k" : @"d", @"sort" : @2},
  38. }];
  39. FIRQuerySnapshot *snapshot =
  40. [self readDocumentSetForRef:[[collRef queryOrderedByField:@"sort" descending:YES]
  41. queryLimitedTo:2]];
  42. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[
  43. @{@"k" : @"d",
  44. @"sort" : @2},
  45. @{@"k" : @"c",
  46. @"sort" : @1}
  47. ]));
  48. }
  49. - (void)testKeyOrderIsDescendingForDescendingInequality {
  50. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  51. @"a" : @{@"foo" : @42},
  52. @"b" : @{@"foo" : @42.0},
  53. @"c" : @{@"foo" : @42},
  54. @"d" : @{@"foo" : @21},
  55. @"e" : @{@"foo" : @21.0},
  56. @"f" : @{@"foo" : @66},
  57. @"g" : @{@"foo" : @66.0},
  58. }];
  59. FIRQuerySnapshot *snapshot =
  60. [self readDocumentSetForRef:[[collRef queryWhereField:@"foo" isGreaterThan:@21]
  61. queryOrderedByField:@"foo"
  62. descending:YES]];
  63. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(snapshot), (@[ @"g", @"f", @"c", @"b", @"a" ]));
  64. }
  65. - (void)testUnaryFilterQueries {
  66. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  67. @"a" : @{@"null" : [NSNull null], @"nan" : @(NAN)},
  68. @"b" : @{@"null" : [NSNull null], @"nan" : @0},
  69. @"c" : @{@"null" : @NO, @"nan" : @(NAN)}
  70. }];
  71. FIRQuerySnapshot *results =
  72. [self readDocumentSetForRef:[[collRef queryWhereField:@"null" isEqualTo:[NSNull null]]
  73. queryWhereField:@"nan"
  74. isEqualTo:@(NAN)]];
  75. XCTAssertEqualObjects(FIRQuerySnapshotGetData(results), (@[
  76. @{@"null" : [NSNull null],
  77. @"nan" : @(NAN)}
  78. ]));
  79. }
  80. - (void)testQueryWithFieldPaths {
  81. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  82. @"a" : @{@"a" : @1},
  83. @"b" : @{@"a" : @2},
  84. @"c" : @{@"a" : @3}
  85. }];
  86. FIRQuery *query =
  87. [collRef queryWhereFieldPath:[[FIRFieldPath alloc] initWithFields:@[ @"a" ]] isLessThan:@3];
  88. query = [query queryOrderedByFieldPath:[[FIRFieldPath alloc] initWithFields:@[ @"a" ]]
  89. descending:YES];
  90. FIRQuerySnapshot *snapshot = [self readDocumentSetForRef:query];
  91. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(snapshot), (@[ @"b", @"a" ]));
  92. }
  93. - (void)testQueryWithPredicate {
  94. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  95. @"a" : @{@"a" : @1},
  96. @"b" : @{@"a" : @2},
  97. @"c" : @{@"a" : @3}
  98. }];
  99. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"a < 3"];
  100. FIRQuery *query = [collRef queryFilteredUsingPredicate:predicate];
  101. query = [query queryOrderedByFieldPath:[[FIRFieldPath alloc] initWithFields:@[ @"a" ]]
  102. descending:YES];
  103. FIRQuerySnapshot *snapshot = [self readDocumentSetForRef:query];
  104. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(snapshot), (@[ @"b", @"a" ]));
  105. }
  106. - (void)testFilterOnInfinity {
  107. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  108. @"a" : @{@"inf" : @(INFINITY)},
  109. @"b" : @{@"inf" : @(-INFINITY)}
  110. }];
  111. FIRQuerySnapshot *results =
  112. [self readDocumentSetForRef:[collRef queryWhereField:@"inf" isEqualTo:@(INFINITY)]];
  113. XCTAssertEqualObjects(FIRQuerySnapshotGetData(results), (@[ @{@"inf" : @(INFINITY)} ]));
  114. }
  115. - (void)testCanExplicitlySortByDocumentID {
  116. NSDictionary *testDocs = @{
  117. @"a" : @{@"key" : @"a"},
  118. @"b" : @{@"key" : @"b"},
  119. @"c" : @{@"key" : @"c"},
  120. };
  121. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  122. // Ideally this would be descending to validate it's different than
  123. // the default, but that requires an extra index
  124. FIRQuerySnapshot *docs =
  125. [self readDocumentSetForRef:[collection queryOrderedByFieldPath:[FIRFieldPath documentID]]];
  126. XCTAssertEqualObjects(FIRQuerySnapshotGetData(docs),
  127. (@[ testDocs[@"a"], testDocs[@"b"], testDocs[@"c"] ]));
  128. }
  129. - (void)testCanQueryByDocumentID {
  130. NSDictionary *testDocs = @{
  131. @"aa" : @{@"key" : @"aa"},
  132. @"ab" : @{@"key" : @"ab"},
  133. @"ba" : @{@"key" : @"ba"},
  134. @"bb" : @{@"key" : @"bb"},
  135. };
  136. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  137. FIRQuerySnapshot *docs =
  138. [self readDocumentSetForRef:[collection queryWhereFieldPath:[FIRFieldPath documentID]
  139. isEqualTo:@"ab"]];
  140. XCTAssertEqualObjects(FIRQuerySnapshotGetData(docs), (@[ testDocs[@"ab"] ]));
  141. }
  142. - (void)testCanQueryByDocumentIDs {
  143. NSDictionary *testDocs = @{
  144. @"aa" : @{@"key" : @"aa"},
  145. @"ab" : @{@"key" : @"ab"},
  146. @"ba" : @{@"key" : @"ba"},
  147. @"bb" : @{@"key" : @"bb"},
  148. };
  149. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  150. FIRQuerySnapshot *docs =
  151. [self readDocumentSetForRef:[collection queryWhereFieldPath:[FIRFieldPath documentID]
  152. isEqualTo:@"ab"]];
  153. XCTAssertEqualObjects(FIRQuerySnapshotGetData(docs), (@[ testDocs[@"ab"] ]));
  154. docs = [self readDocumentSetForRef:[[collection queryWhereFieldPath:[FIRFieldPath documentID]
  155. isGreaterThan:@"aa"]
  156. queryWhereFieldPath:[FIRFieldPath documentID]
  157. isLessThanOrEqualTo:@"ba"]];
  158. XCTAssertEqualObjects(FIRQuerySnapshotGetData(docs), (@[ testDocs[@"ab"], testDocs[@"ba"] ]));
  159. }
  160. - (void)testCanQueryByDocumentIDsUsingRefs {
  161. NSDictionary *testDocs = @{
  162. @"aa" : @{@"key" : @"aa"},
  163. @"ab" : @{@"key" : @"ab"},
  164. @"ba" : @{@"key" : @"ba"},
  165. @"bb" : @{@"key" : @"bb"},
  166. };
  167. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  168. FIRQuerySnapshot *docs = [self
  169. readDocumentSetForRef:[collection queryWhereFieldPath:[FIRFieldPath documentID]
  170. isEqualTo:[collection documentWithPath:@"ab"]]];
  171. XCTAssertEqualObjects(FIRQuerySnapshotGetData(docs), (@[ testDocs[@"ab"] ]));
  172. docs = [self
  173. readDocumentSetForRef:[[collection queryWhereFieldPath:[FIRFieldPath documentID]
  174. isGreaterThan:[collection documentWithPath:@"aa"]]
  175. queryWhereFieldPath:[FIRFieldPath documentID]
  176. isLessThanOrEqualTo:[collection documentWithPath:@"ba"]]];
  177. XCTAssertEqualObjects(FIRQuerySnapshotGetData(docs), (@[ testDocs[@"ab"], testDocs[@"ba"] ]));
  178. }
  179. - (void)testWatchSurvivesNetworkDisconnect {
  180. XCTestExpectation *testExpectiation =
  181. [self expectationWithDescription:@"testWatchSurvivesNetworkDisconnect"];
  182. FIRCollectionReference *collectionRef = [self collectionRef];
  183. FIRDocumentReference *docRef = [collectionRef documentWithAutoID];
  184. FIRFirestore *firestore = collectionRef.firestore;
  185. [collectionRef
  186. addSnapshotListenerWithIncludeMetadataChanges:YES
  187. listener:^(FIRQuerySnapshot *snapshot, NSError *error) {
  188. XCTAssertNil(error);
  189. if (!snapshot.empty && !snapshot.metadata.fromCache) {
  190. [testExpectiation fulfill];
  191. }
  192. }];
  193. [firestore disableNetworkWithCompletion:^(NSError *error) {
  194. XCTAssertNil(error);
  195. [docRef setData:@{@"foo" : @"bar"}];
  196. [firestore enableNetworkWithCompletion:^(NSError *error) {
  197. XCTAssertNil(error);
  198. }];
  199. }];
  200. [self awaitExpectations];
  201. }
  202. - (void)testQueriesFireFromCacheWhenOffline {
  203. NSDictionary *testDocs = @{
  204. @"a" : @{@"foo" : @1},
  205. };
  206. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  207. id<FIRListenerRegistration> registration = [collection
  208. addSnapshotListenerWithIncludeMetadataChanges:YES
  209. listener:self.eventAccumulator.valueEventHandler];
  210. FIRQuerySnapshot *querySnap = [self.eventAccumulator awaitEventWithName:@"initial event"];
  211. XCTAssertEqualObjects(FIRQuerySnapshotGetData(querySnap), @[ @{@"foo" : @1} ]);
  212. XCTAssertEqual(querySnap.metadata.isFromCache, NO);
  213. [self disableNetwork];
  214. querySnap = [self.eventAccumulator awaitEventWithName:@"offline event with isFromCache=YES"];
  215. XCTAssertEqual(querySnap.metadata.isFromCache, YES);
  216. // TODO(b/70631617): There's currently a backend bug that prevents us from using a resume token
  217. // right away (against hexa at least). So we sleep. :-( :-( Anything over ~10ms seems to be
  218. // sufficient.
  219. [NSThread sleepForTimeInterval:0.2f];
  220. [self enableNetwork];
  221. querySnap = [self.eventAccumulator awaitEventWithName:@"back online event with isFromCache=NO"];
  222. XCTAssertEqual(querySnap.metadata.isFromCache, NO);
  223. [registration remove];
  224. }
  225. - (void)testCanHaveMultipleMutationsWhileOffline {
  226. FIRCollectionReference *col = [self collectionRef];
  227. // set a few docs to known values
  228. NSDictionary *initialDocs = @{@"doc1" : @{@"key1" : @"value1"}, @"doc2" : @{@"key2" : @"value2"}};
  229. [self writeAllDocuments:initialDocs toCollection:col];
  230. // go offline for the rest of this test
  231. [self disableNetwork];
  232. // apply *multiple* mutations while offline
  233. [[col documentWithPath:@"doc1"] setData:@{@"key1b" : @"value1b"}];
  234. [[col documentWithPath:@"doc2"] setData:@{@"key2b" : @"value2b"}];
  235. FIRQuerySnapshot *result = [self readDocumentSetForRef:col];
  236. XCTAssertEqualObjects(FIRQuerySnapshotGetData(result), (@[
  237. @{@"key1b" : @"value1b"},
  238. @{@"key2b" : @"value2b"},
  239. ]));
  240. }
  241. - (void)testArrayContainsQueries {
  242. NSDictionary *testDocs = @{
  243. @"a" : @{@"array" : @[ @42 ]},
  244. @"b" : @{@"array" : @[ @"a", @42, @"c" ]},
  245. @"c" : @{@"array" : @[ @41.999, @"42",
  246. @{@"a" : @[ @42 ]} ]},
  247. @"d" : @{@"array" : @[ @42 ], @"array2" : @[ @"bingo" ]}
  248. };
  249. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  250. // Search for 42
  251. FIRQuerySnapshot *snapshot =
  252. [self readDocumentSetForRef:[collection queryWhereField:@"array" arrayContains:@42]];
  253. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[
  254. @{@"array" : @[ @42 ]},
  255. @{@"array" : @[ @"a", @42, @"c" ]},
  256. @{@"array" : @[ @42 ],
  257. @"array2" : @[ @"bingo" ]}
  258. ]));
  259. // NOTE: The backend doesn't currently support null, NaN, objects, or arrays, so there isn't much
  260. // of anything else interesting to test.
  261. }
  262. @end