From 8733e324ecbe10bcb4bc9f1a0b9568c7f32429d3 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 1 Feb 2020 22:41:02 -0500 Subject: Create script to validate dictionary files --- test/dictionary-validate.js | 90 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 test/dictionary-validate.js (limited to 'test') diff --git a/test/dictionary-validate.js b/test/dictionary-validate.js new file mode 100644 index 00000000..971c4971 --- /dev/null +++ b/test/dictionary-validate.js @@ -0,0 +1,90 @@ +const fs = require('fs'); +const path = require('path'); + +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;`)(); + + +function readSchema(relativeFileName) { + const fileName = path.join(__dirname, relativeFileName); + const source = fs.readFileSync(fileName, {encoding: 'utf8'}); + return JSON.parse(source); +} + + +async function validateDictionaryBanks(zip, fileNameFormat, schema) { + let index = 1; + while (true) { + const fileName = fileNameFormat.replace(/%s/, index); + + const file = zip.files[fileName]; + if (!file) { break; } + + const data = JSON.parse(await file.async('string')); + JsonSchema.validate(data, schema); + + ++index; + } +} + +async function validateDictionary(fileName, schemas) { + const source = fs.readFileSync(fileName); + const zip = await JSZip.loadAsync(source); + + const indexFile = zip.files['index.json']; + if (!indexFile) { + throw new Error('No dictionary index found in archive'); + } + + const index = JSON.parse(await indexFile.async('string')); + const version = index.format || index.version; + + 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); +} + + +async function main() { + const dictionaryFileNames = process.argv.slice(2); + if (dictionaryFileNames.length === 0) { + console.log([ + 'Usage:', + ' node dictionary-validate ...' + ].join('\n')); + 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') + }; + + for (const dictionaryFileName of dictionaryFileNames) { + try { + console.log(`Validating ${dictionaryFileName}...`); + await validateDictionary(dictionaryFileName, schemas); + console.log('No issues found'); + } catch (e) { + console.warn(e); + } + } +} + + +main(); -- cgit v1.2.3 From 23ddf84c6dd38479e3bedd8c9fac3fbc18885722 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 8 Feb 2020 13:14:30 -0500 Subject: Add script for testing generic schemas --- test/schema-validate.js | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 test/schema-validate.js (limited to 'test') diff --git a/test/schema-validate.js b/test/schema-validate.js new file mode 100644 index 00000000..ac5a8a85 --- /dev/null +++ b/test/schema-validate.js @@ -0,0 +1,36 @@ +const fs = require('fs'); +const path = require('path'); + +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;`)(); + + +function main() { + const args = process.argv.slice(2); + if (args.length < 2) { + console.log([ + 'Usage:', + ' node schema-validate ...' + ].join('\n')); + return; + } + + const schemaSource = fs.readFileSync(args[0], {encoding: 'utf8'}); + const schema = JSON.parse(schemaSource); + + for (const dataFileName of args.slice(1)) { + try { + console.log(`Validating ${dataFileName}...`); + const dataSource = fs.readFileSync(dataFileName, {encoding: 'utf8'}); + const data = JSON.parse(dataSource); + JsonSchema.validate(data, schema); + console.log('No issues found'); + } catch (e) { + console.warn(e); + } + } +} + + +main(); -- cgit v1.2.3 From 174d9e7429a0f7a45538e56a594ce627239d80c3 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Mon, 17 Feb 2020 14:20:22 -0500 Subject: Add some basic unit tests for JSON schemas --- .github/workflows/ci.yml | 8 +- package.json | 5 +- test/test-schema.js | 236 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 245 insertions(+), 4 deletions(-) create mode 100644 test/test-schema.js (limited to 'test') diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 40bc005d..c65d254b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -17,7 +17,11 @@ jobs: run: npm ci - name: Build run: npm run build --if-present - - name: Run tests - run: npm test + - name: Lint + run: npm run test-lint + env: + CI: true + - name: Tests + run: npm run test-code env: CI: true diff --git a/package.json b/package.json index 8e71de59..b486c25d 100644 --- a/package.json +++ b/package.json @@ -6,8 +6,9 @@ "test": "test" }, "scripts": { - "test": "npm run test-lint", - "test-lint": "eslint ." + "test": "npm run test-lint && npm run test-code", + "test-lint": "eslint .", + "test-code": "node ./test/test-schema.js" }, "repository": { "type": "git", diff --git a/test/test-schema.js b/test/test-schema.js new file mode 100644 index 00000000..2f294e43 --- /dev/null +++ b/test/test-schema.js @@ -0,0 +1,236 @@ +const fs = require('fs'); +const path = require('path'); +const assert = require('assert'); + +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;`)(); + + +function testValidate1() { + const schema = { + allOf: [ + { + type: 'number' + }, + { + anyOf: [ + {minimum: 10, maximum: 100}, + {minimum: -100, maximum: -10} + ] + }, + { + oneOf: [ + {multipleOf: 3}, + {multipleOf: 5} + ] + }, + { + not: [ + {multipleOf: 20} + ] + } + ] + }; + + const schemaValidate = (value, schema) => { + try { + JsonSchema.validate(value, schema); + return true; + } catch (e) { + return false; + } + }; + + const jsValidate = (value) => { + return ( + typeof value === 'number' && + ( + (value >= 10 && value <= 100) || + (value >= -100 && value <= -10) + ) && + ( + ( + (value % 3 )=== 0 || + (value % 5) === 0 + ) && + (value % 15) !== 0 + ) && + (value % 20) !== 0 + ); + }; + + for (let i = -111; i <= 111; i++) { + const actual = schemaValidate(i, schema); + const expected = jsValidate(i); + assert.strictEqual(actual, expected); + } +} + + +function testGetValidValueOrDefault1() { + // Test value defaulting on objects with additionalProperties=false + const schema = { + type: 'object', + required: ['test'], + properties: { + test: { + type: 'string', + default: 'default' + } + }, + additionalProperties: false + }; + + const testData = [ + [ + void(0), + {test: 'default'} + ], + [ + null, + {test: 'default'} + ], + [ + 0, + {test: 'default'} + ], + [ + '', + {test: 'default'} + ], + [ + [], + {test: 'default'} + ], + [ + {}, + {test: 'default'} + ], + [ + {test: 'value'}, + {test: 'value'} + ], + [ + {test2: 'value2'}, + {test: 'default'} + ], + [ + {test: 'value', test2: 'value2'}, + {test: 'value'} + ] + ]; + + for (const [value, expected] of testData) { + const actual = JsonSchema.getValidValueOrDefault(schema, value); + assert.deepStrictEqual(actual, expected); + } +} + +function testGetValidValueOrDefault2() { + // Test value defaulting on objects with additionalProperties=true + const schema = { + type: 'object', + required: ['test'], + properties: { + test: { + type: 'string', + default: 'default' + } + }, + additionalProperties: true + }; + + const testData = [ + [ + {}, + {test: 'default'} + ], + [ + {test: 'value'}, + {test: 'value'} + ], + [ + {test2: 'value2'}, + {test: 'default', test2: 'value2'} + ], + [ + {test: 'value', test2: 'value2'}, + {test: 'value', test2: 'value2'} + ] + ]; + + for (const [value, expected] of testData) { + const actual = JsonSchema.getValidValueOrDefault(schema, value); + assert.deepStrictEqual(actual, expected); + } +} + +function testGetValidValueOrDefault3() { + // Test value defaulting on objects with additionalProperties={schema} + const schema = { + type: 'object', + required: ['test'], + properties: { + test: { + type: 'string', + default: 'default' + } + }, + additionalProperties: { + type: 'number', + default: 10 + } + }; + + const testData = [ + [ + {}, + {test: 'default'} + ], + [ + {test: 'value'}, + {test: 'value'} + ], + [ + {test2: 'value2'}, + {test: 'default', test2: 10} + ], + [ + {test: 'value', test2: 'value2'}, + {test: 'value', test2: 10} + ], + [ + {test2: 2}, + {test: 'default', test2: 2} + ], + [ + {test: 'value', test2: 2}, + {test: 'value', test2: 2} + ], + [ + {test: 'value', test2: 2, test3: null}, + {test: 'value', test2: 2, test3: 10} + ], + [ + {test: 'value', test2: 2, test3: void(0)}, + {test: 'value', test2: 2, test3: 10} + ] + ]; + + for (const [value, expected] of testData) { + const actual = JsonSchema.getValidValueOrDefault(schema, value); + assert.deepStrictEqual(actual, expected); + } +} + + +function main() { + testValidate1(); + testGetValidValueOrDefault1(); + testGetValidValueOrDefault2(); + testGetValidValueOrDefault3(); +} + + +main(); -- cgit v1.2.3 From 7cbebf6897efb32c80c5e258f594d18861974bad Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Mon, 17 Feb 2020 22:30:16 -0500 Subject: Update how main is invoked --- test/dictionary-validate.js | 2 +- test/schema-validate.js | 2 +- test/test-schema.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) (limited to 'test') diff --git a/test/dictionary-validate.js b/test/dictionary-validate.js index 971c4971..dca3bcf5 100644 --- a/test/dictionary-validate.js +++ b/test/dictionary-validate.js @@ -87,4 +87,4 @@ async function main() { } -main(); +if (require.main === module) { main(); } diff --git a/test/schema-validate.js b/test/schema-validate.js index ac5a8a85..309adf83 100644 --- a/test/schema-validate.js +++ b/test/schema-validate.js @@ -33,4 +33,4 @@ function main() { } -main(); +if (require.main === module) { main(); } diff --git a/test/test-schema.js b/test/test-schema.js index 2f294e43..762b8509 100644 --- a/test/test-schema.js +++ b/test/test-schema.js @@ -233,4 +233,4 @@ function main() { } -main(); +if (require.main === module) { main(); } -- cgit v1.2.3 From 06480751a9d9ff1471455f31e3efc99d552a4975 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Mon, 17 Feb 2020 22:31:28 -0500 Subject: Create yomichan-test script to reduce repeated code --- test/dictionary-validate.js | 10 +++------- test/schema-validate.js | 6 ++---- test/test-schema.js | 7 ++----- test/yomichan-test.js | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 40 insertions(+), 16 deletions(-) create mode 100644 test/yomichan-test.js (limited to 'test') diff --git a/test/dictionary-validate.js b/test/dictionary-validate.js index dca3bcf5..082f0aea 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) { diff --git a/test/schema-validate.js b/test/schema-validate.js index 309adf83..1271a611 100644 --- a/test/schema-validate.js +++ b/test/schema-validate.js @@ -1,9 +1,7 @@ const fs = require('fs'); -const path = require('path'); +const yomichanTest = require('./yomichan-test'); -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 {JsonSchema} = yomichanTest.requireScript('ext/bg/js/json-schema.js', ['JsonSchema']); function main() { diff --git a/test/test-schema.js b/test/test-schema.js index 762b8509..ca4f56dd 100644 --- a/test/test-schema.js +++ b/test/test-schema.js @@ -1,10 +1,7 @@ -const fs = require('fs'); -const path = require('path'); const assert = require('assert'); +const yomichanTest = require('./yomichan-test'); -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 {JsonSchema} = yomichanTest.requireScript('ext/bg/js/json-schema.js', ['JsonSchema']); function testValidate1() { diff --git a/test/yomichan-test.js b/test/yomichan-test.js new file mode 100644 index 00000000..602b8d78 --- /dev/null +++ b/test/yomichan-test.js @@ -0,0 +1,33 @@ +const fs = require('fs'); +const path = require('path'); + + +let JSZip = null; + +function requireScript(fileName, exportNames, variables) { + const absoluteFileName = path.join(__dirname, '..', fileName); + const source = fs.readFileSync(absoluteFileName, {encoding: 'utf8'}); + const exportNamesString = Array.isArray(exportNames) ? exportNames.join(',') : ''; + const variablesArgumentName = '__variables__'; + let variableString = ''; + if (typeof variables === 'object' && variables !== null) { + variableString = Object.keys(variables).join(','); + variableString = `const {${variableString}} = ${variablesArgumentName};`; + } + return Function(variablesArgumentName, `'use strict';${variableString}${source}\n;return {${exportNamesString}};`)(variables); +} + +function getJSZip() { + if (JSZip === null) { + process.noDeprecation = true; // Suppress a warning about JSZip + JSZip = require(path.join(__dirname, '../ext/mixed/lib/jszip.min.js')); + process.noDeprecation = false; + } + return JSZip; +} + + +module.exports = { + requireScript, + get JSZip() { return getJSZip(); } +}; -- cgit v1.2.3 From dce5f7e1e532240a2ce3ab8cb8a2ee846572c0b9 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Mon, 17 Feb 2020 22:38:48 -0500 Subject: Allow dictionary-validate to be require'd --- test/dictionary-validate.js | 51 ++++++++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 21 deletions(-) (limited to 'test') diff --git a/test/dictionary-validate.js b/test/dictionary-validate.js index 082f0aea..ccbd4ae4 100644 --- a/test/dictionary-validate.js +++ b/test/dictionary-validate.js @@ -28,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'); } @@ -42,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_%s.json', version === 1 ? schemas.termBankV1 : schemas.termBankV3); + await validateDictionaryBanks(archive, 'term_meta_bank_%s.json', schemas.termMetaBankV3); + await validateDictionaryBanks(archive, 'kanji_bank_%s.json', version === 1 ? schemas.kanjiBankV1 : schemas.kanjiBankV3); + await validateDictionaryBanks(archive, 'kanji_meta_bank_%s.json', schemas.kanjiMetaBankV3); + await validateDictionaryBanks(archive, 'tag_bank_%s.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') + }; } @@ -60,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); @@ -84,3 +87,9 @@ async function main() { if (require.main === module) { main(); } + + +module.exports = { + getSchemas, + validateDictionary +}; -- cgit v1.2.3 From f2f4081d4572c48ebbe6685b8f89889823da0a69 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Mon, 17 Feb 2020 22:39:25 -0500 Subject: Use ? instead of %s --- test/dictionary-validate.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'test') diff --git a/test/dictionary-validate.js b/test/dictionary-validate.js index ccbd4ae4..25a5de88 100644 --- a/test/dictionary-validate.js +++ b/test/dictionary-validate.js @@ -16,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; } @@ -39,11 +39,11 @@ async function validateDictionary(archive, schemas) { JsonSchema.validate(index, schemas.index); - await validateDictionaryBanks(archive, 'term_bank_%s.json', version === 1 ? schemas.termBankV1 : schemas.termBankV3); - await validateDictionaryBanks(archive, 'term_meta_bank_%s.json', schemas.termMetaBankV3); - await validateDictionaryBanks(archive, 'kanji_bank_%s.json', version === 1 ? schemas.kanjiBankV1 : schemas.kanjiBankV3); - await validateDictionaryBanks(archive, 'kanji_meta_bank_%s.json', schemas.kanjiMetaBankV3); - await validateDictionaryBanks(archive, '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() { -- cgit v1.2.3 From 09ea007883360589d8b5aeb72ddb75eb126ce595 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Tue, 18 Feb 2020 22:19:08 -0500 Subject: Add test dictionary data --- test/test-dictionary-data/index.json | 6 ++++ test/test-dictionary-data/kanji_bank_1.json | 42 ++++++++++++++++++++++++ test/test-dictionary-data/kanji_meta_bank_1.json | 4 +++ test/test-dictionary-data/tag_bank_1.json | 7 ++++ test/test-dictionary-data/tag_bank_2.json | 9 +++++ test/test-dictionary-data/term_bank_1.json | 34 +++++++++++++++++++ test/test-dictionary-data/term_meta_bank_1.json | 5 +++ test/yomichan-test.js | 26 +++++++++++++++ 8 files changed, 133 insertions(+) create mode 100644 test/test-dictionary-data/index.json create mode 100644 test/test-dictionary-data/kanji_bank_1.json create mode 100644 test/test-dictionary-data/kanji_meta_bank_1.json create mode 100644 test/test-dictionary-data/tag_bank_1.json create mode 100644 test/test-dictionary-data/tag_bank_2.json create mode 100644 test/test-dictionary-data/term_bank_1.json create mode 100644 test/test-dictionary-data/term_meta_bank_1.json (limited to 'test') diff --git a/test/test-dictionary-data/index.json b/test/test-dictionary-data/index.json new file mode 100644 index 00000000..3034bf38 --- /dev/null +++ b/test/test-dictionary-data/index.json @@ -0,0 +1,6 @@ +{ + "title": "Test Dictionary", + "format": 3, + "revision": "test", + "sequenced": true +} \ No newline at end of file diff --git a/test/test-dictionary-data/kanji_bank_1.json b/test/test-dictionary-data/kanji_bank_1.json new file mode 100644 index 00000000..264f94c1 --- /dev/null +++ b/test/test-dictionary-data/kanji_bank_1.json @@ -0,0 +1,42 @@ +[ + [ + "打", + "ダ ダアス", + "う.つ う.ち- ぶ.つ", + "ktag1 ktag2", + [ + "meaning1", + "meaning2", + "meaning3", + "meaning4", + "meaning5" + ], + { + "kstat1": "1", + "kstat2": "2", + "kstat3": "3", + "kstat4": "4", + "kstat5": "5" + } + ], + [ + "込", + "", + "-こ.む こ.む こ.み -こ.み こ.める", + "ktag1 ktag2", + [ + "meaning1", + "meaning2", + "meaning3", + "meaning4", + "meaning5" + ], + { + "kstat1": "1", + "kstat2": "2", + "kstat3": "3", + "kstat4": "4", + "kstat5": "5" + } + ] +] \ No newline at end of file diff --git a/test/test-dictionary-data/kanji_meta_bank_1.json b/test/test-dictionary-data/kanji_meta_bank_1.json new file mode 100644 index 00000000..73e75b8a --- /dev/null +++ b/test/test-dictionary-data/kanji_meta_bank_1.json @@ -0,0 +1,4 @@ +[ + ["打", "freq", 1], + ["込", "freq", 2] +] \ No newline at end of file diff --git a/test/test-dictionary-data/tag_bank_1.json b/test/test-dictionary-data/tag_bank_1.json new file mode 100644 index 00000000..109ad395 --- /dev/null +++ b/test/test-dictionary-data/tag_bank_1.json @@ -0,0 +1,7 @@ +[ + ["tag1", "category1", 0, "tag1 notes", 0], + ["tag2", "category2", 0, "tag2 notes", 0], + ["tag3", "category3", 0, "tag3 notes", 0], + ["tag4", "category4", 0, "tag4 notes", 0], + ["tag5", "category5", 0, "tag5 notes", 0] +] \ No newline at end of file diff --git a/test/test-dictionary-data/tag_bank_2.json b/test/test-dictionary-data/tag_bank_2.json new file mode 100644 index 00000000..5e7936b3 --- /dev/null +++ b/test/test-dictionary-data/tag_bank_2.json @@ -0,0 +1,9 @@ +[ + ["ktag1", "kcategory1", 0, "ktag1 notes", 0], + ["ktag2", "kcategory2", 0, "ktag2 notes", 0], + ["kstat1", "kcategory3", 0, "kstat1 notes", 0], + ["kstat2", "kcategory4", 0, "kstat2 notes", 0], + ["kstat3", "kcategory5", 0, "kstat3 notes", 0], + ["kstat4", "kcategory6", 0, "kstat4 notes", 0], + ["kstat5", "kcategory7", 0, "kstat5 notes", 0] +] \ No newline at end of file diff --git a/test/test-dictionary-data/term_bank_1.json b/test/test-dictionary-data/term_bank_1.json new file mode 100644 index 00000000..755d9f6a --- /dev/null +++ b/test/test-dictionary-data/term_bank_1.json @@ -0,0 +1,34 @@ +[ + ["打", "だ", "tag1 tag2", "", 2, ["definition1a (打, だ)", "definition1b (打, だ)"], 1, "tag3 tag4 tag5"], + ["打", "ダース", "tag1 tag2", "", 1, ["definition1a (打, ダース)", "definition1b (打, ダース)"], 2, "tag3 tag4 tag5"], + ["打つ", "うつ", "tag1 tag2", "v5", 3, ["definition1a (打つ, うつ)", "definition1b (打つ, うつ)"], 3, "tag3 tag4 tag5"], + ["打つ", "うつ", "tag1 tag2", "v5", 4, ["definition2a (打つ, うつ)", "definition2b (打つ, うつ)"], 3, "tag3 tag4 tag5"], + ["打つ", "うつ", "tag1 tag2", "v5", 5, ["definition3a (打つ, うつ)", "definition3b (打つ, うつ)"], 3, "tag3 tag4 tag5"], + ["打つ", "うつ", "tag1 tag2", "v5", 6, ["definition4a (打つ, うつ)", "definition4b (打つ, うつ)"], 3, "tag3 tag4 tag5"], + ["打つ", "うつ", "tag1 tag2", "v5", 7, ["definition5a (打つ, うつ)", "definition5b (打つ, うつ)"], 3, "tag3 tag4 tag5"], + ["打つ", "うつ", "tag1 tag2", "v5", 8, ["definition6a (打つ, うつ)", "definition6b (打つ, うつ)"], 3, "tag3 tag4 tag5"], + ["打つ", "うつ", "tag1 tag2", "v5", 9, ["definition7a (打つ, うつ)", "definition7b (打つ, うつ)"], 3, "tag3 tag4 tag5"], + ["打つ", "うつ", "tag1 tag2", "v5", 10, ["definition8a (打つ, うつ)", "definition8b (打つ, うつ)"], 3, "tag3 tag4 tag5"], + ["打つ", "うつ", "tag1 tag2", "v5", 11, ["definition9a (打つ, うつ)", "definition9b (打つ, うつ)"], 3, "tag3 tag4 tag5"], + ["打つ", "うつ", "tag1 tag2", "v5", 12, ["definition10a (打つ, うつ)", "definition10b (打つ, うつ)"], 3, "tag3 tag4 tag5"], + ["打つ", "うつ", "tag1 tag2", "v5", 13, ["definition11a (打つ, うつ)", "definition11b (打つ, うつ)"], 3, "tag3 tag4 tag5"], + ["打つ", "うつ", "tag1 tag2", "v5", 14, ["definition12a (打つ, うつ)", "definition12b (打つ, うつ)"], 3, "tag3 tag4 tag5"], + ["打つ", "うつ", "tag1 tag2", "v5", 15, ["definition13a (打つ, うつ)", "definition13b (打つ, うつ)"], 3, "tag3 tag4 tag5"], + ["打つ", "うつ", "tag1 tag2", "v5", 16, ["definition14a (打つ, うつ)", "definition14b (打つ, うつ)"], 3, "tag3 tag4 tag5"], + ["打つ", "うつ", "tag1 tag2", "v5", 17, ["definition15a (打つ, うつ)", "definition15b (打つ, うつ)"], 3, "tag3 tag4 tag5"], + ["打つ", "ぶつ", "tag1 tag2", "v5", 18, ["definition1a (打つ, ぶつ)", "definition1b (打つ, ぶつ)"], 4, "tag3 tag4 tag5"], + ["打つ", "ぶつ", "tag1 tag2", "v5", 19, ["definition2a (打つ, ぶつ)", "definition2b (打つ, ぶつ)"], 4, "tag3 tag4 tag5"], + ["打ち込む", "うちこむ", "tag1 tag2", "v5", 20, ["definition1a (打ち込む, うちこむ)", "definition1b (打ち込む, うちこむ)"], 5, "tag3 tag4 tag5"], + ["打ち込む", "うちこむ", "tag1 tag2", "v5", 21, ["definition2a (打ち込む, うちこむ)", "definition2b (打ち込む, うちこむ)"], 5, "tag5 tag6 tag7"], + ["打ち込む", "うちこむ", "tag1 tag2", "v5", 22, ["definition3a (打ち込む, うちこむ)", "definition3b (打ち込む, うちこむ)"], 5, "tag5 tag6 tag7"], + ["打ち込む", "うちこむ", "tag1 tag2", "v5", 23, ["definition4a (打ち込む, うちこむ)", "definition4b (打ち込む, うちこむ)"], 5, "tag5 tag6 tag7"], + ["打ち込む", "うちこむ", "tag1 tag2", "v5", 24, ["definition5a (打ち込む, うちこむ)", "definition5b (打ち込む, うちこむ)"], 5, "tag5 tag6 tag7"], + ["打ち込む", "うちこむ", "tag1 tag2", "v5", 25, ["definition6a (打ち込む, うちこむ)", "definition6b (打ち込む, うちこむ)"], 5, "tag5 tag6 tag7"], + ["打ち込む", "うちこむ", "tag1 tag2", "v5", 26, ["definition7a (打ち込む, うちこむ)", "definition7b (打ち込む, うちこむ)"], 5, "tag5 tag6 tag7"], + ["打ち込む", "うちこむ", "tag1 tag2", "v5", 27, ["definition8a (打ち込む, うちこむ)", "definition8b (打ち込む, うちこむ)"], 5, "tag5 tag6 tag7"], + ["打ち込む", "うちこむ", "tag1 tag2", "v5", 28, ["definition9a (打ち込む, うちこむ)", "definition9b (打ち込む, うちこむ)"], 5, "tag5 tag6 tag7"], + ["打ち込む", "ぶちこむ", "tag1 tag2", "v5", 29, ["definition1a (打ち込む, ぶちこむ)", "definition1b (打ち込む, ぶちこむ)"], 6, "tag3 tag4 tag5"], + ["打ち込む", "ぶちこむ", "tag1 tag2", "v5", 30, ["definition2a (打ち込む, ぶちこむ)", "definition2b (打ち込む, ぶちこむ)"], 6, "tag3 tag4 tag5"], + ["打ち込む", "ぶちこむ", "tag1 tag2", "v5", 31, ["definition3a (打ち込む, ぶちこむ)", "definition3b (打ち込む, ぶちこむ)"], 6, "tag3 tag4 tag5"], + ["打ち込む", "ぶちこむ", "tag1 tag2", "v5", 32, ["definition4a (打ち込む, ぶちこむ)", "definition4b (打ち込む, ぶちこむ)"], 6, "tag3 tag4 tag5"] +] \ No newline at end of file diff --git a/test/test-dictionary-data/term_meta_bank_1.json b/test/test-dictionary-data/term_meta_bank_1.json new file mode 100644 index 00000000..78096502 --- /dev/null +++ b/test/test-dictionary-data/term_meta_bank_1.json @@ -0,0 +1,5 @@ +[ + ["打", "freq", 1], + ["打つ", "freq", 2], + ["打ち込む", "freq", 3] +] \ No newline at end of file diff --git a/test/yomichan-test.js b/test/yomichan-test.js index 602b8d78..dd4da919 100644 --- a/test/yomichan-test.js +++ b/test/yomichan-test.js @@ -26,8 +26,34 @@ function getJSZip() { return JSZip; } +function createTestDictionaryArchive(dictionaryName) { + const fileNames = [ + 'index.json', + 'tag_bank_1.json', + 'tag_bank_2.json', + 'term_bank_1.json', + 'kanji_bank_1.json', + 'term_meta_bank_1.json', + 'kanji_meta_bank_1.json' + ]; + + const archive = new (getJSZip())(); + + for (const fileName of fileNames) { + const source = fs.readFileSync(path.join(__dirname, 'test-dictionary-data', fileName), {encoding: 'utf8'}); + const json = JSON.parse(source); + if (fileName === 'index.json' && typeof dictionaryName === 'string') { + json.title = dictionaryName; + } + archive.file(fileName, JSON.stringify(json, null, 0)); + } + + return archive; +} + module.exports = { requireScript, + createTestDictionaryArchive, get JSZip() { return getJSZip(); } }; -- cgit v1.2.3 From da079820e63f05b54ac867022f11bcbdcd6acc20 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Tue, 18 Feb 2020 22:19:33 -0500 Subject: Add script to validate the test dictionary data against JSON schemas --- test/test-dictionary.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 test/test-dictionary.js (limited to 'test') diff --git a/test/test-dictionary.js b/test/test-dictionary.js new file mode 100644 index 00000000..84014540 --- /dev/null +++ b/test/test-dictionary.js @@ -0,0 +1,12 @@ +const yomichanTest = require('./yomichan-test'); +const dictionaryValidate = require('./dictionary-validate'); + + +async function main() { + const archive = yomichanTest.createTestDictionaryArchive(); + const schemas = dictionaryValidate.getSchemas(); + await dictionaryValidate.validateDictionary(archive, schemas); +} + + +if (require.main === module) { main(); } -- cgit v1.2.3 From 2829c88e7de0e966587934c49dd1ff1583445792 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Tue, 18 Feb 2020 22:19:55 -0500 Subject: Add script to test database.js --- test/test-database.js | 720 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 720 insertions(+) create mode 100644 test/test-database.js (limited to 'test') diff --git a/test/test-database.js b/test/test-database.js new file mode 100644 index 00000000..b9ac8a46 --- /dev/null +++ b/test/test-database.js @@ -0,0 +1,720 @@ +const assert = require('assert'); +const yomichanTest = require('./yomichan-test'); +require('fake-indexeddb/auto'); + +const chrome = { + runtime: { + onMessage: { + addListener: () => { /* NOP */ } + } + } +}; + +const {Database} = yomichanTest.requireScript('ext/bg/js/database.js', ['Database']); +const {dictFieldSplit, dictTagSanitize} = yomichanTest.requireScript('ext/bg/js/dictionary.js', ['dictFieldSplit', 'dictTagSanitize']); +const {stringReverse, hasOwn} = yomichanTest.requireScript('ext/mixed/js/core.js', ['stringReverse', 'hasOwn'], {chrome}); + +global.window = global; +global.JSZip = yomichanTest.JSZip; +global.dictFieldSplit = dictFieldSplit; +global.dictTagSanitize = dictTagSanitize; +global.stringReverse = stringReverse; +global.hasOwn = hasOwn; + + +function countTermsWithExpression(terms, expression) { + return terms.reduce((i, v) => (i + (v.expression === expression ? 1 : 0)), 0); +} + +function countTermsWithReading(terms, reading) { + return terms.reduce((i, v) => (i + (v.reading === reading ? 1 : 0)), 0); +} + +function countMetasWithMode(metas, mode) { + return metas.reduce((i, v) => (i + (v.mode === mode ? 1 : 0)), 0); +} + +function countKanjiWithCharacter(kanji, character) { + return kanji.reduce((i, v) => (i + (v.character === character ? 1 : 0)), 0); +} + + +async function testDatabase1() { + // 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 iteration data + const iterations = [ + { + cleanup: async () => { + // Test purge + await database.purge(); + await testDatabaseEmpty1(database); + } + }, + { + cleanup: async () => { + // Test deleteDictionary + let progressEvent = false; + await database.deleteDictionary( + title, + () => { + progressEvent = true; + }, + {rate: 1000} + ); + assert.ok(progressEvent); + + await testDatabaseEmpty1(database); + } + }, + { + cleanup: async () => {} + } + ]; + + // Setup database + const database = new Database(); + await database.prepare(); + + for (const {cleanup} of iterations) { + // Import data + let progressEvent = false; + await database.importDictionary( + testDictionarySource, + () => { + progressEvent = true; + }, + {prefixWildcardsSupported: true} + ); + assert.ok(progressEvent); + + // Get info summary + const info = await database.getDictionaryInfo(); + assert.deepStrictEqual(info, [{ + title, + revision: 'test', + sequenced: true, + version: 3, + prefixWildcardsSupported: true + }]); + + // Get counts + const counts = await database.getDictionaryCounts( + info.map((v) => v.title), + true + ); + assert.deepStrictEqual(counts, { + counts: [{kanji: 2, kanjiMeta: 2, terms: 32, termMeta: 3, tagMeta: 12}], + total: {kanji: 2, kanjiMeta: 2, terms: 32, termMeta: 3, tagMeta: 12} + }); + + // Test find* functions + await testFindTermsBulkTest1(database, titles); + await testTindTermsExactBulk1(database, titles); + await testFindTermsBySequenceBulk1(database, title); + await testFindTermMetaBulk1(database, titles); + await testFindKanjiBulk1(database, titles); + await testFindKanjiMetaBulk1(database, titles); + await testFindTagForTitle1(database, title); + + // Cleanup + await cleanup(); + } +} + +async function testDatabaseEmpty1(database) { + const info = await database.getDictionaryInfo(); + assert.deepStrictEqual(info, []); + + const counts = await database.getDictionaryCounts([], true); + assert.deepStrictEqual(counts, { + counts: [], + total: {kanji: 0, kanjiMeta: 0, terms: 0, termMeta: 0, tagMeta: 0} + }); +} + +async function testFindTermsBulkTest1(database, titles) { + const data = [ + { + inputs: [ + { + wildcard: null, + termList: ['打', '打つ', '打ち込む'] + }, + { + wildcard: null, + termList: ['だ', 'ダース', 'うつ', 'ぶつ', 'うちこむ', 'ぶちこむ'] + }, + { + wildcard: 'suffix', + termList: ['打'] + } + ], + expectedResults: { + total: 32, + expressions: [ + ['打', 2], + ['打つ', 17], + ['打ち込む', 13] + ], + readings: [ + ['だ', 1], + ['ダース', 1], + ['うつ', 15], + ['ぶつ', 2], + ['うちこむ', 9], + ['ぶちこむ', 4] + ] + } + }, + { + inputs: [ + { + wildcard: null, + termList: ['込む'] + } + ], + expectedResults: { + total: 0, + expressions: [], + readings: [] + } + }, + { + inputs: [ + { + wildcard: 'prefix', + termList: ['込む'] + } + ], + expectedResults: { + total: 13, + expressions: [ + ['打ち込む', 13] + ], + readings: [ + ['うちこむ', 9], + ['ぶちこむ', 4] + ] + } + }, + { + inputs: [ + { + wildcard: null, + termList: [] + } + ], + expectedResults: { + total: 0, + expressions: [], + readings: [] + } + } + ]; + + for (const {inputs, expectedResults} of data) { + for (const {termList, wildcard} of inputs) { + const results = await database.findTermsBulk(termList, titles, wildcard); + assert.strictEqual(results.length, expectedResults.total); + for (const [expression, count] of expectedResults.expressions) { + assert.strictEqual(countTermsWithExpression(results, expression), count); + } + for (const [reading, count] of expectedResults.readings) { + assert.strictEqual(countTermsWithReading(results, reading), count); + } + } + } +} + +async function testTindTermsExactBulk1(database, titles) { + const data = [ + { + inputs: [ + { + termList: ['打', '打つ', '打ち込む'], + readingList: ['だ', 'うつ', 'うちこむ'] + } + ], + expectedResults: { + total: 25, + expressions: [ + ['打', 1], + ['打つ', 15], + ['打ち込む', 9] + ], + readings: [ + ['だ', 1], + ['うつ', 15], + ['うちこむ', 9] + ] + } + }, + { + inputs: [ + { + termList: ['打', '打つ', '打ち込む'], + readingList: ['だ?', 'うつ?', 'うちこむ?'] + } + ], + expectedResults: { + total: 0, + expressions: [], + readings: [] + } + }, + { + inputs: [ + { + termList: ['打つ', '打つ'], + readingList: ['うつ', 'ぶつ'] + } + ], + expectedResults: { + total: 17, + expressions: [ + ['打つ', 17] + ], + readings: [ + ['うつ', 15], + ['ぶつ', 2] + ] + } + }, + { + inputs: [ + { + termList: ['打つ'], + readingList: ['うちこむ'] + } + ], + expectedResults: { + total: 0, + expressions: [], + readings: [] + } + }, + { + inputs: [ + { + termList: [], + readingList: [] + } + ], + expectedResults: { + total: 0, + expressions: [], + readings: [] + } + } + ]; + + for (const {inputs, expectedResults} of data) { + for (const {termList, readingList} of inputs) { + const results = await database.findTermsExactBulk(termList, readingList, titles); + assert.strictEqual(results.length, expectedResults.total); + for (const [expression, count] of expectedResults.expressions) { + assert.strictEqual(countTermsWithExpression(results, expression), count); + } + for (const [reading, count] of expectedResults.readings) { + assert.strictEqual(countTermsWithReading(results, reading), count); + } + } + } +} + +async function testFindTermsBySequenceBulk1(database, mainDictionary) { + const data = [ + { + inputs: [ + { + sequenceList: [1, 2, 3, 4, 5, 6] + } + ], + expectedResults: { + total: 32, + expressions: [ + ['打', 2], + ['打つ', 17], + ['打ち込む', 13] + ], + readings: [ + ['だ', 1], + ['ダース', 1], + ['うつ', 15], + ['ぶつ', 2], + ['うちこむ', 9], + ['ぶちこむ', 4] + ] + } + }, + { + inputs: [ + { + sequenceList: [1] + } + ], + expectedResults: { + total: 1, + expressions: [ + ['打', 1] + ], + readings: [ + ['だ', 1] + ] + } + }, + { + inputs: [ + { + sequenceList: [2] + } + ], + expectedResults: { + total: 1, + expressions: [ + ['打', 1] + ], + readings: [ + ['ダース', 1] + ] + } + }, + { + inputs: [ + { + sequenceList: [3] + } + ], + expectedResults: { + total: 15, + expressions: [ + ['打つ', 15] + ], + readings: [ + ['うつ', 15] + ] + } + }, + { + inputs: [ + { + sequenceList: [4] + } + ], + expectedResults: { + total: 2, + expressions: [ + ['打つ', 2] + ], + readings: [ + ['ぶつ', 2] + ] + } + }, + { + inputs: [ + { + sequenceList: [5] + } + ], + expectedResults: { + total: 9, + expressions: [ + ['打ち込む', 9] + ], + readings: [ + ['うちこむ', 9] + ] + } + }, + { + inputs: [ + { + sequenceList: [6] + } + ], + expectedResults: { + total: 4, + expressions: [ + ['打ち込む', 4] + ], + readings: [ + ['ぶちこむ', 4] + ] + } + }, + { + inputs: [ + { + sequenceList: [-1] + } + ], + expectedResults: { + total: 0, + expressions: [], + readings: [] + } + }, + { + inputs: [ + { + sequenceList: [] + } + ], + expectedResults: { + total: 0, + expressions: [], + readings: [] + } + } + ]; + + for (const {inputs, expectedResults} of data) { + for (const {sequenceList} of inputs) { + const results = await database.findTermsBySequenceBulk(sequenceList, mainDictionary); + assert.strictEqual(results.length, expectedResults.total); + for (const [expression, count] of expectedResults.expressions) { + assert.strictEqual(countTermsWithExpression(results, expression), count); + } + for (const [reading, count] of expectedResults.readings) { + assert.strictEqual(countTermsWithReading(results, reading), count); + } + } + } +} + +async function testFindTermMetaBulk1(database, titles) { + const data = [ + { + inputs: [ + { + termList: ['打'] + } + ], + expectedResults: { + total: 1, + modes: [ + ['freq', 1] + ] + } + }, + { + inputs: [ + { + termList: ['打つ'] + } + ], + expectedResults: { + total: 1, + modes: [ + ['freq', 1] + ] + } + }, + { + inputs: [ + { + termList: ['打ち込む'] + } + ], + expectedResults: { + total: 1, + modes: [ + ['freq', 1] + ] + } + }, + { + inputs: [ + { + termList: ['?'] + } + ], + expectedResults: { + total: 0, + modes: [] + } + } + ]; + + for (const {inputs, expectedResults} of data) { + for (const {termList} of inputs) { + const results = await database.findTermMetaBulk(termList, titles); + assert.strictEqual(results.length, expectedResults.total); + for (const [mode, count] of expectedResults.modes) { + assert.strictEqual(countMetasWithMode(results, mode), count); + } + } + } +} + +async function testFindKanjiBulk1(database, titles) { + const data = [ + { + inputs: [ + { + kanjiList: ['打'] + } + ], + expectedResults: { + total: 1, + kanji: [ + ['打', 1] + ] + } + }, + { + inputs: [ + { + kanjiList: ['込'] + } + ], + expectedResults: { + total: 1, + kanji: [ + ['込', 1] + ] + } + }, + { + inputs: [ + { + kanjiList: ['?'] + } + ], + expectedResults: { + total: 0, + kanji: [] + } + } + ]; + + for (const {inputs, expectedResults} of data) { + for (const {kanjiList} of inputs) { + const results = await database.findKanjiBulk(kanjiList, titles); + assert.strictEqual(results.length, expectedResults.total); + for (const [kanji, count] of expectedResults.kanji) { + assert.strictEqual(countKanjiWithCharacter(results, kanji), count); + } + } + } +} + +async function testFindKanjiMetaBulk1(database, titles) { + const data = [ + { + inputs: [ + { + kanjiList: ['打'] + } + ], + expectedResults: { + total: 1, + modes: [ + ['freq', 1] + ] + } + }, + { + inputs: [ + { + kanjiList: ['込'] + } + ], + expectedResults: { + total: 1, + modes: [ + ['freq', 1] + ] + } + }, + { + inputs: [ + { + kanjiList: ['?'] + } + ], + expectedResults: { + total: 0, + modes: [] + } + } + ]; + + for (const {inputs, expectedResults} of data) { + for (const {kanjiList} of inputs) { + const results = await database.findKanjiMetaBulk(kanjiList, titles); + assert.strictEqual(results.length, expectedResults.total); + for (const [mode, count] of expectedResults.modes) { + assert.strictEqual(countMetasWithMode(results, mode), count); + } + } + } +} + +async function testFindTagForTitle1(database, title) { + const data = [ + { + inputs: [ + { + name: 'tag1' + } + ], + expectedResults: { + value: {category: 'category1', dictionary: title, name: 'tag1', notes: 'tag1 notes', order: 0, score: 0} + } + }, + { + inputs: [ + { + name: 'ktag1' + } + ], + expectedResults: { + value: {category: 'kcategory1', dictionary: title, name: 'ktag1', notes: 'ktag1 notes', order: 0, score: 0} + } + }, + { + inputs: [ + { + name: 'kstat1' + } + ], + expectedResults: { + value: {category: 'kcategory3', dictionary: title, name: 'kstat1', notes: 'kstat1 notes', order: 0, score: 0} + } + }, + { + inputs: [ + { + name: 'invalid' + } + ], + expectedResults: { + value: null + } + } + ]; + + for (const {inputs, expectedResults} of data) { + for (const {name} of inputs) { + const result = await database.findTagForTitle(name, title); + assert.deepStrictEqual(result, expectedResults.value); + } + } +} + + +async function main() { + await testDatabase1(); +} + + +if (require.main === module) { main(); } -- cgit v1.2.3 From 7901a467217f311566e7a3bc83ceafa17909d511 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Wed, 19 Feb 2020 18:46:27 -0500 Subject: Validate results of importDictionary --- test/test-database.js | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) (limited to 'test') diff --git a/test/test-database.js b/test/test-database.js index b9ac8a46..d66e2137 100644 --- a/test/test-database.js +++ b/test/test-database.js @@ -83,26 +83,30 @@ async function testDatabase1() { await database.prepare(); for (const {cleanup} of iterations) { + const expectedSummary = { + title, + revision: 'test', + sequenced: true, + version: 3, + prefixWildcardsSupported: true + }; + // Import data let progressEvent = false; - await database.importDictionary( + const {result, errors} = await database.importDictionary( testDictionarySource, () => { progressEvent = true; }, {prefixWildcardsSupported: true} ); + assert.deepStrictEqual(errors, []); + assert.deepStrictEqual(result, expectedSummary); assert.ok(progressEvent); // Get info summary const info = await database.getDictionaryInfo(); - assert.deepStrictEqual(info, [{ - title, - revision: 'test', - sequenced: true, - version: 3, - prefixWildcardsSupported: true - }]); + assert.deepStrictEqual(info, [expectedSummary]); // Get counts const counts = await database.getDictionaryCounts( -- cgit v1.2.3 From a8b1e40a1e34178d9907b04f3a8e16a21b9eb42a Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Wed, 19 Feb 2020 19:59:24 -0500 Subject: Close to prevent hangs --- ext/bg/js/database.js | 6 ++++++ test/test-database.js | 2 ++ 2 files changed, 8 insertions(+) (limited to 'test') diff --git a/ext/bg/js/database.js b/ext/bg/js/database.js index b54d832c..02d59c83 100644 --- a/ext/bg/js/database.js +++ b/ext/bg/js/database.js @@ -96,6 +96,12 @@ class Database { } } + async close() { + this.validate(); + this.db.close(); + this.db = null; + } + async purge() { this.validate(); diff --git a/test/test-database.js b/test/test-database.js index d66e2137..cefa297c 100644 --- a/test/test-database.js +++ b/test/test-database.js @@ -130,6 +130,8 @@ async function testDatabase1() { // Cleanup await cleanup(); } + + await database.close(); } async function testDatabaseEmpty1(database) { -- cgit v1.2.3 From 1e573f36c416b3955b2e679c8f3ce7ffa91bf504 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Wed, 19 Feb 2020 20:00:44 -0500 Subject: Test some errors --- test/test-database.js | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) (limited to 'test') 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(); } -- cgit v1.2.3 From 4daf3435c24592352261c1186626abdc86beefb4 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 22 Feb 2020 12:47:09 -0500 Subject: Update database test --- test/test-database.js | 86 +++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 77 insertions(+), 9 deletions(-) (limited to 'test') diff --git a/test/test-database.js b/test/test-database.js index fcf519d7..801f9925 100644 --- a/test/test-database.js +++ b/test/test-database.js @@ -1,3 +1,6 @@ +const fs = require('fs'); +const url = require('url'); +const path = require('path'); const assert = require('assert'); const yomichanTest = require('./yomichan-test'); require('fake-indexeddb/auto'); @@ -5,21 +8,86 @@ require('fake-indexeddb/auto'); const chrome = { runtime: { onMessage: { - addListener: () => { /* NOP */ } + addListener() { /* NOP */ } + }, + getURL(path2) { + return url.pathToFileURL(path.join(__dirname, '..', 'ext', path2.replace(/^\//, ''))); } } }; -const {Database} = yomichanTest.requireScript('ext/bg/js/database.js', ['Database']); +class XMLHttpRequest { + constructor() { + this._eventCallbacks = new Map(); + this._url = ''; + this._responseText = null; + } + + overrideMimeType() { + // NOP + } + + addEventListener(eventName, callback) { + let callbacks = this._eventCallbacks.get(eventName); + if (typeof callbacks === 'undefined') { + callbacks = []; + this._eventCallbacks.set(eventName, callbacks); + } + callbacks.push(callback); + } + + open(action, url) { + this._url = url; + } + + send() { + const filePath = url.fileURLToPath(this._url); + Promise.resolve() + .then(() => { + let source; + try { + source = fs.readFileSync(filePath, {encoding: 'utf8'}); + } catch (e) { + this._trigger('error'); + return; + } + this._responseText = source; + this._trigger('load'); + }); + } + + get responseText() { + return this._responseText; + } + + _trigger(eventName, ...args) { + const callbacks = this._eventCallbacks.get(eventName); + if (typeof callbacks === 'undefined') { return; } + + for (let i = 0, ii = callbacks.length; i < ii; ++i) { + callbacks[i](...args); + } + } +} + +const {JsonSchema} = yomichanTest.requireScript('ext/bg/js/json-schema.js', ['JsonSchema']); const {dictFieldSplit, dictTagSanitize} = yomichanTest.requireScript('ext/bg/js/dictionary.js', ['dictFieldSplit', 'dictTagSanitize']); const {stringReverse, hasOwn} = yomichanTest.requireScript('ext/mixed/js/core.js', ['stringReverse', 'hasOwn'], {chrome}); - -global.window = global; -global.JSZip = yomichanTest.JSZip; -global.dictFieldSplit = dictFieldSplit; -global.dictTagSanitize = dictTagSanitize; -global.stringReverse = stringReverse; -global.hasOwn = hasOwn; +const {requestJson} = yomichanTest.requireScript('ext/bg/js/request.js', ['requestJson'], {XMLHttpRequest}); + +const databaseGlobals = { + chrome, + JsonSchema, + requestJson, + stringReverse, + hasOwn, + dictFieldSplit, + dictTagSanitize, + indexedDB: global.indexedDB, + JSZip: yomichanTest.JSZip +}; +databaseGlobals.window = databaseGlobals; +const {Database} = yomichanTest.requireScript('ext/bg/js/database.js', ['Database'], databaseGlobals); function countTermsWithExpression(terms, expression) { -- cgit v1.2.3 From a54f44122a3a245c1b1579c15b2faf9d50c15c55 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 22 Feb 2020 12:58:33 -0500 Subject: Update location of test dictionary data --- .../data/dictionaries/valid-dictionary1/index.json | 6 ++++ .../valid-dictionary1/kanji_bank_1.json | 42 ++++++++++++++++++++++ .../valid-dictionary1/kanji_meta_bank_1.json | 4 +++ .../dictionaries/valid-dictionary1/tag_bank_1.json | 7 ++++ .../dictionaries/valid-dictionary1/tag_bank_2.json | 9 +++++ .../valid-dictionary1/term_bank_1.json | 34 ++++++++++++++++++ .../valid-dictionary1/term_meta_bank_1.json | 5 +++ test/test-database.js | 4 +-- test/test-dictionary-data/index.json | 6 ---- test/test-dictionary-data/kanji_bank_1.json | 42 ---------------------- test/test-dictionary-data/kanji_meta_bank_1.json | 4 --- test/test-dictionary-data/tag_bank_1.json | 7 ---- test/test-dictionary-data/tag_bank_2.json | 9 ----- test/test-dictionary-data/term_bank_1.json | 34 ------------------ test/test-dictionary-data/term_meta_bank_1.json | 5 --- test/test-dictionary.js | 2 +- test/yomichan-test.js | 15 +++----- 17 files changed, 114 insertions(+), 121 deletions(-) create mode 100644 test/data/dictionaries/valid-dictionary1/index.json create mode 100644 test/data/dictionaries/valid-dictionary1/kanji_bank_1.json create mode 100644 test/data/dictionaries/valid-dictionary1/kanji_meta_bank_1.json create mode 100644 test/data/dictionaries/valid-dictionary1/tag_bank_1.json create mode 100644 test/data/dictionaries/valid-dictionary1/tag_bank_2.json create mode 100644 test/data/dictionaries/valid-dictionary1/term_bank_1.json create mode 100644 test/data/dictionaries/valid-dictionary1/term_meta_bank_1.json delete mode 100644 test/test-dictionary-data/index.json delete mode 100644 test/test-dictionary-data/kanji_bank_1.json delete mode 100644 test/test-dictionary-data/kanji_meta_bank_1.json delete mode 100644 test/test-dictionary-data/tag_bank_1.json delete mode 100644 test/test-dictionary-data/tag_bank_2.json delete mode 100644 test/test-dictionary-data/term_bank_1.json delete mode 100644 test/test-dictionary-data/term_meta_bank_1.json (limited to 'test') diff --git a/test/data/dictionaries/valid-dictionary1/index.json b/test/data/dictionaries/valid-dictionary1/index.json new file mode 100644 index 00000000..3034bf38 --- /dev/null +++ b/test/data/dictionaries/valid-dictionary1/index.json @@ -0,0 +1,6 @@ +{ + "title": "Test Dictionary", + "format": 3, + "revision": "test", + "sequenced": true +} \ No newline at end of file diff --git a/test/data/dictionaries/valid-dictionary1/kanji_bank_1.json b/test/data/dictionaries/valid-dictionary1/kanji_bank_1.json new file mode 100644 index 00000000..264f94c1 --- /dev/null +++ b/test/data/dictionaries/valid-dictionary1/kanji_bank_1.json @@ -0,0 +1,42 @@ +[ + [ + "打", + "ダ ダアス", + "う.つ う.ち- ぶ.つ", + "ktag1 ktag2", + [ + "meaning1", + "meaning2", + "meaning3", + "meaning4", + "meaning5" + ], + { + "kstat1": "1", + "kstat2": "2", + "kstat3": "3", + "kstat4": "4", + "kstat5": "5" + } + ], + [ + "込", + "", + "-こ.む こ.む こ.み -こ.み こ.める", + "ktag1 ktag2", + [ + "meaning1", + "meaning2", + "meaning3", + "meaning4", + "meaning5" + ], + { + "kstat1": "1", + "kstat2": "2", + "kstat3": "3", + "kstat4": "4", + "kstat5": "5" + } + ] +] \ No newline at end of file diff --git a/test/data/dictionaries/valid-dictionary1/kanji_meta_bank_1.json b/test/data/dictionaries/valid-dictionary1/kanji_meta_bank_1.json new file mode 100644 index 00000000..73e75b8a --- /dev/null +++ b/test/data/dictionaries/valid-dictionary1/kanji_meta_bank_1.json @@ -0,0 +1,4 @@ +[ + ["打", "freq", 1], + ["込", "freq", 2] +] \ No newline at end of file diff --git a/test/data/dictionaries/valid-dictionary1/tag_bank_1.json b/test/data/dictionaries/valid-dictionary1/tag_bank_1.json new file mode 100644 index 00000000..109ad395 --- /dev/null +++ b/test/data/dictionaries/valid-dictionary1/tag_bank_1.json @@ -0,0 +1,7 @@ +[ + ["tag1", "category1", 0, "tag1 notes", 0], + ["tag2", "category2", 0, "tag2 notes", 0], + ["tag3", "category3", 0, "tag3 notes", 0], + ["tag4", "category4", 0, "tag4 notes", 0], + ["tag5", "category5", 0, "tag5 notes", 0] +] \ No newline at end of file diff --git a/test/data/dictionaries/valid-dictionary1/tag_bank_2.json b/test/data/dictionaries/valid-dictionary1/tag_bank_2.json new file mode 100644 index 00000000..5e7936b3 --- /dev/null +++ b/test/data/dictionaries/valid-dictionary1/tag_bank_2.json @@ -0,0 +1,9 @@ +[ + ["ktag1", "kcategory1", 0, "ktag1 notes", 0], + ["ktag2", "kcategory2", 0, "ktag2 notes", 0], + ["kstat1", "kcategory3", 0, "kstat1 notes", 0], + ["kstat2", "kcategory4", 0, "kstat2 notes", 0], + ["kstat3", "kcategory5", 0, "kstat3 notes", 0], + ["kstat4", "kcategory6", 0, "kstat4 notes", 0], + ["kstat5", "kcategory7", 0, "kstat5 notes", 0] +] \ No newline at end of file diff --git a/test/data/dictionaries/valid-dictionary1/term_bank_1.json b/test/data/dictionaries/valid-dictionary1/term_bank_1.json new file mode 100644 index 00000000..755d9f6a --- /dev/null +++ b/test/data/dictionaries/valid-dictionary1/term_bank_1.json @@ -0,0 +1,34 @@ +[ + ["打", "だ", "tag1 tag2", "", 2, ["definition1a (打, だ)", "definition1b (打, だ)"], 1, "tag3 tag4 tag5"], + ["打", "ダース", "tag1 tag2", "", 1, ["definition1a (打, ダース)", "definition1b (打, ダース)"], 2, "tag3 tag4 tag5"], + ["打つ", "うつ", "tag1 tag2", "v5", 3, ["definition1a (打つ, うつ)", "definition1b (打つ, うつ)"], 3, "tag3 tag4 tag5"], + ["打つ", "うつ", "tag1 tag2", "v5", 4, ["definition2a (打つ, うつ)", "definition2b (打つ, うつ)"], 3, "tag3 tag4 tag5"], + ["打つ", "うつ", "tag1 tag2", "v5", 5, ["definition3a (打つ, うつ)", "definition3b (打つ, うつ)"], 3, "tag3 tag4 tag5"], + ["打つ", "うつ", "tag1 tag2", "v5", 6, ["definition4a (打つ, うつ)", "definition4b (打つ, うつ)"], 3, "tag3 tag4 tag5"], + ["打つ", "うつ", "tag1 tag2", "v5", 7, ["definition5a (打つ, うつ)", "definition5b (打つ, うつ)"], 3, "tag3 tag4 tag5"], + ["打つ", "うつ", "tag1 tag2", "v5", 8, ["definition6a (打つ, うつ)", "definition6b (打つ, うつ)"], 3, "tag3 tag4 tag5"], + ["打つ", "うつ", "tag1 tag2", "v5", 9, ["definition7a (打つ, うつ)", "definition7b (打つ, うつ)"], 3, "tag3 tag4 tag5"], + ["打つ", "うつ", "tag1 tag2", "v5", 10, ["definition8a (打つ, うつ)", "definition8b (打つ, うつ)"], 3, "tag3 tag4 tag5"], + ["打つ", "うつ", "tag1 tag2", "v5", 11, ["definition9a (打つ, うつ)", "definition9b (打つ, うつ)"], 3, "tag3 tag4 tag5"], + ["打つ", "うつ", "tag1 tag2", "v5", 12, ["definition10a (打つ, うつ)", "definition10b (打つ, うつ)"], 3, "tag3 tag4 tag5"], + ["打つ", "うつ", "tag1 tag2", "v5", 13, ["definition11a (打つ, うつ)", "definition11b (打つ, うつ)"], 3, "tag3 tag4 tag5"], + ["打つ", "うつ", "tag1 tag2", "v5", 14, ["definition12a (打つ, うつ)", "definition12b (打つ, うつ)"], 3, "tag3 tag4 tag5"], + ["打つ", "うつ", "tag1 tag2", "v5", 15, ["definition13a (打つ, うつ)", "definition13b (打つ, うつ)"], 3, "tag3 tag4 tag5"], + ["打つ", "うつ", "tag1 tag2", "v5", 16, ["definition14a (打つ, うつ)", "definition14b (打つ, うつ)"], 3, "tag3 tag4 tag5"], + ["打つ", "うつ", "tag1 tag2", "v5", 17, ["definition15a (打つ, うつ)", "definition15b (打つ, うつ)"], 3, "tag3 tag4 tag5"], + ["打つ", "ぶつ", "tag1 tag2", "v5", 18, ["definition1a (打つ, ぶつ)", "definition1b (打つ, ぶつ)"], 4, "tag3 tag4 tag5"], + ["打つ", "ぶつ", "tag1 tag2", "v5", 19, ["definition2a (打つ, ぶつ)", "definition2b (打つ, ぶつ)"], 4, "tag3 tag4 tag5"], + ["打ち込む", "うちこむ", "tag1 tag2", "v5", 20, ["definition1a (打ち込む, うちこむ)", "definition1b (打ち込む, うちこむ)"], 5, "tag3 tag4 tag5"], + ["打ち込む", "うちこむ", "tag1 tag2", "v5", 21, ["definition2a (打ち込む, うちこむ)", "definition2b (打ち込む, うちこむ)"], 5, "tag5 tag6 tag7"], + ["打ち込む", "うちこむ", "tag1 tag2", "v5", 22, ["definition3a (打ち込む, うちこむ)", "definition3b (打ち込む, うちこむ)"], 5, "tag5 tag6 tag7"], + ["打ち込む", "うちこむ", "tag1 tag2", "v5", 23, ["definition4a (打ち込む, うちこむ)", "definition4b (打ち込む, うちこむ)"], 5, "tag5 tag6 tag7"], + ["打ち込む", "うちこむ", "tag1 tag2", "v5", 24, ["definition5a (打ち込む, うちこむ)", "definition5b (打ち込む, うちこむ)"], 5, "tag5 tag6 tag7"], + ["打ち込む", "うちこむ", "tag1 tag2", "v5", 25, ["definition6a (打ち込む, うちこむ)", "definition6b (打ち込む, うちこむ)"], 5, "tag5 tag6 tag7"], + ["打ち込む", "うちこむ", "tag1 tag2", "v5", 26, ["definition7a (打ち込む, うちこむ)", "definition7b (打ち込む, うちこむ)"], 5, "tag5 tag6 tag7"], + ["打ち込む", "うちこむ", "tag1 tag2", "v5", 27, ["definition8a (打ち込む, うちこむ)", "definition8b (打ち込む, うちこむ)"], 5, "tag5 tag6 tag7"], + ["打ち込む", "うちこむ", "tag1 tag2", "v5", 28, ["definition9a (打ち込む, うちこむ)", "definition9b (打ち込む, うちこむ)"], 5, "tag5 tag6 tag7"], + ["打ち込む", "ぶちこむ", "tag1 tag2", "v5", 29, ["definition1a (打ち込む, ぶちこむ)", "definition1b (打ち込む, ぶちこむ)"], 6, "tag3 tag4 tag5"], + ["打ち込む", "ぶちこむ", "tag1 tag2", "v5", 30, ["definition2a (打ち込む, ぶちこむ)", "definition2b (打ち込む, ぶちこむ)"], 6, "tag3 tag4 tag5"], + ["打ち込む", "ぶちこむ", "tag1 tag2", "v5", 31, ["definition3a (打ち込む, ぶちこむ)", "definition3b (打ち込む, ぶちこむ)"], 6, "tag3 tag4 tag5"], + ["打ち込む", "ぶちこむ", "tag1 tag2", "v5", 32, ["definition4a (打ち込む, ぶちこむ)", "definition4b (打ち込む, ぶちこむ)"], 6, "tag3 tag4 tag5"] +] \ No newline at end of file diff --git a/test/data/dictionaries/valid-dictionary1/term_meta_bank_1.json b/test/data/dictionaries/valid-dictionary1/term_meta_bank_1.json new file mode 100644 index 00000000..78096502 --- /dev/null +++ b/test/data/dictionaries/valid-dictionary1/term_meta_bank_1.json @@ -0,0 +1,5 @@ +[ + ["打", "freq", 1], + ["打つ", "freq", 2], + ["打ち込む", "freq", 3] +] \ No newline at end of file diff --git a/test/test-database.js b/test/test-database.js index 801f9925..216ae5dd 100644 --- a/test/test-database.js +++ b/test/test-database.js @@ -121,7 +121,7 @@ async function clearDatabase() { async function testDatabase1() { // Load dictionary data - const testDictionary = yomichanTest.createTestDictionaryArchive(); + const testDictionary = yomichanTest.createTestDictionaryArchive('valid-dictionary1'); const testDictionarySource = await testDictionary.generateAsync({type: 'string'}); const testDictionaryIndex = JSON.parse(await testDictionary.files['index.json'].async('string')); @@ -800,7 +800,7 @@ async function testFindTagForTitle1(database, title) { async function testDatabase2() { // Load dictionary data - const testDictionary = yomichanTest.createTestDictionaryArchive(); + const testDictionary = yomichanTest.createTestDictionaryArchive('valid-dictionary1'); const testDictionarySource = await testDictionary.generateAsync({type: 'string'}); const testDictionaryIndex = JSON.parse(await testDictionary.files['index.json'].async('string')); diff --git a/test/test-dictionary-data/index.json b/test/test-dictionary-data/index.json deleted file mode 100644 index 3034bf38..00000000 --- a/test/test-dictionary-data/index.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "title": "Test Dictionary", - "format": 3, - "revision": "test", - "sequenced": true -} \ No newline at end of file diff --git a/test/test-dictionary-data/kanji_bank_1.json b/test/test-dictionary-data/kanji_bank_1.json deleted file mode 100644 index 264f94c1..00000000 --- a/test/test-dictionary-data/kanji_bank_1.json +++ /dev/null @@ -1,42 +0,0 @@ -[ - [ - "打", - "ダ ダアス", - "う.つ う.ち- ぶ.つ", - "ktag1 ktag2", - [ - "meaning1", - "meaning2", - "meaning3", - "meaning4", - "meaning5" - ], - { - "kstat1": "1", - "kstat2": "2", - "kstat3": "3", - "kstat4": "4", - "kstat5": "5" - } - ], - [ - "込", - "", - "-こ.む こ.む こ.み -こ.み こ.める", - "ktag1 ktag2", - [ - "meaning1", - "meaning2", - "meaning3", - "meaning4", - "meaning5" - ], - { - "kstat1": "1", - "kstat2": "2", - "kstat3": "3", - "kstat4": "4", - "kstat5": "5" - } - ] -] \ No newline at end of file diff --git a/test/test-dictionary-data/kanji_meta_bank_1.json b/test/test-dictionary-data/kanji_meta_bank_1.json deleted file mode 100644 index 73e75b8a..00000000 --- a/test/test-dictionary-data/kanji_meta_bank_1.json +++ /dev/null @@ -1,4 +0,0 @@ -[ - ["打", "freq", 1], - ["込", "freq", 2] -] \ No newline at end of file diff --git a/test/test-dictionary-data/tag_bank_1.json b/test/test-dictionary-data/tag_bank_1.json deleted file mode 100644 index 109ad395..00000000 --- a/test/test-dictionary-data/tag_bank_1.json +++ /dev/null @@ -1,7 +0,0 @@ -[ - ["tag1", "category1", 0, "tag1 notes", 0], - ["tag2", "category2", 0, "tag2 notes", 0], - ["tag3", "category3", 0, "tag3 notes", 0], - ["tag4", "category4", 0, "tag4 notes", 0], - ["tag5", "category5", 0, "tag5 notes", 0] -] \ No newline at end of file diff --git a/test/test-dictionary-data/tag_bank_2.json b/test/test-dictionary-data/tag_bank_2.json deleted file mode 100644 index 5e7936b3..00000000 --- a/test/test-dictionary-data/tag_bank_2.json +++ /dev/null @@ -1,9 +0,0 @@ -[ - ["ktag1", "kcategory1", 0, "ktag1 notes", 0], - ["ktag2", "kcategory2", 0, "ktag2 notes", 0], - ["kstat1", "kcategory3", 0, "kstat1 notes", 0], - ["kstat2", "kcategory4", 0, "kstat2 notes", 0], - ["kstat3", "kcategory5", 0, "kstat3 notes", 0], - ["kstat4", "kcategory6", 0, "kstat4 notes", 0], - ["kstat5", "kcategory7", 0, "kstat5 notes", 0] -] \ No newline at end of file diff --git a/test/test-dictionary-data/term_bank_1.json b/test/test-dictionary-data/term_bank_1.json deleted file mode 100644 index 755d9f6a..00000000 --- a/test/test-dictionary-data/term_bank_1.json +++ /dev/null @@ -1,34 +0,0 @@ -[ - ["打", "だ", "tag1 tag2", "", 2, ["definition1a (打, だ)", "definition1b (打, だ)"], 1, "tag3 tag4 tag5"], - ["打", "ダース", "tag1 tag2", "", 1, ["definition1a (打, ダース)", "definition1b (打, ダース)"], 2, "tag3 tag4 tag5"], - ["打つ", "うつ", "tag1 tag2", "v5", 3, ["definition1a (打つ, うつ)", "definition1b (打つ, うつ)"], 3, "tag3 tag4 tag5"], - ["打つ", "うつ", "tag1 tag2", "v5", 4, ["definition2a (打つ, うつ)", "definition2b (打つ, うつ)"], 3, "tag3 tag4 tag5"], - ["打つ", "うつ", "tag1 tag2", "v5", 5, ["definition3a (打つ, うつ)", "definition3b (打つ, うつ)"], 3, "tag3 tag4 tag5"], - ["打つ", "うつ", "tag1 tag2", "v5", 6, ["definition4a (打つ, うつ)", "definition4b (打つ, うつ)"], 3, "tag3 tag4 tag5"], - ["打つ", "うつ", "tag1 tag2", "v5", 7, ["definition5a (打つ, うつ)", "definition5b (打つ, うつ)"], 3, "tag3 tag4 tag5"], - ["打つ", "うつ", "tag1 tag2", "v5", 8, ["definition6a (打つ, うつ)", "definition6b (打つ, うつ)"], 3, "tag3 tag4 tag5"], - ["打つ", "うつ", "tag1 tag2", "v5", 9, ["definition7a (打つ, うつ)", "definition7b (打つ, うつ)"], 3, "tag3 tag4 tag5"], - ["打つ", "うつ", "tag1 tag2", "v5", 10, ["definition8a (打つ, うつ)", "definition8b (打つ, うつ)"], 3, "tag3 tag4 tag5"], - ["打つ", "うつ", "tag1 tag2", "v5", 11, ["definition9a (打つ, うつ)", "definition9b (打つ, うつ)"], 3, "tag3 tag4 tag5"], - ["打つ", "うつ", "tag1 tag2", "v5", 12, ["definition10a (打つ, うつ)", "definition10b (打つ, うつ)"], 3, "tag3 tag4 tag5"], - ["打つ", "うつ", "tag1 tag2", "v5", 13, ["definition11a (打つ, うつ)", "definition11b (打つ, うつ)"], 3, "tag3 tag4 tag5"], - ["打つ", "うつ", "tag1 tag2", "v5", 14, ["definition12a (打つ, うつ)", "definition12b (打つ, うつ)"], 3, "tag3 tag4 tag5"], - ["打つ", "うつ", "tag1 tag2", "v5", 15, ["definition13a (打つ, うつ)", "definition13b (打つ, うつ)"], 3, "tag3 tag4 tag5"], - ["打つ", "うつ", "tag1 tag2", "v5", 16, ["definition14a (打つ, うつ)", "definition14b (打つ, うつ)"], 3, "tag3 tag4 tag5"], - ["打つ", "うつ", "tag1 tag2", "v5", 17, ["definition15a (打つ, うつ)", "definition15b (打つ, うつ)"], 3, "tag3 tag4 tag5"], - ["打つ", "ぶつ", "tag1 tag2", "v5", 18, ["definition1a (打つ, ぶつ)", "definition1b (打つ, ぶつ)"], 4, "tag3 tag4 tag5"], - ["打つ", "ぶつ", "tag1 tag2", "v5", 19, ["definition2a (打つ, ぶつ)", "definition2b (打つ, ぶつ)"], 4, "tag3 tag4 tag5"], - ["打ち込む", "うちこむ", "tag1 tag2", "v5", 20, ["definition1a (打ち込む, うちこむ)", "definition1b (打ち込む, うちこむ)"], 5, "tag3 tag4 tag5"], - ["打ち込む", "うちこむ", "tag1 tag2", "v5", 21, ["definition2a (打ち込む, うちこむ)", "definition2b (打ち込む, うちこむ)"], 5, "tag5 tag6 tag7"], - ["打ち込む", "うちこむ", "tag1 tag2", "v5", 22, ["definition3a (打ち込む, うちこむ)", "definition3b (打ち込む, うちこむ)"], 5, "tag5 tag6 tag7"], - ["打ち込む", "うちこむ", "tag1 tag2", "v5", 23, ["definition4a (打ち込む, うちこむ)", "definition4b (打ち込む, うちこむ)"], 5, "tag5 tag6 tag7"], - ["打ち込む", "うちこむ", "tag1 tag2", "v5", 24, ["definition5a (打ち込む, うちこむ)", "definition5b (打ち込む, うちこむ)"], 5, "tag5 tag6 tag7"], - ["打ち込む", "うちこむ", "tag1 tag2", "v5", 25, ["definition6a (打ち込む, うちこむ)", "definition6b (打ち込む, うちこむ)"], 5, "tag5 tag6 tag7"], - ["打ち込む", "うちこむ", "tag1 tag2", "v5", 26, ["definition7a (打ち込む, うちこむ)", "definition7b (打ち込む, うちこむ)"], 5, "tag5 tag6 tag7"], - ["打ち込む", "うちこむ", "tag1 tag2", "v5", 27, ["definition8a (打ち込む, うちこむ)", "definition8b (打ち込む, うちこむ)"], 5, "tag5 tag6 tag7"], - ["打ち込む", "うちこむ", "tag1 tag2", "v5", 28, ["definition9a (打ち込む, うちこむ)", "definition9b (打ち込む, うちこむ)"], 5, "tag5 tag6 tag7"], - ["打ち込む", "ぶちこむ", "tag1 tag2", "v5", 29, ["definition1a (打ち込む, ぶちこむ)", "definition1b (打ち込む, ぶちこむ)"], 6, "tag3 tag4 tag5"], - ["打ち込む", "ぶちこむ", "tag1 tag2", "v5", 30, ["definition2a (打ち込む, ぶちこむ)", "definition2b (打ち込む, ぶちこむ)"], 6, "tag3 tag4 tag5"], - ["打ち込む", "ぶちこむ", "tag1 tag2", "v5", 31, ["definition3a (打ち込む, ぶちこむ)", "definition3b (打ち込む, ぶちこむ)"], 6, "tag3 tag4 tag5"], - ["打ち込む", "ぶちこむ", "tag1 tag2", "v5", 32, ["definition4a (打ち込む, ぶちこむ)", "definition4b (打ち込む, ぶちこむ)"], 6, "tag3 tag4 tag5"] -] \ No newline at end of file diff --git a/test/test-dictionary-data/term_meta_bank_1.json b/test/test-dictionary-data/term_meta_bank_1.json deleted file mode 100644 index 78096502..00000000 --- a/test/test-dictionary-data/term_meta_bank_1.json +++ /dev/null @@ -1,5 +0,0 @@ -[ - ["打", "freq", 1], - ["打つ", "freq", 2], - ["打ち込む", "freq", 3] -] \ No newline at end of file diff --git a/test/test-dictionary.js b/test/test-dictionary.js index 84014540..b9885edc 100644 --- a/test/test-dictionary.js +++ b/test/test-dictionary.js @@ -3,7 +3,7 @@ const dictionaryValidate = require('./dictionary-validate'); async function main() { - const archive = yomichanTest.createTestDictionaryArchive(); + const archive = yomichanTest.createTestDictionaryArchive('valid-dictionary1'); const schemas = dictionaryValidate.getSchemas(); await dictionaryValidate.validateDictionary(archive, schemas); } diff --git a/test/yomichan-test.js b/test/yomichan-test.js index dd4da919..939e0ad2 100644 --- a/test/yomichan-test.js +++ b/test/yomichan-test.js @@ -26,21 +26,14 @@ function getJSZip() { return JSZip; } -function createTestDictionaryArchive(dictionaryName) { - const fileNames = [ - 'index.json', - 'tag_bank_1.json', - 'tag_bank_2.json', - 'term_bank_1.json', - 'kanji_bank_1.json', - 'term_meta_bank_1.json', - 'kanji_meta_bank_1.json' - ]; +function createTestDictionaryArchive(dictionary, dictionaryName) { + const dictionaryDirectory = path.join(__dirname, 'data', 'dictionaries', dictionary); + const fileNames = fs.readdirSync(dictionaryDirectory); const archive = new (getJSZip())(); for (const fileName of fileNames) { - const source = fs.readFileSync(path.join(__dirname, 'test-dictionary-data', fileName), {encoding: 'utf8'}); + const source = fs.readFileSync(path.join(dictionaryDirectory, fileName), {encoding: 'utf8'}); const json = JSON.parse(source); if (fileName === 'index.json' && typeof dictionaryName === 'string') { json.title = dictionaryName; -- cgit v1.2.3 From 7b1a1480dc440eb1c7b1a6170ac0964bc4c7a3fa Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 22 Feb 2020 13:09:18 -0500 Subject: Add a timeout to clearDatabase This will trigger in case something goes wrong and a database isn't closed. --- test/test-database.js | 45 +++++++++++++++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 14 deletions(-) (limited to 'test') diff --git a/test/test-database.js b/test/test-database.js index 216ae5dd..6fe515e6 100644 --- a/test/test-database.js +++ b/test/test-database.js @@ -107,15 +107,25 @@ 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(); - }); - } +function clearDatabase(timeout) { + return new Promise((resolve, reject) => { + const timer = setTimeout(() => { + reject(new Error(`clearDatabase failed to resolve after ${timeout}ms`)); + }, timeout); + + (async () => { + 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(); + }); + } + clearTimeout(timer); + resolve(); + })(); + }); } @@ -840,11 +850,18 @@ async function testDatabase2() { async function main() { - await testDatabase1(); - await clearDatabase(); - - await testDatabase2(); - await clearDatabase(); + const clearTimeout = 5000; + try { + await testDatabase1(); + await clearDatabase(clearTimeout); + + await testDatabase2(); + await clearDatabase(clearTimeout); + } catch (e) { + console.log(e); + process.exit(-1); + throw e; + } } -- cgit v1.2.3 From 12e0923b63492c8ba5ca949d5ce0f3ad8aeb01d0 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 22 Feb 2020 13:38:03 -0500 Subject: Add some basic invalid dictionaries to test --- .../dictionaries/invalid-dictionary1/index.json | 7 ++++ .../dictionaries/invalid-dictionary2/index.json | 7 ++++ .../invalid-dictionary2/kanji_bank_1.json | 3 ++ .../dictionaries/invalid-dictionary3/index.json | 7 ++++ .../invalid-dictionary3/kanji_meta_bank_1.json | 1 + .../dictionaries/invalid-dictionary4/index.json | 7 ++++ .../invalid-dictionary4/tag_bank_1.json | 3 ++ .../dictionaries/invalid-dictionary5/index.json | 7 ++++ .../invalid-dictionary5/term_bank_1.json | 3 ++ .../dictionaries/invalid-dictionary6/index.json | 7 ++++ .../invalid-dictionary6/term_meta_bank_1.json | 1 + test/test-database.js | 42 ++++++++++++++++++++++ 12 files changed, 95 insertions(+) create mode 100644 test/data/dictionaries/invalid-dictionary1/index.json create mode 100644 test/data/dictionaries/invalid-dictionary2/index.json create mode 100644 test/data/dictionaries/invalid-dictionary2/kanji_bank_1.json create mode 100644 test/data/dictionaries/invalid-dictionary3/index.json create mode 100644 test/data/dictionaries/invalid-dictionary3/kanji_meta_bank_1.json create mode 100644 test/data/dictionaries/invalid-dictionary4/index.json create mode 100644 test/data/dictionaries/invalid-dictionary4/tag_bank_1.json create mode 100644 test/data/dictionaries/invalid-dictionary5/index.json create mode 100644 test/data/dictionaries/invalid-dictionary5/term_bank_1.json create mode 100644 test/data/dictionaries/invalid-dictionary6/index.json create mode 100644 test/data/dictionaries/invalid-dictionary6/term_meta_bank_1.json (limited to 'test') diff --git a/test/data/dictionaries/invalid-dictionary1/index.json b/test/data/dictionaries/invalid-dictionary1/index.json new file mode 100644 index 00000000..1be3b360 --- /dev/null +++ b/test/data/dictionaries/invalid-dictionary1/index.json @@ -0,0 +1,7 @@ +{ + "title": "Invalid Dictionary 1", + "format": 0, + "revision": "test", + "sequenced": true, + "description": "Invalid format number" +} \ No newline at end of file diff --git a/test/data/dictionaries/invalid-dictionary2/index.json b/test/data/dictionaries/invalid-dictionary2/index.json new file mode 100644 index 00000000..ba2cc669 --- /dev/null +++ b/test/data/dictionaries/invalid-dictionary2/index.json @@ -0,0 +1,7 @@ +{ + "title": "Invalid Dictionary 2", + "format": 3, + "revision": "test", + "sequenced": true, + "description": "Empty entry in kanji bank" +} \ No newline at end of file diff --git a/test/data/dictionaries/invalid-dictionary2/kanji_bank_1.json b/test/data/dictionaries/invalid-dictionary2/kanji_bank_1.json new file mode 100644 index 00000000..5825bcac --- /dev/null +++ b/test/data/dictionaries/invalid-dictionary2/kanji_bank_1.json @@ -0,0 +1,3 @@ +[ + [] +] \ No newline at end of file diff --git a/test/data/dictionaries/invalid-dictionary3/index.json b/test/data/dictionaries/invalid-dictionary3/index.json new file mode 100644 index 00000000..f23fa3f0 --- /dev/null +++ b/test/data/dictionaries/invalid-dictionary3/index.json @@ -0,0 +1,7 @@ +{ + "title": "Invalid Dictionary 3", + "format": 3, + "revision": "test", + "sequenced": true, + "description": "Invalid type entry in kanji meta bank" +} \ No newline at end of file diff --git a/test/data/dictionaries/invalid-dictionary3/kanji_meta_bank_1.json b/test/data/dictionaries/invalid-dictionary3/kanji_meta_bank_1.json new file mode 100644 index 00000000..9e26dfee --- /dev/null +++ b/test/data/dictionaries/invalid-dictionary3/kanji_meta_bank_1.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/test/data/dictionaries/invalid-dictionary4/index.json b/test/data/dictionaries/invalid-dictionary4/index.json new file mode 100644 index 00000000..542791d7 --- /dev/null +++ b/test/data/dictionaries/invalid-dictionary4/index.json @@ -0,0 +1,7 @@ +{ + "title": "Invalid Dictionary 4", + "format": 3, + "revision": "test", + "sequenced": true, + "description": "Invalid value as part of a tag bank entry" +} \ No newline at end of file diff --git a/test/data/dictionaries/invalid-dictionary4/tag_bank_1.json b/test/data/dictionaries/invalid-dictionary4/tag_bank_1.json new file mode 100644 index 00000000..4f19b476 --- /dev/null +++ b/test/data/dictionaries/invalid-dictionary4/tag_bank_1.json @@ -0,0 +1,3 @@ +[ + [{"invalid": true}, "category1", 0, "tag1 notes", 0] +] \ No newline at end of file diff --git a/test/data/dictionaries/invalid-dictionary5/index.json b/test/data/dictionaries/invalid-dictionary5/index.json new file mode 100644 index 00000000..e0d0f00e --- /dev/null +++ b/test/data/dictionaries/invalid-dictionary5/index.json @@ -0,0 +1,7 @@ +{ + "title": "Invalid Dictionary 5", + "format": 3, + "revision": "test", + "sequenced": true, + "description": "Invalid type as part of a term bank entry" +} \ No newline at end of file diff --git a/test/data/dictionaries/invalid-dictionary5/term_bank_1.json b/test/data/dictionaries/invalid-dictionary5/term_bank_1.json new file mode 100644 index 00000000..7288a996 --- /dev/null +++ b/test/data/dictionaries/invalid-dictionary5/term_bank_1.json @@ -0,0 +1,3 @@ +[ + ["打", "だ", "tag1 tag2", "", 2, false, 1, "tag3 tag4 tag5"] +] \ No newline at end of file diff --git a/test/data/dictionaries/invalid-dictionary6/index.json b/test/data/dictionaries/invalid-dictionary6/index.json new file mode 100644 index 00000000..b91acca3 --- /dev/null +++ b/test/data/dictionaries/invalid-dictionary6/index.json @@ -0,0 +1,7 @@ +{ + "title": "Invalid Dictionary 6", + "format": 3, + "revision": "test", + "sequenced": true, + "description": "Invalid root type for term meta bank" +} \ No newline at end of file diff --git a/test/data/dictionaries/invalid-dictionary6/term_meta_bank_1.json b/test/data/dictionaries/invalid-dictionary6/term_meta_bank_1.json new file mode 100644 index 00000000..02e4a84d --- /dev/null +++ b/test/data/dictionaries/invalid-dictionary6/term_meta_bank_1.json @@ -0,0 +1 @@ +false \ No newline at end of file diff --git a/test/test-database.js b/test/test-database.js index 6fe515e6..4fb3805d 100644 --- a/test/test-database.js +++ b/test/test-database.js @@ -849,6 +849,45 @@ async function testDatabase2() { } +async function testDatabase3() { + const invalidDictionaries = [ + 'invalid-dictionary1', + 'invalid-dictionary2', + 'invalid-dictionary3', + 'invalid-dictionary4', + 'invalid-dictionary5', + 'invalid-dictionary6' + ]; + + // Setup database + const database = new Database(); + await database.prepare(); + + for (const invalidDictionary of invalidDictionaries) { + const testDictionary = yomichanTest.createTestDictionaryArchive(invalidDictionary); + const testDictionarySource = await testDictionary.generateAsync({type: 'string'}); + + let error = null; + try { + await database.importDictionary(testDictionarySource, () => {}, {}); + } catch (e) { + error = e; + } + + if (error === null) { + assert.ok(false, `Expected an error while importing ${invalidDictionary}`); + } else { + const prefix = 'Dictionary has invalid data'; + const message = error.message; + assert.ok(typeof message, 'string'); + assert.ok(message.startsWith(prefix), `Expected error message to start with '${prefix}': ${message}`); + } + } + + await database.close(); +} + + async function main() { const clearTimeout = 5000; try { @@ -857,6 +896,9 @@ async function main() { await testDatabase2(); await clearDatabase(clearTimeout); + + await testDatabase3(); + await clearDatabase(clearTimeout); } catch (e) { console.log(e); process.exit(-1); -- cgit v1.2.3 From a2b72dd3ab4a560d4549aa52912fcb9cd9f275ab Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 22 Feb 2020 13:40:57 -0500 Subject: Update test-dictionary --- test/test-dictionary.js | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) (limited to 'test') diff --git a/test/test-dictionary.js b/test/test-dictionary.js index b9885edc..b157dd5d 100644 --- a/test/test-dictionary.js +++ b/test/test-dictionary.js @@ -3,9 +3,38 @@ const dictionaryValidate = require('./dictionary-validate'); async function main() { - const archive = yomichanTest.createTestDictionaryArchive('valid-dictionary1'); + const dictionaries = [ + {name: 'valid-dictionary1', valid: true}, + {name: 'invalid-dictionary1', valid: false}, + {name: 'invalid-dictionary2', valid: false}, + {name: 'invalid-dictionary3', valid: false}, + {name: 'invalid-dictionary4', valid: false}, + {name: 'invalid-dictionary5', valid: false}, + {name: 'invalid-dictionary6', valid: false} + ]; + const schemas = dictionaryValidate.getSchemas(); - await dictionaryValidate.validateDictionary(archive, schemas); + + for (const {name, valid} of dictionaries) { + const archive = yomichanTest.createTestDictionaryArchive(name); + + let error = null; + try { + await dictionaryValidate.validateDictionary(archive, schemas); + } catch (e) { + error = e; + } + + if (valid) { + if (error !== null) { + throw error; + } + } else { + if (error === null) { + throw new Error(`Expected dictionary ${name} to be invalid`); + } + } + } } -- cgit v1.2.3 From 418e7f9968ba8a6e302ec1e1b6d7dafe4b85fd97 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 22 Feb 2020 14:42:05 -0500 Subject: Update titles type --- test/test-database.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'test') diff --git a/test/test-database.js b/test/test-database.js index 4fb3805d..04ed8100 100644 --- a/test/test-database.js +++ b/test/test-database.js @@ -136,7 +136,9 @@ async function testDatabase1() { const testDictionaryIndex = JSON.parse(await testDictionary.files['index.json'].async('string')); const title = testDictionaryIndex.title; - const titles = [title]; + const titles = new Map([ + [title, {priority: 0, allowSecondarySearches: false}] + ]); // Setup iteration data const iterations = [ @@ -815,7 +817,9 @@ async function testDatabase2() { const testDictionaryIndex = JSON.parse(await testDictionary.files['index.json'].async('string')); const title = testDictionaryIndex.title; - const titles = [title]; + const titles = new Map([ + [title, {priority: 0, allowSecondarySearches: false}] + ]); // Setup database const database = new Database(); -- cgit v1.2.3 From e3cfb3fbc709285e292fe5e8bb68ca8903e6c86b Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Mon, 17 Feb 2020 15:36:58 -0500 Subject: Fix test-schema.js --- test/test-schema.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'test') diff --git a/test/test-schema.js b/test/test-schema.js index ca4f56dd..8ca63167 100644 --- a/test/test-schema.js +++ b/test/test-schema.js @@ -30,7 +30,7 @@ function testValidate1() { ] }; - const schemaValidate = (value, schema) => { + const schemaValidate = (value) => { try { JsonSchema.validate(value, schema); return true; @@ -48,7 +48,7 @@ function testValidate1() { ) && ( ( - (value % 3 )=== 0 || + (value % 3) === 0 || (value % 5) === 0 ) && (value % 15) !== 0 @@ -81,7 +81,7 @@ function testGetValidValueOrDefault1() { const testData = [ [ - void(0), + void 0, {test: 'default'} ], [ @@ -210,7 +210,7 @@ function testGetValidValueOrDefault3() { {test: 'value', test2: 2, test3: 10} ], [ - {test: 'value', test2: 2, test3: void(0)}, + {test: 'value', test2: 2, test3: void 0}, {test: 'value', test2: 2, test3: 10} ] ]; -- cgit v1.2.3 From 6513a15b3b5fd8586226c8823e2680478b97e132 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 22 Feb 2020 14:54:57 -0500 Subject: Fix shadowing --- test/test-database.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'test') diff --git a/test/test-database.js b/test/test-database.js index 04ed8100..44f409dd 100644 --- a/test/test-database.js +++ b/test/test-database.js @@ -36,8 +36,8 @@ class XMLHttpRequest { callbacks.push(callback); } - open(action, url) { - this._url = url; + open(action, url2) { + this._url = url2; } send() { @@ -116,10 +116,10 @@ function clearDatabase(timeout) { (async () => { const indexedDB = global.indexedDB; for (const {name} of await indexedDB.databases()) { - await new Promise((resolve, reject) => { + await new Promise((resolve2, reject2) => { const request = indexedDB.deleteDatabase(name); - request.onerror = (e) => reject(e); - request.onsuccess = () => resolve(); + request.onerror = (e) => reject2(e); + request.onsuccess = () => resolve2(); }); } clearTimeout(timer); -- cgit v1.2.3 From 0f9f2e446dd9c28fa67e4c73a2f573b4c57cfc04 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sun, 23 Feb 2020 11:44:24 -0500 Subject: Clear timer variable --- test/test-database.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'test') diff --git a/test/test-database.js b/test/test-database.js index 44f409dd..add04a03 100644 --- a/test/test-database.js +++ b/test/test-database.js @@ -109,7 +109,8 @@ function countKanjiWithCharacter(kanji, character) { function clearDatabase(timeout) { return new Promise((resolve, reject) => { - const timer = setTimeout(() => { + let timer = setTimeout(() => { + timer = null; reject(new Error(`clearDatabase failed to resolve after ${timeout}ms`)); }, timeout); @@ -122,7 +123,9 @@ function clearDatabase(timeout) { request.onsuccess = () => resolve2(); }); } - clearTimeout(timer); + if (timer !== null) { + clearTimeout(timer); + } resolve(); })(); }); -- cgit v1.2.3 From d87050bbbba6edb64f75e65b614526a3beef9105 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sun, 23 Feb 2020 13:05:48 -0500 Subject: Add missing copyright headers --- test/dictionary-validate.js | 18 ++++++++++++++++++ test/schema-validate.js | 18 ++++++++++++++++++ test/test-database.js | 18 ++++++++++++++++++ test/test-dictionary.js | 18 ++++++++++++++++++ test/test-schema.js | 18 ++++++++++++++++++ test/yomichan-test.js | 18 ++++++++++++++++++ 6 files changed, 108 insertions(+) (limited to 'test') diff --git a/test/dictionary-validate.js b/test/dictionary-validate.js index 25a5de88..14eee2ed 100644 --- a/test/dictionary-validate.js +++ b/test/dictionary-validate.js @@ -1,3 +1,21 @@ +/* + * Copyright (C) 2020 Alex Yatskov + * Author: Alex Yatskov + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + const fs = require('fs'); const path = require('path'); const yomichanTest = require('./yomichan-test'); diff --git a/test/schema-validate.js b/test/schema-validate.js index 1271a611..a4f2d94c 100644 --- a/test/schema-validate.js +++ b/test/schema-validate.js @@ -1,3 +1,21 @@ +/* + * Copyright (C) 2020 Alex Yatskov + * Author: Alex Yatskov + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + const fs = require('fs'); const yomichanTest = require('./yomichan-test'); diff --git a/test/test-database.js b/test/test-database.js index add04a03..c2317881 100644 --- a/test/test-database.js +++ b/test/test-database.js @@ -1,3 +1,21 @@ +/* + * Copyright (C) 2020 Alex Yatskov + * Author: Alex Yatskov + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + const fs = require('fs'); const url = require('url'); const path = require('path'); diff --git a/test/test-dictionary.js b/test/test-dictionary.js index b157dd5d..74f9e62b 100644 --- a/test/test-dictionary.js +++ b/test/test-dictionary.js @@ -1,3 +1,21 @@ +/* + * Copyright (C) 2020 Alex Yatskov + * Author: Alex Yatskov + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + const yomichanTest = require('./yomichan-test'); const dictionaryValidate = require('./dictionary-validate'); diff --git a/test/test-schema.js b/test/test-schema.js index 8ca63167..f4612f86 100644 --- a/test/test-schema.js +++ b/test/test-schema.js @@ -1,3 +1,21 @@ +/* + * Copyright (C) 2020 Alex Yatskov + * Author: Alex Yatskov + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + const assert = require('assert'); const yomichanTest = require('./yomichan-test'); diff --git a/test/yomichan-test.js b/test/yomichan-test.js index 939e0ad2..78bfb9c6 100644 --- a/test/yomichan-test.js +++ b/test/yomichan-test.js @@ -1,3 +1,21 @@ +/* + * Copyright (C) 2020 Alex Yatskov + * Author: Alex Yatskov + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + const fs = require('fs'); const path = require('path'); -- cgit v1.2.3