Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

sql.test.ts 13 KB

You have to be logged in to leave a comment. Sign In
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
  1. import { handleIsSql } from '../../src/assertions/sql';
  2. import type { Assertion, AssertionParams, GradingResult } from '../../src/types';
  3. const assertion: Assertion = {
  4. type: 'is-sql',
  5. };
  6. describe('is-sql assertion', () => {
  7. // -------------------------------------------------- Basic Tests ------------------------------------------------------ //
  8. describe('Basic tests', () => {
  9. it('should pass when the output string is a valid SQL statement', async () => {
  10. const renderedValue = undefined;
  11. const outputString = 'SELECT id, name FROM users';
  12. const result: GradingResult = await handleIsSql({
  13. assertion,
  14. renderedValue,
  15. outputString,
  16. inverse: false,
  17. } as AssertionParams);
  18. expect(result).toEqual({
  19. assertion,
  20. pass: true,
  21. reason: 'Assertion passed',
  22. score: 1,
  23. });
  24. });
  25. it('should fail when the SQL statement contains a syntax error in the ORDER BY clause', async () => {
  26. const renderedValue = undefined;
  27. const outputString = 'SELECT * FROM orders ORDERY BY order_date';
  28. const result: GradingResult = await handleIsSql({
  29. assertion,
  30. renderedValue,
  31. outputString,
  32. inverse: false,
  33. } as AssertionParams);
  34. expect(result).toEqual({
  35. pass: false,
  36. score: 0,
  37. reason: 'SQL statement does not conform to the provided MySQL database syntax.',
  38. assertion,
  39. });
  40. });
  41. it('should fail when the SQL statement uses a reserved keyword as a table name', async () => {
  42. const renderedValue = undefined;
  43. const outputString = 'SELECT * FROM select WHERE id = 1';
  44. const result: GradingResult = await handleIsSql({
  45. assertion,
  46. renderedValue,
  47. outputString,
  48. inverse: false,
  49. } as AssertionParams);
  50. expect(result).toEqual({
  51. pass: false,
  52. score: 0,
  53. reason: 'SQL statement does not conform to the provided MySQL database syntax.',
  54. assertion,
  55. });
  56. });
  57. it('should fail when the SQL statement has an incorrect DELETE syntax', async () => {
  58. const renderedValue = undefined;
  59. const outputString = 'DELETE employees WHERE id = 1';
  60. const result: GradingResult = await handleIsSql({
  61. assertion,
  62. renderedValue,
  63. outputString,
  64. inverse: false,
  65. } as AssertionParams);
  66. expect(result).toEqual({
  67. pass: false,
  68. score: 0,
  69. reason: 'SQL statement does not conform to the provided MySQL database syntax.',
  70. assertion,
  71. });
  72. });
  73. it('should fail when the SQL statement is missing a comma between columns', async () => {
  74. const renderedValue = undefined;
  75. const outputString = 'SELECT first_name last_name FROM employees';
  76. const result: GradingResult = await handleIsSql({
  77. assertion,
  78. renderedValue,
  79. outputString,
  80. inverse: false,
  81. } as AssertionParams);
  82. expect(result).toEqual({
  83. pass: false,
  84. score: 0,
  85. reason: 'SQL statement does not conform to the provided MySQL database syntax.',
  86. assertion,
  87. });
  88. });
  89. it('should fail when the SQL statement contains mismatched backticks', async () => {
  90. const renderedValue = undefined;
  91. const outputString = 'SELECT * FROM `employees';
  92. const result: GradingResult = await handleIsSql({
  93. assertion,
  94. renderedValue,
  95. outputString,
  96. inverse: false,
  97. } as AssertionParams);
  98. expect(result).toMatchObject({
  99. pass: false,
  100. score: 0,
  101. assertion,
  102. });
  103. expect(result.reason).toContain(
  104. 'SQL statement does not conform to the provided MySQL database syntax.',
  105. );
  106. });
  107. it('should pass when the SQL statement contains properly matched backticks', async () => {
  108. const renderedValue = undefined;
  109. const outputString = 'SELECT * FROM `employees` WHERE `id` = 1';
  110. const result: GradingResult = await handleIsSql({
  111. assertion,
  112. renderedValue,
  113. outputString,
  114. inverse: false,
  115. } as AssertionParams);
  116. expect(result).toEqual({
  117. pass: true,
  118. score: 1,
  119. reason: 'Assertion passed',
  120. assertion,
  121. });
  122. });
  123. it('should fail when the SQL statement contains multiple mismatched backticks', async () => {
  124. const renderedValue = undefined;
  125. const outputString = 'SELECT `id, `name FROM `users';
  126. const result: GradingResult = await handleIsSql({
  127. assertion,
  128. renderedValue,
  129. outputString,
  130. inverse: false,
  131. } as AssertionParams);
  132. expect(result).toMatchObject({
  133. pass: false,
  134. score: 0,
  135. });
  136. });
  137. it('should fail when SELECT has invalid column list format', async () => {
  138. const renderedValue = undefined;
  139. const outputString = 'SELECT col1 col2 col3 FROM table1';
  140. const result: GradingResult = await handleIsSql({
  141. assertion,
  142. renderedValue,
  143. outputString,
  144. inverse: false,
  145. } as AssertionParams);
  146. expect(result).toMatchObject({
  147. pass: false,
  148. score: 0,
  149. });
  150. });
  151. });
  152. // ------------------------------------------ Database Specific Syntax Tests ------------------------------------------- //
  153. describe('Database Specific Syntax Tests', () => {
  154. it('should fail if the output SQL statement conforms to MySQL but not PostgreSQL', async () => {
  155. const renderedValue = {
  156. databaseType: 'PostgreSQL',
  157. };
  158. const outputString = `SELECT * FROM employees WHERE id = 1 LOCK IN SHARE MODE`;
  159. const result: GradingResult = await handleIsSql({
  160. assertion,
  161. renderedValue,
  162. outputString,
  163. inverse: false,
  164. } as AssertionParams);
  165. expect(result).toEqual({
  166. pass: false,
  167. score: 0,
  168. reason: 'SQL statement does not conform to the provided PostgreSQL database syntax.',
  169. assertion,
  170. });
  171. });
  172. it('should fail if the output SQL statement conforms to PostgreSQL but not MySQL', async () => {
  173. const renderedValue = {
  174. databaseType: 'MySQL',
  175. };
  176. const outputString = `SELECT first_name, last_name FROM employees WHERE first_name ILIKE 'john%'`;
  177. const result: GradingResult = await handleIsSql({
  178. assertion,
  179. renderedValue,
  180. outputString,
  181. inverse: false,
  182. } as AssertionParams);
  183. expect(result).toEqual({
  184. pass: false,
  185. score: 0,
  186. reason: 'SQL statement does not conform to the provided MySQL database syntax.',
  187. assertion,
  188. });
  189. });
  190. it('should pass if the output SQL statement conforms to PostgreSQL but not MySQL', async () => {
  191. const renderedValue = {
  192. databaseType: 'PostgreSQL',
  193. };
  194. const outputString = `SELECT first_name, last_name FROM employees WHERE first_name ILIKE 'john%'`;
  195. const result: GradingResult = await handleIsSql({
  196. assertion,
  197. renderedValue,
  198. outputString,
  199. inverse: false,
  200. } as AssertionParams);
  201. expect(result).toEqual({
  202. pass: true,
  203. score: 1,
  204. reason: 'Assertion passed',
  205. assertion,
  206. });
  207. });
  208. it('should fail if the output SQL statement uses PostgreSQL-only syntax on MySQL', async () => {
  209. const renderedValue = {
  210. databaseType: 'MySQL',
  211. };
  212. const outputString = 'SELECT generate_series(1, 10);';
  213. const result: GradingResult = await handleIsSql({
  214. assertion,
  215. renderedValue,
  216. outputString,
  217. inverse: false,
  218. } as AssertionParams);
  219. expect(result).toEqual({
  220. pass: false,
  221. score: 0,
  222. reason: 'SQL statement does not conform to the provided MySQL database syntax.',
  223. assertion,
  224. });
  225. });
  226. it('should fail when using generate_series in MySQL even with valid syntax', async () => {
  227. const renderedValue = {
  228. databaseType: 'MySQL',
  229. };
  230. const outputString = 'SELECT * FROM table_name WHERE id IN (SELECT generate_series(1, 5));';
  231. const result: GradingResult = await handleIsSql({
  232. assertion,
  233. renderedValue,
  234. outputString,
  235. inverse: false,
  236. } as AssertionParams);
  237. expect(result).toEqual({
  238. pass: false,
  239. score: 0,
  240. reason: 'SQL statement does not conform to the provided MySQL database syntax.',
  241. assertion,
  242. });
  243. });
  244. });
  245. // ------------------------------------------- White Table/Column List Tests ------------------------------------------- //
  246. describe('White Table/Column List Tests', () => {
  247. it('should fail if the output SQL statement violate allowedTables', async () => {
  248. const renderedValue = {
  249. databaseType: 'MySQL',
  250. allowedTables: ['(select|update|insert|delete)::null::departments'],
  251. };
  252. const outputString = `SELECT * FROM employees`;
  253. const result: GradingResult = await handleIsSql({
  254. assertion,
  255. renderedValue,
  256. outputString,
  257. inverse: false,
  258. } as AssertionParams);
  259. expect(result).toEqual({
  260. pass: false,
  261. score: 0,
  262. reason: `SQL validation failed: authority = 'select::null::employees' is required in table whiteList to execute SQL = 'SELECT * FROM employees'.`,
  263. assertion,
  264. });
  265. });
  266. it('should pass if the output SQL statement does not violate allowedTables', async () => {
  267. const renderedValue = {
  268. databaseType: 'MySQL',
  269. allowedTables: ['(select|update|insert|delete)::null::departments'],
  270. };
  271. const outputString = `SELECT * FROM departments`;
  272. const result: GradingResult = await handleIsSql({
  273. assertion,
  274. renderedValue,
  275. outputString,
  276. inverse: false,
  277. } as AssertionParams);
  278. expect(result).toEqual({
  279. pass: true,
  280. score: 1,
  281. reason: 'Assertion passed',
  282. assertion,
  283. });
  284. });
  285. it('should fail if the output SQL statement violate allowedColumns', async () => {
  286. const renderedValue = {
  287. databaseType: 'MySQL',
  288. allowedColumns: ['select::null::name', 'update::null::id'],
  289. };
  290. const outputString = `SELECT id FROM t`;
  291. const result: GradingResult = await handleIsSql({
  292. assertion,
  293. renderedValue,
  294. outputString,
  295. inverse: false,
  296. } as AssertionParams);
  297. expect(result).toEqual({
  298. pass: false,
  299. score: 0,
  300. reason: `SQL validation failed: authority = 'select::null::id' is required in column whiteList to execute SQL = 'SELECT id FROM t'.`,
  301. assertion,
  302. });
  303. });
  304. it('should pass if the output SQL statement does not violate allowedColumns', async () => {
  305. const renderedValue = {
  306. databaseType: 'MySQL',
  307. allowedColumns: ['insert::department::dept_name', 'insert::department::location'],
  308. };
  309. const outputString = `INSERT INTO department (dept_name, location) VALUES ('Sales', 'New York')`;
  310. const result: GradingResult = await handleIsSql({
  311. assertion,
  312. renderedValue,
  313. outputString,
  314. inverse: false,
  315. } as AssertionParams);
  316. expect(result).toEqual({
  317. pass: true,
  318. score: 1,
  319. reason: 'Assertion passed',
  320. assertion,
  321. });
  322. });
  323. it('should pass when update column whitelist references table name', async () => {
  324. const renderedValue = {
  325. databaseType: 'MySQL',
  326. allowedColumns: ['update::a::id'],
  327. };
  328. const outputString = `UPDATE a SET id = 1`;
  329. const result: GradingResult = await handleIsSql({
  330. assertion,
  331. renderedValue,
  332. outputString,
  333. inverse: false,
  334. } as AssertionParams);
  335. expect(result).toEqual({
  336. pass: true,
  337. score: 1,
  338. reason: 'Assertion passed',
  339. assertion,
  340. });
  341. });
  342. it('should pass when multiple column authorities are provided for update', async () => {
  343. const renderedValue = {
  344. databaseType: 'MySQL',
  345. allowedColumns: ['update::employee::salary', 'select::employee::id'],
  346. };
  347. const outputString = `UPDATE employee SET salary = 50000 WHERE id = 1`;
  348. const result: GradingResult = await handleIsSql({
  349. assertion,
  350. renderedValue,
  351. outputString,
  352. inverse: false,
  353. } as AssertionParams);
  354. expect(result).toEqual({
  355. pass: true,
  356. score: 1,
  357. reason: 'Assertion passed',
  358. assertion,
  359. });
  360. });
  361. it('should pass with normalized whitelist entries', async () => {
  362. const renderedValue = {
  363. databaseType: 'MySQL',
  364. allowedColumns: ['select::employees::name'],
  365. };
  366. const outputString = `SELECT name FROM employees`;
  367. const result: GradingResult = await handleIsSql({
  368. assertion,
  369. renderedValue,
  370. outputString,
  371. inverse: false,
  372. } as AssertionParams);
  373. expect(result).toEqual({
  374. pass: true,
  375. score: 1,
  376. reason: 'Assertion passed',
  377. assertion,
  378. });
  379. });
  380. });
  381. });
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...