foreign-tables.test.ts313 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 foreign tables', async ({ executeQuery }) => { |
| 29 | const { sql: listSql, zod: listZod } = pgMeta.foreignTables.list() |
| 30 | const tables = listZod.parse(await executeQuery(listSql)) |
| 31 | const foreignTable = tables.find(({ name }) => name === 'foreign_table') |
| 32 | expect(foreignTable).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": "16451.1", |
| 46 | "identity_generation": null, |
| 47 | "is_generated": false, |
| 48 | "is_identity": false, |
| 49 | "is_nullable": false, |
| 50 | "is_unique": false, |
| 51 | "is_updatable": true, |
| 52 | "name": "id", |
| 53 | "ordinal_position": 1, |
| 54 | "schema": "public", |
| 55 | "table": "foreign_table", |
| 56 | "table_id": 16451, |
| 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": "16451.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": "name", |
| 74 | "ordinal_position": 2, |
| 75 | "schema": "public", |
| 76 | "table": "foreign_table", |
| 77 | "table_id": 16451, |
| 78 | }, |
| 79 | { |
| 80 | "check": null, |
| 81 | "comment": null, |
| 82 | "data_type": "USER-DEFINED", |
| 83 | "default_value": null, |
| 84 | "enums": [ |
| 85 | "ACTIVE", |
| 86 | "INACTIVE", |
| 87 | ], |
| 88 | "format": "user_status", |
| 89 | "format_schema": "public", |
| 90 | "id": "16451.3", |
| 91 | "identity_generation": null, |
| 92 | "is_generated": false, |
| 93 | "is_identity": false, |
| 94 | "is_nullable": true, |
| 95 | "is_unique": false, |
| 96 | "is_updatable": true, |
| 97 | "name": "status", |
| 98 | "ordinal_position": 3, |
| 99 | "schema": "public", |
| 100 | "table": "foreign_table", |
| 101 | "table_id": 16451, |
| 102 | }, |
| 103 | ], |
| 104 | "comment": null, |
| 105 | "foreign_data_wrapper_handler": "postgres_fdw_handler", |
| 106 | "foreign_data_wrapper_name": "postgres_fdw", |
| 107 | "foreign_server_name": "foreign_server", |
| 108 | "id": Any<Number>, |
| 109 | "name": "foreign_table", |
| 110 | "schema": "public", |
| 111 | } |
| 112 | ` |
| 113 | ) |
| 114 | }) |
| 115 | |
| 116 | withTestDatabase('list foreign tables without columns', async ({ executeQuery }) => { |
| 117 | const { sql: listSql, zod: listZod } = pgMeta.foreignTables.list({ includeColumns: false }) |
| 118 | const tables = listZod.parse(await executeQuery(listSql)) |
| 119 | const foreignTable = tables.find(({ name }) => name === 'foreign_table') |
| 120 | expect(foreignTable).toMatchInlineSnapshot( |
| 121 | { id: expect.any(Number) }, |
| 122 | ` |
| 123 | { |
| 124 | "comment": null, |
| 125 | "foreign_data_wrapper_handler": "postgres_fdw_handler", |
| 126 | "foreign_data_wrapper_name": "postgres_fdw", |
| 127 | "foreign_server_name": "foreign_server", |
| 128 | "id": Any<Number>, |
| 129 | "name": "foreign_table", |
| 130 | "schema": "public", |
| 131 | } |
| 132 | ` |
| 133 | ) |
| 134 | }) |
| 135 | |
| 136 | withTestDatabase('retrieve foreign table by name', async ({ executeQuery }) => { |
| 137 | const { sql: retrieveSql, zod: retrieveZod } = pgMeta.foreignTables.retrieve({ |
| 138 | name: 'foreign_table', |
| 139 | schema: 'public', |
| 140 | }) |
| 141 | const table = retrieveZod.parse((await executeQuery(retrieveSql))[0]) |
| 142 | expect(table).toMatchInlineSnapshot( |
| 143 | { id: expect.any(Number) }, |
| 144 | ` |
| 145 | { |
| 146 | "columns": [ |
| 147 | { |
| 148 | "check": null, |
| 149 | "comment": null, |
| 150 | "data_type": "bigint", |
| 151 | "default_value": null, |
| 152 | "enums": [], |
| 153 | "format": "int8", |
| 154 | "format_schema": "pg_catalog", |
| 155 | "id": "16451.1", |
| 156 | "identity_generation": null, |
| 157 | "is_generated": false, |
| 158 | "is_identity": false, |
| 159 | "is_nullable": false, |
| 160 | "is_unique": false, |
| 161 | "is_updatable": true, |
| 162 | "name": "id", |
| 163 | "ordinal_position": 1, |
| 164 | "schema": "public", |
| 165 | "table": "foreign_table", |
| 166 | "table_id": 16451, |
| 167 | }, |
| 168 | { |
| 169 | "check": null, |
| 170 | "comment": null, |
| 171 | "data_type": "text", |
| 172 | "default_value": null, |
| 173 | "enums": [], |
| 174 | "format": "text", |
| 175 | "format_schema": "pg_catalog", |
| 176 | "id": "16451.2", |
| 177 | "identity_generation": null, |
| 178 | "is_generated": false, |
| 179 | "is_identity": false, |
| 180 | "is_nullable": true, |
| 181 | "is_unique": false, |
| 182 | "is_updatable": true, |
| 183 | "name": "name", |
| 184 | "ordinal_position": 2, |
| 185 | "schema": "public", |
| 186 | "table": "foreign_table", |
| 187 | "table_id": 16451, |
| 188 | }, |
| 189 | { |
| 190 | "check": null, |
| 191 | "comment": null, |
| 192 | "data_type": "USER-DEFINED", |
| 193 | "default_value": null, |
| 194 | "enums": [ |
| 195 | "ACTIVE", |
| 196 | "INACTIVE", |
| 197 | ], |
| 198 | "format": "user_status", |
| 199 | "format_schema": "public", |
| 200 | "id": "16451.3", |
| 201 | "identity_generation": null, |
| 202 | "is_generated": false, |
| 203 | "is_identity": false, |
| 204 | "is_nullable": true, |
| 205 | "is_unique": false, |
| 206 | "is_updatable": true, |
| 207 | "name": "status", |
| 208 | "ordinal_position": 3, |
| 209 | "schema": "public", |
| 210 | "table": "foreign_table", |
| 211 | "table_id": 16451, |
| 212 | }, |
| 213 | ], |
| 214 | "comment": null, |
| 215 | "foreign_data_wrapper_handler": "postgres_fdw_handler", |
| 216 | "foreign_data_wrapper_name": "postgres_fdw", |
| 217 | "foreign_server_name": "foreign_server", |
| 218 | "id": Any<Number>, |
| 219 | "name": "foreign_table", |
| 220 | "schema": "public", |
| 221 | } |
| 222 | ` |
| 223 | ) |
| 224 | }) |
| 225 | |
| 226 | withTestDatabase('retrieve foreign table by id', async ({ executeQuery }) => { |
| 227 | const { sql: retrieveSql, zod: retrieveZod } = pgMeta.foreignTables.retrieve({ |
| 228 | id: 16451, |
| 229 | }) |
| 230 | const table = retrieveZod.parse((await executeQuery(retrieveSql))[0]) |
| 231 | expect(table).toMatchInlineSnapshot( |
| 232 | { id: expect.any(Number) }, |
| 233 | ` |
| 234 | { |
| 235 | "columns": [ |
| 236 | { |
| 237 | "check": null, |
| 238 | "comment": null, |
| 239 | "data_type": "bigint", |
| 240 | "default_value": null, |
| 241 | "enums": [], |
| 242 | "format": "int8", |
| 243 | "format_schema": "pg_catalog", |
| 244 | "id": "16451.1", |
| 245 | "identity_generation": null, |
| 246 | "is_generated": false, |
| 247 | "is_identity": false, |
| 248 | "is_nullable": false, |
| 249 | "is_unique": false, |
| 250 | "is_updatable": true, |
| 251 | "name": "id", |
| 252 | "ordinal_position": 1, |
| 253 | "schema": "public", |
| 254 | "table": "foreign_table", |
| 255 | "table_id": 16451, |
| 256 | }, |
| 257 | { |
| 258 | "check": null, |
| 259 | "comment": null, |
| 260 | "data_type": "text", |
| 261 | "default_value": null, |
| 262 | "enums": [], |
| 263 | "format": "text", |
| 264 | "format_schema": "pg_catalog", |
| 265 | "id": "16451.2", |
| 266 | "identity_generation": null, |
| 267 | "is_generated": false, |
| 268 | "is_identity": false, |
| 269 | "is_nullable": true, |
| 270 | "is_unique": false, |
| 271 | "is_updatable": true, |
| 272 | "name": "name", |
| 273 | "ordinal_position": 2, |
| 274 | "schema": "public", |
| 275 | "table": "foreign_table", |
| 276 | "table_id": 16451, |
| 277 | }, |
| 278 | { |
| 279 | "check": null, |
| 280 | "comment": null, |
| 281 | "data_type": "USER-DEFINED", |
| 282 | "default_value": null, |
| 283 | "enums": [ |
| 284 | "ACTIVE", |
| 285 | "INACTIVE", |
| 286 | ], |
| 287 | "format": "user_status", |
| 288 | "format_schema": "public", |
| 289 | "id": "16451.3", |
| 290 | "identity_generation": null, |
| 291 | "is_generated": false, |
| 292 | "is_identity": false, |
| 293 | "is_nullable": true, |
| 294 | "is_unique": false, |
| 295 | "is_updatable": true, |
| 296 | "name": "status", |
| 297 | "ordinal_position": 3, |
| 298 | "schema": "public", |
| 299 | "table": "foreign_table", |
| 300 | "table_id": 16451, |
| 301 | }, |
| 302 | ], |
| 303 | "comment": null, |
| 304 | "foreign_data_wrapper_handler": "postgres_fdw_handler", |
| 305 | "foreign_data_wrapper_name": "postgres_fdw", |
| 306 | "foreign_server_name": "foreign_server", |
| 307 | "id": Any<Number>, |
| 308 | "name": "foreign_table", |
| 309 | "schema": "public", |
| 310 | } |
| 311 | ` |
| 312 | ) |
| 313 | }) |