forked from isoos/postgresql-dart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvariable_tokenizer_test.dart
306 lines (274 loc) · 9.76 KB
/
variable_tokenizer_test.dart
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
import 'package:postgres/postgres.dart';
import 'package:postgres/src/v3/query_description.dart';
import 'package:test/test.dart';
void main() {
test('can declare variables', () {
final desc =
InternalQueryDescription.named('SELECT @x:int8, @y:boolean, @z');
expect(desc.transformedSql, r'SELECT $1, $2, $3');
expect(
desc.namedVariables,
{'x': 1, 'y': 2, 'z': 3},
);
expect(desc.parameterTypes, [
Type.bigInteger, // x
Type.boolean, // y
null, // z, didn't have a specified type
]);
expect(() => desc.bindParameters(null), throwsArgumentError);
expect(
desc.bindParameters({'x': 4, 'y': true, 'z': TypedValue(Type.text, 'z')}),
[
TypedValue(Type.bigInteger, 4),
TypedValue(Type.boolean, true),
TypedValue(Type.text, 'z'),
],
);
expect(desc.bindParameters({'x': 4, 'y': true, 'z': 'z'}), [
TypedValue(Type.bigInteger, 4),
TypedValue(Type.boolean, true),
TypedValue(Type.unspecified, 'z'),
]);
// Make sure we can still bind by index
expect(
desc.bindParameters([1, true, TypedValue(Type.text, 'z')]),
[
TypedValue(Type.bigInteger, 1),
TypedValue(Type.boolean, true),
TypedValue(Type.text, 'z'),
],
);
expect(
desc.bindParameters([1, true, 3]),
[
TypedValue(Type.bigInteger, 1),
TypedValue(Type.boolean, true),
TypedValue(Type.unspecified, 3),
],
);
});
test('can declare variables by index', () {
final desc = InternalQueryDescription.indexed(
'SELECT ?:int8, ?3:boolean, ?2',
substitution: '?');
expect(desc.transformedSql, r'SELECT $1, $3, $2');
expect(desc.namedVariables, isNull);
expect(desc.parameterTypes, [Type.bigInteger, null, Type.boolean]);
expect(() => desc.bindParameters(null), throwsArgumentError);
expect(
desc.bindParameters([4, TypedValue(Type.text, 'z'), true]),
[
TypedValue(Type.bigInteger, 4),
TypedValue(Type.text, 'z'),
TypedValue(Type.boolean, true),
],
);
expect(desc.bindParameters([4, 'z', true]), [
TypedValue(Type.bigInteger, 4),
TypedValue(Type.unspecified, 'z'),
TypedValue(Type.boolean, true),
]);
});
test('can use the same variable more than once', () {
final desc = InternalQueryDescription.named(
'SELECT * FROM foo WHERE a = @x OR bar = @y OR b = @x');
expect(desc.transformedSql,
r'SELECT * FROM foo WHERE a = $1 OR bar = $2 OR b = $1');
expect(desc.namedVariables?.keys, ['x', 'y']);
});
test('indexed can use same variable more than once', () {
final indexed = InternalQueryDescription.indexed(
'SELECT * FROM foo WHERE a = @ OR bar = @ OR b = @1');
expect(indexed.transformedSql,
r'SELECT * FROM foo WHERE a = $1 OR bar = $2 OR b = $1');
expect(indexed.namedVariables, isNull);
expect(indexed.parameterTypes, hasLength(2));
});
test('can use custom variable symbol', () {
final desc = InternalQueryDescription.named(
'SELECT * FROM foo WHERE a = :x:int8',
substitution: ':');
expect(desc.transformedSql, r'SELECT * FROM foo WHERE a = $1');
expect(desc.namedVariables?.keys, ['x']);
expect(desc.parameterTypes, [Type.bigInteger]);
});
group('can use : substitution symbol and cast operator together', () {
test('simple', () {
final desc = InternalQueryDescription.named(
'SELECT id::text FROM foo WHERE a = :x:int8::int',
substitution: ':');
expect(
desc.transformedSql, r'SELECT id::text FROM foo WHERE a = $1::int');
expect(desc.namedVariables?.keys, ['x']);
expect(desc.parameterTypes, [Type.bigInteger]);
});
test('with comment', () {
final desc = InternalQueryDescription.named(
'SELECT id /**/ :: /**/ text, b::\nint8 FROM foo WHERE a = :x:int8/**/::/**/int8',
substitution: ':');
expect(desc.transformedSql,
'SELECT id :: text, b::\nint8 FROM foo WHERE a = \$1::int8');
expect(desc.namedVariables?.keys, ['x']);
expect(desc.parameterTypes, [Type.bigInteger]);
});
});
test('finds correct end for string literal', () {
final desc = InternalQueryDescription.named(r"SELECT e'@a\\' @b");
expect(desc.transformedSql, r"SELECT e'@a\\' $1");
expect(desc.namedVariables?.keys, ['b']);
});
group('VARCHAR(x)', () {
test('accept with some length', () {
final desc = InternalQueryDescription.named('SELECT @x:_varchar(10), 0');
expect(desc.transformedSql, r'SELECT $1, 0');
expect(desc.namedVariables, {'x': 1});
expect(desc.parameterTypes, [Type.varCharArray]);
});
test('throws', () {
final badSnippets = [
'@x:_varchar(',
'@x:_varchar()',
'@x:_varchar(())',
'@x:_varchar((1))',
'@x:_varchar(a)',
'@x:_varchar( 0 )',
];
for (final snippet in badSnippets) {
expect(
() => InternalQueryDescription.named('SELECT $snippet'),
throwsFormatException,
reason: snippet,
);
expect(
() => InternalQueryDescription.named('SELECT $snippet, 0'),
throwsFormatException,
reason: '$snippet, 0',
);
}
});
});
group('ignores', () {
test('line comments', () {
final desc = InternalQueryDescription.named('SELECT @1, -- @2 \n @3');
expect(desc.transformedSql, r'SELECT $1, $2');
expect(desc.namedVariables?.keys, ['1', '3']);
});
test('block comments', () {
final desc = InternalQueryDescription.named(
'SELECT @1 /* this is ignored: @2 */, @3');
expect(desc.transformedSql, r'SELECT $1 , $2');
expect(desc.namedVariables?.keys, ['1', '3']);
});
test('string literals', () {
final desc = InternalQueryDescription.named(
"SELECT @1, 'isn''t a variable: @2', @3");
expect(desc.transformedSql, r"SELECT $1, 'isn''t a variable: @2', $2");
expect(desc.namedVariables?.keys, ['1', '3']);
});
test('string literals with C-style escapes', () {
final desc = InternalQueryDescription.named(
r"SELECT @1, E'isn\'t a variable: @2', @3");
expect(desc.transformedSql, r"SELECT $1, E'isn\'t a variable: @2', $2");
expect(desc.namedVariables?.keys, ['1', '3']);
});
test('strings with unicode escapes', () {
final desc = InternalQueryDescription.named(r"U&'d\0061t@1\+000061', @2");
expect(desc.transformedSql, r"U&'d\0061t@1\+000061', $1");
expect(desc.namedVariables?.keys, ['2']);
});
test('identifiers', () {
final desc = InternalQueryDescription.named('SELECT @1 AS "@2", @3');
expect(desc.transformedSql, r'SELECT $1 AS "@2", $2');
expect(desc.namedVariables?.keys, ['1', '3']);
});
test('identifiers with unicode escapes', () {
final desc =
InternalQueryDescription.named(r'SELECT U&"d\0061t@1\+000061", @2');
expect(desc.transformedSql, r'SELECT U&"d\0061t@1\+000061", $1');
expect(desc.namedVariables?.keys, ['2']);
});
test('dollar quoted string', () {
final desc = InternalQueryDescription.named(
r"SELECT $foo$ This is a string literal $foo that still hasn't ended here $foo$, @1",
);
expect(
desc.transformedSql,
r"SELECT $foo$ This is a string literal $foo that still hasn't ended here $foo$, $1",
);
expect(desc.namedVariables?.keys, ['1']);
});
test('invalid dollar quoted string', () {
final desc = InternalQueryDescription.named(r'SELECT $foo @1');
expect(desc.transformedSql, r'SELECT $foo $1');
expect(desc.namedVariables?.keys, ['1']);
});
// https://www.postgresql.org/docs/current/functions-json.html
final operators = ['@>', '<@', '@?', '@@'];
for (final operator in operators) {
test('can use $operator', () {
final desc =
InternalQueryDescription.named('SELECT @foo $operator @bar');
expect(desc.transformedSql, 'SELECT \$1 $operator \$2');
expect(desc.namedVariables?.keys, ['foo', 'bar']);
});
}
});
group('throws', () {
test('for variable with empty type name', () {
expect(() => InternalQueryDescription.named('SELECT @var: FROM foo'),
throwsFormatException);
});
test('for invalid type name', () {
expect(
() =>
InternalQueryDescription.named('SELECT @var:nosuchtype FROM foo'),
throwsFormatException);
});
test('for missing variable', () {
expect(
() => InternalQueryDescription.named('SELECT @foo').bindParameters({}),
throwsA(isA<ArgumentError>().having(
(e) => e.message,
'message',
'Missing variable for `foo`',
)),
);
});
test('for missing variable indexed', () {
expect(
() => InternalQueryDescription.indexed('SELECT @').bindParameters([]),
throwsA(isA<ArgumentError>().having(
(e) => e.message,
'message',
'Expected 1 parameters, got 0',
)),
);
});
test('when using map with indexed', () {
expect(
() => InternalQueryDescription.indexed('SELECT @')
.bindParameters({'1': 'foo'}),
throwsA(isA<ArgumentError>().having(
(e) => e.message,
'message',
'Maps are only supported by `Sql.named`',
)),
);
});
test('for superfluous variables', () {
expect(
() =>
InternalQueryDescription.named('SELECT @foo:int4').bindParameters({
'foo': 3,
'X': 'Y',
'Y': 'Z',
}),
throwsA(isA<ArgumentError>().having(
(e) => e.message,
'message',
'Contains superfluous variables: X, Y',
)),
);
});
});
}