Skip to content
This repository has been archived by the owner on Feb 13, 2024. It is now read-only.

Commit

Permalink
Improving startup UT
Browse files Browse the repository at this point in the history
  • Loading branch information
Mitsichury committed Oct 18, 2023
1 parent c7183bf commit 1b51e14
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/database/AcebaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ export class AceBaseClient implements Database {
public async connect(databaseName: string, deleteOnStart = false, databasePath: string): Promise<void> {
const options = { storage: { path: databasePath }, logLevel: 'error' } as AceBaseLocalSettings;
const dbName = `${databaseName}.acebase`;
if (deleteOnStart && fs.existsSync(dbName)) {
const fullPath = `${databasePath}/${dbName}`
if (deleteOnStart && fs.existsSync(fullPath)) {
console.log("ACEBASE - Removing previous data...")
await fs.promises.rm(dbName, { recursive: true });
await fs.promises.rm(fullPath, { recursive: true });
}
this.db = new AceBase(databaseName, options);
return new Promise((resolve, reject) => {
Expand Down
52 changes: 52 additions & 0 deletions src/database/__tests__/Database.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,35 @@ describe("Database implementations", () => {
test("acebaseDb", async () => { await shouldAddfiltersOnER20CAdd(acebaseClient); });
});

describe("Should returns the chainIds from all orders in db", () => {
test("inMemoryDb", async () => { await shouldReturnsChainIdSet(inMemoryDatabase); });
test("acebaseDb", async () => { await shouldReturnsChainIdSet(acebaseClient); });
});

describe("Should load chainIds if exists on startup", () => {
test("acebaseDb", async () => {
const indexedOrder = forgeIndexedOrder(addedOn, expiryDate);
indexedOrder.order.chainId = 1
const anotherOrder = forgeIndexedOrderERC20(addedOn, expiryDate);
anotherOrder.hash = "another_hash";

await acebaseClient.addAllOrderERC20({ "another_hash": anotherOrder });
await acebaseClient.addAllOrder({ "hash": indexedOrder });

const newAcebaseClient = new AceBaseClient();
await newAcebaseClient.connect("dbtest", false, '.');
const chainIds = await newAcebaseClient.getAllChainIds();

expect(chainIds).toEqual([1, 5]);
return Promise.resolve();
});
});

describe("Should bachup last checked blocks", () => {
test("inMemoryDb", async () => { await shouldSaveBlock(inMemoryDatabase); });
test("acebaseDb", async () => { await shouldSaveBlock(acebaseClient); });
});

describe("Should add all & get", () => {
describe('OrderErc20', () => {
test("inMemoryDb", async () => { await addAllAndGetOrdersERC20(inMemoryDatabase); });
Expand Down Expand Up @@ -662,6 +691,29 @@ describe("Database implementations", () => {
return Promise.resolve();
}

async function shouldReturnsChainIdSet(db: Database) {
const indexedOrder = forgeIndexedOrder(addedOn, expiryDate);
indexedOrder.order.chainId = 1
const anotherOrder = forgeIndexedOrderERC20(addedOn, expiryDate);
anotherOrder.hash = "another_hash";

await db.addAllOrderERC20({ "another_hash": anotherOrder });
await db.addAllOrder({ "hash": indexedOrder });
const chainIds = await db.getAllChainIds()

expect(chainIds).toEqual([5, 1]);
return Promise.resolve();
}

async function shouldSaveBlock(db: Database) {

await db.setLastCheckedBlock("addr", 5, 18);
const backedUpBlock = await db.getLastCheckedBlock("addr", 5);

expect(backedUpBlock).toEqual(18);
return Promise.resolve();
}

async function shouldDeleteERC20Order(db: Database) {
const indexedOrder = forgeIndexedOrderERC20(addedOn, expiryDate);
await db.addOrderERC20(indexedOrder);
Expand Down

0 comments on commit 1b51e14

Please sign in to comment.