Schemas.utils.test.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import { describe, expect, test } from 'vitest'
  2. import { getSchemaAsMarkdown, getTableDefinitionAsMarkdown } from './Schemas.utils'
  3. describe('Schemas.utils', () => {
  4. test('getSchemaAsMarkdown returns properly formatted markdown', () => {
  5. const schema = 'public'
  6. const tables = [
  7. {
  8. ref: 'default',
  9. id: 20999,
  10. name: 'test',
  11. description: 'An excellent description',
  12. schema: 'public',
  13. isForeign: false,
  14. columns: [
  15. {
  16. id: '20999.1',
  17. isPrimary: true,
  18. name: 'id',
  19. format: 'int8',
  20. isNullable: false,
  21. isUnique: false,
  22. isUpdateable: true,
  23. isIdentity: true,
  24. description: '',
  25. },
  26. {
  27. id: '20999.2',
  28. isPrimary: false,
  29. name: 'created_at',
  30. format: 'timestamptz',
  31. isNullable: false,
  32. isUnique: false,
  33. isUpdateable: true,
  34. isIdentity: false,
  35. description: '',
  36. },
  37. {
  38. id: '20999.3',
  39. isPrimary: false,
  40. name: 'user_id',
  41. format: 'uuid',
  42. isNullable: true,
  43. isUnique: false,
  44. isUpdateable: true,
  45. isIdentity: false,
  46. description: '',
  47. },
  48. ],
  49. },
  50. {
  51. ref: 'default',
  52. id: 21049,
  53. name: 'test2',
  54. description: '',
  55. schema: 'public',
  56. isForeign: false,
  57. columns: [
  58. {
  59. id: '21049.1',
  60. isPrimary: true,
  61. name: 'id',
  62. format: 'int8',
  63. isNullable: false,
  64. isUnique: false,
  65. isUpdateable: true,
  66. isIdentity: true,
  67. description: '',
  68. },
  69. {
  70. id: '21049.2',
  71. isPrimary: false,
  72. name: 'created_at',
  73. format: 'timestamptz',
  74. isNullable: false,
  75. isUnique: false,
  76. isUpdateable: true,
  77. isIdentity: false,
  78. description: '',
  79. },
  80. ],
  81. },
  82. {
  83. id: 21009,
  84. ref: 'default',
  85. schema: 'auth',
  86. name: 'auth.users.id',
  87. description: '',
  88. isForeign: true,
  89. columns: [],
  90. },
  91. ]
  92. const result = getSchemaAsMarkdown(schema, tables)
  93. expect(result).toBe(`## Table \`test\`
  94. An excellent description
  95. ### Columns
  96. | Name | Type | Constraints |
  97. |------|------|-------------|
  98. | \`id\` | \`int8\` | Primary Identity |
  99. | \`created_at\` | \`timestamptz\` | |
  100. | \`user_id\` | \`uuid\` | Nullable |
  101. ## Table \`test2\`
  102. ### Columns
  103. | Name | Type | Constraints |
  104. |------|------|-------------|
  105. | \`id\` | \`int8\` | Primary Identity |
  106. | \`created_at\` | \`timestamptz\` | |
  107. `)
  108. })
  109. test('getTableDefinitionAsMarkdown returns properly formatted markdown', () => {
  110. const table = {
  111. ref: 'default',
  112. id: 20999,
  113. name: 'test',
  114. description: 'An excellent description',
  115. schema: 'public',
  116. isForeign: false,
  117. columns: [
  118. {
  119. id: '20999.1',
  120. isPrimary: true,
  121. name: 'id',
  122. format: 'int8',
  123. isNullable: false,
  124. isUnique: false,
  125. isUpdateable: true,
  126. isIdentity: true,
  127. description: '',
  128. },
  129. {
  130. id: '20999.2',
  131. isPrimary: false,
  132. name: 'created_at',
  133. format: 'timestamptz',
  134. isNullable: false,
  135. isUnique: false,
  136. isUpdateable: true,
  137. isIdentity: false,
  138. description: '',
  139. },
  140. {
  141. id: '20999.3',
  142. isPrimary: false,
  143. name: 'user_id',
  144. format: 'uuid',
  145. isNullable: true,
  146. isUnique: false,
  147. isUpdateable: true,
  148. isIdentity: false,
  149. description: '',
  150. },
  151. ],
  152. }
  153. const result = getTableDefinitionAsMarkdown(table)
  154. expect(result).toBe(`## Table \`test\`
  155. An excellent description
  156. ### Columns
  157. | Name | Type | Constraints |
  158. |------|------|-------------|
  159. | \`id\` | \`int8\` | Primary Identity |
  160. | \`created_at\` | \`timestamptz\` | |
  161. | \`user_id\` | \`uuid\` | Nullable |
  162. `)
  163. })
  164. })