QueryIntegrationTests.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /*
  2. * Copyright 2023 Google LLC
  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
  17. import FirebaseFirestoreSwift
  18. import Foundation
  19. class QueryIntegrationTests: FSTIntegrationTestCase {
  20. /**
  21. * Checks that running the query while online (against the backend/emulator) results in the same
  22. * documents as running the query while offline. If expectedDocs is provided, it also checks
  23. * that both online and offline query result is equal to the expected documents.
  24. *
  25. * @param query The query to check.
  26. * @param expectedDocs Ordered list of document keys that are expected to match the query.
  27. */
  28. private func checkOnlineAndOfflineQuery(_ query: Query, matchesResult expectedDocs: [String]?) {
  29. let docsFromServer = readDocumentSet(forRef: query,
  30. source: FirestoreSource.server)
  31. let docsFromCache = readDocumentSet(forRef: query,
  32. source: FirestoreSource.cache)
  33. XCTAssertEqual(FIRQuerySnapshotGetIDs(docsFromServer),
  34. FIRQuerySnapshotGetIDs(docsFromCache))
  35. if expectedDocs != nil {
  36. XCTAssertEqual(FIRQuerySnapshotGetIDs(docsFromCache), expectedDocs)
  37. }
  38. }
  39. func testOrQueries() throws {
  40. let collRef = collectionRef(
  41. withDocuments: ["doc1": ["a": 1, "b": 0],
  42. "doc2": ["a": 2, "b": 1],
  43. "doc3": ["a": 3, "b": 2],
  44. "doc4": ["a": 1, "b": 3],
  45. "doc5": ["a": 1, "b": 1]]
  46. )
  47. // Two equalities: a==1 || b==1.
  48. let filter1 = Filter.orFilter(
  49. [Filter.whereField("a", isEqualTo: 1),
  50. Filter.whereField("b", isEqualTo: 1)]
  51. )
  52. checkOnlineAndOfflineQuery(collRef.whereFilter(filter1),
  53. matchesResult: ["doc1", "doc2", "doc4", "doc5"])
  54. // (a==1 && b==0) || (a==3 && b==2)
  55. let filter2 = Filter.orFilter(
  56. [Filter.andFilter(
  57. [Filter.whereField("a", isEqualTo: 1),
  58. Filter.whereField("b", isEqualTo: 0)]
  59. ),
  60. Filter.andFilter(
  61. [Filter.whereField("a", isEqualTo: 3),
  62. Filter.whereField("b", isEqualTo: 2)]
  63. )]
  64. )
  65. checkOnlineAndOfflineQuery(collRef.whereFilter(filter2),
  66. matchesResult: ["doc1", "doc3"])
  67. // a==1 && (b==0 || b==3).
  68. let filter3 = Filter.andFilter(
  69. [Filter.whereField("a", isEqualTo: 1),
  70. Filter.orFilter(
  71. [Filter.whereField("b", isEqualTo: 0),
  72. Filter.whereField("b", isEqualTo: 3)]
  73. )]
  74. )
  75. checkOnlineAndOfflineQuery(collRef.whereFilter(filter3),
  76. matchesResult: ["doc1", "doc4"])
  77. // (a==2 || b==2) && (a==3 || b==3)
  78. let filter4 = Filter.andFilter(
  79. [Filter.orFilter(
  80. [Filter.whereField("a", isEqualTo: 2),
  81. Filter.whereField("b", isEqualTo: 2)]
  82. ),
  83. Filter.orFilter(
  84. [Filter.whereField("a", isEqualTo: 3),
  85. Filter.whereField("b", isEqualTo: 3)]
  86. )]
  87. )
  88. checkOnlineAndOfflineQuery(collRef.whereFilter(filter4),
  89. matchesResult: ["doc3"])
  90. // Test with limits without orderBy (the __name__ ordering is the tie breaker).
  91. let filter5 = Filter.orFilter(
  92. [Filter.whereField("a", isEqualTo: 2),
  93. Filter.whereField("b", isEqualTo: 1)]
  94. )
  95. checkOnlineAndOfflineQuery(collRef.whereFilter(filter5).limit(to: 1),
  96. matchesResult: ["doc2"])
  97. }
  98. func testOrQueriesWithCompositeIndexes() throws {
  99. // TODO(orquery): Enable this test against production when possible.
  100. try XCTSkipIf(!FSTIntegrationTestCase.isRunningAgainstEmulator(),
  101. "Skip this test if running against production because it results in" +
  102. "a 'missing index' error. The Firestore Emulator, however, does serve these queries.")
  103. let collRef = collectionRef(
  104. withDocuments: ["doc1": ["a": 1, "b": 0],
  105. "doc2": ["a": 2, "b": 1],
  106. "doc3": ["a": 3, "b": 2],
  107. "doc4": ["a": 1, "b": 3],
  108. "doc5": ["a": 1, "b": 1]]
  109. )
  110. // with one inequality: a>2 || b==1.
  111. let filter1 = Filter.orFilter(
  112. [Filter.whereField("a", isGreaterThan: 2),
  113. Filter.whereField("b", isEqualTo: 1)]
  114. )
  115. checkOnlineAndOfflineQuery(collRef.whereFilter(filter1),
  116. matchesResult: ["doc5", "doc2", "doc3"])
  117. // Test with limits (implicit order by ASC): (a==1) || (b > 0) LIMIT 2
  118. let filter2 = Filter.orFilter(
  119. [Filter.whereField("a", isEqualTo: 1),
  120. Filter.whereField("b", isGreaterThan: 0)]
  121. )
  122. checkOnlineAndOfflineQuery(collRef.whereFilter(filter2).limit(to: 2),
  123. matchesResult: ["doc1", "doc2"])
  124. // Test with limits (explicit order by): (a==1) || (b > 0) LIMIT_TO_LAST 2
  125. // Note: The public query API does not allow implicit ordering when limitToLast is used.
  126. let filter3 = Filter.orFilter(
  127. [Filter.whereField("a", isEqualTo: 1),
  128. Filter.whereField("b", isGreaterThan: 0)]
  129. )
  130. checkOnlineAndOfflineQuery(collRef.whereFilter(filter3)
  131. .limit(toLast: 2)
  132. .order(by: "b"),
  133. matchesResult: ["doc3", "doc4"])
  134. // Test with limits (explicit order by ASC): (a==2) || (b == 1) ORDER BY a LIMIT 1
  135. let filter4 = Filter.orFilter(
  136. [Filter.whereField("a", isEqualTo: 2),
  137. Filter.whereField("b", isEqualTo: 1)]
  138. )
  139. checkOnlineAndOfflineQuery(collRef.whereFilter(filter4).limit(to: 1)
  140. .order(by: "a"),
  141. matchesResult: ["doc5"])
  142. // Test with limits (explicit order by DESC): (a==2) || (b == 1) ORDER BY a LIMIT_TO_LAST 1
  143. let filter5 = Filter.orFilter(
  144. [Filter.whereField("a", isEqualTo: 2),
  145. Filter.whereField("b", isEqualTo: 1)]
  146. )
  147. checkOnlineAndOfflineQuery(collRef.whereFilter(filter5).limit(toLast: 1)
  148. .order(by: "a"),
  149. matchesResult: ["doc2"])
  150. }
  151. func testOrQueriesWithIn() throws {
  152. let collRef = collectionRef(
  153. withDocuments: ["doc1": ["a": 1, "b": 0],
  154. "doc2": ["b": 1],
  155. "doc3": ["a": 3, "b": 2],
  156. "doc4": ["a": 1, "b": 3],
  157. "doc5": ["a": 1],
  158. "doc6": ["a": 2]]
  159. )
  160. // a==2 || b in [2,3]
  161. let filter = Filter.orFilter(
  162. [Filter.whereField("a", isEqualTo: 2),
  163. Filter.whereField("b", in: [2, 3])]
  164. )
  165. checkOnlineAndOfflineQuery(collRef.whereFilter(filter),
  166. matchesResult: ["doc3", "doc4", "doc6"])
  167. }
  168. func testOrQueriesWithNotIn() throws {
  169. // TODO(orquery): Enable this test against production when possible.
  170. try XCTSkipIf(!FSTIntegrationTestCase.isRunningAgainstEmulator(),
  171. "Skip this test if running against production because it results in " +
  172. "a 'missing index' error. The Firestore Emulator, however, does serve these queries")
  173. let collRef = collectionRef(
  174. withDocuments: ["doc1": ["a": 1, "b": 0],
  175. "doc2": ["b": 1],
  176. "doc3": ["a": 3, "b": 2],
  177. "doc4": ["a": 1, "b": 3],
  178. "doc5": ["a": 1],
  179. "doc6": ["a": 2]]
  180. )
  181. // a==2 || b not-in [2,3]
  182. // Has implicit orderBy b.
  183. let filter = Filter.orFilter(
  184. [Filter.whereField("a", isEqualTo: 2),
  185. Filter.whereField("b", notIn: [2, 3])]
  186. )
  187. checkOnlineAndOfflineQuery(collRef.whereFilter(filter),
  188. matchesResult: ["doc1", "doc2"])
  189. }
  190. func testOrQueriesWithArrayMembership() throws {
  191. let collRef = collectionRef(
  192. withDocuments: ["doc1": ["a": 1, "b": [0]],
  193. "doc2": ["b": 1],
  194. "doc3": ["a": 3, "b": [2, 7]],
  195. "doc4": ["a": 1, "b": [3, 7]],
  196. "doc5": ["a": 1],
  197. "doc6": ["a": 2]]
  198. )
  199. // a==2 || b array-contains 7
  200. let filter1 = Filter.orFilter(
  201. [Filter.whereField("a", isEqualTo: 2),
  202. Filter.whereField("b", arrayContains: 7)]
  203. )
  204. checkOnlineAndOfflineQuery(collRef.whereFilter(filter1),
  205. matchesResult: ["doc3", "doc4", "doc6"])
  206. // a==2 || b array-contains-any [0, 3]
  207. let filter2 = Filter.orFilter(
  208. [Filter.whereField("a", isEqualTo: 2),
  209. Filter.whereField("b", arrayContainsAny: [0, 3])]
  210. )
  211. checkOnlineAndOfflineQuery(collRef.whereFilter(filter2),
  212. matchesResult: ["doc1", "doc4", "doc6"])
  213. }
  214. func testMultipleInOps() throws {
  215. // TODO(orquery): Enable this test against production when possible.
  216. try XCTSkipIf(!FSTIntegrationTestCase.isRunningAgainstEmulator(),
  217. "Skip this test if running against production because it's not yet supported.")
  218. let collRef = collectionRef(
  219. withDocuments: ["doc1": ["a": 1, "b": 0],
  220. "doc2": ["b": 1],
  221. "doc3": ["a": 3, "b": 2],
  222. "doc4": ["a": 1, "b": 3],
  223. "doc5": ["a": 1],
  224. "doc6": ["a": 2]]
  225. )
  226. // Two IN operations on different fields with disjunction.
  227. let filter1 = Filter.orFilter(
  228. [Filter.whereField("a", in: [2, 3]),
  229. Filter.whereField("b", in: [0, 2])]
  230. )
  231. checkOnlineAndOfflineQuery(collRef.whereFilter(filter1).order(by: "a"),
  232. matchesResult: ["doc1", "doc6", "doc3"])
  233. // Two IN operations on same fields with disjunction.
  234. // a IN [0,3] || a IN [0,2] should union them (similar to: a IN [0,2,3]).
  235. let filter2 = Filter.orFilter(
  236. [Filter.whereField("a", in: [0, 3]),
  237. Filter.whereField("a", in: [0, 2])]
  238. )
  239. checkOnlineAndOfflineQuery(collRef.whereFilter(filter2),
  240. matchesResult: ["doc3", "doc6"])
  241. }
  242. func testUsingInWithArrayContainsAny() throws {
  243. // TODO(orquery): Enable this test against production when possible.
  244. try XCTSkipIf(!FSTIntegrationTestCase.isRunningAgainstEmulator(),
  245. "Skip this test if running against production because it's not yet supported.")
  246. let collRef = collectionRef(
  247. withDocuments: ["doc1": ["a": 1, "b": [0]],
  248. "doc2": ["b": [1]],
  249. "doc3": ["a": 3, "b": [2, 7], "c": 10],
  250. "doc4": ["a": 1, "b": [3, 7]],
  251. "doc5": ["a": 1],
  252. "doc6": ["a": 2, "c": 20]]
  253. )
  254. let filter1 = Filter.orFilter(
  255. [Filter.whereField("a", in: [2, 3]),
  256. Filter.whereField("b", arrayContainsAny: [0, 7])]
  257. )
  258. checkOnlineAndOfflineQuery(collRef.whereFilter(filter1),
  259. matchesResult: ["doc1", "doc3", "doc4", "doc6"])
  260. let filter2 = Filter.orFilter(
  261. [Filter.andFilter(
  262. [Filter.whereField("a", in: [2, 3]),
  263. Filter.whereField("c", isEqualTo: 10)]
  264. ),
  265. Filter.whereField("b", arrayContainsAny: [0, 7])]
  266. )
  267. checkOnlineAndOfflineQuery(collRef.whereFilter(filter2),
  268. matchesResult: ["doc1", "doc3", "doc4"])
  269. }
  270. func testUseInWithArrayContains() throws {
  271. let collRef = collectionRef(
  272. withDocuments: ["doc1": ["a": 1, "b": [0]],
  273. "doc2": ["b": [1]],
  274. "doc3": ["a": 3, "b": [2, 7]],
  275. "doc4": ["a": 1, "b": [3, 7]],
  276. "doc5": ["a": 1],
  277. "doc6": ["a": 2]]
  278. )
  279. let filter1 = Filter.orFilter(
  280. [Filter.whereField("a", in: [2, 3]),
  281. Filter.whereField("b", arrayContainsAny: [3])]
  282. )
  283. checkOnlineAndOfflineQuery(collRef.whereFilter(filter1),
  284. matchesResult: ["doc3", "doc4", "doc6"])
  285. let filter2 = Filter.andFilter(
  286. [Filter.whereField("a", in: [2, 3]),
  287. Filter.whereField("b", arrayContains: 7)]
  288. )
  289. checkOnlineAndOfflineQuery(collRef.whereFilter(filter2),
  290. matchesResult: ["doc3"])
  291. let filter3 = Filter.orFilter(
  292. [Filter.whereField("a", in: [2, 3]),
  293. Filter.andFilter(
  294. [Filter.whereField("b", arrayContains: 3),
  295. Filter.whereField("a", isEqualTo: 1)]
  296. )]
  297. )
  298. checkOnlineAndOfflineQuery(collRef.whereFilter(filter3),
  299. matchesResult: ["doc3", "doc4", "doc6"])
  300. let filter4 = Filter.andFilter(
  301. [Filter.whereField("a", in: [2, 3]),
  302. Filter.orFilter(
  303. [Filter.whereField("b", arrayContains: 7),
  304. Filter.whereField("a", isEqualTo: 1)]
  305. )]
  306. )
  307. checkOnlineAndOfflineQuery(collRef.whereFilter(filter4),
  308. matchesResult: ["doc3"])
  309. }
  310. func testOrderByEquality() throws {
  311. // TODO(orquery): Enable this test against production when possible.
  312. try XCTSkipIf(!FSTIntegrationTestCase.isRunningAgainstEmulator(),
  313. "Skip this test if running against production because order-by-equality is not supported yet.")
  314. let collRef = collectionRef(
  315. withDocuments: ["doc1": ["a": 1, "b": [0]],
  316. "doc2": ["b": [1]],
  317. "doc3": ["a": 3, "b": [2, 7], "c": 10],
  318. "doc4": ["a": 1, "b": [3, 7]],
  319. "doc5": ["a": 1],
  320. "doc6": ["a": 2, "c": 20]]
  321. )
  322. checkOnlineAndOfflineQuery(collRef.whereFilter(Filter.whereField("a", isEqualTo: 1)),
  323. matchesResult: ["doc1", "doc4", "doc5"])
  324. checkOnlineAndOfflineQuery(
  325. collRef.whereFilter(Filter.whereField("a", in: [2, 3])).order(by: "a"),
  326. matchesResult: ["doc6", "doc3"]
  327. )
  328. }
  329. }