Skip to content

Commit

Permalink
indexer: add test for sqlite persistence
Browse files Browse the repository at this point in the history
  • Loading branch information
jaipaljadeja committed Jun 30, 2024
1 parent e5eeb7f commit 27d3bde
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
61 changes: 61 additions & 0 deletions packages/indexer/src/plugins/persistence.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import type { Cursor } from "@apibara/protocol";
import { type Database, open } from "sqlite";
import sqlite3 from "sqlite3";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { SqlitePersistence } from "./persistence";

describe("Sqlite Persistence", () => {
let db: Database<sqlite3.Database, sqlite3.Statement>;
let store: SqlitePersistence;

beforeAll(async () => {
db = await open({ driver: sqlite3.Database, filename: ":memory:" });
await SqlitePersistence.initialize(db);
store = new SqlitePersistence(db);
});

afterAll(async () => {
await db.close();
});

it("should store a cursor", async () => {
const cursor: Cursor = {
orderKey: 5_000_000n,
};
await store.put(cursor);
});

it("should get the last cursor", async () => {
const latest = await store.get();

expect(latest).toEqual({
orderKey: 5_000_000n,
uniqueKey: null,
});
});

it("should update last cursor", async () => {
const cursor: Cursor = {
orderKey: 5_000_010n,
uniqueKey: "0x1234567890",
};
await store.put(cursor);
});

it("should get updated cursor", async () => {
const latest = await store.get();

expect(latest).toEqual({
orderKey: 5_000_010n,
uniqueKey: "0x1234567890",
});
});

it("should delete persisted cursor", async () => {
await store.del();

const latest = await store.get();

expect(latest).toBeUndefined();
});
});
2 changes: 1 addition & 1 deletion packages/indexer/src/plugins/persistence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class SqlitePersistence {
id TEXT NOT NULL PRIMARY KEY,
order_key INTEGER NOT NULL,
unique_key TEXT,
PRIMARY KEY (order_key, unique_key)
UNIQUE(order_key, unique_key)
);
`);
}
Expand Down

0 comments on commit 27d3bde

Please sign in to comment.