Skip to content

Commit dc6540a

Browse files
committed
Add commit callback test, cleanup tests
1 parent 3708205 commit dc6540a

File tree

3 files changed

+66
-27
lines changed

3 files changed

+66
-27
lines changed

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ node_js:
88
services:
99
- postgresql
1010

11+
before_script:
12+
- psql -d postgres -c 'CREATE TABLE users(id serial PRIMARY KEY, username VARCHAR (50) NOT NULL);' -U postgres
13+
1114
notifications:
1215
email:
1316
on_success: never

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,16 @@ fastify.post('/user/:username', (req, reply) => {
115115
)
116116
})
117117

118+
/* or with a commit callback
119+
120+
fastify.pg.transact((client, commit) => {
121+
client.query('INSERT INTO users(username) VALUES($1) RETURNING id', [req.params.username], (err, id) => {
122+
commit(err, id)
123+
});
124+
})
125+
126+
*/
127+
118128
fastify.listen(3000, err => {
119129
if (err) throw err
120130
console.log(`server listening on ${fastify.server.address().port}`)

test.js

Lines changed: 53 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -267,22 +267,15 @@ test('fastify.pg.test use transact util with promise', t => {
267267
fastify.ready(err => {
268268
t.error(err)
269269
fastify.pg.test
270-
.query('CREATE TABLE users(id serial PRIMARY KEY, username VARCHAR (50) NOT NULL)')
271-
.then(() => {
270+
.transact(client => client.query('INSERT INTO users(username) VALUES($1) RETURNING id', ['with-promise']))
271+
.then(result => {
272+
t.equals(result.rows.length, 1)
272273
fastify.pg.test
273-
.transact(client => { return client.query('INSERT INTO users(username) VALUES($1) RETURNING id', ['brianc']) })
274+
.query(`SELECT * FROM users WHERE username = 'with-promise'`)
274275
.then(result => {
275-
t.ok(result.rows[0].id === 1)
276-
fastify.pg.test
277-
.query('SELECT * FROM users')
278-
.then(result => {
279-
t.ok(result.rows[0].username === 'brianc')
280-
}).catch(err => {
281-
t.fail(err)
282-
fastify.close()
283-
})
284-
})
285-
.catch(err => {
276+
t.ok(result.rows[0].username === 'with-promise')
277+
fastify.close()
278+
}).catch(err => {
286279
t.fail(err)
287280
fastify.close()
288281
})
@@ -295,7 +288,7 @@ test('fastify.pg.test use transact util with promise', t => {
295288
})
296289

297290
test('fastify.pg.test use transact util with callback', t => {
298-
t.plan(2)
291+
t.plan(4)
299292

300293
const fastify = Fastify()
301294

@@ -306,22 +299,55 @@ test('fastify.pg.test use transact util with callback', t => {
306299

307300
fastify.ready(err => {
308301
t.error(err)
302+
309303
fastify.pg.test
310-
.query('CREATE TABLE users2(id serial PRIMARY KEY, username VARCHAR (50) NOT NULL)')
311-
.then(() => {
304+
.transact(client => client.query('INSERT INTO users(username) VALUES($1) RETURNING id', ['with-callback']), function (err, res) {
305+
t.error(err)
306+
t.equals(res.rows.length, 1)
307+
312308
fastify.pg.test
313-
.transact(client => { return client.query('INSERT INTO users2(username) VALUES($1) RETURNING id', ['brianc']) }, function (err, res) {
314-
if (err) {
315-
t.fail(err)
316-
fastify.close()
317-
} else {
318-
t.ok(res.rows[0].id === 1)
319-
}
309+
.query(`SELECT * FROM users WHERE username = 'with-callback'`)
310+
.then(result => {
311+
t.ok(result.rows[0].username === 'with-callback')
312+
fastify.close()
313+
}).catch(err => {
314+
t.fail(err)
315+
fastify.close()
320316
})
321317
})
322-
.catch(err => {
323-
t.fail(err)
324-
fastify.close()
318+
})
319+
})
320+
321+
test('fastify.pg.test use transact util with commit callback', t => {
322+
t.plan(4)
323+
324+
const fastify = Fastify()
325+
326+
fastify.register(fastifyPostgres, {
327+
name: 'test',
328+
connectionString: 'postgres://postgres@localhost/postgres'
329+
})
330+
331+
fastify.ready(err => {
332+
t.error(err)
333+
334+
fastify.pg.test.transact((client, commit) => {
335+
client.query('INSERT INTO users(username) VALUES($1) RETURNING id', ['commit-callback'], (err, id) => {
336+
commit(err, id)
325337
})
338+
}, function (err, res) {
339+
t.error(err)
340+
t.equals(res.rows.length, 1)
341+
342+
fastify.pg.test
343+
.query(`SELECT * FROM users WHERE username = 'commit-callback'`)
344+
.then(result => {
345+
t.ok(result.rows[0].username === 'commit-callback')
346+
fastify.close()
347+
}).catch(err => {
348+
t.fail(err)
349+
fastify.close()
350+
})
351+
})
326352
})
327353
})

0 commit comments

Comments
 (0)