| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319 |
- 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()
- }
- })
- }
- const cleanNondet = (res: any) => {
- if (!res.data?.tables) return res
- const tables = res.data.tables.map(({ id, ...rest }: any) => rest)
- return { ...res, data: { ...res.data, tables } }
- }
- withTestDatabase('retrieve, create, update, delete', async ({ executeQuery }) => {
- // Create publication
- const { sql: createSql } = pgMeta.publications.create({
- name: 'a',
- publish_insert: true,
- publish_update: true,
- publish_delete: true,
- publish_truncate: false,
- tables: ['users'],
- })
- await executeQuery(createSql)
- // Retrieve publication
- const { sql: retrieveSql, zod: retrieveZod } = pgMeta.publications.retrieve({ name: 'a' })
- const res = retrieveZod.parse((await executeQuery(retrieveSql))[0])
- expect(cleanNondet({ data: res })).toMatchInlineSnapshot(
- { data: { id: expect.any(Number) } },
- `
- {
- "data": {
- "id": Any<Number>,
- "name": "a",
- "owner": "postgres",
- "publish_delete": true,
- "publish_insert": true,
- "publish_truncate": false,
- "publish_update": true,
- "tables": [
- {
- "name": "users",
- "schema": "public",
- },
- ],
- },
- }
- `
- )
- // Update publication
- const { sql: updateSql } = pgMeta.publications.update(res!.id, {
- name: 'b',
- publish_insert: false,
- tables: [],
- })
- await executeQuery(updateSql)
- // Verify update
- const { sql: retrieveUpdatedSql, zod: retrieveUpdatedZod } = pgMeta.publications.retrieve({
- name: 'b',
- })
- const updatedRes = retrieveUpdatedZod.parse((await executeQuery(retrieveUpdatedSql))[0])
- expect(cleanNondet({ data: updatedRes })).toMatchInlineSnapshot(
- { data: { id: expect.any(Number) } },
- `
- {
- "data": {
- "id": Any<Number>,
- "name": "b",
- "owner": "postgres",
- "publish_delete": true,
- "publish_insert": false,
- "publish_truncate": false,
- "publish_update": true,
- "tables": [],
- },
- }
- `
- )
- // Remove publication
- const { sql: removeSql } = pgMeta.publications.remove(updatedRes!)
- await executeQuery(removeSql)
- // Verify removal
- const { sql: verifyRemoveSql } = pgMeta.publications.retrieve({ id: updatedRes!.id })
- const finalRes = await executeQuery(verifyRemoveSql)
- expect(finalRes).toHaveLength(0)
- })
- withTestDatabase('tables with uppercase', async ({ executeQuery }) => {
- // Create table with uppercase name
- await executeQuery(`
- CREATE TABLE public."T" (
- id SERIAL PRIMARY KEY
- );
- `)
- // Create publication
- const { sql: createSql } = pgMeta.publications.create({
- name: 'pub',
- tables: ['T'],
- })
- await executeQuery(createSql)
- // Verify creation
- const { sql: retrieveSql, zod: retrieveZod } = pgMeta.publications.retrieve({ name: 'pub' })
- let res = retrieveZod.parse((await executeQuery(retrieveSql))[0])
- expect(cleanNondet({ data: res })).toMatchInlineSnapshot(
- { data: { id: expect.any(Number) } },
- `
- {
- "data": {
- "id": Any<Number>,
- "name": "pub",
- "owner": "postgres",
- "publish_delete": false,
- "publish_insert": false,
- "publish_truncate": false,
- "publish_update": false,
- "tables": [
- {
- "name": "T",
- "schema": "public",
- },
- ],
- },
- }
- `
- )
- // Update publication
- const { sql: updateSql } = pgMeta.publications.update(res!.id, {
- tables: ['T'],
- })
- await executeQuery(updateSql)
- // Verify update
- const { sql: retrieveUpdatedSql, zod: retrieveUpdatedZod } = pgMeta.publications.retrieve({
- name: 'pub',
- })
- res = retrieveUpdatedZod.parse((await executeQuery(retrieveUpdatedSql))[0])
- expect(cleanNondet({ data: res })).toMatchInlineSnapshot(
- { data: { id: expect.any(Number) } },
- `
- {
- "data": {
- "id": Any<Number>,
- "name": "pub",
- "owner": "postgres",
- "publish_delete": false,
- "publish_insert": false,
- "publish_truncate": false,
- "publish_update": false,
- "tables": [
- {
- "name": "T",
- "schema": "public",
- },
- ],
- },
- }
- `
- )
- // Remove publication
- const { sql: removeSql } = pgMeta.publications.remove(res!)
- await executeQuery(removeSql)
- })
- withTestDatabase('FOR ALL TABLES', async ({ executeQuery }) => {
- // Create publication
- const { sql: createSql } = pgMeta.publications.create({
- name: 'for_all',
- publish_insert: true,
- publish_update: true,
- publish_delete: true,
- publish_truncate: false,
- })
- await executeQuery(createSql)
- // Retrieve and verify
- const { sql: retrieveSql, zod: retrieveZod } = pgMeta.publications.retrieve({ name: 'for_all' })
- const res = retrieveZod.parse((await executeQuery(retrieveSql))[0])
- expect(cleanNondet({ data: res })).toMatchInlineSnapshot(
- { data: { id: expect.any(Number) } },
- `
- {
- "data": {
- "id": Any<Number>,
- "name": "for_all",
- "owner": "postgres",
- "publish_delete": true,
- "publish_insert": true,
- "publish_truncate": false,
- "publish_update": true,
- "tables": null,
- },
- }
- `
- )
- // Remove publication
- const { sql: removeSql } = pgMeta.publications.remove(res!)
- await executeQuery(removeSql)
- })
- withTestDatabase('update no tables -> all tables', async ({ executeQuery }) => {
- // Create publication
- const { sql: createSql } = pgMeta.publications.create({
- name: 'pub',
- tables: [],
- })
- await executeQuery(createSql)
- // Retrieve created publication
- const { sql: retrieveSql, zod: retrieveZod } = pgMeta.publications.retrieve({ name: 'pub' })
- const res = retrieveZod.parse((await executeQuery(retrieveSql))[0])
- // Update publication
- const { sql: updateSql } = pgMeta.publications.update(res!.id, {
- tables: null,
- })
- await executeQuery(updateSql)
- // Verify update
- const { sql: verifyRetrieveSql, zod: verifyRetrieveZod } = pgMeta.publications.retrieve({
- name: 'pub',
- })
- const updatedRes = verifyRetrieveZod.parse((await executeQuery(verifyRetrieveSql))[0])
- expect(cleanNondet({ data: updatedRes })).toMatchInlineSnapshot(
- { data: { id: expect.any(Number) } },
- `
- {
- "data": {
- "id": Any<Number>,
- "name": "pub",
- "owner": "postgres",
- "publish_delete": false,
- "publish_insert": false,
- "publish_truncate": false,
- "publish_update": false,
- "tables": null,
- },
- }
- `
- )
- // Remove publication
- const { sql: removeSql } = pgMeta.publications.remove(updatedRes!)
- await executeQuery(removeSql)
- })
- withTestDatabase('update all tables -> no tables', async ({ executeQuery }) => {
- // Create publication
- const { sql: createSql } = pgMeta.publications.create({
- name: 'pub',
- tables: null,
- })
- await executeQuery(createSql)
- // Retrieve created publication
- const { sql: retrieveSql, zod: retrieveZod } = pgMeta.publications.retrieve({ name: 'pub' })
- const res = retrieveZod.parse((await executeQuery(retrieveSql))[0])
- // Update publication
- const { sql: updateSql } = pgMeta.publications.update(res!.id, {
- tables: [],
- })
- await executeQuery(updateSql)
- // Verify update
- const { sql: verifyRetrieveSql, zod: verifyRetrieveZod } = pgMeta.publications.retrieve({
- name: 'pub',
- })
- const updatedRes = verifyRetrieveZod.parse((await executeQuery(verifyRetrieveSql))[0])
- expect(cleanNondet({ data: updatedRes })).toMatchInlineSnapshot(
- { data: { id: expect.any(Number) } },
- `
- {
- "data": {
- "id": Any<Number>,
- "name": "pub",
- "owner": "postgres",
- "publish_delete": false,
- "publish_insert": false,
- "publish_truncate": false,
- "publish_update": false,
- "tables": [],
- },
- }
- `
- )
- // Remove publication
- const { sql: removeSql } = pgMeta.publications.remove(updatedRes!)
- await executeQuery(removeSql)
- })
|