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