Skip to content

Commit d2c4c5c

Browse files
committed
Improve tests + coverage
1 parent 25400be commit d2c4c5c

File tree

1 file changed

+186
-7
lines changed

1 file changed

+186
-7
lines changed

tests/index.js

+186-7
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ cp.execSync('dropdb ' + options.db + ';createdb ' + options.db)
3838

3939
const sql = postgres(options)
4040

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+
4150
t('Result is array', async() =>
4251
[true, Array.isArray(await sql`select 1`)]
4352
)
@@ -75,10 +84,14 @@ t('String', async() =>
7584
['hello', (await sql`select ${ 'hello' } as x`)[0].x]
7685
)
7786

78-
t('Boolean', async() =>
87+
t('Boolean false', async() =>
7988
[false, (await sql`select ${ false } as x`)[0].x]
8089
)
8190

91+
t('Boolean true', async() =>
92+
[true, (await sql`select ${ true } as x`)[0].x]
93+
)
94+
8295
t('Date', async() => {
8396
const now = Date.now()
8497
return [now, (await sql`select ${ now } as x`)[0].x]
@@ -89,6 +102,10 @@ t('Json', async() => {
89102
return [true, x.a === 1 && x.b === 'hello']
90103
})
91104

105+
t('Empty array', async() =>
106+
[0, (await sql`select ${ sql.array([]) } as x`)[0].x.length]
107+
)
108+
92109
t('Array of Integer', async() =>
93110
[3, (await sql`select ${ sql.array([1, 2, 3]) } as x`)[0].x[2]]
94111
)
@@ -149,12 +166,22 @@ t('Transaction throws on uncaught savepoint', async() => {
149166
await sql`insert into test values(2)`
150167
throw new Error('fail')
151168
})
169+
}).catch(() => 'fail'))]
170+
}, () => sql`drop table test`)
152171

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+
})
154181
}).catch(() => 'fail'))]
155182
}, () => sql`drop table test`)
156183

157-
t('Transaction succeeds on uncaught savepoint', async() => {
184+
t('Transaction succeeds on caught savepoint', async() => {
158185
await sql`create table test (a int)`
159186
await sql.begin(async sql => {
160187
await sql`insert into test values(1)`
@@ -168,6 +195,17 @@ t('Transaction succeeds on uncaught savepoint', async() => {
168195
return [2, (await sql`select count(1) from test`)[0].count]
169196
}, () => sql`drop table test`)
170197

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+
171209
t('Parallel transactions', async() => {
172210
await sql`create table test (a int)`
173211
return ['11', (await Promise.all([
@@ -176,6 +214,15 @@ t('Parallel transactions', async() => {
176214
])).map(x => x.count).join('')]
177215
}, () => sql`drop table test`)
178216

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+
179226
t('Transaction waits', async() => {
180227
await sql`create table test (a int)`
181228
await sql.begin(async sql => {
@@ -288,7 +335,8 @@ t('sql file', async() =>
288335

289336
t('sql file can stream', async() => {
290337
let result
291-
await sql.file(path.join(__dirname, 'select.sql'))
338+
await sql
339+
.file(path.join(__dirname, 'select.sql'), { cache: false })
292340
.stream(({ x }) => result = x)
293341

294342
return [1, result]
@@ -298,13 +346,48 @@ t('sql file throws', async() =>
298346
['ENOENT', (await sql.file('./selectomondo.sql').catch(x => x.code))]
299347
)
300348

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+
301372
t('Connection ended error', async() => {
302373
const sql = postgres(options)
303374

304375
sql.end()
305376
return ['CONNECTION_ENDED', (await sql``.catch(x => x.code))]
306377
})
307378

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+
308391
t('Connection destroyed', async() => {
309392
const sql = postgres(options)
310393
setTimeout(() => sql.end({ timeout: 0 }), 0)
@@ -371,10 +454,47 @@ t('unsafe simple', async() => {
371454

372455
t('listen and notify', async() => {
373456
const sql = postgres(options)
457+
, channel = 'hello'
374458

375459
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'))
378498
.catch(reject)
379499
.then(sql.end)
380500
)]
@@ -438,7 +558,7 @@ t('sql().then throws not tagged error', async() => {
438558
t('sql().catch throws not tagged error', async() => {
439559
let error
440560
try {
441-
sql('select 1').catch(() => {})
561+
await sql('select 1')
442562
} catch(e) {
443563
error = e.code
444564
}
@@ -463,6 +583,10 @@ t('dynamic select as', async () => {
463583
return [2, (await sql`select ${ sql({ a: 1, b: 2 }) }`)[0].b]
464584
})
465585

586+
t('dynamic select as pluck', async () => {
587+
return [undefined, (await sql`select ${ sql({ a: 1, b: 2 }, 'a') }`)[0].b]
588+
})
589+
466590
t('dynamic insert', async () => {
467591
await sql`create table test (a int, b text)`
468592
const x = { a: 42, b: 'the answer' }
@@ -602,3 +726,58 @@ t('bytea serializes and parses', async() => {
602726

603727
return [0, Buffer.compare(buf, (await sql`select x from test`)[0].x)]
604728
})
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

Comments
 (0)