QueryIntegrationTests.swift 12 KB

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