diff --git a/tests/commands/db/export.test.ts b/tests/commands/db/export.test.ts new file mode 100644 index 00000000..babcce00 --- /dev/null +++ b/tests/commands/db/export.test.ts @@ -0,0 +1,38 @@ +import { execSync } from 'child_process' +import * as fs from 'fs-extra' +import os from 'os' + +beforeEach(() => { + fs.emptyDirSync('tests/__data__/output') +}) + +describe('db:export', () => { + let ENV_VAR = 'DATA_DIR=tests/__data__/input/db/export/data API_DIR=tests/__data__/output/api' + if (os.platform() === 'win32') { + ENV_VAR = + 'SET "DATA_DIR=tests/__data__/input/db/export/data" && SET "API_DIR=tests/__data__/output/api" &&' + } + + it('can export data as json', () => { + const cmd = `${ENV_VAR} npm run db:export` + const stdout = execSync(cmd, { encoding: 'utf8' }) + if (process.env.DEBUG === 'true') console.log(cmd, stdout) + + expect(content('output/api/blocklist.json')).toEqual( + content('expected/db/export/api/blocklist.json') + ) + expect(content('output/api/channels.json')).toEqual( + content('expected/db/export/api/channels.json') + ) + expect(content('output/api/timezones.json')).toEqual( + content('expected/db/export/api/timezones.json') + ) + expect(content('output/api/feeds.json')).toEqual(content('expected/db/export/api/feeds.json')) + }) +}) + +function content(filepath: string) { + return fs.readFileSync(`tests/__data__/${filepath}`, { + encoding: 'utf8' + }) +} diff --git a/tests/commands/db/update.test.ts b/tests/commands/db/update.test.ts new file mode 100644 index 00000000..d048f290 --- /dev/null +++ b/tests/commands/db/update.test.ts @@ -0,0 +1,38 @@ +import { execSync } from 'child_process' +import * as fs from 'fs-extra' +import os from 'os' + +beforeEach(() => { + fs.emptyDirSync('tests/__data__/output') + fs.copySync('tests/__data__/input/db/update/data', 'tests/__data__/output/data') +}) + +describe('db:update', () => { + let ENV_VAR = 'DATA_DIR=tests/__data__/output/data' + if (os.platform() === 'win32') { + ENV_VAR = 'SET "DATA_DIR=tests/__data__/output/data" &&' + } + + it('can update db with data from issues', () => { + const cmd = `${ENV_VAR} npm run db:update --silent` + const stdout = execSync(cmd, { encoding: 'utf8' }) + if (process.env.DEBUG === 'true') console.log(cmd, stdout) + + expect(content('output/data/blocklist.csv')).toEqual( + content('expected/db/update/data/blocklist.csv') + ) + expect(content('output/data/channels.csv')).toEqual( + content('expected/db/update/data/channels.csv') + ) + expect(content('output/data/feeds.csv')).toEqual(content('expected/db/update/data/feeds.csv')) + expect(stdout).toEqual( + 'OUTPUT=closes #6871, closes #5871, closes #7901, closes #17612, closes #5901, closes #5902, closes #5903, closes #5701, closes #8900, closes #5900, closes #5899, closes #5898, closes #5897, closes #5891' + ) + }) +}) + +function content(filepath: string) { + return fs.readFileSync(`tests/__data__/${filepath}`, { + encoding: 'utf8' + }) +} diff --git a/tests/commands/db/validate.test.ts b/tests/commands/db/validate.test.ts new file mode 100644 index 00000000..8ad5ca98 --- /dev/null +++ b/tests/commands/db/validate.test.ts @@ -0,0 +1,122 @@ +import { execSync } from 'child_process' +import os from 'os' + +type ExecError = { + status: number + stdout: string +} + +describe('db:validate', () => { + it('shows an error if the number of columns in a row is incorrect', () => { + let ENV_VAR = 'DATA_DIR=tests/__data__/input/db/validate/wrong_num_cols' + if (os.platform() === 'win32') { + ENV_VAR = 'SET "DATA_DIR=tests/__data__/input/db/validate/wrong_num_cols" &&' + } + const cmd = `${ENV_VAR} npm run db:validate` + try { + const stdout = execSync(cmd, { encoding: 'utf8' }) + if (process.env.DEBUG === 'true') console.log(cmd, stdout) + process.exit(1) + } catch (error) { + if (process.env.DEBUG === 'true') console.log(cmd, (error as ExecError).stdout) + expect((error as ExecError).status).toBe(1) + expect((error as ExecError).stdout).toContain('row has the wrong number of columns') + } + }) + + it('shows an error if one of the lines ends with an invalid character', () => { + let ENV_VAR = 'DATA_DIR=tests/__data__/input/db/validate/invalid_line_ending' + if (os.platform() === 'win32') { + ENV_VAR = 'SET "DATA_DIR=tests/__data__/input/db/validate/invalid_line_ending" &&' + } + const cmd = `${ENV_VAR} npm run db:validate` + try { + const stdout = execSync(cmd, { encoding: 'utf8' }) + if (process.env.DEBUG === 'true') console.log(cmd, stdout) + process.exit(1) + } catch (error) { + if (process.env.DEBUG === 'true') console.log(cmd, (error as ExecError).stdout) + expect((error as ExecError).status).toBe(1) + expect((error as ExecError).stdout).toContain( + 'row has the wrong line ending character, should be CRLF' + ) + } + }) + + it('shows an error if there are duplicates in the file', () => { + let ENV_VAR = 'DATA_DIR=tests/__data__/input/db/validate/duplicate' + if (os.platform() === 'win32') { + ENV_VAR = 'SET "DATA_DIR=tests/__data__/input/db/validate/duplicate" &&' + } + const cmd = `${ENV_VAR} npm run db:validate` + try { + const stdout = execSync(cmd, { encoding: 'utf8' }) + if (process.env.DEBUG === 'true') console.log(cmd, stdout) + process.exit(1) + } catch (error) { + if (process.env.DEBUG === 'true') console.log(cmd, (error as ExecError).stdout) + expect((error as ExecError).status).toBe(1) + expect((error as ExecError).stdout).toContain('category with id "aaa" already exists') + expect((error as ExecError).stdout).toContain( + 'blocklist record with channel "002RadioTV.do" and ref "https://en.wikipedia.org/wiki/Lemurs_of_Madagascar_(book)" already exists' + ) + expect((error as ExecError).stdout).toContain( + 'feed with channel "002RadioTV.do" and id "SD" already exists' + ) + expect((error as ExecError).stdout).toContain('3 error(s)') + } + }) + + it('shows an error if the data contains an error', () => { + let ENV_VAR = 'DATA_DIR=tests/__data__/input/db/validate/invalid_value' + if (os.platform() === 'win32') { + ENV_VAR = 'SET "DATA_DIR=tests/__data__/input/db/validate/invalid_value" &&' + } + const cmd = `${ENV_VAR} npm run db:validate` + try { + const stdout = execSync(cmd, { encoding: 'utf8' }) + if (process.env.DEBUG === 'true') console.log(cmd, stdout) + process.exit(1) + } catch (error) { + if (process.env.DEBUG === 'true') console.log(cmd, (error as ExecError).stdout) + expect((error as ExecError).status).toBe(1) + expect((error as ExecError).stdout).toContain('"aaa.us" is missing from the channels.csv') + expect((error as ExecError).stdout).toContain( + '"002RadioTV.do" has an invalid replaced_by "002RadioTV.do@4K"' + ) + expect((error as ExecError).stdout).toContain('"24B.do" does not have a main feed') + expect((error as ExecError).stdout).toContain( + '002RadioTV.do: "website" must be a valid uri with a scheme matching the http|https pattern' + ) + expect((error as ExecError).stdout).toContain( + '"002RadioTV.do" has an more than one main feed' + ) + expect((error as ExecError).stdout).toContain('"0TV.dk@SD" has the wrong channel "0TV.dk"') + expect((error as ExecError).stdout).toContain( + '"0TV.dk@SD" has the wrong broadcast_area "c/BE"' + ) + expect((error as ExecError).stdout).toContain( + '"0TV.dk@SD" has the wrong timezones "Europe/Copenhagen"' + ) + expect((error as ExecError).stdout).toContain( + '0TV.dk@SD: "video_format" with value "576I" fails to match the required pattern' + ) + expect((error as ExecError).stdout).toContain('10 error(s)') + } + }) + + it('does not show an error if all data are correct', () => { + let ENV_VAR = 'DATA_DIR=tests/__data__/input/db/validate/valid_data' + if (os.platform() === 'win32') { + ENV_VAR = 'SET "DATA_DIR=tests/__data__/input/db/validate/valid_data" &&' + } + const cmd = `${ENV_VAR} npm run db:validate` + try { + const stdout = execSync(cmd, { encoding: 'utf8' }) + if (process.env.DEBUG === 'true') console.log(cmd, stdout) + } catch (error) { + if (process.env.DEBUG === 'true') console.log(cmd, (error as ExecError).stdout) + process.exit(1) + } + }) +}) diff --git a/tests/db/export.test.ts b/tests/db/export.test.ts deleted file mode 100644 index f7534dcd..00000000 --- a/tests/db/export.test.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { execSync } from 'child_process' -import * as fs from 'fs-extra' - -beforeEach(() => { - fs.emptyDirSync('tests/__data__/output') -}) - -it('can export data as json', () => { - execSync( - 'DATA_DIR=tests/__data__/input/export/data API_DIR=tests/__data__/output/api npm run db:export', - { - encoding: 'utf8' - } - ) - - expect(content('output/api/blocklist.json')).toEqual(content('expected/api/blocklist.json')) - expect(content('output/api/channels.json')).toEqual(content('expected/api/channels.json')) - expect(content('output/api/timezones.json')).toEqual(content('expected/api/timezones.json')) - expect(content('output/api/feeds.json')).toEqual(content('expected/api/feeds.json')) -}) - -function content(filepath: string) { - return fs.readFileSync(`tests/__data__/${filepath}`, { - encoding: 'utf8' - }) -} diff --git a/tests/db/update.test.ts b/tests/db/update.test.ts deleted file mode 100644 index 94394166..00000000 --- a/tests/db/update.test.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { execSync } from 'child_process' -import * as fs from 'fs-extra' - -beforeEach(() => { - fs.emptyDirSync('tests/__data__/output') - fs.copySync('tests/__data__/input/update/data', 'tests/__data__/output/data') -}) - -it('can update db with data from issues', () => { - const stdout = execSync('DATA_DIR=tests/__data__/output/data npm run db:update --silent', { - encoding: 'utf8' - }) - - expect(content('output/data/blocklist.csv')).toEqual(content('expected/data/blocklist.csv')) - expect(content('output/data/channels.csv')).toEqual(content('expected/data/channels.csv')) - expect(content('output/data/feeds.csv')).toEqual(content('expected/data/feeds.csv')) - expect(stdout).toEqual( - 'OUTPUT=closes #6871, closes #5871, closes #7901, closes #5901, closes #5902, closes #5903, closes #5701, closes #8900, closes #5900, closes #5899, closes #5898, closes #5897, closes #5891' - ) -}) - -function content(filepath: string) { - return fs.readFileSync(`tests/__data__/${filepath}`, { - encoding: 'utf8' - }) -} diff --git a/tests/db/validate.test.ts b/tests/db/validate.test.ts deleted file mode 100644 index a952be02..00000000 --- a/tests/db/validate.test.ts +++ /dev/null @@ -1,98 +0,0 @@ -import { execSync } from 'child_process' - -type ExecError = { - status: number - stdout: string -} - -describe('db:validate', () => { - it('shows an error if the number of columns in a row is incorrect', () => { - try { - execSync('DATA_DIR=tests/__data__/input/validate/wrong_num_cols npm run db:validate', { - encoding: 'utf8' - }) - process.exit(1) - } catch (error) { - expect((error as ExecError).status).toBe(1) - expect((error as ExecError).stdout).toContain( - 'Error: row 2 has the wrong number of columns (categories.csv)' - ) - } - }) - - it('shows an error if one of the lines ends with an invalid character', () => { - try { - execSync('DATA_DIR=tests/__data__/input/validate/invalid_line_ending npm run db:validate', { - encoding: 'utf8' - }) - process.exit(1) - } catch (error) { - expect((error as ExecError).status).toBe(1) - expect((error as ExecError).stdout).toContain( - 'Error: row 1 has the wrong line ending character, should be CRLF (categories.csv)' - ) - } - }) - - it('shows an error if there are duplicates in the file', () => { - try { - execSync('DATA_DIR=tests/__data__/input/validate/duplicate npm run db:validate', { - encoding: 'utf8' - }) - process.exit(1) - } catch (error) { - expect((error as ExecError).status).toBe(1) - expect((error as ExecError).stdout).toContain('entry with the id "aaa" already exists') - expect((error as ExecError).stdout).toContain( - 'entry with the channel "002RadioTV.do" and ref "eee" already exists' - ) - expect((error as ExecError).stdout).toContain( - 'entry with the channel "002RadioTV.do" and id "SD" already exists' - ) - } - }) - - it('shows an error if the data contains an error', () => { - try { - execSync('DATA_DIR=tests/__data__/input/validate/invalid_value npm run db:validate', { - encoding: 'utf8' - }) - process.exit(1) - } catch (error) { - expect((error as ExecError).status).toBe(1) - expect((error as ExecError).stdout).toContain('"aaa.us" is missing in the channels.csv') - expect((error as ExecError).stdout).toContain( - '"002RadioTV.do" has an invalid replaced_by "002RadioTV.do@4K"' - ) - expect((error as ExecError).stdout).toContain( - '"10Channel.do" channel does not have a main feed' - ) - expect((error as ExecError).stdout).toContain('"24B.do" channel does not have a main feed') - expect((error as ExecError).stdout).toContain( - '002RadioTV.do: "website" must be a valid uri with a scheme matching the http|https pattern' - ) - expect((error as ExecError).stdout).toContain( - 'entry with the channel "002RadioTV.do" and is_main "true" already exists' - ) - expect((error as ExecError).stdout).toContain('"0TV.dk" is missing in the channels.csv') - expect((error as ExecError).stdout).toContain( - '"0TV.dk@SD" has the wrong timezone "Europe/Copenhagen"' - ) - expect((error as ExecError).stdout).toContain( - 'SD: "video_format" with value "576I" fails to match the required pattern' - ) - expect((error as ExecError).stdout).toContain('9 error(s)') - } - }) - - it('does not show an error if all data are correct', () => { - try { - execSync('DATA_DIR=tests/__data__/input/validate/valid_data npm run db:validate', { - encoding: 'utf8' - }) - } catch (error) { - console.log((error as ExecError).stdout) - process.exit(1) - } - }) -})