IndexingTests.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 IndexingTests: FSTIntegrationTestCase {
  20. func testAutoIndexCreationSetSuccessfully() throws {
  21. // Use persistent disk cache (explict)
  22. let settings = db.settings
  23. settings.cacheSettings = PersistentCacheSettings()
  24. db.settings = settings
  25. let coll = collectionRef()
  26. let testDocs = [
  27. "a": ["match": true],
  28. "b": ["match": false],
  29. "c": ["match": false],
  30. ]
  31. writeAllDocuments(testDocs, toCollection: coll)
  32. coll.whereField("match", isEqualTo: true)
  33. .getDocuments(source: .cache) { querySnapshot, err in
  34. XCTAssertNil(err)
  35. XCTAssertEqual(querySnapshot!.count, 1)
  36. }
  37. let enableIndexAutoCreation = {
  38. try FSTExceptionCatcher.catchException {
  39. self.db.persistentCacheIndexManager!.enableIndexAutoCreation()
  40. }
  41. }
  42. XCTAssertNoThrow(try enableIndexAutoCreation())
  43. coll.whereField("match", isEqualTo: true)
  44. .getDocuments(source: .cache) { querySnapshot, err in
  45. XCTAssertNil(err)
  46. XCTAssertEqual(querySnapshot!.count, 1)
  47. }
  48. let disableIndexAutoCreation = {
  49. try FSTExceptionCatcher.catchException {
  50. self.db.persistentCacheIndexManager!.disableIndexAutoCreation()
  51. }
  52. }
  53. XCTAssertNoThrow(try disableIndexAutoCreation())
  54. coll.whereField("match", isEqualTo: true)
  55. .getDocuments(source: .cache) { querySnapshot, err in
  56. XCTAssertNil(err)
  57. XCTAssertEqual(querySnapshot!.count, 1)
  58. }
  59. let deleteAllIndexes = {
  60. try FSTExceptionCatcher.catchException {
  61. self.db.persistentCacheIndexManager!.deleteAllIndexes()
  62. }
  63. }
  64. XCTAssertNoThrow(try deleteAllIndexes())
  65. coll.whereField("match", isEqualTo: true)
  66. .getDocuments(source: .cache) { querySnapshot, err in
  67. XCTAssertNil(err)
  68. XCTAssertEqual(querySnapshot!.count, 1)
  69. }
  70. }
  71. func testAutoIndexCreationSetSuccessfullyUsingDefault() throws {
  72. // Use persistent disk cache (default)
  73. let coll = collectionRef()
  74. let testDocs = [
  75. "a": ["match": true],
  76. "b": ["match": false],
  77. "c": ["match": false],
  78. ]
  79. writeAllDocuments(testDocs, toCollection: coll)
  80. coll.whereField("match", isEqualTo: true)
  81. .getDocuments(source: .cache) { querySnapshot, err in
  82. XCTAssertNil(err)
  83. XCTAssertEqual(querySnapshot!.count, 1)
  84. }
  85. let enableIndexAutoCreation = {
  86. try FSTExceptionCatcher.catchException {
  87. self.db.persistentCacheIndexManager!.enableIndexAutoCreation()
  88. }
  89. }
  90. XCTAssertNoThrow(try enableIndexAutoCreation())
  91. coll.whereField("match", isEqualTo: true)
  92. .getDocuments(source: .cache) { querySnapshot, err in
  93. XCTAssertNil(err)
  94. XCTAssertEqual(querySnapshot!.count, 1)
  95. }
  96. let disableIndexAutoCreation = {
  97. try FSTExceptionCatcher.catchException {
  98. self.db.persistentCacheIndexManager!.disableIndexAutoCreation()
  99. }
  100. }
  101. XCTAssertNoThrow(try disableIndexAutoCreation())
  102. coll.whereField("match", isEqualTo: true)
  103. .getDocuments(source: .cache) { querySnapshot, err in
  104. XCTAssertNil(err)
  105. XCTAssertEqual(querySnapshot!.count, 1)
  106. }
  107. let deleteAllIndexes = {
  108. try FSTExceptionCatcher.catchException {
  109. self.db.persistentCacheIndexManager!.deleteAllIndexes()
  110. }
  111. }
  112. XCTAssertNoThrow(try deleteAllIndexes())
  113. coll.whereField("match", isEqualTo: true)
  114. .getDocuments(source: .cache) { querySnapshot, err in
  115. XCTAssertNil(err)
  116. XCTAssertEqual(querySnapshot!.count, 1)
  117. }
  118. }
  119. }