Skip to content

Instantly share code, notes, and snippets.

@trustmaster
Created December 15, 2015 15:36
Show Gist options
  • Save trustmaster/5ed3b0d0777a36e35b70 to your computer and use it in GitHub Desktop.
Save trustmaster/5ed3b0d0777a36e35b70 to your computer and use it in GitHub Desktop.
Testing a NoFlo component with transaction rollback
noflo = require 'noflo'
chai = require 'chai'
uuid = require 'uuid'
db = require '../lib/db'
Tester = require 'noflo-tester'
describe 'SomeComponent', ->
c = new Tester 'some-project/SomeComponent'
productData =
id: uuid.v4()
name: 'Test Product'
afterDone = null
trans = null
before (done) ->
c.start ->
db.transaction (trx) ->
trans = trx
trans 'products'
.insert productData
.then ->
done()
null # Don't return the transaction to avoid auto-commit
.catch (e) ->
# A bit hacky Knex transaction error handling, because .rollback() is an error, huh
if e is undefined or e instanceof Array and e.length is 0 or e.command is 'ROLLBACK'
afterDone()
else
console.log 'Knex error:', e
after (done) ->
# afterDone makes sure the test is over only after transaction is finished
afterDone = done
trans.rollback()
it 'should do something', (done) ->
# Use transaction object as database connection in all the tests
c.receive 'result', (data) ->
chai.expect(data).to.be.a 'number'
trans 'products'
.select()
.where 'id', data
.then (rows) ->
chai.expect(rows).to.have.lengthOf 1
chai.expect(rows[0].name).to.equal 'Test Product'
chai.expect(rows[0].id).to.be.a 'string'
done()
c.receive 'error', (data) ->
done data
c.send
db: trans
id: productData.id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment