Skip to content

Commit

Permalink
Improving tests and harness
Browse files Browse the repository at this point in the history
  • Loading branch information
mkdbns committed Jul 8, 2017
1 parent 4fb38d5 commit a2060b5
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 22 deletions.
2 changes: 1 addition & 1 deletion lib/factory/test/directChannel.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const expect = require('chai').expect
const directChannel = require('../directChannel')

const basic = {
members:[
members: [
'username1',
'username2'
]
Expand Down
50 changes: 50 additions & 0 deletions lib/modules/test/fakedb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//
// Implements a fake db for testing
//
class FakeDB {
//
// Constructor
//
constructor(data) {
this.data = data
this.fetch = this.fetch.bind(this)
this.pipe = this.pipe.bind(this)
}

//
// Simulate a db connection by just returning
// a promise
//
connect() {
return Promise.resolve()
}

//
// Returns all results in one batch
//
fetch() {
return Promise.resolve({
recordset: this.data
})
}

//
// Delivers results to the specified write
// stream
//
pipe(query, writable) {

this.data.forEach(function(record) {
writable.write(record)
})

writable.end()

return writable
}
}

//
// Export the class
//
module.exports = FakeDB
66 changes: 45 additions & 21 deletions lib/modules/test/posts.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const expect = require('chai').expect
const { spy } = require('sinon')
const { Writable } = require('stream')
const posts = require('../posts')
const Fixtures = require('./fixtures')
const FakeDB = require('./fakedb')
const sink = require('./sink')
const context = require('./context')()

describe('modules.posts', function() {

it('should process post objects', function(done) {
before(function() {
//
// Set up context values
//
Expand Down Expand Up @@ -43,26 +43,20 @@ describe('modules.posts', function() {
}
}
}
})

beforeEach(function() {
//
// Stub the data source
// Stub the output stream
//
context.jabber.pipe.callsFake(function(query, writable) {
Fixtures.posts.ok.forEach(function(post) {
writable.write(post)
})
writable.end()
return writable
})
context.output = sink()
})

it('should process post objects', function(done) {
//
// Stub the output stream
// Set up the DB
//
context.output = new Writable({
objectMode: true,
write(chunk, encoding, callback) {
return callback()
}
})
spy(context.output, 'write')
context.jabber = new FakeDB(Fixtures.posts.ok)
//
// Process the posts
//
Expand All @@ -88,7 +82,37 @@ describe('modules.posts', function() {
})
})

afterEach(function() {
context.jabber.pipe.reset()
it('should fail on user not found', function(done) {
//
// Set up the DB
//
context.jabber = new FakeDB(Fixtures.posts.userNotFound)
//
// Process the posts
//
posts(context).then(function(c) {
expect(c).to.be.undefined
}).catch(function(e){
expect(e).to.be.an('error')
expect(e.message).to.equal('user [email protected] not found')
done()
})
})

it('should fail on body not found', function(done) {
//
// Set up the DB
//
context.jabber = new FakeDB(Fixtures.posts.bodyNotFound)
//
// Process the posts
//
posts(context).then(function(c) {
expect(c).to.be.undefined
}).catch(function(e){
expect(e).to.be.an('error')
expect(e.message).to.equal('channel [email protected] not found')
done()
})
})
})
18 changes: 18 additions & 0 deletions lib/modules/test/sink.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const { spy } = require('sinon')
const { Writable } = require('stream')

//
// Returns a spied writable stream
//
module.exports = function() {

var writable = new Writable({
objectMode: true,
write(chunk, encoding, callback) {
return callback()
}
})
spy(writable, 'write')

return writable
}

0 comments on commit a2060b5

Please sign in to comment.