Skip to content

Commit

Permalink
test: add unit tests for database
Browse files Browse the repository at this point in the history
  • Loading branch information
liu-zhipeng committed May 30, 2023
1 parent 2cb0fb5 commit 2540428
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
50 changes: 50 additions & 0 deletions packages/adapters/database/test/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ import {
saveStableSwapPoolEvent,
saveStableSwapTransfers,
saveStableSwapLpBalances,
getMessageRootStatusFromIndex,
getAggregateRootByRootAndDomain,
} from "../src/client";

describe("Database client", () => {
Expand Down Expand Up @@ -1136,6 +1138,54 @@ describe("Database client", () => {
expect(queryRes.rows[0].processed).to.eq(false);
});

it("should get getAggregateRootByRootAndDomain", async () => {
const roots: ReceivedAggregateRoot[] = [];
for (let _i = 0; _i < 10; _i++) {
const m = mock.entity.receivedAggregateRoot();
m.domain = mock.domain.A;
roots.push(m);
}

await saveReceivedAggregateRoot(roots, pool);

const receivedAggregatedRoot = await getAggregateRootByRootAndDomain(mock.domain.A, roots[1].root, "ASC", pool);

expect(receivedAggregatedRoot).to.deep.eq(roots[1]);
});

it("should get getMessageRootStatusFromIndex", async () => {
const messages: RootMessage[] = [];
const roots: AggregatedRoot[] = [];
const totalCount = 100;
const processedCount = 10;
const aggregatedCount = 20;
for (let _i = 0; _i < totalCount; _i++) {
const rootMessage = mock.entity.rootMessage();
rootMessage.spokeDomain = mock.domain.A;
rootMessage.count = _i;
rootMessage.processed = _i < processedCount;
messages.push(rootMessage);

const m = mock.entity.aggregatedRoot();
m.index = _i;
m.domain = mock.domain.A;
m.receivedRoot = _i < aggregatedCount ? rootMessage.root : getRandomBytes32();
roots.push(m);
}

await saveSentRootMessages(messages, pool);
await saveAggregatedRoots(roots, pool);

const messageStatus = await getMessageRootStatusFromIndex(mock.domain.A, 0, pool);

expect(messageStatus).to.deep.eq({
aggregatedCount: aggregatedCount,
lastAggregatedRoot: roots[aggregatedCount - 1].id,
processedCount: processedCount,
unprocessedCount: totalCount - processedCount,
});
});

it("should save assets", async () => {
const assets = [mock.entity.asset(), mock.entity.asset(), mock.entity.asset()];
await saveAssets(assets, pool);
Expand Down
6 changes: 3 additions & 3 deletions packages/utils/src/types/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -439,9 +439,9 @@ export const convertFromDbReceivedAggregateRoot = (message: any): ReceivedAggreg
*/
export const convertFromDbRootStatus = (status: any): RootMessageStatus => {
const obj = {
processedCount: status.processed_count,
unprocessedCount: status.unprocessed_count,
aggregatedCount: status.aggregated_count,
processedCount: +status.processed_count,
unprocessedCount: +status.unprocessed_count,
aggregatedCount: +status.aggregated_count,
lastAggregatedRoot: status.last_aggregated_id ? status.last_aggregated_id.split("-")[0] : undefined,
};
return sanitizeNull(obj);
Expand Down

0 comments on commit 2540428

Please sign in to comment.