Create tests

This commit is contained in:
freearhey 2023-10-07 05:14:00 +03:00
parent d5e243aca4
commit 66ec908b6e
3 changed files with 124 additions and 0 deletions

24
tests/db/export.test.ts Normal file
View file

@ -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'
})
}

25
tests/db/update.test.ts Normal file
View file

@ -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'
})
}

75
tests/db/validate.test.ts Normal file
View file

@ -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)')
}
})
})