Wrappers.utils.test.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. import { describe, expect, it } from 'vitest'
  2. import { BRIVEN_TARGET_SCHEMA_OPTION, WRAPPER_HANDLERS } from './Wrappers.constants'
  3. import { Table } from './Wrappers.types'
  4. import {
  5. getEditionFormSchema,
  6. getTableFormSchema,
  7. getWrapperCreationFormSchema,
  8. } from './Wrappers.utils'
  9. const stripe_wrapper = {
  10. name: 'stripe_wrapper',
  11. handlerName: WRAPPER_HANDLERS.STRIPE,
  12. validatorName: 'stripe_fdw_validator',
  13. icon: '',
  14. description: 'Payment processing and subscription management',
  15. extensionName: 'StripeFdw',
  16. label: 'Stripe',
  17. docsUrl: '',
  18. server: {
  19. options: [
  20. {
  21. name: 'api_key_id',
  22. label: 'Stripe Secret Key',
  23. required: true,
  24. encrypted: true,
  25. secureEntry: true,
  26. urlHelper: 'https://stripe.com/docs/keys',
  27. },
  28. {
  29. name: 'api_url',
  30. label: 'Stripe API URL',
  31. defaultValue: 'https://api.stripe.com/v1',
  32. required: false,
  33. encrypted: false,
  34. secureEntry: false,
  35. },
  36. BRIVEN_TARGET_SCHEMA_OPTION,
  37. ],
  38. },
  39. tables: [],
  40. }
  41. describe('getWrapperCreationFormSchema', () => {
  42. it('should build a schema that validates tables mode', () => {
  43. const schema = getWrapperCreationFormSchema(stripe_wrapper)
  44. const result = schema.safeParse({ mode: 'tables' })
  45. expect(result.success).toEqual(false)
  46. // Common required field
  47. expect(
  48. result.error?.issues.find((issue) => issue.path.some((p) => p === 'wrapper_name'))
  49. ).toBeDefined()
  50. // Table mode specific field
  51. expect(
  52. result.error?.issues.find((issue) => issue.path.some((p) => p === 'tables'))
  53. ).toBeDefined()
  54. })
  55. it('should build a schema that validates schema mode', () => {
  56. const schema = getWrapperCreationFormSchema(stripe_wrapper)
  57. const result = schema.safeParse({ mode: 'schema' })
  58. expect(result.success).toEqual(false)
  59. // Common required field
  60. expect(
  61. result.error?.issues.find((issue) => issue.path.some((p) => p === 'wrapper_name'))
  62. ).toBeDefined()
  63. // Schema mode specific field
  64. expect(
  65. result.error?.issues.find((issue) => issue.path.some((p) => p === 'source_schema'))
  66. ).toBeDefined()
  67. expect(
  68. result.error?.issues.find((issue) => issue.path.some((p) => p === 'target_schema'))
  69. ).toBeDefined()
  70. })
  71. it('should build a schema that validates wrapper specific options', () => {
  72. const schema = getWrapperCreationFormSchema(stripe_wrapper)
  73. const result = schema.safeParse({ mode: 'tables' })
  74. expect(result.success).toEqual(false)
  75. // Required option
  76. expect(
  77. result.error?.issues.find((issue) => issue.path.some((p) => p === 'api_key_id'))
  78. ).toBeDefined()
  79. // Optional option
  80. expect(
  81. result.error?.issues.find((issue) => issue.path.some((p) => p === 'api_url'))
  82. ).toBeUndefined()
  83. })
  84. })
  85. describe('getEditionFormSchema', () => {
  86. it('should build a schema that validates wrapper specific options', () => {
  87. const schema = getEditionFormSchema(stripe_wrapper)
  88. const result = schema.safeParse({ mode: 'tables' })
  89. expect(result.success).toEqual(false)
  90. // Common required field
  91. expect(
  92. result.error?.issues.find((issue) => issue.path.some((p) => p === 'wrapper_name'))
  93. ).toBeDefined()
  94. expect(
  95. result.error?.issues.find((issue) => issue.path.some((p) => p === 'tables'))
  96. ).toBeDefined()
  97. // Required option
  98. expect(
  99. result.error?.issues.find((issue) => issue.path.some((p) => p === 'api_key_id'))
  100. ).toBeDefined()
  101. // Optional option
  102. expect(
  103. result.error?.issues.find((issue) => issue.path.some((p) => p === 'api_url'))
  104. ).toBeUndefined()
  105. })
  106. })
  107. describe('getTableFormSchema', () => {
  108. const table_schema = {
  109. label: 'Accounts',
  110. description: 'List of accounts on your Stripe account',
  111. availableColumns: [
  112. {
  113. name: 'id',
  114. type: 'text',
  115. },
  116. {
  117. name: 'business_type',
  118. type: 'text',
  119. },
  120. {
  121. name: 'country',
  122. type: 'text',
  123. },
  124. {
  125. name: 'email',
  126. type: 'text',
  127. },
  128. {
  129. name: 'type',
  130. type: 'text',
  131. },
  132. {
  133. name: 'created',
  134. type: 'timestamp',
  135. },
  136. {
  137. name: 'attrs',
  138. type: 'jsonb',
  139. },
  140. ],
  141. options: [
  142. {
  143. name: 'object',
  144. defaultValue: 'accounts',
  145. editable: false,
  146. required: true,
  147. type: 'text',
  148. },
  149. {
  150. name: 'rowid_column',
  151. label: 'Row ID Column',
  152. defaultValue: 'id',
  153. editable: true,
  154. required: false,
  155. type: 'text',
  156. },
  157. ],
  158. } satisfies Table
  159. it('should build a schema that validates tables with existing schema', () => {
  160. const schema = getTableFormSchema(table_schema)
  161. const result = schema.safeParse({})
  162. expect(result.success).toEqual(false)
  163. // Common required field
  164. expect(
  165. result.error?.issues.find((issue) => issue.path.some((p) => p === 'table_name'))
  166. ).toBeDefined()
  167. expect(
  168. result.error?.issues.find((issue) => issue.path.some((p) => p === 'schema'))
  169. ).toBeDefined()
  170. expect(
  171. result.error?.issues.find((issue) => issue.path.some((p) => p === 'columns'))
  172. ).toBeDefined()
  173. // Required options
  174. expect(
  175. result.error?.issues.find((issue) => issue.path.some((p) => p === 'object'))
  176. ).toBeDefined()
  177. // Optional options
  178. expect(
  179. result.error?.issues.find((issue) => issue.path.some((p) => p === 'rowid_column'))
  180. ).toBeUndefined()
  181. })
  182. it('should build a schema that validates tables with custom schema', () => {
  183. const schema = getTableFormSchema(table_schema)
  184. // Because the schema_name is validated in zod superRefine, the normal validation
  185. // must have succeeded first
  186. const result = schema.safeParse({
  187. table_name: 'test',
  188. schema: 'custom',
  189. columns: [],
  190. object: 'acounts',
  191. })
  192. expect(result.success).toEqual(false)
  193. expect(
  194. result.error?.issues.find((issue) => issue.path.some((p) => p === 'schema_name'))
  195. ).toBeDefined()
  196. })
  197. })