From 66ec908b6e3ff54a62716e37f47e2a5a322f5869 Mon Sep 17 00:00:00 2001 From: freearhey <7253922+freearhey@users.noreply.github.com> Date: Sat, 7 Oct 2023 05:14:00 +0300 Subject: [PATCH] Create tests --- tests/db/export.test.ts | 24 +++++++++++++ tests/db/update.test.ts | 25 +++++++++++++ tests/db/validate.test.ts | 75 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 124 insertions(+) create mode 100644 tests/db/export.test.ts create mode 100644 tests/db/update.test.ts create mode 100644 tests/db/validate.test.ts diff --git a/tests/db/export.test.ts b/tests/db/export.test.ts new file mode 100644 index 00000000..e807de52 --- /dev/null +++ b/tests/db/export.test.ts @@ -0,0 +1,24 @@ +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/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')) +}) + +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 new file mode 100644 index 00000000..255bbaeb --- /dev/null +++ b/tests/db/update.test.ts @@ -0,0 +1,25 @@ +import { execSync } from 'child_process' +import * as fs from 'fs-extra' + +beforeEach(() => { + fs.emptyDirSync('tests/__data__/output') + fs.copySync('tests/__data__/input/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(stdout).toEqual( + 'OUTPUT=closes #5871, closes #5901, closes #5701, 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 new file mode 100644 index 00000000..820fc3f5 --- /dev/null +++ b/tests/db/validate.test.ts @@ -0,0 +1,75 @@ +import { execSync } from 'child_process' + +type ExecError = { + status: number + stdout: string +} + +describe('db:validate', () => { + it('shows an error if there is an empty line at the end of the file', () => { + try { + execSync('DATA_DIR=tests/__data__/input/validate/empty_line 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: empty lines at the end of file not allowed (channels.csv)' + ) + } + }) + + 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') + } + }) + + it('shows an error if an invalid value is specified', () => { + 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('1 error(s)') + } + }) +})