diff --git a/web/database/queries/report-queries.test.js b/web/database/queries/report-queries.test.js --- a/web/database/queries/report-queries.test.js +++ b/web/database/queries/report-queries.test.js @@ -1,81 +1,54 @@ // @flow -import initSqlJs from 'sql.js'; +import { getDatabaseModule } from '../db-module.js'; +import { clearSensitiveData } from '../utils/db-utils.js'; -import { - createReportTable, - getAllReports, - removeAllReports, - removeReports, - updateReport, -} from './report-queries.js'; -import { parseMultiStatementSQLiteResult } from '../utils/db-utils.js'; +const FILE_PATH = 'test.sqlite'; describe('Report Store queries', () => { - let db; + let queryExecutor; + let dbModule; beforeAll(async () => { - const SQL = await initSqlJs(); - db = new SQL.Database(); - createReportTable(db); + dbModule = getDatabaseModule(); }); beforeEach(() => { - db.exec(` - INSERT INTO reports VALUES ("1", "{report_content_1"); - INSERT INTO reports VALUES ("2", "{report_content_2}"); - `); + queryExecutor = new dbModule.SQLiteQueryExecutor(FILE_PATH); + queryExecutor.replaceReport({ id: '1', report: '{report_content_1}' }); + queryExecutor.replaceReport({ id: '2', report: '{report_content_2}' }); }); afterEach(() => { - db.exec(`DELETE FROM reports`); - }); - - it('should create table', () => { - const res = db.exec(`PRAGMA table_info(reports);`); - const [parsedRes] = parseMultiStatementSQLiteResult(res); - - expect(parsedRes).toHaveLength(2); - expect(parsedRes).toEqual( - expect.arrayContaining([ - expect.objectContaining({ - name: 'id', - type: 'TEXT', - }), - expect.objectContaining({ - name: 'report', - type: 'TEXT', - }), - ]), - ); + clearSensitiveData(dbModule, FILE_PATH, queryExecutor); }); it('should return all reports', () => { - const reports = getAllReports(db); + const reports = queryExecutor.getAllReports(); expect(reports).toHaveLength(2); }); it('should remove all reports', () => { - removeAllReports(db); - const reports = getAllReports(db); + queryExecutor.removeAllReports(); + const reports = queryExecutor.getAllReports(); expect(reports).toHaveLength(0); }); it('should insert not existing report', () => { const id = '3'; const report = '{some_content}'; - updateReport(db, id, report); + queryExecutor.replaceReport({ id, report }); - const reports = getAllReports(db); + const reports = queryExecutor.getAllReports(); expect(reports).toHaveLength(3); expect(reports).toContainEqual({ id, report }); }); it('should reports with given id', () => { const id = '2'; - removeReports(db, [id]); + queryExecutor.removeReports([id]); - const reports = getAllReports(db); + const reports = queryExecutor.getAllReports(); expect(reports).toHaveLength(1); expect(reports).not.toContain( expect.objectContaining({