|
18 | 18 | import { expect } from 'chai'; |
19 | 19 |
|
20 | 20 | import { |
| 21 | + deleteAllPersistentCacheIndexes, |
21 | 22 | disablePersistentCacheIndexAutoCreation, |
22 | 23 | doc, |
23 | 24 | enablePersistentCacheIndexAutoCreation, |
@@ -149,4 +150,66 @@ apiDescribe('PersistentCacheIndexManager', persistence => { |
149 | 150 | }); |
150 | 151 | }); |
151 | 152 | }); |
| 153 | + |
| 154 | + describe('delete all persistent cache indexes', () => { |
| 155 | + it('deleteAllPersistentCacheIndexes() on new instance should succeed', () => |
| 156 | + withTestDb(persistence, async db => { |
| 157 | + const indexManager = getPersistentCacheIndexManager(db)!; |
| 158 | + deleteAllPersistentCacheIndexes(indexManager); |
| 159 | + })); |
| 160 | + |
| 161 | + it('deleteAllPersistentCacheIndexes() should be successful when auto-indexing is enabled', () => |
| 162 | + withTestDb(persistence, async db => { |
| 163 | + const indexManager = getPersistentCacheIndexManager(db)!; |
| 164 | + enablePersistentCacheIndexAutoCreation(indexManager); |
| 165 | + deleteAllPersistentCacheIndexes(indexManager); |
| 166 | + })); |
| 167 | + |
| 168 | + it('deleteAllPersistentCacheIndexes() should be successful when auto-indexing is disabled', () => |
| 169 | + withTestDb(persistence, async db => { |
| 170 | + const indexManager = getPersistentCacheIndexManager(db)!; |
| 171 | + enablePersistentCacheIndexAutoCreation(indexManager); |
| 172 | + disablePersistentCacheIndexAutoCreation(indexManager); |
| 173 | + deleteAllPersistentCacheIndexes(indexManager); |
| 174 | + })); |
| 175 | + |
| 176 | + it('deleteAllPersistentCacheIndexes() after terminate() should throw', () => |
| 177 | + withTestDb(persistence, async db => { |
| 178 | + const indexManager = getPersistentCacheIndexManager(db)!; |
| 179 | + terminate(db).catch(e => expect.fail(`terminate() failed: ${e}`)); |
| 180 | + expect(() => deleteAllPersistentCacheIndexes(indexManager)).to.throw( |
| 181 | + 'The client has already been terminated.' |
| 182 | + ); |
| 183 | + })); |
| 184 | + |
| 185 | + it('query returns correct results when auto-created index has been deleted', () => { |
| 186 | + const testDocs = partitionedTestDocs({ |
| 187 | + matching: { documentData: { match: true }, documentCount: 1 }, |
| 188 | + nonmatching: { documentData: { match: false }, documentCount: 100 } |
| 189 | + }); |
| 190 | + return withTestCollection(persistence, testDocs, async (coll, db) => { |
| 191 | + const indexManager = getPersistentCacheIndexManager(db)!; |
| 192 | + enablePersistentCacheIndexAutoCreation(indexManager); |
| 193 | + |
| 194 | + // Populate the local cache with the entire collection's contents. |
| 195 | + await getDocs(coll); |
| 196 | + |
| 197 | + // Run a query that matches only one of the documents in the collection; |
| 198 | + // this should cause an index to be auto-created. |
| 199 | + const query_ = query(coll, where('match', '==', true)); |
| 200 | + const snapshot1 = await getDocsFromCache(query_); |
| 201 | + expect(snapshot1.size).to.equal(1); |
| 202 | + |
| 203 | + // Delete the index |
| 204 | + deleteAllPersistentCacheIndexes(indexManager); |
| 205 | + |
| 206 | + // Run the query that matches only one of the documents again, which |
| 207 | + // should _still_ return the one and only document that matches. Since |
| 208 | + // the public API surface does not reveal whether an index was used, |
| 209 | + // there isn't anything else that can be verified. |
| 210 | + const snapshot2 = await getDocsFromCache(query_); |
| 211 | + expect(snapshot2.size).to.equal(1); |
| 212 | + }); |
| 213 | + }); |
| 214 | + }); |
152 | 215 | }); |
0 commit comments