extended dataapi tests

This commit is contained in:
dswbx
2025-01-17 05:02:26 +01:00
parent 8a6d8329f3
commit 2a015ba0a1
4 changed files with 64 additions and 9 deletions

View File

@@ -1,6 +1,16 @@
import { describe, expect, it } from "bun:test"; import { afterAll, beforeAll, describe, expect, it } from "bun:test";
import { Guard } from "../../src/auth";
import { parse } from "../../src/core/utils";
import { DataApi } from "../../src/data/api/DataApi"; import { DataApi } from "../../src/data/api/DataApi";
import { DataController } from "../../src/data/api/DataController";
import { dataConfigSchema } from "../../src/data/data-schema";
import * as proto from "../../src/data/prototype";
import { disableConsoleLog, enableConsoleLog, schemaToEm } from "../helper";
beforeAll(disableConsoleLog);
afterAll(enableConsoleLog);
const dataConfig = parse(dataConfigSchema, {});
describe("DataApi", () => { describe("DataApi", () => {
it("should switch to post for long url reads", async () => { it("should switch to post for long url reads", async () => {
const api = new DataApi(); const api = new DataApi();
@@ -13,4 +23,48 @@ describe("DataApi", () => {
expect(post.request.method).toBe("POST"); expect(post.request.method).toBe("POST");
expect(new URL(post.request.url).pathname).toBe(`/api/data/${"a".repeat(1000)}/query`); expect(new URL(post.request.url).pathname).toBe(`/api/data/${"a".repeat(1000)}/query`);
}); });
it("returns result", async () => {
const schema = proto.em({
posts: proto.entity("posts", { title: proto.text() })
});
const em = schemaToEm(schema);
await em.schema().sync({ force: true });
const payload = [{ title: "foo" }, { title: "bar" }, { title: "baz" }];
await em.mutator("posts").insertMany(payload);
const ctx: any = { em, guard: new Guard() };
const controller = new DataController(ctx, dataConfig);
const app = controller.getController();
{
const res = (await app.request("/posts")) as Response;
const { data } = await res.json();
expect(data.length).toEqual(3);
}
// @ts-ignore tests
const api = new DataApi({ basepath: "/", queryLengthLimit: 50 });
// @ts-ignore protected
api.fetcher = app.request as typeof fetch;
{
const req = api.readMany("posts", { select: ["title"] });
expect(req.request.method).toBe("GET");
const res = await req;
expect(res.data).toEqual(payload);
}
{
const req = api.readMany("posts", {
select: ["title"],
limit: 100000,
offset: 0,
sort: "id"
});
expect(req.request.method).toBe("POST");
const res = await req;
expect(res.data).toEqual(payload);
}
});
}); });

View File

@@ -10,16 +10,11 @@ import {
WithBuilder WithBuilder
} from "../../../src/data"; } from "../../../src/data";
import * as proto from "../../../src/data/prototype"; import * as proto from "../../../src/data/prototype";
import { compileQb, prettyPrintQb } from "../../helper"; import { compileQb, prettyPrintQb, schemaToEm } from "../../helper";
import { getDummyConnection } from "../helper"; import { getDummyConnection } from "../helper";
const { dummyConnection } = getDummyConnection(); const { dummyConnection } = getDummyConnection();
function schemaToEm(s: ReturnType<(typeof proto)["em"]>): EntityManager<any> {
const { dummyConnection } = getDummyConnection();
return new EntityManager(Object.values(s.entities), dummyConnection, s.relations, s.indices);
}
describe("[data] WithBuilder", async () => { describe("[data] WithBuilder", async () => {
test("validate withs", async () => { test("validate withs", async () => {
const schema = proto.em( const schema = proto.em(

View File

@@ -2,7 +2,8 @@ import { unlink } from "node:fs/promises";
import type { SelectQueryBuilder, SqliteDatabase } from "kysely"; import type { SelectQueryBuilder, SqliteDatabase } from "kysely";
import Database from "libsql"; import Database from "libsql";
import { format as sqlFormat } from "sql-formatter"; import { format as sqlFormat } from "sql-formatter";
import { SqliteLocalConnection } from "../src/data"; import { type Connection, EntityManager, SqliteLocalConnection } from "../src/data";
import type { em as protoEm } from "../src/data/prototype";
export function getDummyDatabase(memory: boolean = true): { export function getDummyDatabase(memory: boolean = true): {
dummyDb: SqliteDatabase; dummyDb: SqliteDatabase;
@@ -62,3 +63,8 @@ export function prettyPrintQb(qb: SelectQueryBuilder<any, any, any>) {
const { sql, parameters } = qb.compile(); const { sql, parameters } = qb.compile();
console.log("$", sqlFormat(sql), "\n[params]", parameters); console.log("$", sqlFormat(sql), "\n[params]", parameters);
} }
export function schemaToEm(s: ReturnType<typeof protoEm>, conn?: Connection): EntityManager<any> {
const connection = conn ? conn : getDummyConnection().dummyConnection;
return new EntityManager(Object.values(s.entities), connection, s.relations, s.indices);
}

View File

@@ -281,7 +281,7 @@ export class DataController extends Controller {
return c.notFound(); return c.notFound();
} }
const options = (await c.req.valid("json")) as RepoQuery; const options = (await c.req.valid("json")) as RepoQuery;
console.log("options", options); //console.log("options", options);
const result = await this.em.repository(entity).findMany(options); const result = await this.em.repository(entity).findMany(options);
return c.json(this.repoResult(result), { status: result.data ? 200 : 404 }); return c.json(this.repoResult(result), { status: result.data ? 200 : 404 });