diff --git a/bridge/src/sqlite3-exchange-history-store.ts b/bridge/src/sqlite3-exchange-history-store.ts index e79a0e4..6ca27af 100644 --- a/bridge/src/sqlite3-exchange-history-store.ts +++ b/bridge/src/sqlite3-exchange-history-store.ts @@ -82,10 +82,10 @@ export class Sqlite3ExchangeHistoryStore implements IExchangeHistoryStore { PRIMARY KEY(network, tx_id) ); CREATE INDEX IF NOT EXISTS exchange_history_idx ON exchange_histories(sender);`; - return new Promise((resolve, error) => { + return new Promise((resolve, reject) => { database.run(CREATE_TABLE_QUERY, (e) => { if (e) { - error(); + reject(e); } else { resolve(); } diff --git a/bridge/src/sqlite3-monitor-state-store.ts b/bridge/src/sqlite3-monitor-state-store.ts index 0115b56..7a86d45 100644 --- a/bridge/src/sqlite3-monitor-state-store.ts +++ b/bridge/src/sqlite3-monitor-state-store.ts @@ -2,6 +2,7 @@ import { IMonitorStateStore } from "./interfaces/monitor-state-store"; import { Database } from "sqlite3"; import { TransactionLocation } from "./types/transaction-location"; import { promisify } from "util"; +import { rejects } from "assert"; export class Sqlite3MonitorStateStore implements IMonitorStateStore { private readonly _database: Database; @@ -24,10 +25,10 @@ export class Sqlite3MonitorStateStore implements IMonitorStateStore { block_hash TEXT NOT NULL, tx_id TEXT )`; - return new Promise((resolve, error) => { + return new Promise((resolve, reject) => { database.run(CREATE_TABLE_QUERY, (e) => { if (e) { - error(); + reject(e); } else { resolve(); } diff --git a/bridge/test/sqlite3-exchange-history-store.spec.ts b/bridge/test/sqlite3-exchange-history-store.spec.ts index 92d0b85..077c78b 100644 --- a/bridge/test/sqlite3-exchange-history-store.spec.ts +++ b/bridge/test/sqlite3-exchange-history-store.spec.ts @@ -4,6 +4,7 @@ import { join } from "path"; import { promises } from "fs"; import { ExchangeHistory } from "../src/interfaces/exchange-history-store"; import { TransactionStatus } from "../src/types/transaction-status"; +import { Database } from "sqlite3"; describe("Sqlite3ExchangeHistoryStore", () => { let store: Sqlite3ExchangeHistoryStore; @@ -165,4 +166,20 @@ describe("Sqlite3ExchangeHistoryStore", () => { expect(() => store.close()).not.toThrowError(); expect(() => store.close()).toThrowError(); }); + + it("should reject when database.run fails", async () => { + // Mock Database class + const mockDatabase = { + run: jest.fn().mockImplementation((query, callback) => { + const error = new Error("SQLITE_ERROR: syntax error"); + callback(error); + }), + } as unknown as Database; + + await expect( + Sqlite3ExchangeHistoryStore["initialize"](mockDatabase) + ).rejects.toThrow("SQLITE_ERROR: syntax error"); + + expect(mockDatabase.run).toHaveBeenCalled(); + }); }); diff --git a/bridge/test/sqlite3-monitor-state-store.spec.ts b/bridge/test/sqlite3-monitor-state-store.spec.ts index 28e9711..3f5eea3 100644 --- a/bridge/test/sqlite3-monitor-state-store.spec.ts +++ b/bridge/test/sqlite3-monitor-state-store.spec.ts @@ -2,6 +2,7 @@ import { Sqlite3MonitorStateStore } from "../src/sqlite3-monitor-state-store"; import { tmpdir } from "os"; import { join } from "path"; import { promises } from "fs"; +import { Database } from "sqlite3"; describe("Sqlite3MonitorStateStore", () => { let stateStore: Sqlite3MonitorStateStore; @@ -61,4 +62,20 @@ describe("Sqlite3MonitorStateStore", () => { expect(() => stateStore.close()).not.toThrowError(); expect(() => stateStore.close()).toThrowError(); }); + + it("should reject when database.run fails", async () => { + // Mock Database class + const mockDatabase = { + run: jest.fn().mockImplementation((query, callback) => { + const error = new Error("SQLITE_ERROR: syntax error"); + callback(error); + }), + } as unknown as Database; + + await expect( + Sqlite3MonitorStateStore["initialize"](mockDatabase) + ).rejects.toThrow("SQLITE_ERROR: syntax error"); + + expect(mockDatabase.run).toHaveBeenCalled(); + }); });