FSTQueryTests.mm 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  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 "Firestore/Source/Core/FSTQuery.h"
  17. #import <XCTest/XCTest.h>
  18. #import "Firestore/Source/API/FIRFirestore+Internal.h"
  19. #import "Firestore/Source/Model/FSTDocument.h"
  20. #import "Firestore/Example/Tests/Util/FSTHelpers.h"
  21. #include "Firestore/core/src/firebase/firestore/model/database_id.h"
  22. #include "Firestore/core/src/firebase/firestore/model/field_path.h"
  23. #include "Firestore/core/src/firebase/firestore/model/resource_path.h"
  24. #include "Firestore/core/src/firebase/firestore/util/string_apple.h"
  25. #include "Firestore/core/test/firebase/firestore/testutil/testutil.h"
  26. #include "absl/strings/string_view.h"
  27. namespace testutil = firebase::firestore::testutil;
  28. namespace util = firebase::firestore::util;
  29. using firebase::firestore::model::DatabaseId;
  30. using firebase::firestore::model::DocumentComparator;
  31. using firebase::firestore::model::DocumentState;
  32. using firebase::firestore::model::FieldPath;
  33. using firebase::firestore::model::ResourcePath;
  34. using firebase::firestore::util::ComparisonResult;
  35. NS_ASSUME_NONNULL_BEGIN
  36. /** Convenience methods for building test queries. */
  37. @interface FSTQuery (Tests)
  38. - (FSTQuery *)queryByAddingSortBy:(const absl::string_view)key ascending:(BOOL)ascending;
  39. @end
  40. @implementation FSTQuery (Tests)
  41. - (FSTQuery *)queryByAddingSortBy:(const absl::string_view)key ascending:(BOOL)ascending {
  42. return [self queryByAddingSortOrder:[FSTSortOrder sortOrderWithFieldPath:testutil::Field(key)
  43. ascending:ascending]];
  44. }
  45. @end
  46. @interface FSTQueryTests : XCTestCase
  47. @end
  48. @implementation FSTQueryTests
  49. - (void)testConstructor {
  50. const ResourcePath path{"rooms", "Firestore", "messages", "0001"};
  51. FSTQuery *query = [FSTQuery queryWithPath:path];
  52. XCTAssertNotNil(query);
  53. XCTAssertEqual(query.sortOrders.count, 1);
  54. XCTAssertEqual(query.sortOrders[0].field.CanonicalString(), FieldPath::kDocumentKeyPath);
  55. XCTAssertEqual(query.sortOrders[0].ascending, YES);
  56. XCTAssertEqual(query.explicitSortOrders.count, 0);
  57. }
  58. - (void)testOrderBy {
  59. FSTQuery *query = FSTTestQuery("rooms/Firestore/messages");
  60. query =
  61. [query queryByAddingSortOrder:[FSTSortOrder sortOrderWithFieldPath:testutil::Field("length")
  62. ascending:NO]];
  63. XCTAssertEqual(query.sortOrders.count, 2);
  64. XCTAssertEqual(query.sortOrders[0].field.CanonicalString(), "length");
  65. XCTAssertEqual(query.sortOrders[0].ascending, NO);
  66. XCTAssertEqual(query.sortOrders[1].field.CanonicalString(), FieldPath::kDocumentKeyPath);
  67. XCTAssertEqual(query.sortOrders[1].ascending, NO);
  68. XCTAssertEqual(query.explicitSortOrders.count, 1);
  69. XCTAssertEqual(query.explicitSortOrders[0].field.CanonicalString(), "length");
  70. XCTAssertEqual(query.explicitSortOrders[0].ascending, NO);
  71. }
  72. - (void)testMatchesBasedOnDocumentKey {
  73. FSTDocument *doc1 =
  74. FSTTestDoc("rooms/eros/messages/1", 0, @{@"text" : @"msg1"}, DocumentState::kSynced);
  75. FSTDocument *doc2 =
  76. FSTTestDoc("rooms/eros/messages/2", 0, @{@"text" : @"msg2"}, DocumentState::kSynced);
  77. FSTDocument *doc3 =
  78. FSTTestDoc("rooms/other/messages/1", 0, @{@"text" : @"msg3"}, DocumentState::kSynced);
  79. // document query
  80. FSTQuery *query = FSTTestQuery("rooms/eros/messages/1");
  81. XCTAssertTrue([query matchesDocument:doc1]);
  82. XCTAssertFalse([query matchesDocument:doc2]);
  83. XCTAssertFalse([query matchesDocument:doc3]);
  84. }
  85. - (void)testMatchesCorrectlyForShallowAncestorQuery {
  86. FSTDocument *doc1 =
  87. FSTTestDoc("rooms/eros/messages/1", 0, @{@"text" : @"msg1"}, DocumentState::kSynced);
  88. FSTDocument *doc1Meta =
  89. FSTTestDoc("rooms/eros/messages/1/meta/1", 0, @{@"meta" : @"mv"}, DocumentState::kSynced);
  90. FSTDocument *doc2 =
  91. FSTTestDoc("rooms/eros/messages/2", 0, @{@"text" : @"msg2"}, DocumentState::kSynced);
  92. FSTDocument *doc3 =
  93. FSTTestDoc("rooms/other/messages/1", 0, @{@"text" : @"msg3"}, DocumentState::kSynced);
  94. // shallow ancestor query
  95. FSTQuery *query = FSTTestQuery("rooms/eros/messages");
  96. XCTAssertTrue([query matchesDocument:doc1]);
  97. XCTAssertFalse([query matchesDocument:doc1Meta]);
  98. XCTAssertTrue([query matchesDocument:doc2]);
  99. XCTAssertFalse([query matchesDocument:doc3]);
  100. }
  101. - (void)testEmptyFieldsAreAllowedForQueries {
  102. FSTDocument *doc1 =
  103. FSTTestDoc("rooms/eros/messages/1", 0, @{@"text" : @"msg1"}, DocumentState::kSynced);
  104. FSTDocument *doc2 = FSTTestDoc("rooms/eros/messages/2", 0, @{}, DocumentState::kSynced);
  105. FSTQuery *query = [FSTTestQuery("rooms/eros/messages")
  106. queryByAddingFilter:FSTTestFilter("text", @"==", @"msg1")];
  107. XCTAssertTrue([query matchesDocument:doc1]);
  108. XCTAssertFalse([query matchesDocument:doc2]);
  109. }
  110. - (void)testMatchesPrimitiveValuesForFilters {
  111. FSTQuery *query1 =
  112. [FSTTestQuery("collection") queryByAddingFilter:FSTTestFilter("sort", @">=", @(2))];
  113. FSTQuery *query2 =
  114. [FSTTestQuery("collection") queryByAddingFilter:FSTTestFilter("sort", @"<=", @(2))];
  115. FSTDocument *doc1 = FSTTestDoc("collection/1", 0, @{@"sort" : @1}, DocumentState::kSynced);
  116. FSTDocument *doc2 = FSTTestDoc("collection/2", 0, @{@"sort" : @2}, DocumentState::kSynced);
  117. FSTDocument *doc3 = FSTTestDoc("collection/3", 0, @{@"sort" : @3}, DocumentState::kSynced);
  118. FSTDocument *doc4 = FSTTestDoc("collection/4", 0, @{@"sort" : @NO}, DocumentState::kSynced);
  119. FSTDocument *doc5 = FSTTestDoc("collection/5", 0, @{@"sort" : @"string"}, DocumentState::kSynced);
  120. FSTDocument *doc6 = FSTTestDoc("collection/6", 0, @{}, DocumentState::kSynced);
  121. XCTAssertFalse([query1 matchesDocument:doc1]);
  122. XCTAssertTrue([query1 matchesDocument:doc2]);
  123. XCTAssertTrue([query1 matchesDocument:doc3]);
  124. XCTAssertFalse([query1 matchesDocument:doc4]);
  125. XCTAssertFalse([query1 matchesDocument:doc5]);
  126. XCTAssertFalse([query1 matchesDocument:doc6]);
  127. XCTAssertTrue([query2 matchesDocument:doc1]);
  128. XCTAssertTrue([query2 matchesDocument:doc2]);
  129. XCTAssertFalse([query2 matchesDocument:doc3]);
  130. XCTAssertFalse([query2 matchesDocument:doc4]);
  131. XCTAssertFalse([query2 matchesDocument:doc5]);
  132. XCTAssertFalse([query2 matchesDocument:doc6]);
  133. }
  134. - (void)testArrayContainsFilter {
  135. FSTQuery *query = [FSTTestQuery("collection")
  136. queryByAddingFilter:FSTTestFilter("array", @"array_contains", @42)];
  137. // not an array.
  138. FSTDocument *doc = FSTTestDoc("collection/1", 0, @{@"array" : @1}, DocumentState::kSynced);
  139. XCTAssertFalse([query matchesDocument:doc]);
  140. // empty array.
  141. doc = FSTTestDoc("collection/1", 0, @{@"array" : @[]}, DocumentState::kSynced);
  142. XCTAssertFalse([query matchesDocument:doc]);
  143. // array without element (and make sure it doesn't match in a nested field or a different field).
  144. doc = FSTTestDoc(
  145. "collection/1", 0,
  146. @{@"array" : @[ @41, @"42", @{@"a" : @42, @"b" : @[ @42 ]} ], @"different" : @[ @42 ]},
  147. DocumentState::kSynced);
  148. XCTAssertFalse([query matchesDocument:doc]);
  149. // array with element.
  150. doc = FSTTestDoc("collection/1", 0, @{@"array" : @[ @1, @"2", @42, @{@"a" : @1} ]},
  151. DocumentState::kSynced);
  152. XCTAssertTrue([query matchesDocument:doc]);
  153. }
  154. - (void)testArrayContainsFilterWithObjectValue {
  155. // Search for arrays containing the object { a: [42] }
  156. FSTQuery *query = [FSTTestQuery("collection")
  157. queryByAddingFilter:FSTTestFilter("array", @"array_contains", @{@"a" : @[ @42 ]})];
  158. // array without element.
  159. FSTDocument *doc = FSTTestDoc("collection/1", 0, @{
  160. @"array" : @[
  161. @{@"a" : @42}, @{@"a" : @[ @42, @43 ]}, @{@"b" : @[ @42 ]}, @{@"a" : @[ @42 ], @"b" : @42}
  162. ]
  163. },
  164. DocumentState::kSynced);
  165. XCTAssertFalse([query matchesDocument:doc]);
  166. // array with element.
  167. doc = FSTTestDoc("collection/1", 0, @{@"array" : @[ @1, @"2", @42, @{@"a" : @[ @42 ]} ]},
  168. DocumentState::kSynced);
  169. XCTAssertTrue([query matchesDocument:doc]);
  170. }
  171. - (void)testNullFilter {
  172. FSTQuery *query =
  173. [FSTTestQuery("collection") queryByAddingFilter:FSTTestFilter("sort", @"==", [NSNull null])];
  174. FSTDocument *doc1 =
  175. FSTTestDoc("collection/1", 0, @{@"sort" : [NSNull null]}, DocumentState::kSynced);
  176. FSTDocument *doc2 = FSTTestDoc("collection/2", 0, @{@"sort" : @2}, DocumentState::kSynced);
  177. FSTDocument *doc3 = FSTTestDoc("collection/2", 0, @{@"sort" : @3.1}, DocumentState::kSynced);
  178. FSTDocument *doc4 = FSTTestDoc("collection/4", 0, @{@"sort" : @NO}, DocumentState::kSynced);
  179. FSTDocument *doc5 = FSTTestDoc("collection/5", 0, @{@"sort" : @"string"}, DocumentState::kSynced);
  180. XCTAssertTrue([query matchesDocument:doc1]);
  181. XCTAssertFalse([query matchesDocument:doc2]);
  182. XCTAssertFalse([query matchesDocument:doc3]);
  183. XCTAssertFalse([query matchesDocument:doc4]);
  184. XCTAssertFalse([query matchesDocument:doc5]);
  185. }
  186. - (void)testNanFilter {
  187. FSTQuery *query =
  188. [FSTTestQuery("collection") queryByAddingFilter:FSTTestFilter("sort", @"==", @(NAN))];
  189. FSTDocument *doc1 = FSTTestDoc("collection/1", 0, @{@"sort" : @(NAN)}, DocumentState::kSynced);
  190. FSTDocument *doc2 = FSTTestDoc("collection/2", 0, @{@"sort" : @2}, DocumentState::kSynced);
  191. FSTDocument *doc3 = FSTTestDoc("collection/2", 0, @{@"sort" : @3.1}, DocumentState::kSynced);
  192. FSTDocument *doc4 = FSTTestDoc("collection/4", 0, @{@"sort" : @NO}, DocumentState::kSynced);
  193. FSTDocument *doc5 = FSTTestDoc("collection/5", 0, @{@"sort" : @"string"}, DocumentState::kSynced);
  194. XCTAssertTrue([query matchesDocument:doc1]);
  195. XCTAssertFalse([query matchesDocument:doc2]);
  196. XCTAssertFalse([query matchesDocument:doc3]);
  197. XCTAssertFalse([query matchesDocument:doc4]);
  198. XCTAssertFalse([query matchesDocument:doc5]);
  199. }
  200. - (void)testDoesNotMatchComplexObjectsForFilters {
  201. FSTQuery *query1 =
  202. [FSTTestQuery("collection") queryByAddingFilter:FSTTestFilter("sort", @"<=", @(2))];
  203. FSTQuery *query2 =
  204. [FSTTestQuery("collection") queryByAddingFilter:FSTTestFilter("sort", @">=", @(2))];
  205. FSTDocument *doc1 = FSTTestDoc("collection/1", 0, @{@"sort" : @2}, DocumentState::kSynced);
  206. FSTDocument *doc2 = FSTTestDoc("collection/2", 0, @{@"sort" : @[]}, DocumentState::kSynced);
  207. FSTDocument *doc3 = FSTTestDoc("collection/3", 0, @{@"sort" : @[ @1 ]}, DocumentState::kSynced);
  208. FSTDocument *doc4 =
  209. FSTTestDoc("collection/4", 0, @{@"sort" : @{@"foo" : @2}}, DocumentState::kSynced);
  210. FSTDocument *doc5 =
  211. FSTTestDoc("collection/5", 0, @{@"sort" : @{@"foo" : @"bar"}}, DocumentState::kSynced);
  212. FSTDocument *doc6 =
  213. FSTTestDoc("collection/6", 0, @{@"sort" : @{}}, DocumentState::kSynced); // no sort field
  214. FSTDocument *doc7 =
  215. FSTTestDoc("collection/7", 0, @{@"sort" : @[ @3, @1 ]}, DocumentState::kSynced);
  216. XCTAssertTrue([query1 matchesDocument:doc1]);
  217. XCTAssertFalse([query1 matchesDocument:doc2]);
  218. XCTAssertFalse([query1 matchesDocument:doc3]);
  219. XCTAssertFalse([query1 matchesDocument:doc4]);
  220. XCTAssertFalse([query1 matchesDocument:doc5]);
  221. XCTAssertFalse([query1 matchesDocument:doc6]);
  222. XCTAssertFalse([query1 matchesDocument:doc7]);
  223. XCTAssertTrue([query2 matchesDocument:doc1]);
  224. XCTAssertFalse([query2 matchesDocument:doc2]);
  225. XCTAssertFalse([query2 matchesDocument:doc3]);
  226. XCTAssertFalse([query2 matchesDocument:doc4]);
  227. XCTAssertFalse([query2 matchesDocument:doc5]);
  228. XCTAssertFalse([query2 matchesDocument:doc6]);
  229. XCTAssertFalse([query2 matchesDocument:doc7]);
  230. }
  231. - (void)testDoesntRemoveComplexObjectsWithOrderBy {
  232. FSTQuery *query1 = [FSTTestQuery("collection")
  233. queryByAddingSortOrder:[FSTSortOrder sortOrderWithFieldPath:testutil::Field("sort")
  234. ascending:YES]];
  235. FSTDocument *doc1 = FSTTestDoc("collection/1", 0, @{@"sort" : @2}, DocumentState::kSynced);
  236. FSTDocument *doc2 = FSTTestDoc("collection/2", 0, @{@"sort" : @[]}, DocumentState::kSynced);
  237. FSTDocument *doc3 = FSTTestDoc("collection/3", 0, @{@"sort" : @[ @1 ]}, DocumentState::kSynced);
  238. FSTDocument *doc4 =
  239. FSTTestDoc("collection/4", 0, @{@"sort" : @{@"foo" : @2}}, DocumentState::kSynced);
  240. FSTDocument *doc5 =
  241. FSTTestDoc("collection/5", 0, @{@"sort" : @{@"foo" : @"bar"}}, DocumentState::kSynced);
  242. FSTDocument *doc6 = FSTTestDoc("collection/6", 0, @{}, DocumentState::kSynced);
  243. XCTAssertTrue([query1 matchesDocument:doc1]);
  244. XCTAssertTrue([query1 matchesDocument:doc2]);
  245. XCTAssertTrue([query1 matchesDocument:doc3]);
  246. XCTAssertTrue([query1 matchesDocument:doc4]);
  247. XCTAssertTrue([query1 matchesDocument:doc5]);
  248. XCTAssertFalse([query1 matchesDocument:doc6]);
  249. }
  250. - (void)testFiltersBasedOnArrayValue {
  251. FSTQuery *baseQuery = FSTTestQuery("collection");
  252. FSTDocument *doc1 =
  253. FSTTestDoc("collection/doc", 0, @{@"tags" : @[ @"foo", @1, @YES ]}, DocumentState::kSynced);
  254. NSArray<FSTFilter *> *matchingFilters = @[ FSTTestFilter("tags", @"==", @[ @"foo", @1, @YES ]) ];
  255. NSArray<FSTFilter *> *nonMatchingFilters = @[
  256. FSTTestFilter("tags", @"==", @"foo"),
  257. FSTTestFilter("tags", @"==", @[ @"foo", @1 ]),
  258. FSTTestFilter("tags", @"==", @[ @"foo", @YES, @1 ]),
  259. ];
  260. for (FSTFilter *filter in matchingFilters) {
  261. XCTAssertTrue([[baseQuery queryByAddingFilter:filter] matchesDocument:doc1]);
  262. }
  263. for (FSTFilter *filter in nonMatchingFilters) {
  264. XCTAssertFalse([[baseQuery queryByAddingFilter:filter] matchesDocument:doc1]);
  265. }
  266. }
  267. - (void)testFiltersBasedOnObjectValue {
  268. FSTQuery *baseQuery = FSTTestQuery("collection");
  269. FSTDocument *doc1 = FSTTestDoc(
  270. "collection/doc", 0, @{@"tags" : @{@"foo" : @"foo", @"a" : @0, @"b" : @YES, @"c" : @(NAN)}},
  271. DocumentState::kSynced);
  272. NSArray<FSTFilter *> *matchingFilters = @[
  273. FSTTestFilter("tags", @"==", @{@"foo" : @"foo", @"a" : @0, @"b" : @YES, @"c" : @(NAN)}),
  274. FSTTestFilter("tags", @"==", @{@"b" : @YES, @"a" : @0, @"foo" : @"foo", @"c" : @(NAN)}),
  275. FSTTestFilter("tags.foo", @"==", @"foo")
  276. ];
  277. NSArray<FSTFilter *> *nonMatchingFilters = @[
  278. FSTTestFilter("tags", @"==", @"foo"), FSTTestFilter("tags", @"==", @{
  279. @"foo" : @"foo",
  280. @"a" : @0,
  281. @"b" : @YES,
  282. })
  283. ];
  284. for (FSTFilter *filter in matchingFilters) {
  285. XCTAssertTrue([[baseQuery queryByAddingFilter:filter] matchesDocument:doc1]);
  286. }
  287. for (FSTFilter *filter in nonMatchingFilters) {
  288. XCTAssertFalse([[baseQuery queryByAddingFilter:filter] matchesDocument:doc1]);
  289. }
  290. }
  291. /**
  292. * Checks that an ordered array of elements yields the correct pair-wise comparison result for the
  293. * supplied comparator.
  294. */
  295. - (void)assertCorrectComparisonsWithArray:(NSArray *)array
  296. comparator:(const DocumentComparator &)comp {
  297. [array enumerateObjectsUsingBlock:^(id iObj, NSUInteger i, BOOL *outerStop) {
  298. [array enumerateObjectsUsingBlock:^(id _Nonnull jObj, NSUInteger j, BOOL *innerStop) {
  299. ComparisonResult expected = util::Compare(i, j);
  300. ComparisonResult actual = comp.Compare(iObj, jObj);
  301. XCTAssertEqual(actual, expected, @"Compared %@ to %@ at (%lu, %lu).", iObj, jObj,
  302. (unsigned long)i, (unsigned long)j);
  303. }];
  304. }];
  305. }
  306. - (void)testSortsDocumentsInTheCorrectOrder {
  307. FSTQuery *query = FSTTestQuery("collection");
  308. query = [query queryByAddingSortOrder:[FSTSortOrder sortOrderWithFieldPath:testutil::Field("sort")
  309. ascending:YES]];
  310. // clang-format off
  311. NSArray<FSTDocument *> *docs = @[
  312. FSTTestDoc("collection/1", 0, @{@"sort": [NSNull null]}, DocumentState::kSynced),
  313. FSTTestDoc("collection/1", 0, @{@"sort": @NO}, DocumentState::kSynced),
  314. FSTTestDoc("collection/1", 0, @{@"sort": @YES}, DocumentState::kSynced),
  315. FSTTestDoc("collection/1", 0, @{@"sort": @1}, DocumentState::kSynced),
  316. FSTTestDoc("collection/2", 0, @{@"sort": @1}, DocumentState::kSynced), // by key
  317. FSTTestDoc("collection/3", 0, @{@"sort": @1}, DocumentState::kSynced), // by key
  318. FSTTestDoc("collection/1", 0, @{@"sort": @1.9}, DocumentState::kSynced),
  319. FSTTestDoc("collection/1", 0, @{@"sort": @2}, DocumentState::kSynced),
  320. FSTTestDoc("collection/1", 0, @{@"sort": @2.1}, DocumentState::kSynced),
  321. FSTTestDoc("collection/1", 0, @{@"sort": @""}, DocumentState::kSynced),
  322. FSTTestDoc("collection/1", 0, @{@"sort": @"a"}, DocumentState::kSynced),
  323. FSTTestDoc("collection/1", 0, @{@"sort": @"ab"}, DocumentState::kSynced),
  324. FSTTestDoc("collection/1", 0, @{@"sort": @"b"}, DocumentState::kSynced),
  325. FSTTestDoc("collection/1", 0, @{@"sort":
  326. FSTTestRef("project", DatabaseId::kDefault, @"collection/id1")}, DocumentState::kSynced),
  327. ];
  328. // clang-format on
  329. [self assertCorrectComparisonsWithArray:docs comparator:query.comparator];
  330. }
  331. - (void)testSortsDocumentsUsingMultipleFields {
  332. FSTQuery *query = FSTTestQuery("collection");
  333. query =
  334. [query queryByAddingSortOrder:[FSTSortOrder sortOrderWithFieldPath:testutil::Field("sort1")
  335. ascending:YES]];
  336. query =
  337. [query queryByAddingSortOrder:[FSTSortOrder sortOrderWithFieldPath:testutil::Field("sort2")
  338. ascending:YES]];
  339. // clang-format off
  340. NSArray<FSTDocument *> *docs =
  341. @[FSTTestDoc("collection/1", 0, @{@"sort1": @1, @"sort2": @1}, DocumentState::kSynced),
  342. FSTTestDoc("collection/1", 0, @{@"sort1": @1, @"sort2": @2}, DocumentState::kSynced),
  343. FSTTestDoc("collection/2", 0, @{@"sort1": @1, @"sort2": @2}, DocumentState::kSynced), // by key
  344. FSTTestDoc("collection/3", 0, @{@"sort1": @1, @"sort2": @2}, DocumentState::kSynced), // by key
  345. FSTTestDoc("collection/1", 0, @{@"sort1": @1, @"sort2": @3}, DocumentState::kSynced),
  346. FSTTestDoc("collection/1", 0, @{@"sort1": @2, @"sort2": @1}, DocumentState::kSynced),
  347. FSTTestDoc("collection/1", 0, @{@"sort1": @2, @"sort2": @2}, DocumentState::kSynced),
  348. FSTTestDoc("collection/2", 0, @{@"sort1": @2, @"sort2": @2}, DocumentState::kSynced), // by key
  349. FSTTestDoc("collection/3", 0, @{@"sort1": @2, @"sort2": @2}, DocumentState::kSynced), // by key
  350. FSTTestDoc("collection/1", 0, @{@"sort1": @2, @"sort2": @3}, DocumentState::kSynced),
  351. ];
  352. // clang-format on
  353. [self assertCorrectComparisonsWithArray:docs comparator:query.comparator];
  354. }
  355. - (void)testSortsDocumentsWithDescendingToo {
  356. FSTQuery *query = FSTTestQuery("collection");
  357. query =
  358. [query queryByAddingSortOrder:[FSTSortOrder sortOrderWithFieldPath:testutil::Field("sort1")
  359. ascending:NO]];
  360. query =
  361. [query queryByAddingSortOrder:[FSTSortOrder sortOrderWithFieldPath:testutil::Field("sort2")
  362. ascending:NO]];
  363. // clang-format off
  364. NSArray<FSTDocument *> *docs =
  365. @[FSTTestDoc("collection/1", 0, @{@"sort1": @2, @"sort2": @3}, DocumentState::kSynced),
  366. FSTTestDoc("collection/3", 0, @{@"sort1": @2, @"sort2": @2}, DocumentState::kSynced),
  367. FSTTestDoc("collection/2", 0, @{@"sort1": @2, @"sort2": @2}, DocumentState::kSynced), // by key
  368. FSTTestDoc("collection/1", 0, @{@"sort1": @2, @"sort2": @2}, DocumentState::kSynced), // by key
  369. FSTTestDoc("collection/1", 0, @{@"sort1": @2, @"sort2": @1}, DocumentState::kSynced),
  370. FSTTestDoc("collection/1", 0, @{@"sort1": @1, @"sort2": @3}, DocumentState::kSynced),
  371. FSTTestDoc("collection/3", 0, @{@"sort1": @1, @"sort2": @2}, DocumentState::kSynced),
  372. FSTTestDoc("collection/2", 0, @{@"sort1": @1, @"sort2": @2}, DocumentState::kSynced), // by key
  373. FSTTestDoc("collection/1", 0, @{@"sort1": @1, @"sort2": @2}, DocumentState::kSynced), // by key
  374. FSTTestDoc("collection/1", 0, @{@"sort1": @1, @"sort2": @1}, DocumentState::kSynced),
  375. ];
  376. // clang-format on
  377. [self assertCorrectComparisonsWithArray:docs comparator:query.comparator];
  378. }
  379. - (void)testEquality {
  380. FSTQuery *q11 = FSTTestQuery("foo");
  381. q11 = [q11 queryByAddingFilter:FSTTestFilter("i1", @"<", @(2))];
  382. q11 = [q11 queryByAddingFilter:FSTTestFilter("i2", @"==", @(3))];
  383. FSTQuery *q12 = FSTTestQuery("foo");
  384. q12 = [q12 queryByAddingFilter:FSTTestFilter("i2", @"==", @(3))];
  385. q12 = [q12 queryByAddingFilter:FSTTestFilter("i1", @"<", @(2))];
  386. FSTQuery *q21 = FSTTestQuery("foo");
  387. FSTQuery *q22 = FSTTestQuery("foo");
  388. FSTQuery *q31 = FSTTestQuery("foo/bar");
  389. FSTQuery *q32 = FSTTestQuery("foo/bar");
  390. FSTQuery *q41 = FSTTestQuery("foo");
  391. q41 = [q41 queryByAddingSortBy:"foo" ascending:YES];
  392. q41 = [q41 queryByAddingSortBy:"bar" ascending:YES];
  393. FSTQuery *q42 = FSTTestQuery("foo");
  394. q42 = [q42 queryByAddingSortBy:"foo" ascending:YES];
  395. q42 = [q42 queryByAddingSortBy:"bar" ascending:YES];
  396. FSTQuery *q43Diff = FSTTestQuery("foo");
  397. q43Diff = [q43Diff queryByAddingSortBy:"bar" ascending:YES];
  398. q43Diff = [q43Diff queryByAddingSortBy:"foo" ascending:YES];
  399. FSTQuery *q51 = FSTTestQuery("foo");
  400. q51 = [q51 queryByAddingSortBy:"foo" ascending:YES];
  401. q51 = [q51 queryByAddingFilter:FSTTestFilter("foo", @">", @(2))];
  402. FSTQuery *q52 = FSTTestQuery("foo");
  403. q52 = [q52 queryByAddingFilter:FSTTestFilter("foo", @">", @(2))];
  404. q52 = [q52 queryByAddingSortBy:"foo" ascending:YES];
  405. FSTQuery *q53Diff = FSTTestQuery("foo");
  406. q53Diff = [q53Diff queryByAddingFilter:FSTTestFilter("bar", @">", @(2))];
  407. q53Diff = [q53Diff queryByAddingSortBy:"bar" ascending:YES];
  408. FSTQuery *q61 = FSTTestQuery("foo");
  409. q61 = [q61 queryBySettingLimit:10];
  410. // XCTAssertEqualObjects(q11, q12); // TODO(klimt): not canonical yet
  411. XCTAssertNotEqualObjects(q11, q21);
  412. XCTAssertNotEqualObjects(q11, q31);
  413. XCTAssertNotEqualObjects(q11, q41);
  414. XCTAssertNotEqualObjects(q11, q51);
  415. XCTAssertNotEqualObjects(q11, q61);
  416. XCTAssertEqualObjects(q21, q22);
  417. XCTAssertNotEqualObjects(q21, q31);
  418. XCTAssertNotEqualObjects(q21, q41);
  419. XCTAssertNotEqualObjects(q21, q51);
  420. XCTAssertNotEqualObjects(q21, q61);
  421. XCTAssertEqualObjects(q31, q32);
  422. XCTAssertNotEqualObjects(q31, q41);
  423. XCTAssertNotEqualObjects(q31, q51);
  424. XCTAssertNotEqualObjects(q31, q61);
  425. XCTAssertEqualObjects(q41, q42);
  426. XCTAssertNotEqualObjects(q41, q43Diff);
  427. XCTAssertNotEqualObjects(q41, q51);
  428. XCTAssertNotEqualObjects(q41, q61);
  429. XCTAssertEqualObjects(q51, q52);
  430. XCTAssertNotEqualObjects(q51, q53Diff);
  431. XCTAssertNotEqualObjects(q51, q61);
  432. }
  433. - (void)testUniqueIds {
  434. FSTQuery *q11 = FSTTestQuery("foo");
  435. q11 = [q11 queryByAddingFilter:FSTTestFilter("i1", @"<", @(2))];
  436. q11 = [q11 queryByAddingFilter:FSTTestFilter("i2", @"==", @(3))];
  437. FSTQuery *q12 = FSTTestQuery("foo");
  438. q12 = [q12 queryByAddingFilter:FSTTestFilter("i2", @"==", @(3))];
  439. q12 = [q12 queryByAddingFilter:FSTTestFilter("i1", @"<", @(2))];
  440. FSTQuery *q21 = FSTTestQuery("foo");
  441. FSTQuery *q22 = FSTTestQuery("foo");
  442. FSTQuery *q31 = FSTTestQuery("foo/bar");
  443. FSTQuery *q32 = FSTTestQuery("foo/bar");
  444. FSTQuery *q41 = FSTTestQuery("foo");
  445. q41 = [q41 queryByAddingSortBy:"foo" ascending:YES];
  446. q41 = [q41 queryByAddingSortBy:"bar" ascending:YES];
  447. FSTQuery *q42 = FSTTestQuery("foo");
  448. q42 = [q42 queryByAddingSortBy:"foo" ascending:YES];
  449. q42 = [q42 queryByAddingSortBy:"bar" ascending:YES];
  450. FSTQuery *q43Diff = FSTTestQuery("foo");
  451. q43Diff = [q43Diff queryByAddingSortBy:"bar" ascending:YES];
  452. q43Diff = [q43Diff queryByAddingSortBy:"foo" ascending:YES];
  453. FSTQuery *q51 = FSTTestQuery("foo");
  454. q51 = [q51 queryByAddingSortBy:"foo" ascending:YES];
  455. q51 = [q51 queryByAddingFilter:FSTTestFilter("foo", @">", @(2))];
  456. FSTQuery *q52 = FSTTestQuery("foo");
  457. q52 = [q52 queryByAddingFilter:FSTTestFilter("foo", @">", @(2))];
  458. q52 = [q52 queryByAddingSortBy:"foo" ascending:YES];
  459. FSTQuery *q53Diff = FSTTestQuery("foo");
  460. q53Diff = [q53Diff queryByAddingFilter:FSTTestFilter("bar", @">", @(2))];
  461. q53Diff = [q53Diff queryByAddingSortBy:"bar" ascending:YES];
  462. FSTQuery *q61 = FSTTestQuery("foo");
  463. q61 = [q61 queryBySettingLimit:10];
  464. // XCTAssertEqual(q11.hash, q12.hash); // TODO(klimt): not canonical yet
  465. XCTAssertNotEqual(q11.hash, q21.hash);
  466. XCTAssertNotEqual(q11.hash, q31.hash);
  467. XCTAssertNotEqual(q11.hash, q41.hash);
  468. XCTAssertNotEqual(q11.hash, q51.hash);
  469. XCTAssertNotEqual(q11.hash, q61.hash);
  470. XCTAssertEqual(q21.hash, q22.hash);
  471. XCTAssertNotEqual(q21.hash, q31.hash);
  472. XCTAssertNotEqual(q21.hash, q41.hash);
  473. XCTAssertNotEqual(q21.hash, q51.hash);
  474. XCTAssertNotEqual(q21.hash, q61.hash);
  475. XCTAssertEqual(q31.hash, q32.hash);
  476. XCTAssertNotEqual(q31.hash, q41.hash);
  477. XCTAssertNotEqual(q31.hash, q51.hash);
  478. XCTAssertNotEqual(q31.hash, q61.hash);
  479. XCTAssertEqual(q41.hash, q42.hash);
  480. XCTAssertNotEqual(q41.hash, q43Diff.hash);
  481. XCTAssertNotEqual(q41.hash, q51.hash);
  482. XCTAssertNotEqual(q41.hash, q61.hash);
  483. XCTAssertEqual(q51.hash, q52.hash);
  484. XCTAssertNotEqual(q51.hash, q53Diff.hash);
  485. XCTAssertNotEqual(q51.hash, q61.hash);
  486. }
  487. - (void)testImplicitOrderBy {
  488. FSTQuery *baseQuery = FSTTestQuery("foo");
  489. // Default is ascending
  490. XCTAssertEqualObjects(baseQuery.sortOrders,
  491. @[ FSTTestOrderBy(FieldPath::kDocumentKeyPath, @"asc") ]);
  492. // Explicit key ordering is respected
  493. XCTAssertEqualObjects(
  494. [baseQuery queryByAddingSortOrder:FSTTestOrderBy(FieldPath::kDocumentKeyPath, @"asc")]
  495. .sortOrders,
  496. @[ FSTTestOrderBy(FieldPath::kDocumentKeyPath, @"asc") ]);
  497. XCTAssertEqualObjects(
  498. [baseQuery queryByAddingSortOrder:FSTTestOrderBy(FieldPath::kDocumentKeyPath, @"desc")]
  499. .sortOrders,
  500. @[ FSTTestOrderBy(FieldPath::kDocumentKeyPath, @"desc") ]);
  501. XCTAssertEqualObjects(
  502. [[baseQuery queryByAddingSortOrder:FSTTestOrderBy("foo", @"asc")]
  503. queryByAddingSortOrder:FSTTestOrderBy(FieldPath::kDocumentKeyPath, @"asc")]
  504. .sortOrders,
  505. (@[ FSTTestOrderBy("foo", @"asc"), FSTTestOrderBy(FieldPath::kDocumentKeyPath, @"asc") ]));
  506. XCTAssertEqualObjects(
  507. [[baseQuery queryByAddingSortOrder:FSTTestOrderBy("foo", @"asc")]
  508. queryByAddingSortOrder:FSTTestOrderBy(FieldPath::kDocumentKeyPath, @"desc")]
  509. .sortOrders,
  510. (@[ FSTTestOrderBy("foo", @"asc"), FSTTestOrderBy(FieldPath::kDocumentKeyPath, @"desc") ]));
  511. // Inequality filters add order bys
  512. XCTAssertEqualObjects(
  513. [baseQuery queryByAddingFilter:FSTTestFilter("foo", @"<", @5)].sortOrders,
  514. (@[ FSTTestOrderBy("foo", @"asc"), FSTTestOrderBy(FieldPath::kDocumentKeyPath, @"asc") ]));
  515. // Descending order by applies to implicit key ordering
  516. XCTAssertEqualObjects(
  517. [baseQuery queryByAddingSortOrder:FSTTestOrderBy("foo", @"desc")].sortOrders,
  518. (@[ FSTTestOrderBy("foo", @"desc"), FSTTestOrderBy(FieldPath::kDocumentKeyPath, @"desc") ]));
  519. XCTAssertEqualObjects([[baseQuery queryByAddingSortOrder:FSTTestOrderBy("foo", @"asc")]
  520. queryByAddingSortOrder:FSTTestOrderBy("bar", @"desc")]
  521. .sortOrders,
  522. (@[
  523. FSTTestOrderBy("foo", @"asc"), FSTTestOrderBy("bar", @"desc"),
  524. FSTTestOrderBy(FieldPath::kDocumentKeyPath, @"desc")
  525. ]));
  526. XCTAssertEqualObjects([[baseQuery queryByAddingSortOrder:FSTTestOrderBy("foo", @"desc")]
  527. queryByAddingSortOrder:FSTTestOrderBy("bar", @"asc")]
  528. .sortOrders,
  529. (@[
  530. FSTTestOrderBy("foo", @"desc"), FSTTestOrderBy("bar", @"asc"),
  531. FSTTestOrderBy(FieldPath::kDocumentKeyPath, @"asc")
  532. ]));
  533. }
  534. @end
  535. NS_ASSUME_NONNULL_END