-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathnameCasing.test.js
231 lines (212 loc) · 8.43 KB
/
nameCasing.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import { describe, expect, it, test, vi } from 'vitest';
import { nameCasing } from './nameCasing';
describe('nameCasing', () => {
it('no tables or views passed no errors', () => {
const mockReporter = vi.fn();
nameCasing.process({
options: [],
schemaObject: {
tables: [],
views: [],
},
report: mockReporter,
});
expect(mockReporter).toBeCalledTimes(0);
});
test.each`
type | param | expected1 | expected2
${`default`} | ${null} | ${`th_is_is_no_sn_a_k_e_ca_s_e`} | ${`neit_he_r_is_this`}
${`snake-case`} | ${`snake`} | ${`th_is_is_no_sn_a_k_e_ca_s_e`} | ${`neit_he_r_is_this`}
${`dash case`} | ${`dash`} | ${`th-is-is-no-sn-a-k-e-ca-s-e`} | ${`neit-he-r-is-this`}
${`camel case`} | ${`camel`} | ${`thIsIsNoSnAKECaSE`} | ${`neitHeRIsThis`}
${`pascal case`} | ${`pascal`} | ${`ThIsIsNoSnAKECaSE`} | ${`NeitHeRIsThis`}
`(
'$type : param of $param applies to table names and requires $expected',
({ _type, param, expected1, expected2 }) => {
const mockReporter = vi.fn();
nameCasing.process({
options: [param],
schemaObject: {
name: 'schema',
tables: [
{ name: 'Th-IsIsNoSnA_kECaSE', columns: [] },
{ name: 'neit_he-rIsThis', columns: [] },
],
views: [],
},
report: mockReporter,
});
if (param == null) {
param = 'snake';
}
expect(mockReporter).toBeCalledTimes(2);
expect(mockReporter).toBeCalledWith(
expect.objectContaining({
rule: 'name-casing',
identifier: 'schema.Th-IsIsNoSnA_kECaSE',
message: `The table Th-IsIsNoSnA_kECaSE seems to be mixed-cased rather than ${param}-cased.`,
suggestedMigration: `ALTER TABLE "Th-IsIsNoSnA_kECaSE" RENAME TO "${expected1}";`,
}),
);
expect(mockReporter).toBeCalledWith(
expect.objectContaining({
rule: 'name-casing',
identifier: 'schema.neit_he-rIsThis',
message: `The table neit_he-rIsThis seems to be mixed-cased rather than ${param}-cased.`,
suggestedMigration: `ALTER TABLE "neit_he-rIsThis" RENAME TO "${expected2}";`,
}),
);
},
);
test.each`
type | param | expected1 | expected2
${`default`} | ${null} | ${`th_is_is_no_sn_a_k_e_ca_s_e`} | ${`neit_he_r_is_this`}
${`snake-case`} | ${`snake`} | ${`th_is_is_no_sn_a_k_e_ca_s_e`} | ${`neit_he_r_is_this`}
${`dash case`} | ${`dash`} | ${`th-is-is-no-sn-a-k-e-ca-s-e`} | ${`neit-he-r-is-this`}
${`camel case`} | ${`camel`} | ${`thIsIsNoSnAKECaSE`} | ${`neitHeRIsThis`}
${`pascal case`} | ${`pascal`} | ${`ThIsIsNoSnAKECaSE`} | ${`NeitHeRIsThis`}
`(
'$type : param of $param applies to view names and requires $expected',
({ _type, param, expected1, expected2 }) => {
const mockReporter = vi.fn();
nameCasing.process({
options: [param],
schemaObject: {
name: 'schema',
tables: [],
views: [
{ name: 'Th-IsIsNoSnA_kECaSE', columns: [] },
{ name: 'neit_he-rIsThis', columns: [] },
],
},
report: mockReporter,
});
if (param == null) {
param = 'snake';
}
expect(mockReporter).toBeCalledTimes(2);
expect(mockReporter).toBeCalledWith(
expect.objectContaining({
rule: 'name-casing',
identifier: 'schema.Th-IsIsNoSnA_kECaSE',
message: `The view Th-IsIsNoSnA_kECaSE seems to be mixed-cased rather than ${param}-cased.`,
suggestedMigration: `ALTER VIEW "Th-IsIsNoSnA_kECaSE" RENAME TO "${expected1}";`,
}),
);
expect(mockReporter).toBeCalledWith(
expect.objectContaining({
rule: 'name-casing',
identifier: 'schema.neit_he-rIsThis',
message: `The view neit_he-rIsThis seems to be mixed-cased rather than ${param}-cased.`,
suggestedMigration: `ALTER VIEW "neit_he-rIsThis" RENAME TO "${expected2}";`,
}),
);
},
);
test.each`
type | param | expected1 | expected2
${`default`} | ${null} | ${`th_is_is_no_sn_a_k_e_ca_s_e`} | ${`neit_he_r_is_this`}
${`snake-case`} | ${`snake`} | ${`th_is_is_no_sn_a_k_e_ca_s_e`} | ${`neit_he_r_is_this`}
${`dash case`} | ${`dash`} | ${`th-is-is-no-sn-a-k-e-ca-s-e`} | ${`neit-he-r-is-this`}
${`camel case`} | ${`camel`} | ${`thIsIsNoSnAKECaSE`} | ${`neitHeRIsThis`}
${`pascal case`} | ${`pascal`} | ${`ThIsIsNoSnAKECaSE`} | ${`NeitHeRIsThis`}
`(
'$type : param of $param applies to view names and requires $expected',
({ _type, param, expected1, expected2 }) => {
const mockReporter = vi.fn();
nameCasing.process({
options: [param],
schemaObject: {
name: 'schema',
tables: [
{
name: 'one_table',
columns: [
{ name: 'Th-IsIsNoSnA_kECaSE' },
{ name: 'snake_case' },
],
},
{
name: 'two_table',
columns: [{ name: 'neit_he-rIsThis' }, { name: 'snake_case' }],
},
],
views: [],
},
report: mockReporter,
});
if (param == null) {
param = 'snake';
}
expect(mockReporter).toBeCalledWith(
expect.objectContaining({
rule: 'name-casing',
identifier: 'schema.one_table.Th-IsIsNoSnA_kECaSE',
message: `The column Th-IsIsNoSnA_kECaSE on the table one_table seems to be mixed-cased rather than ${param}-cased.`,
suggestedMigration: `ALTER TABLE "one_table" RENAME COLUMN "Th-IsIsNoSnA_kECaSE" TO "${expected1}";`,
}),
);
expect(mockReporter).toBeCalledWith(
expect.objectContaining({
rule: 'name-casing',
identifier: 'schema.two_table.neit_he-rIsThis',
message: `The column neit_he-rIsThis on the table two_table seems to be mixed-cased rather than ${param}-cased.`,
suggestedMigration: `ALTER TABLE "two_table" RENAME COLUMN "neit_he-rIsThis" TO "${expected2}";`,
}),
);
},
);
test.each`
type | param | expected1 | expected2
${`default`} | ${null} | ${`th_is_is_no_sn_a_k_e_ca_s_e`} | ${`neit_he_r_is_this`}
${`snake-case`} | ${`snake`} | ${`th_is_is_no_sn_a_k_e_ca_s_e`} | ${`neit_he_r_is_this`}
${`dash case`} | ${`dash`} | ${`th-is-is-no-sn-a-k-e-ca-s-e`} | ${`neit-he-r-is-this`}
${`camel case`} | ${`camel`} | ${`thIsIsNoSnAKECaSE`} | ${`neitHeRIsThis`}
${`pascal case`} | ${`pascal`} | ${`ThIsIsNoSnAKECaSE`} | ${`NeitHeRIsThis`}
`(
'$type : param of $param applies to view names and requires $expected',
({ _type, param, expected1, expected2 }) => {
const mockReporter = vi.fn();
nameCasing.process({
options: [param],
schemaObject: {
name: 'schema',
views: [
{
name: 'one_view',
columns: [
{ name: 'Th-IsIsNoSnA_kECaSE' },
{ name: 'snake_case' },
],
},
{
name: 'two_view',
columns: [{ name: 'neit_he-rIsThis' }, { name: 'snake_case' }],
},
],
tables: [],
},
report: mockReporter,
});
if (param == null) {
param = 'snake';
}
expect(mockReporter).toBeCalledWith(
expect.objectContaining({
rule: 'name-casing',
identifier: 'schema.one_view.Th-IsIsNoSnA_kECaSE',
message: `The column Th-IsIsNoSnA_kECaSE on the view one_view seems to be mixed-cased rather than ${param}-cased.`,
suggestedMigration: `ALTER VIEW "one_view" RENAME COLUMN "Th-IsIsNoSnA_kECaSE" TO "${expected1}";`,
}),
);
expect(mockReporter).toBeCalledWith(
expect.objectContaining({
rule: 'name-casing',
identifier: 'schema.two_view.neit_he-rIsThis',
message: `The column neit_he-rIsThis on the view two_view seems to be mixed-cased rather than ${param}-cased.`,
suggestedMigration: `ALTER VIEW "two_view" RENAME COLUMN "neit_he-rIsThis" TO "${expected2}";`,
}),
);
},
);
});