schemas.test.ts137 lines · main
1import { afterAll, beforeAll, expect, test } from 'vitest'
2
3import pgMeta from '../src/index'
4import { cleanupRoot, createTestDatabase } from './db/utils'
5
6beforeAll(async () => {
7 // Any global setup if needed
8})
9
10afterAll(async () => {
11 await cleanupRoot()
12})
13
14const withTestDatabase = (
15 name: string,
16 fn: (db: Awaited<ReturnType<typeof createTestDatabase>>) => Promise<void>
17) => {
18 test(name, async () => {
19 const db = await createTestDatabase()
20 try {
21 await fn(db)
22 } finally {
23 await db.cleanup()
24 }
25 })
26}
27
28// Example of refactored tests using the wrapper
29withTestDatabase('list with system schemas', async ({ executeQuery }) => {
30 const { sql, zod } = await pgMeta.schemas.list({ includeSystemSchemas: true })
31 const res = zod.parse(await executeQuery(sql))
32
33 expect(res.find(({ name }) => name === 'pg_catalog')).toMatchInlineSnapshot(
34 { id: expect.any(Number) },
35 `
36 {
37 "comment": "system catalog schema",
38 "id": Any<Number>,
39 "name": "pg_catalog",
40 "owner": "postgres",
41 }
42 `
43 )
44})
45
46withTestDatabase('list without system schemas', async ({ executeQuery }) => {
47 const { sql, zod } = await pgMeta.schemas.list({ includeSystemSchemas: false })
48 const rq = await executeQuery<typeof zod._type>(sql)
49 const res = zod.parse(rq)
50
51 expect(res.find(({ name }) => name === 'pg_catalog')).toMatchInlineSnapshot(`undefined`)
52 expect(res.find(({ name }) => name === 'public')).toMatchInlineSnapshot(
53 { id: expect.any(Number) },
54 `
55 {
56 "comment": "standard public schema",
57 "id": Any<Number>,
58 "name": "public",
59 "owner": "postgres",
60 }
61 `
62 )
63})
64
65withTestDatabase('retrieve, create, update, delete', async ({ executeQuery }) => {
66 // Create schema
67 const { sql: createSql } = await pgMeta.schemas.create({ name: 's' })
68 await executeQuery(createSql)
69
70 // Get the created schema
71 const { sql: retrieveSql, zod } = await pgMeta.schemas.retrieve({ name: 's' })
72 const createRes = zod.parse((await executeQuery(retrieveSql))[0])
73 expect(createRes).toMatchInlineSnapshot(
74 { id: expect.any(Number) },
75 `
76 {
77 "comment": null,
78 "id": Any<Number>,
79 "name": "s",
80 "owner": "postgres",
81 }
82 `
83 )
84
85 // Retrieve schema again to verify
86 const { sql: retrieveSql2, zod: retrieveZod } = await pgMeta.schemas.retrieve({
87 id: createRes!.id,
88 })
89 const retrieveRes = retrieveZod.parse((await executeQuery(retrieveSql2))[0])
90 expect(retrieveRes).toMatchInlineSnapshot(
91 { id: expect.any(Number) },
92 `
93 {
94 "comment": null,
95 "id": Any<Number>,
96 "name": "s",
97 "owner": "postgres",
98 }
99 `
100 )
101
102 // Update schema
103 const { sql: updateSql } = await pgMeta.schemas.update(
104 { id: createRes!.id },
105 {
106 name: 'ss',
107 owner: 'postgres',
108 }
109 )
110 await executeQuery(updateSql)
111
112 // Get the updated schema
113 const { sql: retrieveUpdatedSql, zod: retrieveUpdatedZod } = await pgMeta.schemas.retrieve({
114 name: 'ss',
115 })
116 const updateRes = retrieveUpdatedZod.parse((await executeQuery(retrieveUpdatedSql))[0])
117 expect(updateRes).toMatchInlineSnapshot(
118 { id: expect.any(Number) },
119 `
120 {
121 "comment": null,
122 "id": Any<Number>,
123 "name": "ss",
124 "owner": "postgres",
125 }
126 `
127 )
128
129 // Delete schema
130 const { sql: deleteSql } = await pgMeta.schemas.remove({ id: updateRes!.id })
131 await executeQuery(deleteSql)
132
133 // Verify deletion
134 const { sql: finalRetrieveSql } = await pgMeta.schemas.retrieve({ id: updateRes!.id })
135 const finalRes = await executeQuery(finalRetrieveSql)
136 expect(finalRes).toMatchInlineSnapshot(`[]`)
137})