@@ -38,6 +38,15 @@ cp.execSync('dropdb ' + options.db + ';createdb ' + options.db)
38
38
39
39
const sql = postgres ( options )
40
40
41
+ t ( 'Connects with no options' , async ( ) => {
42
+ const sql = postgres ( )
43
+
44
+ const result = ( await sql `select 1 as x` ) [ 0 ] . x
45
+ sql . end ( )
46
+
47
+ return [ 1 , result ]
48
+ } )
49
+
41
50
t ( 'Result is array' , async ( ) =>
42
51
[ true , Array . isArray ( await sql `select 1` ) ]
43
52
)
@@ -75,10 +84,14 @@ t('String', async() =>
75
84
[ 'hello' , ( await sql `select ${ 'hello' } as x` ) [ 0 ] . x ]
76
85
)
77
86
78
- t ( 'Boolean' , async ( ) =>
87
+ t ( 'Boolean false ' , async ( ) =>
79
88
[ false , ( await sql `select ${ false } as x` ) [ 0 ] . x ]
80
89
)
81
90
91
+ t ( 'Boolean true' , async ( ) =>
92
+ [ true , ( await sql `select ${ true } as x` ) [ 0 ] . x ]
93
+ )
94
+
82
95
t ( 'Date' , async ( ) => {
83
96
const now = Date . now ( )
84
97
return [ now , ( await sql `select ${ now } as x` ) [ 0 ] . x ]
@@ -89,6 +102,10 @@ t('Json', async() => {
89
102
return [ true , x . a === 1 && x . b === 'hello' ]
90
103
} )
91
104
105
+ t ( 'Empty array' , async ( ) =>
106
+ [ 0 , ( await sql `select ${ sql . array ( [ ] ) } as x` ) [ 0 ] . x . length ]
107
+ )
108
+
92
109
t ( 'Array of Integer' , async ( ) =>
93
110
[ 3 , ( await sql `select ${ sql . array ( [ 1 , 2 , 3 ] ) } as x` ) [ 0 ] . x [ 2 ] ]
94
111
)
@@ -149,12 +166,22 @@ t('Transaction throws on uncaught savepoint', async() => {
149
166
await sql `insert into test values(2)`
150
167
throw new Error ( 'fail' )
151
168
} )
169
+ } ) . catch ( ( ) => 'fail' ) ) ]
170
+ } , ( ) => sql `drop table test` )
152
171
153
- await sql `insert into test values(3)`
172
+ t ( 'Transaction throws on uncaught named savepoint' , async ( ) => {
173
+ await sql `create table test (a int)`
174
+
175
+ return [ 'fail' , ( await sql . begin ( async sql => {
176
+ await sql `insert into test values(1)`
177
+ await sql . savepoint ( 'watpoint' , async sql => {
178
+ await sql `insert into test values(2)`
179
+ throw new Error ( 'fail' )
180
+ } )
154
181
} ) . catch ( ( ) => 'fail' ) ) ]
155
182
} , ( ) => sql `drop table test` )
156
183
157
- t ( 'Transaction succeeds on uncaught savepoint' , async ( ) => {
184
+ t ( 'Transaction succeeds on caught savepoint' , async ( ) => {
158
185
await sql `create table test (a int)`
159
186
await sql . begin ( async sql => {
160
187
await sql `insert into test values(1)`
@@ -168,6 +195,17 @@ t('Transaction succeeds on uncaught savepoint', async() => {
168
195
return [ 2 , ( await sql `select count(1) from test` ) [ 0 ] . count ]
169
196
} , ( ) => sql `drop table test` )
170
197
198
+ t ( 'Savepoint returns Result' , async ( ) => {
199
+ let result
200
+ await sql . begin ( async sql => {
201
+ result = await sql . savepoint ( sql =>
202
+ sql `select 1 as x`
203
+ )
204
+ } )
205
+
206
+ return [ 1 , result [ 0 ] . x ]
207
+ } )
208
+
171
209
t ( 'Parallel transactions' , async ( ) => {
172
210
await sql `create table test (a int)`
173
211
return [ '11' , ( await Promise . all ( [
@@ -176,6 +214,15 @@ t('Parallel transactions', async() => {
176
214
] ) ) . map ( x => x . count ) . join ( '' ) ]
177
215
} , ( ) => sql `drop table test` )
178
216
217
+ t ( 'Transactions array' , async ( ) => {
218
+ await sql `create table test (a int)`
219
+
220
+ return [ '11' , ( await sql . begin ( sql => [
221
+ sql `select 1` . then ( x => x ) ,
222
+ sql `select 1`
223
+ ] ) ) . map ( x => x . count ) . join ( '' ) ]
224
+ } , ( ) => sql `drop table test` )
225
+
179
226
t ( 'Transaction waits' , async ( ) => {
180
227
await sql `create table test (a int)`
181
228
await sql . begin ( async sql => {
@@ -288,7 +335,8 @@ t('sql file', async() =>
288
335
289
336
t ( 'sql file can stream' , async ( ) => {
290
337
let result
291
- await sql . file ( path . join ( __dirname , 'select.sql' ) )
338
+ await sql
339
+ . file ( path . join ( __dirname , 'select.sql' ) , { cache : false } )
292
340
. stream ( ( { x } ) => result = x )
293
341
294
342
return [ 1 , result ]
@@ -298,13 +346,48 @@ t('sql file throws', async() =>
298
346
[ 'ENOENT' , ( await sql . file ( './selectomondo.sql' ) . catch ( x => x . code ) ) ]
299
347
)
300
348
349
+ t ( 'sql file cached' , async ( ) => {
350
+ await sql . file ( path . join ( __dirname , 'select.sql' ) )
351
+ await new Promise ( r => setTimeout ( r , 20 ) )
352
+
353
+ return [ 1 , ( await sql . file ( path . join ( __dirname , 'select.sql' ) ) ) [ 0 ] . x ]
354
+ } )
355
+
356
+ t ( 'Connection ended promise' , async ( ) => {
357
+ const sql = postgres ( options )
358
+
359
+ await sql . end ( )
360
+
361
+ return [ undefined , await sql . end ( ) ]
362
+ } )
363
+
364
+ t ( 'Connection ended timeout' , async ( ) => {
365
+ const sql = postgres ( options )
366
+
367
+ await sql . end ( { timeout : 10 } )
368
+
369
+ return [ undefined , await sql . end ( ) ]
370
+ } )
371
+
301
372
t ( 'Connection ended error' , async ( ) => {
302
373
const sql = postgres ( options )
303
374
304
375
sql . end ( )
305
376
return [ 'CONNECTION_ENDED' , ( await sql `` . catch ( x => x . code ) ) ]
306
377
} )
307
378
379
+ t ( 'Connection end does not cancel query' , async ( ) => {
380
+ const sql = postgres ( options )
381
+
382
+ await sql `select 1`
383
+
384
+ const promise = sql `select 1 as x`
385
+
386
+ sql . end ( )
387
+
388
+ return [ 1 , ( await promise ) [ 0 ] . x ]
389
+ } )
390
+
308
391
t ( 'Connection destroyed' , async ( ) => {
309
392
const sql = postgres ( options )
310
393
setTimeout ( ( ) => sql . end ( { timeout : 0 } ) , 0 )
@@ -371,10 +454,47 @@ t('unsafe simple', async() => {
371
454
372
455
t ( 'listen and notify' , async ( ) => {
373
456
const sql = postgres ( options )
457
+ , channel = 'hello'
374
458
375
459
return [ 'world' , await new Promise ( ( resolve , reject ) =>
376
- sql . listen ( 'hello' , resolve )
377
- . then ( ( ) => sql . notify ( 'hello' , 'world' ) )
460
+ sql . listen ( channel , resolve )
461
+ . then ( ( ) => sql . notify ( channel , 'world' ) )
462
+ . catch ( reject )
463
+ . then ( sql . end )
464
+ ) ]
465
+ } )
466
+
467
+ t ( 'double listen' , async ( ) => {
468
+ const sql = postgres ( options )
469
+ , channel = 'hello'
470
+
471
+ let count = 0
472
+
473
+ await new Promise ( ( resolve , reject ) =>
474
+ sql . listen ( channel , resolve )
475
+ . then ( ( ) => sql . notify ( channel , 'world' ) )
476
+ . catch ( reject )
477
+ ) . then ( ( ) => count ++ )
478
+
479
+ await new Promise ( ( resolve , reject ) =>
480
+ sql . listen ( channel , resolve )
481
+ . then ( ( ) => sql . notify ( channel , 'world' ) )
482
+ . catch ( reject )
483
+ ) . then ( ( ) => count ++ )
484
+
485
+ // for coverage
486
+ sql . listen ( 'weee' , ( ) => { } ) . then ( sql . end )
487
+
488
+ return [ 2 , count ]
489
+ } )
490
+
491
+ t ( 'listen and notify with weird name' , async ( ) => {
492
+ const sql = postgres ( options )
493
+ , channel = 'wat-;ø§'
494
+
495
+ return [ 'world' , await new Promise ( ( resolve , reject ) =>
496
+ sql . listen ( channel , resolve )
497
+ . then ( ( ) => sql . notify ( channel , 'world' ) )
378
498
. catch ( reject )
379
499
. then ( sql . end )
380
500
) ]
@@ -438,7 +558,7 @@ t('sql().then throws not tagged error', async() => {
438
558
t ( 'sql().catch throws not tagged error' , async ( ) => {
439
559
let error
440
560
try {
441
- sql ( 'select 1' ) . catch ( ( ) => { } )
561
+ await sql ( 'select 1' )
442
562
} catch ( e ) {
443
563
error = e . code
444
564
}
@@ -463,6 +583,10 @@ t('dynamic select as', async () => {
463
583
return [ 2 , ( await sql `select ${ sql ( { a : 1 , b : 2 } ) } ` ) [ 0 ] . b ]
464
584
} )
465
585
586
+ t ( 'dynamic select as pluck' , async ( ) => {
587
+ return [ undefined , ( await sql `select ${ sql ( { a : 1 , b : 2 } , 'a' ) } ` ) [ 0 ] . b ]
588
+ } )
589
+
466
590
t ( 'dynamic insert' , async ( ) => {
467
591
await sql `create table test (a int, b text)`
468
592
const x = { a : 42 , b : 'the answer' }
@@ -602,3 +726,58 @@ t('bytea serializes and parses', async() => {
602
726
603
727
return [ 0 , Buffer . compare ( buf , ( await sql `select x from test` ) [ 0 ] . x ) ]
604
728
} )
729
+
730
+ t ( 'Transform row' , async ( ) => {
731
+ const sql = postgres ( {
732
+ ...options ,
733
+ transform : { row : x => 1 }
734
+ } )
735
+
736
+ return [ 1 , ( await sql `select 'wat'` ) [ 0 ] ]
737
+ } )
738
+
739
+ t ( 'Transform row stream' , async ( ) => {
740
+ let result
741
+ const sql = postgres ( {
742
+ ...options ,
743
+ transform : { row : x => 1 }
744
+ } )
745
+
746
+ await sql `select 1` . stream ( x => result = x )
747
+
748
+ return [ 1 , result ]
749
+ } )
750
+
751
+ t ( 'Transform value' , async ( ) => {
752
+ const sql = postgres ( {
753
+ ...options ,
754
+ transform : { value : x => 1 }
755
+ } )
756
+
757
+ return [ 1 , ( await sql `select 'wat' as x` ) [ 0 ] . x ]
758
+ } )
759
+
760
+ t ( 'Unix socket' , async ( ) => {
761
+ const sql = postgres ( {
762
+ ...options ,
763
+ host : '/tmp'
764
+ } )
765
+
766
+ return [ 1 , ( await sql `select 1 as x` ) [ 0 ] . x ]
767
+ } )
768
+
769
+ t ( 'Big result' , async ( ) => {
770
+ return [ 100000 , ( await sql `select * from generate_series(1, 100000)` ) . count ]
771
+ } )
772
+
773
+ t ( 'Debug works' , async ( ) => {
774
+ let result
775
+ const sql = postgres ( {
776
+ ...options ,
777
+ debug : ( connection_id , str , args ) => result = str
778
+ } )
779
+
780
+ await sql `select 1`
781
+
782
+ return [ 'select 1' , result ]
783
+ } )
0 commit comments