From 4c51822bf052a1bab324e68b1924324372319333 Mon Sep 17 00:00:00 2001 From: Gabriel De Los Rios Date: Sat, 17 Jan 2026 22:16:11 -0300 Subject: [PATCH] test: extends coverage for sqlite service --- src/app/services/sqlite.spec.ts | 45 ++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/src/app/services/sqlite.spec.ts b/src/app/services/sqlite.spec.ts index 7487456..50ff2f8 100644 --- a/src/app/services/sqlite.spec.ts +++ b/src/app/services/sqlite.spec.ts @@ -2,6 +2,7 @@ import { TestBed } from '@angular/core/testing'; import { vi } from 'vitest'; import { Sqlite } from './sqlite'; import { WebSqlite } from 'angular-web-sqlite'; +import { BatchOp } from '../types/sqlite.type'; describe('Sqlite', () => { let service: Sqlite; @@ -9,7 +10,9 @@ describe('Sqlite', () => { beforeEach(() => { webSqlite = { - init: vi.fn(), + init: vi.fn().mockResolvedValue(undefined), + executeSql: vi.fn().mockResolvedValue({ rows: [] }), + batchSql: vi.fn().mockResolvedValue({ rows: [] }), }; TestBed.configureTestingModule({ @@ -21,4 +24,44 @@ describe('Sqlite', () => { it('should be created', () => { 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)); + }); });