Skip to content

Commit dfcd356

Browse files
committed
test: added more tests
1 parent 084fd7c commit dfcd356

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed

tests/tables.test.ts

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1842,6 +1842,120 @@ describe("tables", () => {
18421842
consoleSpy.mockRestore();
18431843
});
18441844

1845+
test("pg - primary key with serial default should not be optional", () => {
1846+
const testTable = pgTable("test", {
1847+
id: serial("id").primaryKey(),
1848+
name: text().notNull(),
1849+
});
1850+
1851+
const result = createZeroTableBuilder("test", testTable, {
1852+
id: true,
1853+
name: true,
1854+
});
1855+
1856+
const expected = table("test")
1857+
.columns({
1858+
id: number(),
1859+
name: string(),
1860+
})
1861+
.primaryKey("id");
1862+
1863+
expectTableSchemaDeepEqual(result.build()).toEqual(expected.build());
1864+
});
1865+
1866+
test("pg - primary key with uuid defaultRandom should not be optional", () => {
1867+
const testTable = pgTable("test", {
1868+
id: uuid("id").primaryKey().defaultRandom(),
1869+
name: text().notNull(),
1870+
});
1871+
1872+
const result = createZeroTableBuilder("test", testTable, {
1873+
id: true,
1874+
name: true,
1875+
});
1876+
1877+
const expected = table("test")
1878+
.columns({
1879+
id: string(),
1880+
name: string(),
1881+
})
1882+
.primaryKey("id");
1883+
1884+
expectTableSchemaDeepEqual(result.build()).toEqual(expected.build());
1885+
});
1886+
1887+
test("pg - primary key with sql default should not be optional", () => {
1888+
const testTable = pgTable("test", {
1889+
id: uuid("id")
1890+
.primaryKey()
1891+
.default(sql`gen_random_uuid()`),
1892+
name: text().notNull(),
1893+
});
1894+
1895+
const result = createZeroTableBuilder("test", testTable, {
1896+
id: true,
1897+
name: true,
1898+
});
1899+
1900+
const expected = table("test")
1901+
.columns({
1902+
id: string(),
1903+
name: string(),
1904+
})
1905+
.primaryKey("id");
1906+
1907+
expectTableSchemaDeepEqual(result.build()).toEqual(expected.build());
1908+
});
1909+
1910+
test("pg - composite primary key with defaults should not be optional", () => {
1911+
const testTable = pgTable(
1912+
"test",
1913+
{
1914+
tenantId: text("tenant_id").notNull(),
1915+
id: serial("id").notNull(),
1916+
name: text().notNull(),
1917+
},
1918+
(t) => [primaryKey({ columns: [t.tenantId, t.id] })],
1919+
);
1920+
1921+
const result = createZeroTableBuilder("test", testTable, {
1922+
tenantId: true,
1923+
id: true,
1924+
name: true,
1925+
});
1926+
1927+
const expected = table("test")
1928+
.columns({
1929+
tenantId: string().from("tenant_id"),
1930+
id: number(),
1931+
name: string(),
1932+
})
1933+
.primaryKey("tenantId", "id");
1934+
1935+
expectTableSchemaDeepEqual(result.build()).toEqual(expected.build());
1936+
});
1937+
1938+
test("pg - timestamp primary key with defaultNow should not be optional", () => {
1939+
const testTable = pgTable("test", {
1940+
id: timestamp("id").primaryKey().defaultNow(),
1941+
name: text().notNull(),
1942+
});
1943+
1944+
const result = createZeroTableBuilder("test", testTable, {
1945+
id: true,
1946+
name: true,
1947+
});
1948+
1949+
const expected = table("test")
1950+
.columns({
1951+
id: number(),
1952+
name: string(),
1953+
})
1954+
.primaryKey("id");
1955+
1956+
expectTableSchemaDeepEqual(result.build()).toEqual(expected.build());
1957+
});
1958+
18451959
test("pg - no primary key", ({ expect }) => {
18461960
const testTable = pgTable("test", {
18471961
id: text(),

0 commit comments

Comments
 (0)