File tree Expand file tree Collapse file tree 4 files changed +70
-32
lines changed Expand file tree Collapse file tree 4 files changed +70
-32
lines changed Original file line number Diff line number Diff line change @@ -29,9 +29,6 @@ import {
29
29
SqlToken ,
30
30
UnnestToken ,
31
31
} from '../tokens' ;
32
- import {
33
- InvalidInputError ,
34
- } from '../errors' ;
35
32
import createSqlTokenSqlFragment from './createSqlTokenSqlFragment' ;
36
33
37
34
const log = Logger . child ( {
@@ -80,14 +77,6 @@ export default () => {
80
77
}
81
78
}
82
79
83
- if ( rawSql . trim ( ) === '' ) {
84
- throw new InvalidInputError ( 'Unexpected SQL input. Query cannot be empty.' ) ;
85
- }
86
-
87
- if ( rawSql . trim ( ) === '$1' ) {
88
- throw new InvalidInputError ( 'Unexpected SQL input. Query cannot be empty. Found only value binding.' ) ;
89
- }
90
-
91
80
const query = deepFreeze ( {
92
81
sql : rawSql ,
93
82
type : SqlToken ,
Original file line number Diff line number Diff line change @@ -13,6 +13,7 @@ import {
13
13
BackendTerminatedError ,
14
14
CheckIntegrityConstraintViolationError ,
15
15
ForeignKeyIntegrityConstraintViolationError ,
16
+ InvalidInputError ,
16
17
NotNullIntegrityConstraintViolationError ,
17
18
QueryCancelledError ,
18
19
UnexpectedStateError ,
@@ -51,6 +52,14 @@ export default async (
51
52
throw new UnexpectedStateError ( 'Cannot use terminated connection.' ) ;
52
53
}
53
54
55
+ if ( rawSql . trim ( ) === '' ) {
56
+ throw new InvalidInputError ( 'Unexpected SQL input. Query cannot be empty.' ) ;
57
+ }
58
+
59
+ if ( rawSql . trim ( ) === '$1' ) {
60
+ throw new InvalidInputError ( 'Unexpected SQL input. Query cannot be empty. Found only value binding.' ) ;
61
+ }
62
+
54
63
const queryInputTime = process . hrtime . bigint ( ) ;
55
64
56
65
let stackTrace = null ;
Original file line number Diff line number Diff line change
1
+ // @flow
2
+
3
+ import Roarr from 'roarr' ;
4
+ import test , {
5
+ beforeEach ,
6
+ } from 'ava' ;
7
+ import executeQuery from '../../../src/routines/executeQuery' ;
8
+ import {
9
+ InvalidInputError ,
10
+ } from '../../../src/errors' ;
11
+ import createClientConfiguration from '../../helpers/createClientConfiguration' ;
12
+
13
+ const createConnectionStub = ( ) => {
14
+ return {
15
+ connection : {
16
+ slonik : {
17
+ terminated : false ,
18
+ } ,
19
+ } ,
20
+ } ;
21
+ } ;
22
+
23
+ beforeEach ( ( t ) => {
24
+ t . context . logger = Roarr ;
25
+ t . context . connection = createConnectionStub ( ) ;
26
+ t . context . executionRoutine = ( ) => { } ;
27
+ } ) ;
28
+
29
+ test ( 'throws a descriptive error if query is empty' , async ( t ) => {
30
+ const error = await t . throwsAsync ( ( ) => {
31
+ return executeQuery (
32
+ t . context . logger ,
33
+ t . context . connection ,
34
+ createClientConfiguration ( ) ,
35
+ '' ,
36
+ [ ] ,
37
+ 'foo' ,
38
+ t . context . executionRoutine
39
+ ) ;
40
+ } ) ;
41
+
42
+ t . assert ( error instanceof InvalidInputError ) ;
43
+ t . assert ( error . message === 'Unexpected SQL input. Query cannot be empty.' ) ;
44
+ } ) ;
45
+
46
+ test ( 'throws a descriptive error if the entire query is a value binding' , async ( t ) => {
47
+ const error = await t . throwsAsync ( ( ) => {
48
+ return executeQuery (
49
+ t . context . logger ,
50
+ t . context . connection ,
51
+ createClientConfiguration ( ) ,
52
+ '$1' ,
53
+ [ ] ,
54
+ 'foo' ,
55
+ t . context . executionRoutine
56
+ ) ;
57
+ } ) ;
58
+
59
+ t . assert ( error instanceof InvalidInputError ) ;
60
+ t . assert ( error . message === 'Unexpected SQL input. Query cannot be empty. Found only value binding.' ) ;
61
+ } ) ;
Original file line number Diff line number Diff line change @@ -5,9 +5,6 @@ import createSqlTag from '../../../../src/factories/createSqlTag';
5
5
import {
6
6
SqlToken ,
7
7
} from '../../../../src/tokens' ;
8
- import {
9
- InvalidInputError ,
10
- } from '../../../../src/errors' ;
11
8
12
9
const sql = createSqlTag ( ) ;
13
10
@@ -21,24 +18,6 @@ test('creates an object describing a query', (t) => {
21
18
} ) ;
22
19
} ) ;
23
20
24
- test ( 'throws a descriptive error if query is empty' , ( t ) => {
25
- const error = t . throws ( ( ) => {
26
- sql `` ;
27
- } ) ;
28
-
29
- t . assert ( error instanceof InvalidInputError ) ;
30
- t . assert ( error . message === 'Unexpected SQL input. Query cannot be empty.' ) ;
31
- } ) ;
32
-
33
- test ( 'throws a descriptive error if the entire query is a value binding' , ( t ) => {
34
- const error = t . throws ( ( ) => {
35
- sql `${ 1 } ` ;
36
- } ) ;
37
-
38
- t . assert ( error instanceof InvalidInputError ) ;
39
- t . assert ( error . message === 'Unexpected SQL input. Query cannot be empty. Found only value binding.' ) ;
40
- } ) ;
41
-
42
21
test ( 'creates an object describing query value bindings' , ( t ) => {
43
22
const query = sql `SELECT ${ 'foo' } ` ;
44
23
You can’t perform that action at this time.
0 commit comments