diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2020-02-22 12:07:09 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-22 12:07:09 -0500 |
commit | 0e31139734efd51799efcb6357c074059c78858d (patch) | |
tree | 87604c886aa8bfcdd1787809d6a20e3f2057597f /test/dictionary-validate.js | |
parent | 174d9e7429a0f7a45538e56a594ce627239d80c3 (diff) | |
parent | 1e573f36c416b3955b2e679c8f3ce7ffa91bf504 (diff) |
Merge pull request #371 from toasted-nutbread/database-tests
Database tests
Diffstat (limited to 'test/dictionary-validate.js')
-rw-r--r-- | test/dictionary-validate.js | 65 |
1 files changed, 35 insertions, 30 deletions
diff --git a/test/dictionary-validate.js b/test/dictionary-validate.js index 971c4971..25a5de88 100644 --- a/test/dictionary-validate.js +++ b/test/dictionary-validate.js @@ -1,13 +1,9 @@ const fs = require('fs'); const path = require('path'); +const yomichanTest = require('./yomichan-test'); -process.noDeprecation = true; // Suppress a warning about JSZip -const JSZip = require(path.join(__dirname, '../ext/mixed/lib/jszip.min.js')); -process.noDeprecation = false; - -const jsonSchemaFileName = path.join(__dirname, '../ext/bg/js/json-schema.js'); -const jsonSchemaFileSource = fs.readFileSync(jsonSchemaFileName, {encoding: 'utf8'}); -const JsonSchema = Function(`'use strict';${jsonSchemaFileSource};return JsonSchema;`)(); +const JSZip = yomichanTest.JSZip; +const {JsonSchema} = yomichanTest.requireScript('ext/bg/js/json-schema.js', ['JsonSchema']); function readSchema(relativeFileName) { @@ -20,7 +16,7 @@ function readSchema(relativeFileName) { async function validateDictionaryBanks(zip, fileNameFormat, schema) { let index = 1; while (true) { - const fileName = fileNameFormat.replace(/%s/, index); + const fileName = fileNameFormat.replace(/\?/, index); const file = zip.files[fileName]; if (!file) { break; } @@ -32,11 +28,8 @@ async function validateDictionaryBanks(zip, fileNameFormat, schema) { } } -async function validateDictionary(fileName, schemas) { - const source = fs.readFileSync(fileName); - const zip = await JSZip.loadAsync(source); - - const indexFile = zip.files['index.json']; +async function validateDictionary(archive, schemas) { + const indexFile = archive.files['index.json']; if (!indexFile) { throw new Error('No dictionary index found in archive'); } @@ -46,11 +39,24 @@ async function validateDictionary(fileName, schemas) { JsonSchema.validate(index, schemas.index); - await validateDictionaryBanks(zip, 'term_bank_%s.json', version === 1 ? schemas.termBankV1 : schemas.termBankV3); - await validateDictionaryBanks(zip, 'term_meta_bank_%s.json', schemas.termMetaBankV3); - await validateDictionaryBanks(zip, 'kanji_bank_%s.json', version === 1 ? schemas.kanjiBankV1 : schemas.kanjiBankV3); - await validateDictionaryBanks(zip, 'kanji_meta_bank_%s.json', schemas.kanjiMetaBankV3); - await validateDictionaryBanks(zip, 'tag_bank_%s.json', schemas.tagBankV3); + await validateDictionaryBanks(archive, 'term_bank_?.json', version === 1 ? schemas.termBankV1 : schemas.termBankV3); + await validateDictionaryBanks(archive, 'term_meta_bank_?.json', schemas.termMetaBankV3); + await validateDictionaryBanks(archive, 'kanji_bank_?.json', version === 1 ? schemas.kanjiBankV1 : schemas.kanjiBankV3); + await validateDictionaryBanks(archive, 'kanji_meta_bank_?.json', schemas.kanjiMetaBankV3); + await validateDictionaryBanks(archive, 'tag_bank_?.json', schemas.tagBankV3); +} + +function getSchemas() { + return { + index: readSchema('../ext/bg/data/dictionary-index-schema.json'), + kanjiBankV1: readSchema('../ext/bg/data/dictionary-kanji-bank-v1-schema.json'), + kanjiBankV3: readSchema('../ext/bg/data/dictionary-kanji-bank-v3-schema.json'), + kanjiMetaBankV3: readSchema('../ext/bg/data/dictionary-kanji-meta-bank-v3-schema.json'), + tagBankV3: readSchema('../ext/bg/data/dictionary-tag-bank-v3-schema.json'), + termBankV1: readSchema('../ext/bg/data/dictionary-term-bank-v1-schema.json'), + termBankV3: readSchema('../ext/bg/data/dictionary-term-bank-v3-schema.json'), + termMetaBankV3: readSchema('../ext/bg/data/dictionary-term-meta-bank-v3-schema.json') + }; } @@ -64,21 +70,14 @@ async function main() { return; } - const schemas = { - index: readSchema('../ext/bg/data/dictionary-index-schema.json'), - kanjiBankV1: readSchema('../ext/bg/data/dictionary-kanji-bank-v1-schema.json'), - kanjiBankV3: readSchema('../ext/bg/data/dictionary-kanji-bank-v3-schema.json'), - kanjiMetaBankV3: readSchema('../ext/bg/data/dictionary-kanji-meta-bank-v3-schema.json'), - tagBankV3: readSchema('../ext/bg/data/dictionary-tag-bank-v3-schema.json'), - termBankV1: readSchema('../ext/bg/data/dictionary-term-bank-v1-schema.json'), - termBankV3: readSchema('../ext/bg/data/dictionary-term-bank-v3-schema.json'), - termMetaBankV3: readSchema('../ext/bg/data/dictionary-term-meta-bank-v3-schema.json') - }; + const schemas = getSchemas(); for (const dictionaryFileName of dictionaryFileNames) { try { console.log(`Validating ${dictionaryFileName}...`); - await validateDictionary(dictionaryFileName, schemas); + const source = fs.readFileSync(dictionaryFileName); + const archive = await JSZip.loadAsync(source); + await validateDictionary(archive, schemas); console.log('No issues found'); } catch (e) { console.warn(e); @@ -87,4 +86,10 @@ async function main() { } -main(); +if (require.main === module) { main(); } + + +module.exports = { + getSchemas, + validateDictionary +}; |