-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathinputValidation.test.ts
More file actions
71 lines (64 loc) · 3.21 KB
/
Copy pathinputValidation.test.ts
File metadata and controls
71 lines (64 loc) · 3.21 KB
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
// Copyright (c) 2026 Databricks, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import { expect } from 'chai';
import Int64 from 'node-int64';
import {
emptyToUndefined,
countParameterMarkers,
assertBindableValue,
} from '../../../lib/sea/SeaInputValidation';
import { DBSQLParameter, DBSQLParameterType } from '../../../lib/DBSQLParameter';
import ParameterError from '../../../lib/errors/ParameterError';
describe('SeaInputValidation.emptyToUndefined', () => {
it('maps empty string and null/undefined to undefined; passes real values', () => {
expect(emptyToUndefined('')).to.equal(undefined);
expect(emptyToUndefined(null)).to.equal(undefined);
expect(emptyToUndefined(undefined)).to.equal(undefined);
expect(emptyToUndefined('samples')).to.equal('samples');
expect(emptyToUndefined('%')).to.equal('%');
});
});
describe('SeaInputValidation.countParameterMarkers', () => {
it('counts bare markers', () => {
expect(countParameterMarkers('SELECT ?')).to.equal(1);
expect(countParameterMarkers('SELECT * FROM t WHERE a = ? AND b = ?')).to.equal(2);
expect(countParameterMarkers('SELECT 1')).to.equal(0);
});
it('ignores markers inside string literals, identifiers, and comments', () => {
expect(countParameterMarkers("SELECT '?' AS q")).to.equal(0);
expect(countParameterMarkers('SELECT "?" AS q')).to.equal(0);
expect(countParameterMarkers('SELECT `a?b` FROM t')).to.equal(0);
expect(countParameterMarkers('SELECT 1 -- ? in a line comment\n, ?')).to.equal(1);
expect(countParameterMarkers('SELECT /* ? in block */ ?')).to.equal(1);
expect(countParameterMarkers("SELECT 'it''s ?' , ?")).to.equal(1); // escaped quote
});
});
describe('SeaInputValidation.assertBindableValue', () => {
it('accepts scalars, Date, Int64, bigint, null, and DBSQLParameter', () => {
expect(() => assertBindableValue(42, 'p')).to.not.throw();
expect(() => assertBindableValue('x', 'p')).to.not.throw();
expect(() => assertBindableValue(true, 'p')).to.not.throw();
expect(() => assertBindableValue(BigInt(10), 'p')).to.not.throw();
expect(() => assertBindableValue(null, 'p')).to.not.throw();
expect(() => assertBindableValue(new Date(), 'p')).to.not.throw();
expect(() => assertBindableValue(new Int64(5), 'p')).to.not.throw();
expect(() => assertBindableValue(new DBSQLParameter({ type: DBSQLParameterType.INTEGER, value: 1 }), 'p')).to.not.throw();
});
it('rejects arrays (compound types)', () => {
expect(() => assertBindableValue([1, 2, 3] as never, 'ordinalParameters[0]')).to.throw(ParameterError, /array/);
});
it('rejects plain objects', () => {
expect(() => assertBindableValue({ a: 1 } as never, 'p')).to.throw(ParameterError, /object/);
});
});