schemas.test.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { afterAll, beforeAll, expect, test } from 'vitest'
  2. import pgMeta from '../src/index'
  3. import { cleanupRoot, createTestDatabase } from './db/utils'
  4. beforeAll(async () => {
  5. // Any global setup if needed
  6. })
  7. afterAll(async () => {
  8. await cleanupRoot()
  9. })
  10. const withTestDatabase = (
  11. name: string,
  12. fn: (db: Awaited<ReturnType<typeof createTestDatabase>>) => Promise<void>
  13. ) => {
  14. test(name, async () => {
  15. const db = await createTestDatabase()
  16. try {
  17. await fn(db)
  18. } finally {
  19. await db.cleanup()
  20. }
  21. })
  22. }
  23. // Example of refactored tests using the wrapper
  24. withTestDatabase('list with system schemas', async ({ executeQuery }) => {
  25. const { sql, zod } = await pgMeta.schemas.list({ includeSystemSchemas: true })
  26. const res = zod.parse(await executeQuery(sql))
  27. expect(res.find(({ name }) => name === 'pg_catalog')).toMatchInlineSnapshot(
  28. { id: expect.any(Number) },
  29. `
  30. {
  31. "comment": "system catalog schema",
  32. "id": Any<Number>,
  33. "name": "pg_catalog",
  34. "owner": "postgres",
  35. }
  36. `
  37. )
  38. })
  39. withTestDatabase('list without system schemas', async ({ executeQuery }) => {
  40. const { sql, zod } = await pgMeta.schemas.list({ includeSystemSchemas: false })
  41. const rq = await executeQuery<typeof zod._type>(sql)
  42. const res = zod.parse(rq)
  43. expect(res.find(({ name }) => name === 'pg_catalog')).toMatchInlineSnapshot(`undefined`)
  44. expect(res.find(({ name }) => name === 'public')).toMatchInlineSnapshot(
  45. { id: expect.any(Number) },
  46. `
  47. {
  48. "comment": "standard public schema",
  49. "id": Any<Number>,
  50. "name": "public",
  51. "owner": "postgres",
  52. }
  53. `
  54. )
  55. })
  56. withTestDatabase('retrieve, create, update, delete', async ({ executeQuery }) => {
  57. // Create schema
  58. const { sql: createSql } = await pgMeta.schemas.create({ name: 's' })
  59. await executeQuery(createSql)
  60. // Get the created schema
  61. const { sql: retrieveSql, zod } = await pgMeta.schemas.retrieve({ name: 's' })
  62. const createRes = zod.parse((await executeQuery(retrieveSql))[0])
  63. expect(createRes).toMatchInlineSnapshot(
  64. { id: expect.any(Number) },
  65. `
  66. {
  67. "comment": null,
  68. "id": Any<Number>,
  69. "name": "s",
  70. "owner": "postgres",
  71. }
  72. `
  73. )
  74. // Retrieve schema again to verify
  75. const { sql: retrieveSql2, zod: retrieveZod } = await pgMeta.schemas.retrieve({
  76. id: createRes!.id,
  77. })
  78. const retrieveRes = retrieveZod.parse((await executeQuery(retrieveSql2))[0])
  79. expect(retrieveRes).toMatchInlineSnapshot(
  80. { id: expect.any(Number) },
  81. `
  82. {
  83. "comment": null,
  84. "id": Any<Number>,
  85. "name": "s",
  86. "owner": "postgres",
  87. }
  88. `
  89. )
  90. // Update schema
  91. const { sql: updateSql } = await pgMeta.schemas.update(
  92. { id: createRes!.id },
  93. {
  94. name: 'ss',
  95. owner: 'postgres',
  96. }
  97. )
  98. await executeQuery(updateSql)
  99. // Get the updated schema
  100. const { sql: retrieveUpdatedSql, zod: retrieveUpdatedZod } = await pgMeta.schemas.retrieve({
  101. name: 'ss',
  102. })
  103. const updateRes = retrieveUpdatedZod.parse((await executeQuery(retrieveUpdatedSql))[0])
  104. expect(updateRes).toMatchInlineSnapshot(
  105. { id: expect.any(Number) },
  106. `
  107. {
  108. "comment": null,
  109. "id": Any<Number>,
  110. "name": "ss",
  111. "owner": "postgres",
  112. }
  113. `
  114. )
  115. // Delete schema
  116. const { sql: deleteSql } = await pgMeta.schemas.remove({ id: updateRes!.id })
  117. await executeQuery(deleteSql)
  118. // Verify deletion
  119. const { sql: finalRetrieveSql } = await pgMeta.schemas.retrieve({ id: updateRes!.id })
  120. const finalRes = await executeQuery(finalRetrieveSql)
  121. expect(finalRes).toMatchInlineSnapshot(`[]`)
  122. })