IndexingTests.swift 4.1 KB

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