Skip to content

Commit 4877ac3

Browse files
committed
test: add tests for the reworked config
1 parent 0337126 commit 4877ac3

1 file changed

Lines changed: 98 additions & 0 deletions

File tree

tests/config.test.ts

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import { describe, expect, test } from "vitest";
2+
import { drizzleZeroConfig } from "../src/relations";
3+
import { pgTable, serial, text, primaryKey } from "drizzle-orm/pg-core";
4+
5+
describe("drizzleZeroConfig with explicit table and column configuration", () => {
6+
const users = pgTable("users", {
7+
id: serial("id").primaryKey(),
8+
name: text("name").notNull(),
9+
email: text("email").notNull(),
10+
phone: text("phone"),
11+
});
12+
13+
const posts = pgTable("posts", {
14+
id: serial("id").primaryKey(),
15+
title: text("title").notNull(),
16+
content: text("content"),
17+
userId: serial("user_id").references(() => users.id),
18+
});
19+
20+
const comments = pgTable("comments", {
21+
id: serial("id").primaryKey(),
22+
content: text("content").notNull(),
23+
postId: serial("post_id").references(() => posts.id),
24+
});
25+
26+
const usersToPosts = pgTable(
27+
"users_to_posts",
28+
{
29+
userId: serial("user_id").references(() => users.id),
30+
postId: serial("post_id").references(() => posts.id),
31+
role: text("role", { enum: ["owner", "editor"] }),
32+
},
33+
(t: any) => ({
34+
pk: primaryKey({ columns: [t.userId, t.postId] }),
35+
}),
36+
);
37+
38+
const drizzleSchema = { users, posts, comments, usersToPosts };
39+
40+
test("should include all tables and columns when no config is provided", () => {
41+
const schema = drizzleZeroConfig(drizzleSchema);
42+
43+
// All tables should be present
44+
expect(Object.keys(schema.tables).length).toBe(4);
45+
expect(new Set(Object.keys(schema.tables))).toStrictEqual(
46+
new Set(["users", "posts", "comments", "usersToPosts"]),
47+
);
48+
49+
// All columns should be present
50+
expect(
51+
new Set(Object.keys((schema.tables as any).users.columns)),
52+
).toStrictEqual(new Set(["id", "name", "email", "phone"]));
53+
expect(
54+
new Set(Object.keys((schema.tables as any).posts.columns)),
55+
).toStrictEqual(new Set(["id", "title", "content", "userId"]));
56+
expect(
57+
new Set(Object.keys((schema.tables as any).comments.columns)),
58+
).toStrictEqual(new Set(["id", "content", "postId"]));
59+
expect(
60+
new Set(Object.keys((schema.tables as any).usersToPosts.columns)),
61+
).toStrictEqual(new Set(["userId", "postId", "role"]));
62+
});
63+
64+
test("should handle explicit table and column configurations", () => {
65+
const schema = drizzleZeroConfig(drizzleSchema, {
66+
tables: {
67+
users: {
68+
name: true,
69+
email: false,
70+
// phone is not mentioned, should be excluded
71+
},
72+
usersToPosts: true, // include all columns
73+
posts: false,
74+
// comments table is not mentioned, should be excluded
75+
},
76+
});
77+
78+
// `users` and `usersToPosts` should be in the schema
79+
expect(Object.keys(schema.tables).length).toBe(2);
80+
expect(new Set(Object.keys(schema.tables))).toStrictEqual(
81+
new Set(["users", "usersToPosts"]),
82+
);
83+
84+
// `posts` and `comments` should be excluded
85+
expect((schema.tables as any).posts).toBe(undefined);
86+
expect((schema.tables as any).comments).toBe(undefined);
87+
88+
// `users` table should have `id` (pk) and `name`
89+
expect(
90+
new Set(Object.keys((schema.tables as any).users.columns)),
91+
).toStrictEqual(new Set(["id", "name"]));
92+
93+
// `usersToPosts` table should have all its columns
94+
expect(
95+
new Set(Object.keys((schema.tables as any).usersToPosts.columns)),
96+
).toStrictEqual(new Set(["userId", "postId", "role"]));
97+
});
98+
});

0 commit comments

Comments
 (0)