poseidon-mongo
Version:
A future wrapper around the Node Native MongoDB driver using Poseidon.
137 lines (112 loc) • 4.14 kB
text/coffeescript
Promise = require 'bluebird'
{Driver} = require '../index'
{MongoClient} = require 'mongodb'
describe 'The Driver class', ->
beforeEach (next) ->
= new Driver()
.reset()
.then ->
next()
.done()
return
it 'has a static internal configuration object', ->
expect(._configuration).to.deep.equal {}
it 'has an internal cache of database connections', ->
expect(._connections).to.deep.equal {}
describe 'the reset function', ->
beforeEach (next) ->
.configure('test', {})
.openConnection('test')
.then ->
next()
.done()
it 'resets the driver connections and configuration caches', (next) ->
.reset()
.then =>
expect(._connections).to.deep.equal {}
expect(._configuration).to.deep.equal {}
next()
.done()
it 'closes all open connections', (next) ->
sinon.spy(, 'closeConnection')
.configure('foo', {})
.openConnection('foo')
.then =>
.reset()
.then =>
expect(.closeConnection).to.have.been.calledTwice
next()
.done()
describe 'the configure function', ->
it 'creates a default configuration if nothing is provided', ->
defaultConfig =
hosts: ['localhost:27017']
auth: null
database: 'default'
options: null
.configure('test', {})
expect(._configuration['test']).to.deep.equal defaultConfig
it 'saves the given configuration', ->
config =
hosts: ['mordor:10000']
auth: 'sauron:precious'
database: 'mountdoom'
options: null
.configure('mordor', config)
expect(._configuration['mordor']).to.deep.equal config
it 'throws an error if the auth format is wrong', ->
config =
auth: 'khazadum'
invalidConfig = =>
.configure('test', config)
expect(invalidConfig).to.throw Error, /Invalid authentication credentials/
it 'throws an error if no configuration is provided', ->
invalidCall = =>
.configure('test')
expect(invalidCall).to.throw Error, /Configuration object required/
describe 'the openConnection function', ->
beforeEach ->
.configure('test', {})
afterEach ->
._connections = {}
it 'calls the MongoClient connect function and caches it', (next) ->
conn = .openConnection('test')
expect(Promise.is(conn)).to.equal true
expect(._connections['test']).to.deep.equal conn
conn.then ->
next()
return
.done()
it 'returns the cached connection if it already exists', (next) ->
.openConnection('test')
conn = .openConnection('test')
expect(Promise.is(conn)).to.equal true
expect(._connections['test']).to.deep.equal conn
conn.then ->
next()
return
.done()
it 'throws an error if the connection was not configured', ->
.openConnection('foo')
.catch (err) ->
expect(err.message).to.equal 'Connection not configured'
describe 'the closeConnection function', ->
it 'throws an error if the connection does not exist', ->
.closeConnection('foo')
.catch (err) ->
expect(err.message).to.equal 'Connection does not exist'
it 'deletes the connection from the cache and calls the db close function', (next) ->
db = null
.configure('test', {})
.openConnection('test').then =>
._connections['test']
.then (_db) =>
db = _db
sinon.spy(_db, 'close')
.closeConnection('test')
.then =>
expect(._connections['test']).to.be.undefined
expect(db.close).to.have.been.calledWith true
next()
return
.done()