materialized-views.test.ts 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 materialized views', async ({ executeQuery }) => {
  24. const { sql: listSql, zod: listZod } = pgMeta.materializedViews.list()
  25. const views = listZod.parse(await executeQuery(listSql))
  26. const todosMaterializedView = views.find(({ name }) => name === 'todos_matview')
  27. expect(todosMaterializedView).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": "16431.1",
  41. "identity_generation": null,
  42. "is_generated": false,
  43. "is_identity": false,
  44. "is_nullable": true,
  45. "is_unique": false,
  46. "is_updatable": false,
  47. "name": "id",
  48. "ordinal_position": 1,
  49. "schema": "public",
  50. "table": "todos_matview",
  51. "table_id": 16431,
  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": "16431.2",
  62. "identity_generation": null,
  63. "is_generated": false,
  64. "is_identity": false,
  65. "is_nullable": true,
  66. "is_unique": false,
  67. "is_updatable": false,
  68. "name": "details",
  69. "ordinal_position": 2,
  70. "schema": "public",
  71. "table": "todos_matview",
  72. "table_id": 16431,
  73. },
  74. {
  75. "check": null,
  76. "comment": null,
  77. "data_type": "bigint",
  78. "default_value": null,
  79. "enums": [],
  80. "format": "int8",
  81. "format_schema": "pg_catalog",
  82. "id": "16431.3",
  83. "identity_generation": null,
  84. "is_generated": false,
  85. "is_identity": false,
  86. "is_nullable": true,
  87. "is_unique": false,
  88. "is_updatable": false,
  89. "name": "user-id",
  90. "ordinal_position": 3,
  91. "schema": "public",
  92. "table": "todos_matview",
  93. "table_id": 16431,
  94. },
  95. ],
  96. "comment": null,
  97. "id": Any<Number>,
  98. "is_populated": true,
  99. "name": "todos_matview",
  100. "schema": "public",
  101. }
  102. `
  103. )
  104. })
  105. withTestDatabase('list materialized views without columns', async ({ executeQuery }) => {
  106. const { sql: listSql, zod: listZod } = pgMeta.materializedViews.list({ includeColumns: false })
  107. const views = listZod.parse(await executeQuery(listSql))
  108. const todosMaterializedView = views.find(({ name }) => name === 'todos_matview')
  109. expect(todosMaterializedView).toMatchInlineSnapshot(
  110. { id: expect.any(Number) },
  111. `
  112. {
  113. "comment": null,
  114. "id": Any<Number>,
  115. "is_populated": true,
  116. "name": "todos_matview",
  117. "schema": "public",
  118. }
  119. `
  120. )
  121. })
  122. withTestDatabase('retrieve materialized view by name', async ({ executeQuery }) => {
  123. const { sql: retrieveSql, zod: retrieveZod } = pgMeta.materializedViews.retrieve({
  124. name: 'todos_matview',
  125. schema: 'public',
  126. })
  127. const view = retrieveZod.parse((await executeQuery(retrieveSql))[0])
  128. expect(view).toMatchInlineSnapshot(
  129. { id: expect.any(Number) },
  130. `
  131. {
  132. "columns": [
  133. {
  134. "check": null,
  135. "comment": null,
  136. "data_type": "bigint",
  137. "default_value": null,
  138. "enums": [],
  139. "format": "int8",
  140. "format_schema": "pg_catalog",
  141. "id": "16431.1",
  142. "identity_generation": null,
  143. "is_generated": false,
  144. "is_identity": false,
  145. "is_nullable": true,
  146. "is_unique": false,
  147. "is_updatable": false,
  148. "name": "id",
  149. "ordinal_position": 1,
  150. "schema": "public",
  151. "table": "todos_matview",
  152. "table_id": 16431,
  153. },
  154. {
  155. "check": null,
  156. "comment": null,
  157. "data_type": "text",
  158. "default_value": null,
  159. "enums": [],
  160. "format": "text",
  161. "format_schema": "pg_catalog",
  162. "id": "16431.2",
  163. "identity_generation": null,
  164. "is_generated": false,
  165. "is_identity": false,
  166. "is_nullable": true,
  167. "is_unique": false,
  168. "is_updatable": false,
  169. "name": "details",
  170. "ordinal_position": 2,
  171. "schema": "public",
  172. "table": "todos_matview",
  173. "table_id": 16431,
  174. },
  175. {
  176. "check": null,
  177. "comment": null,
  178. "data_type": "bigint",
  179. "default_value": null,
  180. "enums": [],
  181. "format": "int8",
  182. "format_schema": "pg_catalog",
  183. "id": "16431.3",
  184. "identity_generation": null,
  185. "is_generated": false,
  186. "is_identity": false,
  187. "is_nullable": true,
  188. "is_unique": false,
  189. "is_updatable": false,
  190. "name": "user-id",
  191. "ordinal_position": 3,
  192. "schema": "public",
  193. "table": "todos_matview",
  194. "table_id": 16431,
  195. },
  196. ],
  197. "comment": null,
  198. "id": Any<Number>,
  199. "is_populated": true,
  200. "name": "todos_matview",
  201. "schema": "public",
  202. }
  203. `
  204. )
  205. })
  206. withTestDatabase('retrieve materialized view by id', async ({ executeQuery }) => {
  207. const { sql: retrieveSql, zod: retrieveZod } = pgMeta.materializedViews.retrieve({
  208. id: 16431,
  209. })
  210. const view = retrieveZod.parse((await executeQuery(retrieveSql))[0])
  211. expect(view).toMatchInlineSnapshot(
  212. { id: expect.any(Number) },
  213. `
  214. {
  215. "columns": [
  216. {
  217. "check": null,
  218. "comment": null,
  219. "data_type": "bigint",
  220. "default_value": null,
  221. "enums": [],
  222. "format": "int8",
  223. "format_schema": "pg_catalog",
  224. "id": "16431.1",
  225. "identity_generation": null,
  226. "is_generated": false,
  227. "is_identity": false,
  228. "is_nullable": true,
  229. "is_unique": false,
  230. "is_updatable": false,
  231. "name": "id",
  232. "ordinal_position": 1,
  233. "schema": "public",
  234. "table": "todos_matview",
  235. "table_id": 16431,
  236. },
  237. {
  238. "check": null,
  239. "comment": null,
  240. "data_type": "text",
  241. "default_value": null,
  242. "enums": [],
  243. "format": "text",
  244. "format_schema": "pg_catalog",
  245. "id": "16431.2",
  246. "identity_generation": null,
  247. "is_generated": false,
  248. "is_identity": false,
  249. "is_nullable": true,
  250. "is_unique": false,
  251. "is_updatable": false,
  252. "name": "details",
  253. "ordinal_position": 2,
  254. "schema": "public",
  255. "table": "todos_matview",
  256. "table_id": 16431,
  257. },
  258. {
  259. "check": null,
  260. "comment": null,
  261. "data_type": "bigint",
  262. "default_value": null,
  263. "enums": [],
  264. "format": "int8",
  265. "format_schema": "pg_catalog",
  266. "id": "16431.3",
  267. "identity_generation": null,
  268. "is_generated": false,
  269. "is_identity": false,
  270. "is_nullable": true,
  271. "is_unique": false,
  272. "is_updatable": false,
  273. "name": "user-id",
  274. "ordinal_position": 3,
  275. "schema": "public",
  276. "table": "todos_matview",
  277. "table_id": 16431,
  278. },
  279. ],
  280. "comment": null,
  281. "id": Any<Number>,
  282. "is_populated": true,
  283. "name": "todos_matview",
  284. "schema": "public",
  285. }
  286. `
  287. )
  288. })