| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- import { afterAll, beforeAll, expect, test } from 'vitest'
- import pgMeta from '../src/index'
- import { cleanupRoot, createTestDatabase } from './db/utils'
- beforeAll(async () => {
- // Any global setup if needed
- })
- afterAll(async () => {
- await cleanupRoot()
- })
- const withTestDatabase = (
- name: string,
- fn: (db: Awaited<ReturnType<typeof createTestDatabase>>) => Promise<void>
- ) => {
- test(name, async () => {
- const db = await createTestDatabase()
- try {
- await fn(db)
- } finally {
- await db.cleanup()
- }
- })
- }
- withTestDatabase('list foreign tables', async ({ executeQuery }) => {
- const { sql: listSql, zod: listZod } = pgMeta.foreignTables.list()
- const tables = listZod.parse(await executeQuery(listSql))
- const foreignTable = tables.find(({ name }) => name === 'foreign_table')
- expect(foreignTable).toMatchInlineSnapshot(
- { id: expect.any(Number) },
- `
- {
- "columns": [
- {
- "check": null,
- "comment": null,
- "data_type": "bigint",
- "default_value": null,
- "enums": [],
- "format": "int8",
- "format_schema": "pg_catalog",
- "id": "16451.1",
- "identity_generation": null,
- "is_generated": false,
- "is_identity": false,
- "is_nullable": false,
- "is_unique": false,
- "is_updatable": true,
- "name": "id",
- "ordinal_position": 1,
- "schema": "public",
- "table": "foreign_table",
- "table_id": 16451,
- },
- {
- "check": null,
- "comment": null,
- "data_type": "text",
- "default_value": null,
- "enums": [],
- "format": "text",
- "format_schema": "pg_catalog",
- "id": "16451.2",
- "identity_generation": null,
- "is_generated": false,
- "is_identity": false,
- "is_nullable": true,
- "is_unique": false,
- "is_updatable": true,
- "name": "name",
- "ordinal_position": 2,
- "schema": "public",
- "table": "foreign_table",
- "table_id": 16451,
- },
- {
- "check": null,
- "comment": null,
- "data_type": "USER-DEFINED",
- "default_value": null,
- "enums": [
- "ACTIVE",
- "INACTIVE",
- ],
- "format": "user_status",
- "format_schema": "public",
- "id": "16451.3",
- "identity_generation": null,
- "is_generated": false,
- "is_identity": false,
- "is_nullable": true,
- "is_unique": false,
- "is_updatable": true,
- "name": "status",
- "ordinal_position": 3,
- "schema": "public",
- "table": "foreign_table",
- "table_id": 16451,
- },
- ],
- "comment": null,
- "foreign_data_wrapper_handler": "postgres_fdw_handler",
- "foreign_data_wrapper_name": "postgres_fdw",
- "foreign_server_name": "foreign_server",
- "id": Any<Number>,
- "name": "foreign_table",
- "schema": "public",
- }
- `
- )
- })
- withTestDatabase('list foreign tables without columns', async ({ executeQuery }) => {
- const { sql: listSql, zod: listZod } = pgMeta.foreignTables.list({ includeColumns: false })
- const tables = listZod.parse(await executeQuery(listSql))
- const foreignTable = tables.find(({ name }) => name === 'foreign_table')
- expect(foreignTable).toMatchInlineSnapshot(
- { id: expect.any(Number) },
- `
- {
- "comment": null,
- "foreign_data_wrapper_handler": "postgres_fdw_handler",
- "foreign_data_wrapper_name": "postgres_fdw",
- "foreign_server_name": "foreign_server",
- "id": Any<Number>,
- "name": "foreign_table",
- "schema": "public",
- }
- `
- )
- })
- withTestDatabase('retrieve foreign table by name', async ({ executeQuery }) => {
- const { sql: retrieveSql, zod: retrieveZod } = pgMeta.foreignTables.retrieve({
- name: 'foreign_table',
- schema: 'public',
- })
- const table = retrieveZod.parse((await executeQuery(retrieveSql))[0])
- expect(table).toMatchInlineSnapshot(
- { id: expect.any(Number) },
- `
- {
- "columns": [
- {
- "check": null,
- "comment": null,
- "data_type": "bigint",
- "default_value": null,
- "enums": [],
- "format": "int8",
- "format_schema": "pg_catalog",
- "id": "16451.1",
- "identity_generation": null,
- "is_generated": false,
- "is_identity": false,
- "is_nullable": false,
- "is_unique": false,
- "is_updatable": true,
- "name": "id",
- "ordinal_position": 1,
- "schema": "public",
- "table": "foreign_table",
- "table_id": 16451,
- },
- {
- "check": null,
- "comment": null,
- "data_type": "text",
- "default_value": null,
- "enums": [],
- "format": "text",
- "format_schema": "pg_catalog",
- "id": "16451.2",
- "identity_generation": null,
- "is_generated": false,
- "is_identity": false,
- "is_nullable": true,
- "is_unique": false,
- "is_updatable": true,
- "name": "name",
- "ordinal_position": 2,
- "schema": "public",
- "table": "foreign_table",
- "table_id": 16451,
- },
- {
- "check": null,
- "comment": null,
- "data_type": "USER-DEFINED",
- "default_value": null,
- "enums": [
- "ACTIVE",
- "INACTIVE",
- ],
- "format": "user_status",
- "format_schema": "public",
- "id": "16451.3",
- "identity_generation": null,
- "is_generated": false,
- "is_identity": false,
- "is_nullable": true,
- "is_unique": false,
- "is_updatable": true,
- "name": "status",
- "ordinal_position": 3,
- "schema": "public",
- "table": "foreign_table",
- "table_id": 16451,
- },
- ],
- "comment": null,
- "foreign_data_wrapper_handler": "postgres_fdw_handler",
- "foreign_data_wrapper_name": "postgres_fdw",
- "foreign_server_name": "foreign_server",
- "id": Any<Number>,
- "name": "foreign_table",
- "schema": "public",
- }
- `
- )
- })
- withTestDatabase('retrieve foreign table by id', async ({ executeQuery }) => {
- const { sql: retrieveSql, zod: retrieveZod } = pgMeta.foreignTables.retrieve({
- id: 16451,
- })
- const table = retrieveZod.parse((await executeQuery(retrieveSql))[0])
- expect(table).toMatchInlineSnapshot(
- { id: expect.any(Number) },
- `
- {
- "columns": [
- {
- "check": null,
- "comment": null,
- "data_type": "bigint",
- "default_value": null,
- "enums": [],
- "format": "int8",
- "format_schema": "pg_catalog",
- "id": "16451.1",
- "identity_generation": null,
- "is_generated": false,
- "is_identity": false,
- "is_nullable": false,
- "is_unique": false,
- "is_updatable": true,
- "name": "id",
- "ordinal_position": 1,
- "schema": "public",
- "table": "foreign_table",
- "table_id": 16451,
- },
- {
- "check": null,
- "comment": null,
- "data_type": "text",
- "default_value": null,
- "enums": [],
- "format": "text",
- "format_schema": "pg_catalog",
- "id": "16451.2",
- "identity_generation": null,
- "is_generated": false,
- "is_identity": false,
- "is_nullable": true,
- "is_unique": false,
- "is_updatable": true,
- "name": "name",
- "ordinal_position": 2,
- "schema": "public",
- "table": "foreign_table",
- "table_id": 16451,
- },
- {
- "check": null,
- "comment": null,
- "data_type": "USER-DEFINED",
- "default_value": null,
- "enums": [
- "ACTIVE",
- "INACTIVE",
- ],
- "format": "user_status",
- "format_schema": "public",
- "id": "16451.3",
- "identity_generation": null,
- "is_generated": false,
- "is_identity": false,
- "is_nullable": true,
- "is_unique": false,
- "is_updatable": true,
- "name": "status",
- "ordinal_position": 3,
- "schema": "public",
- "table": "foreign_table",
- "table_id": 16451,
- },
- ],
- "comment": null,
- "foreign_data_wrapper_handler": "postgres_fdw_handler",
- "foreign_data_wrapper_name": "postgres_fdw",
- "foreign_server_name": "foreign_server",
- "id": Any<Number>,
- "name": "foreign_table",
- "schema": "public",
- }
- `
- )
- })
|