QueryIntegrationTests.swift 12 KB

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