Skip to content

Commit

Permalink
add testcode
Browse files Browse the repository at this point in the history
  • Loading branch information
carolk-dev committed Dec 24, 2024
1 parent 6f8167c commit 8c198f5
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
4 changes: 2 additions & 2 deletions bridge/src/sqlite3-exchange-history-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
5 changes: 3 additions & 2 deletions bridge/src/sqlite3-monitor-state-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}
Expand Down
17 changes: 17 additions & 0 deletions bridge/test/sqlite3-exchange-history-store.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
});
});
17 changes: 17 additions & 0 deletions bridge/test/sqlite3-monitor-state-store.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
});
});

0 comments on commit 8c198f5

Please sign in to comment.