68 lines
2.4 KiB
TypeScript
68 lines
2.4 KiB
TypeScript
import { createDbAdapter, unregisterEngine } from "@suaveplan/api-db-adapter";
|
|
import type { TenantId } from "@suaveplan/api-types/tenant";
|
|
import { afterEach, describe, expect, it } from "@suaveplan/testing/runner";
|
|
import {
|
|
registerSqliteMemoryEngine,
|
|
runSqliteMemoryMigrations,
|
|
type SqliteMigrationsFsReader,
|
|
} from "../../src/index.js";
|
|
|
|
/**
|
|
* Integration test mirroring how Stage-5 `createTestHost` will wire this
|
|
* adapter when the api-host helper lands: register the engine, construct a
|
|
* `DbAdapter` via the same `createDbAdapter` Stage-4 entry point production
|
|
* uses, drive migrations, and exercise the scoped surface.
|
|
*/
|
|
describe("api-db-sqlite-memory integration", () => {
|
|
afterEach(() => unregisterEngine("sqlite-memory"));
|
|
|
|
it('createTestHost resolves engine:"sqlite-memory" to this adapter', {
|
|
timeout: 5000,
|
|
}, async () => {
|
|
registerSqliteMemoryEngine();
|
|
const adapter = await createDbAdapter({
|
|
engine: "sqlite-memory",
|
|
url: ":memory:",
|
|
});
|
|
try {
|
|
expect(adapter.admin.engine).toBe("sqlite-memory");
|
|
} finally {
|
|
await adapter.close();
|
|
}
|
|
});
|
|
|
|
it("end-to-end: migrations + scoped writes + admin reads", {
|
|
timeout: 5000,
|
|
}, async () => {
|
|
registerSqliteMemoryEngine();
|
|
const adapter = await createDbAdapter({
|
|
engine: "sqlite-memory",
|
|
url: ":memory:",
|
|
tenantColumn: "tenant_id",
|
|
});
|
|
try {
|
|
const fs: SqliteMigrationsFsReader = {
|
|
readdir: async () => ["001_init.sql"],
|
|
readFile: async () =>
|
|
"CREATE TABLE posts (id INTEGER PRIMARY KEY, tenant_id TEXT, title TEXT)",
|
|
};
|
|
await runSqliteMemoryMigrations({
|
|
plugin: "posts",
|
|
admin: adapter.admin,
|
|
migrationsDir: "/v",
|
|
fs,
|
|
});
|
|
const acme = adapter.scoped("acme" as TenantId);
|
|
await acme.execute("INSERT INTO posts (title) VALUES (?)", [
|
|
"hello",
|
|
]);
|
|
const rows = await adapter.admin.query<
|
|
{ title: string; tenant_id: string }[]
|
|
>("SELECT title, tenant_id FROM posts");
|
|
expect(rows).toEqual([{ title: "hello", tenant_id: "acme" }]);
|
|
} finally {
|
|
await adapter.close();
|
|
}
|
|
});
|
|
});
|