bot/tests/Database.test.ts
2023-01-28 18:10:38 +09:00

43 lines
1.2 KiB
TypeScript

import Database from '../src/Database'
import sqlite3 from 'sqlite3'
describe('Test Database', () => {
const DBPATH = `${__dirname}/../db/db.sqlite3`
const db = new sqlite3.Database(DBPATH)
const getData = () => {
return new Promise((resolve, reject) => {
db.all('SELECT * FROM statement;', async (err, rows) => {
if (err) reject(err)
resolve(rows)
}).close()
})
}
test('Get rows', () => {
getData().then(async rows =>
expect(await new Database(DBPATH).get()).toEqual(rows)
)
})
test('Insert row', () => {
const db = new Database(DBPATH)
return db.get().then(async data1 => {
db.run('INSERT INTO statement(text) VALUES(?)', ['TEST'], err => {
if (err) throw err
})
const data2 = await db.get()
expect(data1[data1.length - 1]).not.toEqual(data2[data2.length - 1])
})
})
test('Delete row', () => {
const db = new Database(DBPATH)
return db.get().then(async data1 => {
db.run('DELETE FROM statement WHERE text=?;', ['TEST'], err => {
if (err) throw err
})
const data2 = await db.get()
expect(data1[data1.length - 1]).not.toEqual(data2[data2.length - 1])
})
})
})