FIRQueryTests.mm 13 KB

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