Skip to content

Commit 9709996

Browse files
committed
Fix tests
1 parent 32e8fa4 commit 9709996

File tree

2 files changed

+116
-62
lines changed

2 files changed

+116
-62
lines changed

tests/index.js

+114-62
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ cp.execSync('dropdb ' + options.db + ';createdb ' + options.db)
4141

4242
const sql = postgres(options)
4343

44-
t('Result is array',
45-
async() => [true, Array.isArray(await sql`select 1`)]
44+
t('Result is array', async() =>
45+
[true, Array.isArray(await sql`select 1`)]
4646
)
4747

4848
t('Result has count', async() =>
@@ -55,11 +55,12 @@ t('Result has command', async() =>
5555

5656
t('Create table', async() =>
5757
['CREATE TABLE', (await sql`create table test(int int)`).command]
58-
)
58+
, () => sql`drop table test`)
5959

60-
t('Drop table', async() =>
61-
['DROP TABLE', (await sql`drop table test`).command]
62-
)
60+
t('Drop table', async() => {
61+
await sql`create table test(int int)`
62+
return ['DROP TABLE', (await sql`drop table test`).command]
63+
})
6364

6465
t('null', async() =>
6566
[null, (await sql`select ${ null } as x`)[0].x]
@@ -148,7 +149,6 @@ t('Transaction throws on uncaught savepoint', async() => {
148149
throw new Error('fail')
149150
})
150151

151-
/* c8 ignore next */
152152
await sql`insert into test values(3)`
153153
}).catch(() => 'fail'))]
154154
}, () => sql`drop table test`)
@@ -189,7 +189,6 @@ t('Fail with proper error on no host', async() =>
189189
})).code]
190190
)
191191

192-
// If your local db doesn't support ssl this will be tested in CI
193192
t('Connect using SSL', async() =>
194193
[true, (await new Promise((resolve, reject) => {
195194
postgres({
@@ -285,75 +284,50 @@ t('Message not supported', async() => {
285284
return ['MESSAGE_NOT_SUPPORTED', await sql`copy test to stdout`.catch(x => x.code)]
286285
}, () => sql`drop table test`)
287286

288-
t('transform', async() => {
287+
t('transform column', async() => {
289288
const sql = postgres({
290289
...options,
291-
transform: x => x.split('').reverse().join('')
290+
transform: { column: x => x.split('').reverse().join('') }
292291
})
293292

294293
await sql`create table test (hello_world int)`
295294
await sql`insert into test values (1)`
296295
return ['dlrow_olleh', Object.keys((await sql`select * from test`)[0])[0]]
297296
}, () => sql`drop table test`)
298297

299-
t('toPascal', async() => {
298+
t('column toPascal', async() => {
300299
const sql = postgres({
301300
...options,
302-
transform: postgres.toPascal
301+
transform: { column: postgres.toPascal }
303302
})
304303

305304
await sql`create table test (hello_world int)`
306305
await sql`insert into test values (1)`
307306
return ['HelloWorld', Object.keys((await sql`select * from test`)[0])[0]]
308307
}, () => sql`drop table test`)
309308

310-
t('toCamel', async() => {
309+
t('column toCamel', async() => {
311310
const sql = postgres({
312311
...options,
313-
transform: postgres.toCamel
312+
transform: { column: postgres.toCamel }
314313
})
315314

316315
await sql`create table test (hello_world int)`
317316
await sql`insert into test values (1)`
318317
return ['helloWorld', Object.keys((await sql`select * from test`)[0])[0]]
319318
}, () => sql`drop table test`)
320319

321-
t('toKebab', async() => {
320+
t('column toKebab', async() => {
322321
const sql = postgres({
323322
...options,
324-
transform: postgres.toKebab
323+
transform: { column: postgres.toKebab }
325324
})
326325

327326
await sql`create table test (hello_world int)`
328327
await sql`insert into test values (1)`
329328
return ['hello-world', Object.keys((await sql`select * from test`)[0])[0]]
330329
}, () => sql`drop table test`)
331330

332-
t('row helper', async() => {
333-
const obj = { a: 1, b: 'hello', c: false }
334-
await sql`create table test (a int, b text, c bool)`
335-
await sql`insert into test (a, b, c) values ${ sql.row(obj, 'a', 'b', 'c') }`
336-
337-
const [x] = await sql`select * from test`
338-
return [true, x.a === 1 && x.b === 'hello' && x.c === false]
339-
}, () => sql`drop table test`)
340-
341-
t('multi rows helper', async() => {
342-
const obj = { a: 1, b: 'hello', c: false }
343-
const arr = [obj, obj]
344-
345-
await sql`create table test (a int, b text, c bool)`
346-
await sql`insert into test (a, b, c) values ${ sql.rows(arr, 'a', 'b', 'c') }`
347-
await sql`insert into test (a, b, c) values ${ sql.rows(arr, x => [x.a, x.b, x.c]) }`
348-
await sql`insert into test (a, b, c) values ${ sql.rows(arr.map(x => [x.a, x.b, x.c])) }`
349-
350-
const x = await sql`select * from test`
351-
return [true, x[0].a === 1 && x[0].b === 'hello' && x[0].c === false &&
352-
x[1].a === 1 && x[1].b === 'hello' && x[1].c === false &&
353-
x.count === 6
354-
]
355-
}, () => sql`drop table test`)
356-
357331
t('unsafe', async() => {
358332
await sql`create table test (x int)`
359333
return [1, (await sql.unsafe('insert into test values ($1) returning *', [1]))[0].x]
@@ -367,12 +341,10 @@ t('listen and notify', async() => {
367341
const sql = postgres(options)
368342

369343
return ['world', await new Promise((resolve, reject) =>
370-
sql.listen('hello', x => {
371-
resolve(x)
372-
sql.end()
373-
})
344+
sql.listen('hello', resolve)
374345
.then(() => sql.notify('hello', 'world'))
375346
.catch(reject)
347+
.then(sql.end)
376348
)]
377349
})
378350

@@ -386,7 +358,7 @@ t('responds with server parameters (application_name)', async() =>
386358
t('onconnect', async() => {
387359
const sql = postgres({
388360
...options,
389-
onconnect: () => 'something'
361+
onconnect: () => 'connected'
390362
})
391363

392364
return [1, (await sql`select 1 as x`)[0].x]
@@ -417,32 +389,112 @@ t('has server parameters', async() => {
417389

418390
t('big query body', async() => {
419391
await sql`create table test (x int)`
420-
return [1000, (await sql`insert into test values ${
421-
sql.rows([...Array(1000).keys()].map(x => [x]))
392+
return [1000, (await sql`insert into test ${
393+
sql([...Array(1000).keys()].map(x => ({ x })))
422394
}`).count]
423395
}, () => sql`drop table test`)
424396

397+
t('Throws if more than 65534 parameters', async() => {
398+
await sql`create table test (x int)`
399+
return ['MAX_PARAMETERS_EXCEEDED', (await sql`insert into test ${
400+
sql([...Array(65535).keys()].map(x => ({ x })))
401+
}`.catch(e => e.code))]
402+
}, () => sql`drop table test`)
425403

426-
/*
404+
t('let postgres do implicit cast of unknown types', async() => {
405+
await sql`create table test (x timestamp with time zone)`
406+
const [{ x }] = await sql`insert into test values (${ new Date().toISOString() }) returning *`
407+
return [true, x instanceof Date]
408+
}, () => sql`drop table test`)
427409

410+
t('only allows one statement', async() =>
411+
['42601', await sql`select 1; select 2`.catch(e => e.code)]
412+
)
428413

429-
t('select column vars', async() => {
430-
await sql`create table test (x int)`
431-
await sql`insert into test values (1)`
432-
return [1, (await sql`select ${ 'x' } from test`)[0].x]
414+
t('await sql() throws not tagged error', async() => {
415+
let error
416+
try {
417+
await sql('select 1')
418+
} catch(e) {
419+
error = e.code
420+
}
421+
return ['NOT_TAGGED_CALL', error]
433422
})
434423

435-
t('select column vars', async() => {
436-
await sql`create table test (x int)`
437-
await sql`insert into test values (1)`
438-
return [1, (await sql`select ${ 'x' } from test`)[0].x]
424+
t('sql().then throws not tagged error', async() => {
425+
let error
426+
try {
427+
sql('select 1').then(() => {})
428+
} catch(e) {
429+
error = e.code
430+
}
431+
return ['NOT_TAGGED_CALL', error]
439432
})
440433

441-
t('select column vars', async() => {
442-
await sql`create table test (x int)`
443-
await sql`insert into test values (1)`
444-
return [1, (await sql`select ${ 'x' } from test`)[0].x]
434+
t('sql().catch throws not tagged error', async() => {
435+
let error
436+
try {
437+
sql('select 1').catch(() => {})
438+
} catch(e) {
439+
error = e.code
440+
}
441+
return ['NOT_TAGGED_CALL', error]
445442
})
446443

444+
t('sql().finally throws not tagged error', async() => {
445+
let error
446+
try {
447+
sql('select 1').finally(() => {})
448+
} catch(e) {
449+
error = e.code
450+
}
451+
return ['NOT_TAGGED_CALL', error]
452+
})
453+
454+
t('dynamic column name', async () => {
455+
return ['!not_valid', Object.keys((await sql`select 1 as ${ sql('!not_valid') }`)[0])[0]]
456+
})
457+
458+
t('dynamic select as', async () => {
459+
return [2, (await sql`select ${ sql({ a: 1, b: 2 }) }`)[0].b]
460+
})
461+
462+
t('dynamic insert', async () => {
463+
await sql`create table test (a int, b text)`
464+
const x = { a: 42, b: 'the answer' }
465+
466+
return ['the answer', (await sql`insert into test ${ sql(x) } returning *`)[0].b]
467+
}, () => sql`drop table test`)
468+
469+
t('dynamic multi row insert', async () => {
470+
await sql`create table test (a int, b text)`
471+
const x = { a: 42, b: 'the answer' }
472+
473+
return ['the answer', (await sql`insert into test ${ sql([x, x]) } returning *`)[1].b]
474+
}, () => sql`drop table test`)
475+
476+
t('dynamic update', async () => {
477+
await sql`create table test (a int, b text)`
478+
await sql`insert into test (a, b) values (17, 'wrong')`
479+
480+
return ['the answer', (await sql`update test set ${ sql({ a: 42, b: 'the answer' }) } returning *`)[0].b]
481+
}, () => sql`drop table test`)
482+
483+
t('dynamic update pluck', async () => {
484+
await sql`create table test (a int, b text)`
485+
await sql`insert into test (a, b) values (17, 'wrong')`
447486

448-
*/
487+
return ['wrong', (await sql`update test set ${ sql({ a: 42, b: 'the answer' }, 'a') } returning *`)[0].b]
488+
}, () => sql`drop table test`)
489+
490+
t('dynamic select array', async () => {
491+
await sql`create table test (a int, b text)`
492+
await sql`insert into test (a, b) values (42, 'yay')`
493+
return ['yay', (await sql`select ${ sql(['a', 'b']) } from test`)[0].b]
494+
}, () => sql`drop table test`)
495+
496+
t('dynamic select args', async () => {
497+
await sql`create table test (a int, b text)`
498+
await sql`insert into test (a, b) values (42, 'yay')`
499+
return ['yay', (await sql`select ${ sql('a', 'b') } from test`)[0].b]
500+
}, () => sql`drop table test`)

tests/test.js

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ async function test(o, name, fn, after) {
3737

3838
process.on('exit', exit)
3939

40+
process.on('SIGINT', exit)
41+
4042
function exit() {
4143
console.log('')
4244
let success = true

0 commit comments

Comments
 (0)