2
2
3
3
var jsonSql = require ( '../lib' ) ( ) ;
4
4
var Builder = require ( '../lib' ) . Builder ;
5
- var expect = require ( 'expect.js' ) ;
5
+ var expect = require ( 'chai' ) . expect ;
6
6
7
7
describe ( 'Builder' , function ( ) {
8
8
it ( 'should have fields' , function ( ) {
9
- expect ( jsonSql ) . to . be . ok ( ) ;
10
- expect ( jsonSql ) . to . be . a ( Builder ) ;
9
+ expect ( jsonSql ) . to . be . ok ;
10
+ expect ( jsonSql ) . to . be . an . instanceof ( Builder ) ;
11
11
12
- expect ( jsonSql . dialect ) . to . be . ok ( ) ;
12
+ expect ( jsonSql . dialect ) . to . be . ok ;
13
13
14
- expect ( jsonSql . _query ) . to . be ( '' ) ;
15
- expect ( jsonSql . _values ) . to . eql ( { } ) ;
14
+ expect ( jsonSql . _query ) . to . be . equal ( '' ) ;
15
+ expect ( jsonSql . _values ) . to . be . eql ( { } ) ;
16
16
17
- expect ( jsonSql . dialect . blocks ) . to . be . ok ( ) ;
18
- expect ( jsonSql . dialect . templates ) . to . be . ok ( ) ;
19
- expect ( jsonSql . dialect . conditions ) . to . be . ok ( ) ;
20
- expect ( jsonSql . dialect . modifiers ) . to . be . ok ( ) ;
21
- expect ( jsonSql . dialect . logicalOperators ) . to . be . ok ( ) ;
17
+ expect ( jsonSql . dialect . blocks ) . to . be . ok ;
18
+ expect ( jsonSql . dialect . templates ) . to . be . ok ;
19
+ expect ( jsonSql . dialect . conditions ) . to . be . ok ;
20
+ expect ( jsonSql . dialect . modifiers ) . to . be . ok ;
21
+ expect ( jsonSql . dialect . logicalOperators ) . to . be . ok ;
22
22
} ) ;
23
23
24
24
it ( 'should throw error with wrong `type` property' , function ( ) {
25
25
expect ( function ( ) {
26
26
jsonSql . build ( {
27
27
type : 'wrong'
28
28
} ) ;
29
- } ) . to . throwError ( function ( e ) {
30
- expect ( e ) . to . be . a ( Error ) ;
31
- expect ( e . message ) . to . be ( 'Unknown template type "wrong"' ) ;
32
- } ) ;
29
+ } ) . to . throw ( 'Unknown template type "wrong"' ) ;
33
30
} ) ;
34
31
35
32
it ( 'should throw error without `table`, `query` and `select` properties' , function ( ) {
36
33
expect ( function ( ) {
37
34
jsonSql . build ( { } ) ;
38
- } ) . to . throwError ( function ( e ) {
39
- expect ( e ) . to . be . a ( Error ) ;
40
- expect ( e . message ) . to . be ( 'Neither `table`, `query`, `select`, `expression` properties ' +
41
- 'are not set in `select` clause' ) ;
42
- } ) ;
35
+ } ) . to . throw ( 'Neither `table`, `query`, `select`, `expression` properties ' +
36
+ 'are not set in `select` clause' ) ;
43
37
} ) ;
44
38
45
39
it ( 'should throw error with both `table` and `select` properties' , function ( ) {
@@ -48,10 +42,7 @@ describe('Builder', function() {
48
42
table : 'users' ,
49
43
select : { table : 'payments' }
50
44
} ) ;
51
- } ) . to . throwError ( function ( e ) {
52
- expect ( e ) . to . be . a ( Error ) ;
53
- expect ( e . message ) . to . be ( 'Wrong using `table`, `select` properties together in `select` clause' ) ;
54
- } ) ;
45
+ } ) . to . throw ( 'Wrong using `table`, `select` properties together in `select` clause' ) ;
55
46
} ) ;
56
47
57
48
it ( 'should throw error with both `table` and `query` properties' , function ( ) {
@@ -60,10 +51,7 @@ describe('Builder', function() {
60
51
table : 'users' ,
61
52
query : { table : 'payments' }
62
53
} ) ;
63
- } ) . to . throwError ( function ( e ) {
64
- expect ( e ) . to . be . a ( Error ) ;
65
- expect ( e . message ) . to . be ( 'Wrong using `table`, `query` properties together in `select` clause' ) ;
66
- } ) ;
54
+ } ) . to . throw ( 'Wrong using `table`, `query` properties together in `select` clause' ) ;
67
55
} ) ;
68
56
69
57
it ( 'should throw error with both `query` and `select` properties' , function ( ) {
@@ -72,10 +60,7 @@ describe('Builder', function() {
72
60
query : { table : 'payments' } ,
73
61
select : { table : 'payments' }
74
62
} ) ;
75
- } ) . to . throwError ( function ( e ) {
76
- expect ( e ) . to . be . a ( Error ) ;
77
- expect ( e . message ) . to . be ( 'Wrong using `query`, `select` properties together in `select` clause' ) ;
78
- } ) ;
63
+ } ) . to . throw ( 'Wrong using `query`, `select` properties together in `select` clause' ) ;
79
64
} ) ;
80
65
81
66
it ( 'should throw error without `name` property in `with` clause' , function ( ) {
@@ -88,10 +73,7 @@ describe('Builder', function() {
88
73
} ] ,
89
74
table : 'users'
90
75
} ) ;
91
- } ) . to . throwError ( function ( e ) {
92
- expect ( e ) . to . be . a ( Error ) ;
93
- expect ( e . message ) . to . be ( '`name` property is not set in `with` clause' ) ;
94
- } ) ;
76
+ } ) . to . throw ( '`name` property is not set in `with` clause' ) ;
95
77
} ) ;
96
78
97
79
it ( 'should throw error without `query` and `select` properties in `with` clause' , function ( ) {
@@ -102,11 +84,7 @@ describe('Builder', function() {
102
84
} ] ,
103
85
table : 'users'
104
86
} ) ;
105
- } ) . to . throwError ( function ( e ) {
106
- expect ( e ) . to . be . a ( Error ) ;
107
- expect ( e . message ) . to . be ( 'Neither `query`, `select`, `expression` properties ' +
108
- 'are not set in `with` clause' ) ;
109
- } ) ;
87
+ } ) . to . throw ( 'Neither `query`, `select`, `expression` properties are not set in `with` clause' ) ;
110
88
} ) ;
111
89
112
90
it ( 'should throw error with both `query` and `select` properties in `with` clause' , function ( ) {
@@ -119,10 +97,7 @@ describe('Builder', function() {
119
97
} ] ,
120
98
table : 'users'
121
99
} ) ;
122
- } ) . to . throwError ( function ( e ) {
123
- expect ( e ) . to . be . a ( Error ) ;
124
- expect ( e . message ) . to . be ( 'Wrong using `query`, `select` properties together in `with` clause' ) ;
125
- } ) ;
100
+ } ) . to . throw ( 'Wrong using `query`, `select` properties together in `with` clause' ) ;
126
101
} ) ;
127
102
128
103
it ( 'should be ok with array in `with` clause' , function ( ) {
@@ -136,9 +111,9 @@ describe('Builder', function() {
136
111
table : 'users'
137
112
} ) ;
138
113
139
- expect ( result . query ) . to . be ( 'with "payments" as (select * from "payments") select * from ' +
114
+ expect ( result . query ) . to . be . equal ( 'with "payments" as (select * from "payments") select * from ' +
140
115
'"users";' ) ;
141
- expect ( result . values ) . to . eql ( { } ) ;
116
+ expect ( result . values ) . to . be . eql ( { } ) ;
142
117
} ) ;
143
118
144
119
it ( 'should be ok with object in `with` clause' , function ( ) {
@@ -153,25 +128,25 @@ describe('Builder', function() {
153
128
table : 'users'
154
129
} ) ;
155
130
156
- expect ( result . query ) . to . be ( 'with "payments" as (select * from "payments") select * from ' +
131
+ expect ( result . query ) . to . be . equal ( 'with "payments" as (select * from "payments") select * from ' +
157
132
'"users";' ) ;
158
- expect ( result . values ) . to . eql ( { } ) ;
133
+ expect ( result . values ) . to . be . eql ( { } ) ;
159
134
} ) ;
160
135
161
136
it ( 'should create array values with option `namedValues` = false' , function ( ) {
162
137
jsonSql . configure ( {
163
138
namedValues : false
164
139
} ) ;
165
140
166
- expect ( jsonSql . _values ) . to . eql ( [ ] ) ;
141
+ expect ( jsonSql . _values ) . to . be . eql ( [ ] ) ;
167
142
168
143
var result = jsonSql . build ( {
169
144
table : 'users' ,
170
145
condition : { name : 'John' }
171
146
} ) ;
172
147
173
- expect ( result . query ) . to . be ( 'select * from "users" where "name" = $1;' ) ;
174
- expect ( result . values ) . to . eql ( [ 'John' ] ) ;
148
+ expect ( result . query ) . to . be . equal ( 'select * from "users" where "name" = $1;' ) ;
149
+ expect ( result . values ) . to . be . eql ( [ 'John' ] ) ;
175
150
} ) ;
176
151
177
152
it ( 'should use prefix `@` for values with option `valuesPrefix` = @' , function ( ) {
@@ -184,8 +159,8 @@ describe('Builder', function() {
184
159
condition : { name : 'John' }
185
160
} ) ;
186
161
187
- expect ( result . query ) . to . be ( 'select * from "users" where "name" = @p1;' ) ;
188
- expect ( result . values ) . to . eql ( { p1 : 'John' } ) ;
162
+ expect ( result . query ) . to . be . equal ( 'select * from "users" where "name" = @p1;' ) ;
163
+ expect ( result . values ) . to . be . eql ( { p1 : 'John' } ) ;
189
164
} ) ;
190
165
191
166
it ( 'should return prefixed values with method `prefixValues`' , function ( ) {
@@ -194,9 +169,9 @@ describe('Builder', function() {
194
169
condition : { name : 'John' }
195
170
} ) ;
196
171
197
- expect ( result . query ) . to . be ( 'select * from "users" where "name" = @p1;' ) ;
198
- expect ( result . values ) . to . eql ( { p1 : 'John' } ) ;
199
- expect ( result . prefixValues ( ) ) . to . eql ( { '@p1' : 'John' } ) ;
172
+ expect ( result . query ) . to . be . equal ( 'select * from "users" where "name" = @p1;' ) ;
173
+ expect ( result . values ) . to . be . eql ( { p1 : 'John' } ) ;
174
+ expect ( result . prefixValues ( ) ) . to . be . eql ( { '@p1' : 'John' } ) ;
200
175
} ) ;
201
176
202
177
it ( 'should return array values with method `getValuesArray`' , function ( ) {
@@ -205,9 +180,9 @@ describe('Builder', function() {
205
180
condition : { name : 'John' }
206
181
} ) ;
207
182
208
- expect ( result . query ) . to . be ( 'select * from "users" where "name" = @p1;' ) ;
209
- expect ( result . values ) . to . eql ( { p1 : 'John' } ) ;
210
- expect ( result . getValuesArray ( ) ) . to . eql ( [ 'John' ] ) ;
183
+ expect ( result . query ) . to . be . equal ( 'select * from "users" where "name" = @p1;' ) ;
184
+ expect ( result . values ) . to . be . eql ( { p1 : 'John' } ) ;
185
+ expect ( result . getValuesArray ( ) ) . to . be . eql ( [ 'John' ] ) ;
211
186
} ) ;
212
187
213
188
it ( 'should return object values with method `getValuesObject`' , function ( ) {
@@ -216,35 +191,35 @@ describe('Builder', function() {
216
191
namedValues : false
217
192
} ) ;
218
193
219
- expect ( jsonSql . _values ) . to . eql ( [ ] ) ;
194
+ expect ( jsonSql . _values ) . to . be . eql ( [ ] ) ;
220
195
221
196
var result = jsonSql . build ( {
222
197
table : 'users' ,
223
198
condition : { name : 'John' }
224
199
} ) ;
225
200
226
- expect ( result . query ) . to . be ( 'select * from "users" where "name" = $1;' ) ;
227
- expect ( result . values ) . to . eql ( [ 'John' ] ) ;
228
- expect ( result . prefixValues ( ) ) . to . eql ( { '$1' : 'John' } ) ;
229
- expect ( result . getValuesObject ( ) ) . to . eql ( { 1 : 'John' } ) ;
201
+ expect ( result . query ) . to . be . equal ( 'select * from "users" where "name" = $1;' ) ;
202
+ expect ( result . values ) . to . be . eql ( [ 'John' ] ) ;
203
+ expect ( result . prefixValues ( ) ) . to . be . eql ( { '$1' : 'John' } ) ;
204
+ expect ( result . getValuesObject ( ) ) . to . be . eql ( { 1 : 'John' } ) ;
230
205
} ) ;
231
206
232
207
it ( 'should create query without values with option `separatedValues` = false' , function ( ) {
233
208
jsonSql . configure ( {
234
209
separatedValues : false
235
210
} ) ;
236
211
237
- expect ( jsonSql . _values ) . to . not . be . ok ( ) ;
238
- expect ( jsonSql . _placeholderId ) . to . not . be . ok ( ) ;
212
+ expect ( jsonSql . _values ) . to . not . be . ok ;
213
+ expect ( jsonSql . _placeholderId ) . to . not . be . ok ;
239
214
240
215
var result = jsonSql . build ( {
241
216
type : 'insert' ,
242
217
table : 'users' ,
243
218
values : { name : 'John' , surname : 'Doe' }
244
219
} ) ;
245
220
246
- expect ( result . query ) . to . be ( 'insert into "users" ("name", "surname") values (\'John\', \'Doe\');' ) ;
247
- expect ( result . values ) . to . not . be . ok ( ) ;
221
+ expect ( result . query ) . to . be . equal ( 'insert into "users" ("name", "surname") values (\'John\', \'Doe\');' ) ;
222
+ expect ( result . values ) . to . not . be . ok ;
248
223
} ) ;
249
224
250
225
it ( 'should create query without wrapping identifiers with option `wrappedIdentifiers` = false' ,
@@ -259,7 +234,7 @@ describe('Builder', function() {
259
234
values : { name : 'John' }
260
235
} ) ;
261
236
262
- expect ( result . query ) . to . be ( 'insert into users (name) values ($p1);' ) ;
237
+ expect ( result . query ) . to . be . equal ( 'insert into users (name) values ($p1);' ) ;
263
238
}
264
239
) ;
265
240
@@ -276,10 +251,7 @@ describe('Builder', function() {
276
251
'users.a.b' : 1
277
252
}
278
253
} ) ;
279
- } ) . to . throwError ( function ( e ) {
280
- expect ( e ) . to . be . a ( Error ) ;
281
- expect ( e . message ) . to . be ( 'Can\'t wrap identifier with name name "users.a.b"' ) ;
282
- } ) ;
254
+ } ) . to . throw ( 'Can\'t wrap identifier with name name "users.a.b"' ) ;
283
255
} ) ;
284
256
285
257
it ( 'shouldn\'t wrap identifiers twice' , function ( ) {
@@ -296,7 +268,7 @@ describe('Builder', function() {
296
268
}
297
269
} ) ;
298
270
299
- expect ( result . query ) . to . be ( 'insert into "users" ("name", "users"."age") values ($p1, 22);' ) ;
271
+ expect ( result . query ) . to . be . equal ( 'insert into "users" ("name", "users"."age") values ($p1, 22);' ) ;
300
272
} ) ;
301
273
302
274
it ( 'should wrap identifiers with dots' , function ( ) {
@@ -313,6 +285,6 @@ describe('Builder', function() {
313
285
}
314
286
} ) ;
315
287
316
- expect ( result . query ) . to . be ( 'insert into "users" ("name", "users"."age") values ($p1, 22);' ) ;
288
+ expect ( result . query ) . to . be . equal ( 'insert into "users" ("name", "users"."age") values ($p1, 22);' ) ;
317
289
} ) ;
318
290
} ) ;
0 commit comments