views.test.ts296 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
28withTestDatabase('list views', async ({ executeQuery }) => {
29 const { sql: listSql, zod: listZod } = pgMeta.views.list()
30 const views = listZod.parse(await executeQuery(listSql))
31 const todosView = views.find(({ name }) => name === 'todos_view')
32 expect(todosView).toMatchInlineSnapshot(
33 { id: expect.any(Number) },
34 `
35 {
36 "columns": [
37 {
38 "check": null,
39 "comment": null,
40 "data_type": "bigint",
41 "default_value": null,
42 "enums": [],
43 "format": "int8",
44 "format_schema": "pg_catalog",
45 "id": "16423.1",
46 "identity_generation": null,
47 "is_generated": false,
48 "is_identity": false,
49 "is_nullable": true,
50 "is_unique": false,
51 "is_updatable": true,
52 "name": "id",
53 "ordinal_position": 1,
54 "schema": "public",
55 "table": "todos_view",
56 "table_id": 16423,
57 },
58 {
59 "check": null,
60 "comment": null,
61 "data_type": "text",
62 "default_value": null,
63 "enums": [],
64 "format": "text",
65 "format_schema": "pg_catalog",
66 "id": "16423.2",
67 "identity_generation": null,
68 "is_generated": false,
69 "is_identity": false,
70 "is_nullable": true,
71 "is_unique": false,
72 "is_updatable": true,
73 "name": "details",
74 "ordinal_position": 2,
75 "schema": "public",
76 "table": "todos_view",
77 "table_id": 16423,
78 },
79 {
80 "check": null,
81 "comment": null,
82 "data_type": "bigint",
83 "default_value": null,
84 "enums": [],
85 "format": "int8",
86 "format_schema": "pg_catalog",
87 "id": "16423.3",
88 "identity_generation": null,
89 "is_generated": false,
90 "is_identity": false,
91 "is_nullable": true,
92 "is_unique": false,
93 "is_updatable": true,
94 "name": "user-id",
95 "ordinal_position": 3,
96 "schema": "public",
97 "table": "todos_view",
98 "table_id": 16423,
99 },
100 ],
101 "comment": null,
102 "id": Any<Number>,
103 "is_updatable": true,
104 "name": "todos_view",
105 "schema": "public",
106 }
107 `
108 )
109})
110
111withTestDatabase('list views without columns', async ({ executeQuery }) => {
112 const { sql: listSql, zod: listZod } = pgMeta.views.list({ includeColumns: false })
113 const views = listZod.parse(await executeQuery(listSql))
114 const todosView = views.find(({ name }) => name === 'todos_view')
115 expect(todosView).toMatchInlineSnapshot(
116 { id: expect.any(Number) },
117 `
118 {
119 "comment": null,
120 "id": Any<Number>,
121 "is_updatable": true,
122 "name": "todos_view",
123 "schema": "public",
124 }
125 `
126 )
127})
128
129withTestDatabase('retrieve view by name', async ({ executeQuery }) => {
130 const { sql: retrieveSql, zod: retrieveZod } = pgMeta.views.retrieve({
131 name: 'todos_view',
132 schema: 'public',
133 })
134 const view = retrieveZod.parse((await executeQuery(retrieveSql))[0])
135 expect(view).toMatchInlineSnapshot(
136 { id: expect.any(Number) },
137 `
138 {
139 "columns": [
140 {
141 "check": null,
142 "comment": null,
143 "data_type": "bigint",
144 "default_value": null,
145 "enums": [],
146 "format": "int8",
147 "format_schema": "pg_catalog",
148 "id": "16423.1",
149 "identity_generation": null,
150 "is_generated": false,
151 "is_identity": false,
152 "is_nullable": true,
153 "is_unique": false,
154 "is_updatable": true,
155 "name": "id",
156 "ordinal_position": 1,
157 "schema": "public",
158 "table": "todos_view",
159 "table_id": 16423,
160 },
161 {
162 "check": null,
163 "comment": null,
164 "data_type": "text",
165 "default_value": null,
166 "enums": [],
167 "format": "text",
168 "format_schema": "pg_catalog",
169 "id": "16423.2",
170 "identity_generation": null,
171 "is_generated": false,
172 "is_identity": false,
173 "is_nullable": true,
174 "is_unique": false,
175 "is_updatable": true,
176 "name": "details",
177 "ordinal_position": 2,
178 "schema": "public",
179 "table": "todos_view",
180 "table_id": 16423,
181 },
182 {
183 "check": null,
184 "comment": null,
185 "data_type": "bigint",
186 "default_value": null,
187 "enums": [],
188 "format": "int8",
189 "format_schema": "pg_catalog",
190 "id": "16423.3",
191 "identity_generation": null,
192 "is_generated": false,
193 "is_identity": false,
194 "is_nullable": true,
195 "is_unique": false,
196 "is_updatable": true,
197 "name": "user-id",
198 "ordinal_position": 3,
199 "schema": "public",
200 "table": "todos_view",
201 "table_id": 16423,
202 },
203 ],
204 "comment": null,
205 "id": Any<Number>,
206 "is_updatable": true,
207 "name": "todos_view",
208 "schema": "public",
209 }
210 `
211 )
212})
213
214withTestDatabase('retrieve view by id', async ({ executeQuery }) => {
215 const { sql: retrieveSql, zod: retrieveZod } = pgMeta.views.retrieve({
216 id: 16423,
217 })
218 const view = retrieveZod.parse((await executeQuery(retrieveSql))[0])
219 expect(view).toMatchInlineSnapshot(
220 { id: expect.any(Number) },
221 `
222 {
223 "columns": [
224 {
225 "check": null,
226 "comment": null,
227 "data_type": "bigint",
228 "default_value": null,
229 "enums": [],
230 "format": "int8",
231 "format_schema": "pg_catalog",
232 "id": "16423.1",
233 "identity_generation": null,
234 "is_generated": false,
235 "is_identity": false,
236 "is_nullable": true,
237 "is_unique": false,
238 "is_updatable": true,
239 "name": "id",
240 "ordinal_position": 1,
241 "schema": "public",
242 "table": "todos_view",
243 "table_id": 16423,
244 },
245 {
246 "check": null,
247 "comment": null,
248 "data_type": "text",
249 "default_value": null,
250 "enums": [],
251 "format": "text",
252 "format_schema": "pg_catalog",
253 "id": "16423.2",
254 "identity_generation": null,
255 "is_generated": false,
256 "is_identity": false,
257 "is_nullable": true,
258 "is_unique": false,
259 "is_updatable": true,
260 "name": "details",
261 "ordinal_position": 2,
262 "schema": "public",
263 "table": "todos_view",
264 "table_id": 16423,
265 },
266 {
267 "check": null,
268 "comment": null,
269 "data_type": "bigint",
270 "default_value": null,
271 "enums": [],
272 "format": "int8",
273 "format_schema": "pg_catalog",
274 "id": "16423.3",
275 "identity_generation": null,
276 "is_generated": false,
277 "is_identity": false,
278 "is_nullable": true,
279 "is_unique": false,
280 "is_updatable": true,
281 "name": "user-id",
282 "ordinal_position": 3,
283 "schema": "public",
284 "table": "todos_view",
285 "table_id": 16423,
286 },
287 ],
288 "comment": null,
289 "id": Any<Number>,
290 "is_updatable": true,
291 "name": "todos_view",
292 "schema": "public",
293 }
294 `
295 )
296})