From 1e573f36c416b3955b2e679c8f3ce7ffa91bf504 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Wed, 19 Feb 2020 20:00:44 -0500 Subject: [PATCH] Test some errors --- test/test-database.js | 57 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/test/test-database.js b/test/test-database.js index cefa297c..fcf519d7 100644 --- a/test/test-database.js +++ b/test/test-database.js @@ -39,6 +39,18 @@ function countKanjiWithCharacter(kanji, character) { } +async function clearDatabase() { + const indexedDB = global.indexedDB; + for (const {name} of await indexedDB.databases()) { + await new Promise((resolve, reject) => { + const request = indexedDB.deleteDatabase(name); + request.onerror = (e) => reject(e); + request.onsuccess = () => resolve(); + }); + } +} + + async function testDatabase1() { // Load dictionary data const testDictionary = yomichanTest.createTestDictionaryArchive(); @@ -718,8 +730,53 @@ async function testFindTagForTitle1(database, title) { } +async function testDatabase2() { + // Load dictionary data + const testDictionary = yomichanTest.createTestDictionaryArchive(); + const testDictionarySource = await testDictionary.generateAsync({type: 'string'}); + const testDictionaryIndex = JSON.parse(await testDictionary.files['index.json'].async('string')); + + const title = testDictionaryIndex.title; + const titles = [title]; + + // Setup database + const database = new Database(); + + // Error: not prepared + await assert.rejects(async () => await database.purge()); + await assert.rejects(async () => await database.deleteDictionary(title, () => {}, {})); + await assert.rejects(async () => await database.findTermsBulk(['?'], titles, null)); + await assert.rejects(async () => await database.findTermsExactBulk(['?'], ['?'], titles)); + await assert.rejects(async () => await database.findTermsBySequenceBulk([1], title)); + await assert.rejects(async () => await database.findTermMetaBulk(['?'], titles)); + await assert.rejects(async () => await database.findTermMetaBulk(['?'], titles)); + await assert.rejects(async () => await database.findKanjiBulk(['?'], titles)); + await assert.rejects(async () => await database.findKanjiMetaBulk(['?'], titles)); + await assert.rejects(async () => await database.findTagForTitle('tag', title)); + await assert.rejects(async () => await database.getDictionaryInfo()); + await assert.rejects(async () => await database.getDictionaryCounts(titles, true)); + await assert.rejects(async () => await database.importDictionary(testDictionarySource, () => {}, {})); + + await database.prepare(); + + // Error: already prepared + await assert.rejects(async () => await database.prepare()); + + await database.importDictionary(testDictionarySource, () => {}, {}); + + // Error: dictionary already imported + await assert.rejects(async () => await database.importDictionary(testDictionarySource, () => {}, {})); + + await database.close(); +} + + async function main() { await testDatabase1(); + await clearDatabase(); + + await testDatabase2(); + await clearDatabase(); }