|
| 1 | +import {Table} from "../src/schema"; |
| 2 | +import {integer, serial, timestamp, uuid, varchar} from "../src/types/column-type"; |
| 3 | + |
| 4 | +// Define test interfaces |
| 5 | +interface User { |
| 6 | + id: string; |
| 7 | + name: string; |
| 8 | + email: string; |
| 9 | + age?: number; |
| 10 | + createdAt: Date; |
| 11 | +} |
| 12 | + |
| 13 | +interface Post { |
| 14 | + id: string; |
| 15 | + title: string; |
| 16 | + content?: string; |
| 17 | + authorId: string; |
| 18 | + createdAt: Date; |
| 19 | +} |
| 20 | + |
| 21 | +describe("Schema and Table", () => { |
| 22 | + describe("Table Creation", () => { |
| 23 | + it("should create a table with basic columns", () => { |
| 24 | + const table = new Table<User>("users"); |
| 25 | + table.column("id", uuid()); |
| 26 | + table.column("name", varchar(255)); |
| 27 | + table.column("email", varchar(255)); |
| 28 | + table.column("age", integer()); |
| 29 | + table.column("createdAt", timestamp()).default("now()"); |
| 30 | + const sql = table.createTableSql(); |
| 31 | + expect(sql).toContain("CREATE TABLE IF NOT EXISTS users"); |
| 32 | + expect(sql).toContain('"id" UUID'); |
| 33 | + expect(sql).toContain('"name" VARCHAR(255)'); |
| 34 | + expect(sql).toContain('"email" VARCHAR(255)'); |
| 35 | + expect(sql).toContain('"age" INTEGER'); |
| 36 | + expect(sql).toContain('"createdAt" TIMESTAMP'); |
| 37 | + }); |
| 38 | + |
| 39 | + it("should create a table with primary key", () => { |
| 40 | + const table = new Table<User>("users"); |
| 41 | + table.column("id", uuid()).primaryKey(); |
| 42 | + table.column("name", varchar()); |
| 43 | + |
| 44 | + const sql = table.createTableSql() |
| 45 | + expect(sql).toContain(`UUID PRIMARY KEY NOT NULL`); |
| 46 | + }); |
| 47 | + |
| 48 | + it("should create a table with not null constraints", () => { |
| 49 | + const table = new Table<User>("users"); |
| 50 | + table.column("id", serial()).primaryKey(); |
| 51 | + table.column("name", varchar()).notNull(); |
| 52 | + table.column("email", varchar()).notNull(); |
| 53 | + |
| 54 | + const sql = table.createTableSql(); |
| 55 | + expect(sql).toContain('"name" VARCHAR(255) NOT NULL'); |
| 56 | + expect(sql).toContain('"email" VARCHAR(255) NOT NULL'); |
| 57 | + }); |
| 58 | + |
| 59 | + it("should create a table with unique constraints", () => { |
| 60 | + const table = new Table<User>("users"); |
| 61 | + table.column("id", uuid()).primaryKey(); |
| 62 | + table.column("email", varchar()).unique(); |
| 63 | + |
| 64 | + const sql = table.createTableSql(); |
| 65 | + expect(sql).toContain('"email" VARCHAR(255) UNIQUE'); |
| 66 | + }); |
| 67 | + |
| 68 | + it("should create a table with default values", () => { |
| 69 | + const table = new Table<User>("users"); |
| 70 | + table.column("id", uuid()).primaryKey().$defaultUUID(); |
| 71 | + table.column("createdAt", timestamp()).default("now()"); |
| 72 | + |
| 73 | + const sql = table.createTableSql(); |
| 74 | + expect(sql).toContain('"createdAt" TIMESTAMP DEFAULT now()'); |
| 75 | + }); |
| 76 | + |
| 77 | + it("should create a table with foreign key references", () => { |
| 78 | + const table = new Table<Post>("posts"); |
| 79 | + table.column("id", uuid()).primaryKey().$defaultUUID(); |
| 80 | + table.column("authorId", uuid()).references({table: "users", column: "id", onDelete: 'CASCADE'}); |
| 81 | + |
| 82 | + const sql = table.createTableSql(); |
| 83 | + expect(sql).toContain('"authorId" UUID REFERENCES users(id)'); |
| 84 | + }); |
| 85 | + // |
| 86 | + // it("should create a table with all constraints combined", () => { |
| 87 | + // const table = new Table<User>("users"); |
| 88 | + // table.column("id", "UUID").primaryKey(); |
| 89 | + // table.column("name", "VARCHAR(255)").notNull(); |
| 90 | + // table.column("email", "VARCHAR(255)").unique().notNull(); |
| 91 | + // table.column("age", "INTEGER"); |
| 92 | + // table |
| 93 | + // .column("createdAt", "TIMESTAMP") |
| 94 | + // .default("CURRENT_TIMESTAMP") |
| 95 | + // .notNull(); |
| 96 | + // |
| 97 | + // const sql = table.toString(); |
| 98 | + // expect(sql).toContain('"id" UUID PRIMARY KEY'); |
| 99 | + // expect(sql).toContain('"name" VARCHAR(255) NOT NULL'); |
| 100 | + // expect(sql).toContain('"email" VARCHAR(255) UNIQUE NOT NULL'); |
| 101 | + // expect(sql).toContain('"age" INTEGER'); |
| 102 | + // expect(sql).toContain( |
| 103 | + // '"createdAt" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL' |
| 104 | + // ); |
| 105 | + // }); |
| 106 | + // |
| 107 | + // it("should handle complex table relationships", () => { |
| 108 | + // const usersTable = new Table<User>("users"); |
| 109 | + // usersTable.column("id", "UUID").primaryKey(); |
| 110 | + // usersTable.column("name", "VARCHAR(255)").notNull(); |
| 111 | + // |
| 112 | + // const postsTable = new Table<Post>("posts"); |
| 113 | + // postsTable.column("id", "UUID").primaryKey(); |
| 114 | + // postsTable.column("title", "VARCHAR(255)").notNull(); |
| 115 | + // postsTable.column("content", "TEXT"); |
| 116 | + // postsTable.column("authorId", "UUID").notNull().references("users", "id"); |
| 117 | + // postsTable |
| 118 | + // .column("createdAt", "TIMESTAMP") |
| 119 | + // .default("CURRENT_TIMESTAMP") |
| 120 | + // .notNull(); |
| 121 | + // |
| 122 | + // const postsSql = postsTable.toString(); |
| 123 | + // expect(postsSql).toContain( |
| 124 | + // '"authorId" UUID NOT NULL REFERENCES users(id)' |
| 125 | + // ); |
| 126 | + // expect(postsSql).toContain( |
| 127 | + // '"createdAt" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL' |
| 128 | + // ); |
| 129 | + // }); |
| 130 | + // |
| 131 | + // it("should handle different default value types", () => { |
| 132 | + // const table = new Table<any>("test"); |
| 133 | + // table.column("id", "UUID").primaryKey(); |
| 134 | + // table.column("stringDefault", "VARCHAR(255)").default("default value"); |
| 135 | + // table.column("numberDefault", "INTEGER").default(42); |
| 136 | + // table.column("nullDefault", "VARCHAR(255)").default(null); |
| 137 | + // |
| 138 | + // const sql = table.toString(); |
| 139 | + // expect(sql).toContain( |
| 140 | + // "\"stringDefault\" VARCHAR(255) DEFAULT 'default value'" |
| 141 | + // ); |
| 142 | + // expect(sql).toContain('"numberDefault" INTEGER DEFAULT 42'); |
| 143 | + // expect(sql).toContain('"nullDefault" VARCHAR(255) DEFAULT NULL'); |
| 144 | + // }); |
| 145 | + }); |
| 146 | +}); |
0 commit comments