poseidon-mongo
Version:
A future wrapper around the Node Native MongoDB driver using Poseidon.
110 lines (102 loc) • 2.79 kB
text/coffeescript
Promise = require 'bluebird'
{Driver, Cursor} = require '../index'
Mongo = require 'mongodb'
describe 'The Cursor class', ->
beforeEach (next) ->
self = @
@mongoCursor = null
MongoDriver = new Driver()
MongoDriver.configure('default', {})
MongoDriver.openConnection('default')
.then (db) ->
db.collection('test', (err, _collection) ->
_collection.find({}, (err, _cursor) ->
self.mongoCursor = _cursor
next()
)
)
.done()
it 'wraps a MongoDB cursor', ->
cursor = new Cursor(@mongoCursor)
expect(cursor.instance).to.deep.equal @mongoCursor
it 'throws an error if the passed cursor is not a Mongo Cursor instance', ->
invalidCall = ->
cursor = new Cursor({})
expect(invalidCall).to.throw Error, /Object must be an instance of Mongo Cursor/
it 'has wrapped versions of MongoDB Cursor functions which return normal values', (next)->
cursor = new Cursor(@mongoCursor)
fns = [
'toArray'
'each'
'count'
'nextObject'
'explain'
'close'
]
args = [
[]
[]
[]
[]
[]
]
count = 0
fns.forEach (fn, index) ->
sinon.spy(Mongo.Cursor.prototype, fn)
result = cursor[fn].apply cursor, args[index]
expect(Promise.is(result)).to.equal true
result
.catch () ->
return
.finally () ->
expect(Mongo.Cursor.prototype[fn]).to.have.been.called
count += 1
Mongo.Cursor.prototype[fn].restore()
if count is fns.length then next()
.done()
return
it 'has wrapped versions of MongoDB Cursor functions which are synchronous', ->
cursor = new Cursor(@mongoCursor)
fns = [
'stream'
'isClosed'
]
args = [
[]
[]
]
count = 0
fns.forEach (fn, index) ->
sinon.spy(Mongo.Cursor.prototype, fn)
result = cursor[fn].apply cursor, args[index]
expect(Promise.is(result)).to.equal false
expect(Mongo.Cursor.prototype[fn]).to.have.been.called
Mongo.Cursor.prototype[fn].restore()
return
it 'has wrapped versions of MongoDB Cursor functions which are synchronous and chainable', ->
cursor = new Cursor(@mongoCursor)
fns = [
'rewind'
'sort'
'setReadPreference'
'skip'
'limit'
'batchSize'
]
args = [
[]
[]
[]
[]
[]
[]
]
count = 0
fns.forEach (fn, index) ->
sinon.spy(Mongo.Cursor.prototype, fn)
result = cursor[fn].apply cursor, args[index]
expect(Promise.is(result)).to.equal false
expect(result).to.deep.equal cursor
expect(Mongo.Cursor.prototype[fn]).to.have.been.called
Mongo.Cursor.prototype[fn].restore()
return