forked from promptfoo/promptfoo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql.test.ts
296 lines (278 loc) · 10.9 KB
/
sql.test.ts
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
/* eslint-disable jest/no-commented-out-tests */
import { handleIsSql } from '../../src/assertions/sql';
import type { Assertion, AssertionParams, GradingResult } from '../../src/types';
const assertion: Assertion = {
type: 'is-sql',
};
describe('is-sql assertion', () => {
// -------------------------------------------------- Basic Tests ------------------------------------------------------ //
describe('Basic tests', () => {
it('should pass when the output string is a valid SQL statement', async () => {
const renderedValue = undefined;
const outputString = 'SELECT id, name FROM users';
const result: GradingResult = await handleIsSql({
assertion,
renderedValue,
outputString,
inverse: false,
} as AssertionParams);
expect(result).toEqual({
assertion,
pass: true,
reason: 'Assertion passed',
score: 1,
});
});
it('should fail when the SQL statement contains a syntax error in the ORDER BY clause', async () => {
const renderedValue = undefined;
const outputString = 'SELECT * FROM orders ORDERY BY order_date';
const result: GradingResult = await handleIsSql({
assertion,
renderedValue,
outputString,
inverse: false,
} as AssertionParams);
expect(result).toEqual({
pass: false,
score: 0,
reason: 'SQL statement does not conform to the provided MySQL database syntax.',
assertion,
});
});
it('should fail when the SQL statement uses a reserved keyword as a table name', async () => {
const renderedValue = undefined;
const outputString = 'SELECT * FROM select WHERE id = 1';
const result: GradingResult = await handleIsSql({
assertion,
renderedValue,
outputString,
inverse: false,
} as AssertionParams);
expect(result).toEqual({
pass: false,
score: 0,
reason: 'SQL statement does not conform to the provided MySQL database syntax.',
assertion,
});
});
it('should fail when the SQL statement has an incorrect DELETE syntax', async () => {
const renderedValue = undefined;
const outputString = 'DELETE employees WHERE id = 1';
const result: GradingResult = await handleIsSql({
assertion,
renderedValue,
outputString,
inverse: false,
} as AssertionParams);
expect(result).toEqual({
pass: false,
score: 0,
reason: 'SQL statement does not conform to the provided MySQL database syntax.',
assertion,
});
});
/**
* Catches an incorrect output from node-sql-parser package
* The parser cannot identify the syntax error: missing comma between column names
*/
// it('should fail when the output string is an invalid SQL statement', () => {
// const renderedValue = undefined;
// const outputString = 'SELECT first_name last_name FROM employees';
// const result = testFunction(renderedValue, outputString, false);
// expect(result.pass).toBe(false);
// expect(result.reason).toBe('SQL statement does not conform to the provided MySQL database syntax.');
// });
/**
* Catches an incorrect output from node-sql-parser package
* The parser cannot identify the syntax error: misuse of backticks (`)
*/
// it('should fail when the output string is an invalid SQL statement', () => {
// const renderedValue = undefined;
// const outputString = 'SELECT * FROM `employees`';
// const result = testFunction(renderedValue, outputString, false);
// expect(result.pass).toBe(false);
// expect(result.reason).toBe('SQL statement does not conform to the provided MySQL database syntax.');
// });
});
// ------------------------------------------ Database Specific Syntax Tests ------------------------------------------- //
describe('Database Specific Syntax Tests', () => {
it('should fail if the output SQL statement conforms to MySQL but not PostgreSQL', async () => {
const renderedValue = {
databaseType: 'PostgreSQL',
};
const outputString = `SELECT * FROM employees WHERE id = 1 LOCK IN SHARE MODE`;
const result: GradingResult = await handleIsSql({
assertion,
renderedValue,
outputString,
inverse: false,
} as AssertionParams);
expect(result).toEqual({
pass: false,
score: 0,
reason: 'SQL statement does not conform to the provided PostgreSQL database syntax.',
assertion,
});
});
it('should fail if the output SQL statement conforms to PostgreSQL but not MySQL', async () => {
const renderedValue = {
databaseType: 'MySQL',
};
const outputString = `SELECT first_name, last_name FROM employees WHERE first_name ILIKE 'john%'`;
const result: GradingResult = await handleIsSql({
assertion,
renderedValue,
outputString,
inverse: false,
} as AssertionParams);
expect(result).toEqual({
pass: false,
score: 0,
reason: 'SQL statement does not conform to the provided MySQL database syntax.',
assertion,
});
});
it('should pass if the output SQL statement conforms to PostgreSQL but not MySQL', async () => {
const renderedValue = {
databaseType: 'PostgreSQL',
};
const outputString = `SELECT first_name, last_name FROM employees WHERE first_name ILIKE 'john%'`;
const result: GradingResult = await handleIsSql({
assertion,
renderedValue,
outputString,
inverse: false,
} as AssertionParams);
expect(result).toEqual({
pass: true,
score: 1,
reason: 'Assertion passed',
assertion,
});
});
/**
* Catches an incorrect output from node-sql-parser package
* The parser cannot differentiate certain syntax between MySQL and PostgreSQL
*/
// it('should fail if the output SQL statement conforms to PostgreSQL but not MySQL', () => {
// const renderedValue = {
// databaseType: 'MySQL',
// };
// const outputString = `SELECT generate_series(1, 10);`;
// const result = testFunction(renderedValue, outputString, false);
// expect(result.pass).toBe(false);
// expect(result.reason).toBe('SQL statement does not conform to the provided MySQL database syntax.');
// });
});
// ------------------------------------------- White Table/Column List Tests ------------------------------------------- //
describe('White Table/Column List Tests', () => {
it('should fail if the output SQL statement violate allowedTables', async () => {
const renderedValue = {
databaseType: 'MySQL',
allowedTables: ['(select|update|insert|delete)::null::departments'],
};
const outputString = `SELECT * FROM employees`;
const result: GradingResult = await handleIsSql({
assertion,
renderedValue,
outputString,
inverse: false,
} as AssertionParams);
expect(result).toEqual({
pass: false,
score: 0,
reason: `SQL validation failed: authority = 'select::null::employees' is required in table whiteList to execute SQL = 'SELECT * FROM employees'.`,
assertion,
});
});
it('should pass if the output SQL statement does not violate allowedTables', async () => {
const renderedValue = {
databaseType: 'MySQL',
allowedTables: ['(select|update|insert|delete)::null::departments'],
};
const outputString = `SELECT * FROM departments`;
const result: GradingResult = await handleIsSql({
assertion,
renderedValue,
outputString,
inverse: false,
} as AssertionParams);
expect(result).toEqual({
pass: true,
score: 1,
reason: 'Assertion passed',
assertion,
});
});
it('should fail if the output SQL statement violate allowedColumns', async () => {
const renderedValue = {
databaseType: 'MySQL',
allowedColumns: ['select::null::name', 'update::null::id'],
};
const outputString = `SELECT id FROM t`;
const result: GradingResult = await handleIsSql({
assertion,
renderedValue,
outputString,
inverse: false,
} as AssertionParams);
expect(result).toEqual({
pass: false,
score: 0,
reason: `SQL validation failed: authority = 'select::null::id' is required in column whiteList to execute SQL = 'SELECT id FROM t'.`,
assertion,
});
});
it('should pass if the output SQL statement does not violate allowedColumns', async () => {
const renderedValue = {
databaseType: 'MySQL',
allowedColumns: ['insert::department::dept_name', 'insert::department::location'],
};
const outputString = `INSERT INTO department (dept_name, location) VALUES ('Sales', 'New York')`;
const result: GradingResult = await handleIsSql({
assertion,
renderedValue,
outputString,
inverse: false,
} as AssertionParams);
expect(result).toEqual({
pass: true,
score: 1,
reason: 'Assertion passed',
assertion,
});
});
/**
* Catches an incorrect output from node-sql-parser package
* Error message: message: "authority = 'update::null::id' is required in column whiteList to execute SQL = 'UPDATE a SET id = 1'"
* issue: In this test case, the 'whiteListCheck' function in node-sql-parser requires an explicit 'update::null::id'
* in the whitelist to allow SQL statement like `UPDATE a SET id = 1`, despite the presence
* of rule `update::a::id`
*/
// it('should pass if the output SQL statement does not violate allowedColumns', () => {
// const renderedValue = {
// databaseType: 'MySQL',
// allowedColumns: ['update::a::id'],
// };
// const outputString = `UPDATE a SET id = 1`;
// const result = testFunction(renderedValue, outputString, false);
// expect(result.pass).toBe(true);
// expect(result.reason).toBe('Assertion passed');
// });
/**
* Similar issue: the error message is Error: authority = 'select::null::id' is required
* in column whiteList to execute SQL = 'UPDATE employee SET salary = 50000 WHERE id = 1'
*/
// it('should pass if the output SQL statement does not violate allowedColumns', () => {
// const renderedValue = {
// databaseType: 'MySQL',
// allowedColumns: ['update::employee::salary','select::employee::id'],
// };
// const outputString = `UPDATE employee SET salary = 50000 WHERE id = 1`;
// const result = testFunction(renderedValue, outputString, false);
// expect(result.pass).toBe(true);
// expect(result.reason).toBe('Assertion passed');
// });
});
/* eslint-enable jest/no-commented-out-tests */
});