-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathtables.test.ts
More file actions
67 lines (53 loc) · 2.03 KB
/
Copy pathtables.test.ts
File metadata and controls
67 lines (53 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { describe, it, expect } from "vitest";
import { orderTables } from "./tables";
import { SortableTable } from "@/lib/table";
// `relationships` holds the ids of the tables this table depends on (its foreign
// keys / parents), so a valid topological order lists dependencies first.
describe("orderTables", () => {
it("orders a dependency chain parent-first", () => {
const tables: SortableTable[] = [
{ tableId: "C", relationships: ["B"] },
{ tableId: "A", relationships: [] },
{ tableId: "B", relationships: ["A"] },
];
const order = orderTables(tables);
expect(order).toHaveLength(3);
expect(order.indexOf("A")).toBeLessThan(order.indexOf("B"));
expect(order.indexOf("B")).toBeLessThan(order.indexOf("C"));
});
it("places a table after every dependency it references", () => {
const tables: SortableTable[] = [
{ tableId: "orders", relationships: ["users", "products"] },
{ tableId: "users", relationships: [] },
{ tableId: "products", relationships: [] },
];
const order = orderTables(tables);
expect(order.indexOf("users")).toBeLessThan(order.indexOf("orders"));
expect(order.indexOf("products")).toBeLessThan(order.indexOf("orders"));
});
it("throws a CircularDependencyError describing the cycle", () => {
const tables: SortableTable[] = [
{ tableId: "A", relationships: ["B"] },
{ tableId: "B", relationships: ["A"] },
];
let caught: unknown;
try {
orderTables(tables);
} catch (error) {
caught = error;
}
expect(caught).toBeDefined();
const err = caught as {
success: boolean;
message: string;
cycle: string[];
};
expect(err.success).toBe(false);
expect(err.message).toBe("Cycle detected");
expect(Array.isArray(err.cycle)).toBe(true);
// the cycle is reported as a closed loop (first node repeated at the end)
expect(err.cycle[0]).toBe(err.cycle[err.cycle.length - 1]);
expect(err.cycle).toContain("A");
expect(err.cycle).toContain("B");
});
});