From 4c69a6cd43a68421bb0ded9c12a9d10f4e124035 Mon Sep 17 00:00:00 2001 From: smori Date: Fri, 22 Sep 2023 23:26:39 +0900 Subject: [PATCH] [test] database.js --- test/components/database.js | 116 ++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 test/components/database.js diff --git a/test/components/database.js b/test/components/database.js new file mode 100644 index 0000000..2dd0958 --- /dev/null +++ b/test/components/database.js @@ -0,0 +1,116 @@ +const assert = require('assert'); +const describe = require('mocha').describe; +const it = require('mocha').it; + +const Database = require('../../src/components/database'); +const ngram = require('../../src/tokenizer/ngram'); + +/** + * @param {string[]} tokens + */ +const pageData = (tokens) => { + return { + dataForSearch: ngram.createForTokens(tokens, 3), + }; +}; + +/** + * @param {Database} database + * @param {string} localePath + * @param {string} query + */ +const search = (database, localePath, query) => { + return database.search(localePath, ngram.createForSearch(query, 3), 10); +}; + +describe('components', () => { + describe('database', () => { + describe('simple search', () => { + const database = new Database(); + database.add('/', { + 'v-001': pageData(['動物', '犬', '猫']), + 'v-002': pageData(['動物', '鳥', '魚']), + }); + + it('search: 1', () => { + const result = search(database, '/', '犬'); + + assert.deepStrictEqual(result, ['v-001']); + }); + + it('search: 2', () => { + const result = search(database, '/', '魚'); + + assert.deepStrictEqual(result, ['v-002']); + }); + + it('search: 3', () => { + const result = search(database, '/', '動物'); + + assert.deepStrictEqual(result, ['v-001', 'v-002']); + }); + }); + + describe('multiple locale paths', () => { + const database = new Database(); + database.add('/', { + 'v-101': pageData(['日本']), + 'v-102': pageData(['アメリカ']), + }); + database.add('/en/', { + 'v-201': pageData(['Japan']), + 'v-202': pageData(['USA']), + }); + + it('search: 1', () => { + const result = search(database, '/', '日本'); + + assert.deepStrictEqual(result, ['v-101']); + }); + + it('search: 2', () => { + const result = search(database, '/', 'Japan'); + + assert.deepStrictEqual(result, []); + }); + + it('search: 3', () => { + const result = search(database, '/en/', '日本'); + + assert.deepStrictEqual(result, []); + }); + + it('search: 4', () => { + const result = search(database, '/en/', 'Japan'); + + assert.deepStrictEqual(result, ['v-201']); + }); + }); + + describe('matcher', () => { + const database = new Database(); + database.add('/', { + 'v-001': pageData(['123', 'いぬ', 'ねこ']), + 'v-002': pageData(['123', 'イヌ', 'ネコ']), + }); + + it('search: 1', () => { + const result = search(database, '/', '123'); + + assert.deepStrictEqual(result, ['v-001', 'v-002']); + }); + + it('search: 2', () => { + const result = search(database, '/', '123'); + + assert.deepStrictEqual(result, ['v-001', 'v-002']); + }); + + it('search: 3', () => { + const result = search(database, '/', 'ねこ'); + + assert.deepStrictEqual(result, ['v-001', 'v-002']); + }); + }); + }); +});