QueryIntegrationTests.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 testOrQueriesWithArrayMembership() throws {
  169. let collRef = collectionRef(
  170. withDocuments: ["doc1": ["a": 1, "b": [0]],
  171. "doc2": ["b": 1],
  172. "doc3": ["a": 3, "b": [2, 7]],
  173. "doc4": ["a": 1, "b": [3, 7]],
  174. "doc5": ["a": 1],
  175. "doc6": ["a": 2]]
  176. )
  177. // a==2 || b array-contains 7
  178. let filter1 = Filter.orFilter(
  179. [Filter.whereField("a", isEqualTo: 2),
  180. Filter.whereField("b", arrayContains: 7)]
  181. )
  182. checkOnlineAndOfflineQuery(collRef.whereFilter(filter1),
  183. matchesResult: ["doc3", "doc4", "doc6"])
  184. // a==2 || b array-contains-any [0, 3]
  185. let filter2 = Filter.orFilter(
  186. [Filter.whereField("a", isEqualTo: 2),
  187. Filter.whereField("b", arrayContainsAny: [0, 3])]
  188. )
  189. checkOnlineAndOfflineQuery(collRef.whereFilter(filter2),
  190. matchesResult: ["doc1", "doc4", "doc6"])
  191. }
  192. func testMultipleInOps() throws {
  193. let collRef = collectionRef(
  194. withDocuments: ["doc1": ["a": 1, "b": 0],
  195. "doc2": ["b": 1],
  196. "doc3": ["a": 3, "b": 2],
  197. "doc4": ["a": 1, "b": 3],
  198. "doc5": ["a": 1],
  199. "doc6": ["a": 2]]
  200. )
  201. // Two IN operations on different fields with disjunction.
  202. let filter1 = Filter.orFilter(
  203. [Filter.whereField("a", in: [2, 3]),
  204. Filter.whereField("b", in: [0, 2])]
  205. )
  206. checkOnlineAndOfflineQuery(collRef.whereFilter(filter1).order(by: "a"),
  207. matchesResult: ["doc1", "doc6", "doc3"])
  208. // Two IN operations on same fields with disjunction.
  209. // a IN [0,3] || a IN [0,2] should union them (similar to: a IN [0,2,3]).
  210. let filter2 = Filter.orFilter(
  211. [Filter.whereField("a", in: [0, 3]),
  212. Filter.whereField("a", in: [0, 2])]
  213. )
  214. checkOnlineAndOfflineQuery(collRef.whereFilter(filter2),
  215. matchesResult: ["doc3", "doc6"])
  216. }
  217. func testUsingInWithArrayContainsAny() throws {
  218. let collRef = collectionRef(
  219. withDocuments: ["doc1": ["a": 1, "b": [0]],
  220. "doc2": ["b": [1]],
  221. "doc3": ["a": 3, "b": [2, 7], "c": 10],
  222. "doc4": ["a": 1, "b": [3, 7]],
  223. "doc5": ["a": 1],
  224. "doc6": ["a": 2, "c": 20]]
  225. )
  226. let filter1 = Filter.orFilter(
  227. [Filter.whereField("a", in: [2, 3]),
  228. Filter.whereField("b", arrayContainsAny: [0, 7])]
  229. )
  230. checkOnlineAndOfflineQuery(collRef.whereFilter(filter1),
  231. matchesResult: ["doc1", "doc3", "doc4", "doc6"])
  232. let filter2 = Filter.orFilter(
  233. [Filter.andFilter(
  234. [Filter.whereField("a", in: [2, 3]),
  235. Filter.whereField("c", isEqualTo: 10)]
  236. ),
  237. Filter.whereField("b", arrayContainsAny: [0, 7])]
  238. )
  239. checkOnlineAndOfflineQuery(collRef.whereFilter(filter2),
  240. matchesResult: ["doc1", "doc3", "doc4"])
  241. }
  242. func testUseInWithArrayContains() throws {
  243. let collRef = collectionRef(
  244. withDocuments: ["doc1": ["a": 1, "b": [0]],
  245. "doc2": ["b": [1]],
  246. "doc3": ["a": 3, "b": [2, 7]],
  247. "doc4": ["a": 1, "b": [3, 7]],
  248. "doc5": ["a": 1],
  249. "doc6": ["a": 2]]
  250. )
  251. let filter1 = Filter.orFilter(
  252. [Filter.whereField("a", in: [2, 3]),
  253. Filter.whereField("b", arrayContainsAny: [3])]
  254. )
  255. checkOnlineAndOfflineQuery(collRef.whereFilter(filter1),
  256. matchesResult: ["doc3", "doc4", "doc6"])
  257. let filter2 = Filter.andFilter(
  258. [Filter.whereField("a", in: [2, 3]),
  259. Filter.whereField("b", arrayContains: 7)]
  260. )
  261. checkOnlineAndOfflineQuery(collRef.whereFilter(filter2),
  262. matchesResult: ["doc3"])
  263. let filter3 = Filter.orFilter(
  264. [Filter.whereField("a", in: [2, 3]),
  265. Filter.andFilter(
  266. [Filter.whereField("b", arrayContains: 3),
  267. Filter.whereField("a", isEqualTo: 1)]
  268. )]
  269. )
  270. checkOnlineAndOfflineQuery(collRef.whereFilter(filter3),
  271. matchesResult: ["doc3", "doc4", "doc6"])
  272. let filter4 = Filter.andFilter(
  273. [Filter.whereField("a", in: [2, 3]),
  274. Filter.orFilter(
  275. [Filter.whereField("b", arrayContains: 7),
  276. Filter.whereField("a", isEqualTo: 1)]
  277. )]
  278. )
  279. checkOnlineAndOfflineQuery(collRef.whereFilter(filter4),
  280. matchesResult: ["doc3"])
  281. }
  282. func testOrderByEquality() throws {
  283. // TODO(orquery): Enable this test against production when possible.
  284. try XCTSkipIf(!FSTIntegrationTestCase.isRunningAgainstEmulator(),
  285. "Skip this test if running against production because order-by-equality is not supported yet.")
  286. let collRef = collectionRef(
  287. withDocuments: ["doc1": ["a": 1, "b": [0]],
  288. "doc2": ["b": [1]],
  289. "doc3": ["a": 3, "b": [2, 7], "c": 10],
  290. "doc4": ["a": 1, "b": [3, 7]],
  291. "doc5": ["a": 1],
  292. "doc6": ["a": 2, "c": 20]]
  293. )
  294. checkOnlineAndOfflineQuery(collRef.whereFilter(Filter.whereField("a", isEqualTo: 1)),
  295. matchesResult: ["doc1", "doc4", "doc5"])
  296. checkOnlineAndOfflineQuery(
  297. collRef.whereFilter(Filter.whereField("a", in: [2, 3])).order(by: "a"),
  298. matchesResult: ["doc6", "doc3"]
  299. )
  300. }
  301. }