materialized-views.test.ts296 lines · main
| 1 | import { afterAll, beforeAll, expect, test } from 'vitest' |
| 2 | |
| 3 | import pgMeta from '../src/index' |
| 4 | import { cleanupRoot, createTestDatabase } from './db/utils' |
| 5 | |
| 6 | beforeAll(async () => { |
| 7 | // Any global setup if needed |
| 8 | }) |
| 9 | |
| 10 | afterAll(async () => { |
| 11 | await cleanupRoot() |
| 12 | }) |
| 13 | |
| 14 | const 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 | withTestDatabase('list materialized views', async ({ executeQuery }) => { |
| 29 | const { sql: listSql, zod: listZod } = pgMeta.materializedViews.list() |
| 30 | const views = listZod.parse(await executeQuery(listSql)) |
| 31 | const todosMaterializedView = views.find(({ name }) => name === 'todos_matview') |
| 32 | expect(todosMaterializedView).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": "16431.1", |
| 46 | "identity_generation": null, |
| 47 | "is_generated": false, |
| 48 | "is_identity": false, |
| 49 | "is_nullable": true, |
| 50 | "is_unique": false, |
| 51 | "is_updatable": false, |
| 52 | "name": "id", |
| 53 | "ordinal_position": 1, |
| 54 | "schema": "public", |
| 55 | "table": "todos_matview", |
| 56 | "table_id": 16431, |
| 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": "16431.2", |
| 67 | "identity_generation": null, |
| 68 | "is_generated": false, |
| 69 | "is_identity": false, |
| 70 | "is_nullable": true, |
| 71 | "is_unique": false, |
| 72 | "is_updatable": false, |
| 73 | "name": "details", |
| 74 | "ordinal_position": 2, |
| 75 | "schema": "public", |
| 76 | "table": "todos_matview", |
| 77 | "table_id": 16431, |
| 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": "16431.3", |
| 88 | "identity_generation": null, |
| 89 | "is_generated": false, |
| 90 | "is_identity": false, |
| 91 | "is_nullable": true, |
| 92 | "is_unique": false, |
| 93 | "is_updatable": false, |
| 94 | "name": "user-id", |
| 95 | "ordinal_position": 3, |
| 96 | "schema": "public", |
| 97 | "table": "todos_matview", |
| 98 | "table_id": 16431, |
| 99 | }, |
| 100 | ], |
| 101 | "comment": null, |
| 102 | "id": Any<Number>, |
| 103 | "is_populated": true, |
| 104 | "name": "todos_matview", |
| 105 | "schema": "public", |
| 106 | } |
| 107 | ` |
| 108 | ) |
| 109 | }) |
| 110 | |
| 111 | withTestDatabase('list materialized views without columns', async ({ executeQuery }) => { |
| 112 | const { sql: listSql, zod: listZod } = pgMeta.materializedViews.list({ includeColumns: false }) |
| 113 | const views = listZod.parse(await executeQuery(listSql)) |
| 114 | const todosMaterializedView = views.find(({ name }) => name === 'todos_matview') |
| 115 | expect(todosMaterializedView).toMatchInlineSnapshot( |
| 116 | { id: expect.any(Number) }, |
| 117 | ` |
| 118 | { |
| 119 | "comment": null, |
| 120 | "id": Any<Number>, |
| 121 | "is_populated": true, |
| 122 | "name": "todos_matview", |
| 123 | "schema": "public", |
| 124 | } |
| 125 | ` |
| 126 | ) |
| 127 | }) |
| 128 | |
| 129 | withTestDatabase('retrieve materialized view by name', async ({ executeQuery }) => { |
| 130 | const { sql: retrieveSql, zod: retrieveZod } = pgMeta.materializedViews.retrieve({ |
| 131 | name: 'todos_matview', |
| 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": "16431.1", |
| 149 | "identity_generation": null, |
| 150 | "is_generated": false, |
| 151 | "is_identity": false, |
| 152 | "is_nullable": true, |
| 153 | "is_unique": false, |
| 154 | "is_updatable": false, |
| 155 | "name": "id", |
| 156 | "ordinal_position": 1, |
| 157 | "schema": "public", |
| 158 | "table": "todos_matview", |
| 159 | "table_id": 16431, |
| 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": "16431.2", |
| 170 | "identity_generation": null, |
| 171 | "is_generated": false, |
| 172 | "is_identity": false, |
| 173 | "is_nullable": true, |
| 174 | "is_unique": false, |
| 175 | "is_updatable": false, |
| 176 | "name": "details", |
| 177 | "ordinal_position": 2, |
| 178 | "schema": "public", |
| 179 | "table": "todos_matview", |
| 180 | "table_id": 16431, |
| 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": "16431.3", |
| 191 | "identity_generation": null, |
| 192 | "is_generated": false, |
| 193 | "is_identity": false, |
| 194 | "is_nullable": true, |
| 195 | "is_unique": false, |
| 196 | "is_updatable": false, |
| 197 | "name": "user-id", |
| 198 | "ordinal_position": 3, |
| 199 | "schema": "public", |
| 200 | "table": "todos_matview", |
| 201 | "table_id": 16431, |
| 202 | }, |
| 203 | ], |
| 204 | "comment": null, |
| 205 | "id": Any<Number>, |
| 206 | "is_populated": true, |
| 207 | "name": "todos_matview", |
| 208 | "schema": "public", |
| 209 | } |
| 210 | ` |
| 211 | ) |
| 212 | }) |
| 213 | |
| 214 | withTestDatabase('retrieve materialized view by id', async ({ executeQuery }) => { |
| 215 | const { sql: retrieveSql, zod: retrieveZod } = pgMeta.materializedViews.retrieve({ |
| 216 | id: 16431, |
| 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": "16431.1", |
| 233 | "identity_generation": null, |
| 234 | "is_generated": false, |
| 235 | "is_identity": false, |
| 236 | "is_nullable": true, |
| 237 | "is_unique": false, |
| 238 | "is_updatable": false, |
| 239 | "name": "id", |
| 240 | "ordinal_position": 1, |
| 241 | "schema": "public", |
| 242 | "table": "todos_matview", |
| 243 | "table_id": 16431, |
| 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": "16431.2", |
| 254 | "identity_generation": null, |
| 255 | "is_generated": false, |
| 256 | "is_identity": false, |
| 257 | "is_nullable": true, |
| 258 | "is_unique": false, |
| 259 | "is_updatable": false, |
| 260 | "name": "details", |
| 261 | "ordinal_position": 2, |
| 262 | "schema": "public", |
| 263 | "table": "todos_matview", |
| 264 | "table_id": 16431, |
| 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": "16431.3", |
| 275 | "identity_generation": null, |
| 276 | "is_generated": false, |
| 277 | "is_identity": false, |
| 278 | "is_nullable": true, |
| 279 | "is_unique": false, |
| 280 | "is_updatable": false, |
| 281 | "name": "user-id", |
| 282 | "ordinal_position": 3, |
| 283 | "schema": "public", |
| 284 | "table": "todos_matview", |
| 285 | "table_id": 16431, |
| 286 | }, |
| 287 | ], |
| 288 | "comment": null, |
| 289 | "id": Any<Number>, |
| 290 | "is_populated": true, |
| 291 | "name": "todos_matview", |
| 292 | "schema": "public", |
| 293 | } |
| 294 | ` |
| 295 | ) |
| 296 | }) |