-
-
Notifications
You must be signed in to change notification settings - Fork 34
feature(automatic-transactions): Creates a decorator to make transactions easier for the developer #76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feature(automatic-transactions): Creates a decorator to make transactions easier for the developer #76
Changes from 6 commits
3de300e
90febc6
95623b9
3d5c5ca
b9a417f
91fc85d
9513db6
3fea7fe
f4b730d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module.exports = (existingHandler, newHandler) => { | ||
jsumners marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (Array.isArray(existingHandler)) { | ||
return [ | ||
...existingHandler, | ||
newHandler | ||
] | ||
} else if (typeof existingHandler === 'function') { | ||
return [existingHandler, newHandler] | ||
} else { | ||
return [newHandler] | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
"main": "index.js", | ||
"types": "index.d.ts", | ||
"scripts": { | ||
"testonly": "tap -J test/*.test.js && npm run test:typescript", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why this? Can you remove? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I found this useful because when I was developing I frequently wanted to see if I'd broken tests. When running that I didn't want something like indentation linting to stop the tests running. Happy to remove, but that was my reasoning. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok |
||
"test": "standard && tap -J test/*.test.js && npm run test:typescript", | ||
"test:typescript": "tsd", | ||
"test:report": "standard && tap -J --coverage-report=html test/*.test.js", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
'use strict' | ||
|
||
const t = require('tap') | ||
const test = t.test | ||
const addHandler = require('../add-handler') | ||
|
||
test('addHandler - ', t => { | ||
test('when existing handler is not defined', t => { | ||
t.plan(1) | ||
|
||
const handlers = addHandler( | ||
undefined, | ||
'test' | ||
) | ||
|
||
t.same(handlers, ['test']) | ||
}) | ||
test('when existing handler is a array', t => { | ||
t.plan(1) | ||
|
||
const handlers = addHandler( | ||
['test'], | ||
'again' | ||
) | ||
|
||
t.same(handlers, ['test', 'again']) | ||
}) | ||
test('when existing handler is a function', t => { | ||
t.plan(2) | ||
|
||
const stub = () => 'test' | ||
|
||
const handlers = addHandler( | ||
stub, | ||
'again' | ||
) | ||
|
||
t.same(handlers[0](), 'test') | ||
t.same(handlers[1], 'again') | ||
}) | ||
|
||
t.end() | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
'use strict' | ||
|
||
const t = require('tap') | ||
const test = t.test | ||
const Fastify = require('fastify') | ||
const fastifyPostgres = require('../index') | ||
const { connectionString } = require('./helpers') | ||
|
||
const extractUserCount = response => parseInt(JSON.parse(response.payload).rows[0].userCount) | ||
|
||
test('fastify postgress useTransaction route option', t => { | ||
test('queries that succeed provided', async t => { | ||
const fastify = Fastify() | ||
t.teardown(() => fastify.close()) | ||
|
||
await fastify.register(fastifyPostgres, { | ||
connectionString | ||
}) | ||
|
||
await fastify.pg.query('TRUNCATE users') | ||
|
||
fastify.get('/count-users', async (req, reply) => { | ||
const result = await fastify.pg.query('SELECT COUNT(*) AS "userCount" FROM users WHERE username=\'pass-opt-in\'') | ||
|
||
reply.send(result) | ||
}) | ||
|
||
fastify.get('/pass', { pg: { transact: true } }, async (req, reply) => { | ||
await req.pg.query('INSERT INTO users(username) VALUES($1) RETURNING id', ['pass-opt-in']) | ||
await req.pg.query('INSERT INTO users(username) VALUES($1) RETURNING id', ['pass-opt-in']) | ||
reply.send('complete') | ||
}) | ||
|
||
await fastify.inject({ url: '/pass' }) | ||
|
||
const response = await fastify.inject({ | ||
method: 'GET', | ||
url: '/count-users' | ||
}) | ||
|
||
t.is(extractUserCount(response), 2) | ||
}) | ||
test('queries that succeed provided to a namespace', async t => { | ||
const fastify = Fastify() | ||
t.teardown(() => fastify.close()) | ||
|
||
await fastify.register(fastifyPostgres, { | ||
connectionString, | ||
name: 'test' | ||
}) | ||
|
||
await fastify.pg.test.query('TRUNCATE users') | ||
|
||
fastify.get('/count-users', async (req, reply) => { | ||
const result = await fastify.pg.test.query('SELECT COUNT(*) AS "userCount" FROM users WHERE username=\'pass-opt-in\'') | ||
|
||
reply.send(result) | ||
}) | ||
|
||
fastify.get('/pass', { pg: { transact: 'test' } }, async (req, reply) => { | ||
await req.pg.test.query('INSERT INTO users(username) VALUES($1) RETURNING id', ['pass-opt-in']) | ||
await req.pg.test.query('INSERT INTO users(username) VALUES($1) RETURNING id', ['pass-opt-in']) | ||
|
||
reply.send('complete') | ||
}) | ||
|
||
await fastify.inject({ url: '/pass' }) | ||
|
||
const response = await fastify.inject({ | ||
method: 'GET', | ||
url: '/count-users' | ||
}) | ||
|
||
t.is(extractUserCount(response), 2) | ||
}) | ||
test('queries that fail provided', async t => { | ||
const fastify = Fastify() | ||
t.teardown(() => fastify.close()) | ||
|
||
await fastify.register(fastifyPostgres, { | ||
connectionString | ||
}) | ||
|
||
await fastify.pg.query('TRUNCATE users') | ||
|
||
fastify.get('/count-users', async (req, reply) => { | ||
const result = await fastify.pg.query('SELECT COUNT(*) AS "userCount" FROM users WHERE username=\'fail-opt-in\'') | ||
|
||
reply.send(result) | ||
}) | ||
|
||
fastify.get('/fail', { pg: { transact: true } }, async (req, reply) => { | ||
await req.pg.query('INSERT INTO users(username) VALUES($1) RETURNING id', ['fail-opt-in']) | ||
await req.pg.query('INSERT INTO users(username) VALUES($1) RETURNING id', ['fail-opt-in']) | ||
await req.pg.query('INSERT INTO nope(username) VALUES($1) RETURNING id', ['fail-opt-in']) | ||
reply.send('complete') | ||
}) | ||
|
||
await fastify.inject({ url: '/fail' }) | ||
|
||
const response = await fastify.inject({ | ||
method: 'GET', | ||
url: '/count-users' | ||
}) | ||
|
||
t.is(extractUserCount(response), 0) | ||
}) | ||
|
||
t.end() | ||
}) | ||
|
||
test('combinations of registrationOptions.name and routeOptions.pg.transact that should not add hooks', t => { | ||
test('transact not set', t => { | ||
t.plan(1) | ||
|
||
const fastify = Fastify() | ||
t.teardown(() => fastify.close()) | ||
|
||
fastify.register(fastifyPostgres, { | ||
connectionString | ||
}) | ||
|
||
fastify.get('/', (req, reply) => { | ||
t.is(req.pg, null) | ||
}) | ||
|
||
fastify.inject({ url: '/' }) | ||
}) | ||
test('name set and transact not set', t => { | ||
t.plan(1) | ||
|
||
const fastify = Fastify() | ||
t.teardown(() => fastify.close()) | ||
|
||
fastify.register(fastifyPostgres, { | ||
connectionString, | ||
name: 'test' | ||
}) | ||
|
||
fastify.get('/', (req, reply) => { | ||
t.is(req.pg, null) | ||
}) | ||
|
||
fastify.inject({ url: '/' }) | ||
}) | ||
test('name set and transact set to true', t => { | ||
t.plan(1) | ||
|
||
const fastify = Fastify() | ||
t.teardown(() => fastify.close()) | ||
|
||
fastify.register(fastifyPostgres, { | ||
connectionString, | ||
name: 'test' | ||
}) | ||
|
||
fastify.get('/', { pg: { transact: true } }, (req, reply) => { | ||
t.is(req.pg, null) | ||
}) | ||
|
||
fastify.inject({ url: '/' }) | ||
}) | ||
test('name not set and transact set to string', t => { | ||
t.plan(1) | ||
|
||
const fastify = Fastify() | ||
t.teardown(() => fastify.close()) | ||
|
||
fastify.register(fastifyPostgres, { | ||
connectionString | ||
}) | ||
|
||
fastify.get('/', { pg: { transact: 'test' } }, (req, reply) => { | ||
t.is(req.pg, null) | ||
}) | ||
|
||
fastify.inject({ url: '/' }) | ||
}) | ||
test('name and transact set to different strings', t => { | ||
t.plan(1) | ||
|
||
const fastify = Fastify() | ||
t.teardown(() => fastify.close()) | ||
|
||
fastify.register(fastifyPostgres, { | ||
connectionString, | ||
name: 'test' | ||
}) | ||
|
||
fastify.get('/', { pg: { transact: 'different' } }, (req, reply) => { | ||
t.is(req.pg, null) | ||
}) | ||
|
||
fastify.inject({ url: '/' }) | ||
}) | ||
t.end() | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use strict is missing.
Maybe it's better to move this file inside
lib/