test: extends coverage for sqlite service

This commit is contained in:
2026-01-17 22:16:11 -03:00
parent 8e53710c90
commit 4c51822bf0

View File

@@ -2,6 +2,7 @@ import { TestBed } from '@angular/core/testing';
import { vi } from 'vitest'; import { vi } from 'vitest';
import { Sqlite } from './sqlite'; import { Sqlite } from './sqlite';
import { WebSqlite } from 'angular-web-sqlite'; import { WebSqlite } from 'angular-web-sqlite';
import { BatchOp } from '../types/sqlite.type';
describe('Sqlite', () => { describe('Sqlite', () => {
let service: Sqlite; let service: Sqlite;
@@ -9,7 +10,9 @@ describe('Sqlite', () => {
beforeEach(() => { beforeEach(() => {
webSqlite = { webSqlite = {
init: vi.fn(), init: vi.fn().mockResolvedValue(undefined),
executeSql: vi.fn().mockResolvedValue({ rows: [] }),
batchSql: vi.fn().mockResolvedValue({ rows: [] }),
}; };
TestBed.configureTestingModule({ TestBed.configureTestingModule({
@@ -21,4 +24,44 @@ describe('Sqlite', () => {
it('should be created', () => { it('should be created', () => {
expect(service).toBeTruthy(); expect(service).toBeTruthy();
}); });
it('should init database', () => {
service.initializeDatabase('test').then(() => expect(webSqlite.init).toHaveBeenCalledTimes(1));
});
it('should call executeSql with the given parameters', () => {
const SQL_MOCK = `INSERT INTO MOCK VALUES ( ?, ? );`;
const PARAMS_MOCK = [1, 'MOCK'];
service
.executeQuery(SQL_MOCK, PARAMS_MOCK)
.then(() =>
expect(webSqlite.executeSql).toHaveBeenCalledExactlyOnceWith(SQL_MOCK, PARAMS_MOCK),
);
});
it('should call batchSql with the given parameters', () => {
const batch: BatchOp[] = [
['DROP TABLE chain;', []],
['DROP TABLE establishment;', []],
];
service
.batchSqlOperations(batch)
.then(() => expect(webSqlite.batchSql).toHaveBeenCalledExactlyOnceWith(batch));
});
it('should drop all existing tables that are not sqlite engine related', () => {
const batchSqlOperationsSpy = vi
.spyOn(service, 'batchSqlOperations')
.mockResolvedValue({ rows: [] });
const TABLES_QUERY_RESULT_MOCK = {
rows: [{ name: 'chain' }, { name: 'establishment' }, { name: 'sqlite_sequence' }],
};
const expectedBatch = TABLES_QUERY_RESULT_MOCK.rows
.filter((r) => !r.name.startsWith('sqlite_'))
.map((table) => [`DROP TABLE ${table.name};`, []]);
webSqlite.executeSql = vi.fn().mockResolvedValue(TABLES_QUERY_RESULT_MOCK);
service
.dropAllTables()
.then(() => expect(batchSqlOperationsSpy).toHaveBeenCalledExactlyOnceWith(expectedBatch));
});
}); });