foreign-tables.test.ts 8.7 KB

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