Initial Commit
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
import type { SurrealClientView } from "../../src/types/adapter.js";
|
||||
|
||||
/**
|
||||
* In-memory `SurrealClientView` used by unit tests. Records every call.
|
||||
*
|
||||
* Lives under `src/` (not `tests/`) so coverage configs that exclude
|
||||
* `tests/**` still account for usage — but it is intentionally never
|
||||
* referenced by production exports (the root barrel never re-exports it),
|
||||
* so consumers do not see it.
|
||||
*/
|
||||
/**
|
||||
* The adapter rewrites named placeholders into positional `$pN` and passes
|
||||
* params as a positional array. FakeClient reconstructs a named record
|
||||
* keyed by `pN` so unit tests can inspect bound values predictably.
|
||||
*/
|
||||
export class FakeClient implements SurrealClientView {
|
||||
public isConnected = true;
|
||||
public log: Array<{
|
||||
sql: string;
|
||||
params: readonly unknown[];
|
||||
vars: Record<string, unknown>;
|
||||
}> = [];
|
||||
protected readonly queryHandler: (
|
||||
sql: string,
|
||||
vars: Record<string, unknown>
|
||||
) => Record<string, unknown>[];
|
||||
|
||||
public constructor(
|
||||
handler: (
|
||||
sql: string,
|
||||
vars: Record<string, unknown>
|
||||
) => Record<string, unknown>[] = () => []
|
||||
) {
|
||||
this.queryHandler = handler;
|
||||
}
|
||||
|
||||
public async query<T = Record<string, unknown>>(
|
||||
sql: string,
|
||||
params?: readonly unknown[]
|
||||
): Promise<T[]> {
|
||||
const arr = params ?? [];
|
||||
const vars: Record<string, unknown> = {};
|
||||
for (let i = 0; i < arr.length; i++) {
|
||||
vars[`p${i}`] = arr[i];
|
||||
}
|
||||
this.log.push({ sql, params: arr, vars });
|
||||
return this.queryHandler(sql, vars) as T[];
|
||||
}
|
||||
|
||||
public async execute(sql: string): Promise<{ rowsAffected: number }> {
|
||||
this.log.push({ sql, params: [], vars: {} });
|
||||
return { rowsAffected: 0 };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user