Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions services/retl-runner/src/source-preflight.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { describe, it, expect, vi } from "vitest";
import { z } from "zod";
import type { WarehouseReader } from "@jitsu/warehouse-query";
import type { ReverseRunConfig } from "@jitsu/warehouse-query/src/runtime";
import {
Expand Down Expand Up @@ -51,4 +52,57 @@ describe("source preflight", () => {
).rejects.toThrow("No audience changes were submitted");
expect(reader.close).toHaveBeenCalledTimes(1);
});
it("accepts composition-only mappings and stages their source columns for replay", async () => {
const reader = source();
const signal = new AbortController().signal;
const composed = {
...config,
options: { ...config.options, mapping: {}, objectMappings: { contact: { email: "email" } } },
};
// An object-only projection exercises the shared contract without requiring a new provider stream.
const project = vi.fn((row: { contact: { email: string } }) =>
projectGoogleAudience("upsert", google.stream.rowType.parse(row.contact))
);
const staged = await prepareSource(
composed,
{
...validation,
projection: { rowType: z.object({ contact: z.object({ email: z.string().email() }) }), project },
},
reader,
signal,
defaultLimits
);
try {
expect(staged.count).toBe(2);
expect(project).toHaveBeenCalledTimes(2);
expect(project).toHaveBeenNthCalledWith(1, { contact: { email: "One@example.com" } });
expect(project).toHaveBeenNthCalledWith(2, { contact: { email: "Two@example.com" } });
const rows: Record<string, unknown>[] = [];
for await (const row of staged.rows(signal)) rows.push(row.row);
expect(rows).toEqual([
{ id: "1", email: "One@example.com" },
{ id: "2", email: "Two@example.com" },
]);
expect(reader.stream).toHaveBeenCalledTimes(1);
expect(reader.close).toHaveBeenCalledTimes(1);
} finally {
await staged.close();
}
});
const emptyObjectMappings: ReverseRunConfig["options"]["objectMappings"][] = [undefined, {}, { contact: {} }];
it.each(emptyObjectMappings)("rejects empty mappings/compositions before reading rows (%#)", async objectMappings => {
const reader = source();
await expect(
prepareSource(
{ ...config, options: { ...config.options, mapping: {}, objectMappings } },
validation,
reader,
new AbortController().signal,
defaultLimits
)
).rejects.toThrow("read 0 rows, saved 0");
expect(reader.stream).not.toHaveBeenCalled();
expect(reader.close).toHaveBeenCalledTimes(1);
});
});
2 changes: 1 addition & 1 deletion services/retl-runner/src/source-preflight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export async function prepareSource(
config.options.mode === "mirror" && !config.model.cursor && !config.model.deleteColumn,
"Preflight requires a full mirror source"
);
ensure(Object.keys(config.options.mapping).length > 0, "Invalid mirror mapping");
ensure(mappedColumns(config.options).length > 0, "Invalid mirror mapping");
index.sql.exec("CREATE TABLE source_rows (sequence INTEGER PRIMARY KEY, key TEXT NOT NULL, row BLOB NOT NULL)");
const put = index.sql.prepare("INSERT INTO source_rows VALUES (?,?,?)");
const columns = new Set([...config.model.primaryKey, ...mappedColumns(config.options)]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,14 @@ it.each([
const rows = await collect(reader.stream(m));
expect(rows.map(row => row.deleted)).toEqual([false, true]);
const preview = await reader.preview(query);
expect(rows.map(row => row.row.removed)).toEqual(preview.rows.map(row => row.removed));
expect(rows[1].checkpoint?.value).toBe(String(preview.rows[1].removed));
// Preview has no ordering guarantee; match by primary key, not UNION ALL branch order.
const previewById = new Map(preview.rows.map(row => [String(row.id), row]));
for (const row of rows) {
const previewRow = previewById.get(String(row.row.id));
expect(previewRow).toBeDefined();
expect(row.row.removed).toBe(previewRow!.removed);
expect(row.checkpoint?.value).toBe(String(previewRow!.removed));
}
expect(await collect(reader.stream(m, rows[0].checkpoint))).toEqual(rows.slice(1));
});
it.each([
Expand Down
Loading