FSTQueryTests.mm 29 KB

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