-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
114 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
// | ||
|
@@ -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 | ||
// | ||
|
@@ -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() | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |