Skip to content

Commit

Permalink
update tests and impl
Browse files Browse the repository at this point in the history
  • Loading branch information
junosuarez committed Oct 24, 2013
1 parent 439e69e commit 8b33129
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 32 deletions.
2 changes: 1 addition & 1 deletion index.js
Expand Up @@ -9,7 +9,7 @@ var Minq = module.exports = function Minq (provider) {
var self = this
this.provider = provider
this.ready = Q(this.provider.ready)
.then(this._initialize)
.then(this._initialize.bind(this))
.then(function (provider) {
return self
})
Expand Down
18 changes: 13 additions & 5 deletions mongodb.js
Expand Up @@ -38,6 +38,13 @@ proto.getCollectionNames = function () {
})
}

proto.dropCollection = function (collectionName) {
return this._collection({collection: collectionName})
.then(function (collection) {
return Q.ninvoke(collection, 'drop')
})
}

// (Query) => Promise
proto.run = function (query) {
var self = this
Expand Down Expand Up @@ -85,8 +92,7 @@ proto.runAsStream = function (query) {
self._collection(query).then(function (collection) {
self._find(collection, query)
.then(function (cursor) {
cursor.toStream().pipe(outStream)
outStream.end()
cursor.stream().pipe(outStream)
})
.then(null, function (err) {
process.nextTick(function () {
Expand Down Expand Up @@ -115,7 +121,9 @@ proto._read = function (collection, query) {

// (MongoCollection, Query) => Promise<MongoCursor>
proto._find = function(collection, query) {
return Q.ninvoke(collection, 'find', query.query)
query.options = query.options || {}
query.options.fields = query.options.fields || {}
return Q.ninvoke(collection, 'find', query.query, query.options)
}

// (Query) => Promise<MongoCollection>
Expand All @@ -127,12 +135,12 @@ proto._collection = function(query) {

// (MongoCollection, Query) => Promise<Number>
proto._count = function (collection, query) {
return collection.count(query.query)
return Q.ninvoke(collection, 'count', query.query)
}

// (MongoCollection?, Query) => Promise<Boolean>
proto._exists = function (collection, query) {
return this._count(query.query).then(function (count) {
return this._count(collection, query.query).then(function (count) {
return count > 0
})
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -29,7 +29,7 @@
"dependencies": {
"q": "~0.9.4",
"through": "~2.2.0",
"mongodb": "~1.2.12",
"mongodb": "~1.3.19",
"maskurl": "~0.1.0",
"quotemeta": "0.0.0",
"to-string": "~0.2.0",
Expand Down
14 changes: 6 additions & 8 deletions query.js
Expand Up @@ -8,7 +8,6 @@ var Query = module.exports = function Query(db){
collection: null,
command: 'read',
query: {},
projection: null,
options: {
safe: true
}
Expand All @@ -22,7 +21,6 @@ proto.clone = function () {
var q = new Query(this._.db)
q._.collection = this._.collection
q._.query = deepClone(this._.query)
q._.projection = deepClone(this._.projection)
q._.options = deepClone(this._.options)
return q
}
Expand All @@ -47,13 +45,13 @@ proto.not = function (key) {
proto.select = function (projection) {
// accept arrays of field names to include (dot-notation ok)
if (Array.isArray(projection)) {
this._.projection = projection.reduce(function (projection, field) {
projection[field] = true
return projection
}, {})
} else {
this._.projection = projection
return this.select(projection.reduce(function (projection, field) {
projection[field] = true
return projection
}, {}))
}

this._.options.fields = projection
return this
}
proto.limit = function (number) {
Expand Down
2 changes: 1 addition & 1 deletion test/index.test.js
Expand Up @@ -51,7 +51,7 @@ describe('Minq', function () {

var minq = new Minq(provider)
minq.ready.then(function () {
minq._initialize.should.have.been.called
minq._initialize.should.have.been.calledOn(minq)
})
.then(done, done)
})
Expand Down
126 changes: 115 additions & 11 deletions test/mongodb.test.js
Expand Up @@ -121,6 +121,29 @@ describe('MongoDb', function () {
})
})

describe('#dropCollection', function () {
it('calls underlying drop', function (done) {
var collection = {
drop: function (callback) {
collection.drop.called = true
process.nextTick(function () {
callback(null)
})
}
}

var mongodb = new MongoDb({})
mongodb._collection = sinon.stub().returns(Q(collection))

mongodb.dropCollection('foo').then(function (collectionNames) {
mongodb._collection.should.have.been.calledOnce
mongodb._collection.firstCall.args.should.deep.equal([{collection: 'foo'}])
collection.drop.called.should.equal(true)
})
.then(done, done)
})
})

describe('#run', function () {
['read','count','exists','insert', 'update', 'findAndModify', 'modifyAndFind', 'pull',
'upsert', 'remove', 'removeAll', 'aggregate'].forEach(function (command) {
Expand Down Expand Up @@ -200,11 +223,20 @@ describe('MongoDb', function () {

var q = StubQuery()
var mdb = new MongoDb()
var rs = new stream.Readable()
rs._read = function () {
}
rs._pipe = rs.pipe
rs.pipe = function () {
rs.pipe.called = true
rs.pipe.args = arguments
return rs._pipe.apply(rs, arguments)
}
var underlyingStream = {
pipe: sinon.spy()
pipe: sinon.stub().returns(rs)
}
var cursor = {
toStream: sinon.stub().returns(underlyingStream)
stream: sinon.stub().returns(rs)
}
var collection = {}
mdb._collection = sinon.stub().returns(Q(collection))
Expand All @@ -217,22 +249,27 @@ describe('MongoDb', function () {
mdb._collection.should.have.been.calledWithExactly(q)
mdb._find.should.have.been.calledOnce
mdb._find.should.have.been.calledWithExactly(collection, q)
cursor.toStream.should.have.been.calledOnce
underlyingStream.pipe.should.have.been.calledOnce
cursor.stream.should.have.been.calledOnce
rs.pipe.called.should.equal(true)
rs.pipe.args[0].should.equal(s)
done()
} catch (e) {
done(e)
}
})
s.on('error', done)

process.nextTick(function () {
rs.emit('end')
})

})
})

describe('#_find', function () {
it('calls find', function (done) {

var find = function (query, callback) {
var find = function (query, opts, callback) {
find.args = arguments
process.nextTick(function () {
callback(null, [])
Expand All @@ -251,6 +288,64 @@ describe('MongoDb', function () {
.then(done, done)

})
it('passes default options', function (done) {

var find = function (query, opts, callback) {
find.args = arguments
process.nextTick(function () {
callback(null, [])
})
}
var collection = {find:find}

var q = StubQuery()
q.collection = 'fooCollection'
q.query = {a: 1}
var mdb = new MongoDb()

mdb._find(collection, q).then(function () {
// necessary to return fields
find.args[1].fields.should.deep.equal({})
})
.then(done, done)

})
it('passes projection, skip, limit, and options', function (done) {

var find = function (query, opts, callback) {
find.args = arguments
process.nextTick(function () {
callback(null, [])
})
}
var collection = {find:find}

var q = StubQuery()
q.collection = 'fooCollection'
q.query = {a: 1}
q.projection = {a: true}
q.skip = 10
q.limit = 20
q.options = {
fields: {a: true},
skip: 10,
limit: 20,
explain: true
}
var mdb = new MongoDb()

mdb._find(collection, q).then(function () {
find.args[0].should.equal(q.query)
find.args[1].should.deep.equal({
fields: q.projection,
skip: 10,
limit: 20,
explain: true
})
})
.then(done, done)

})
})

describe('#_read', function () {
Expand Down Expand Up @@ -314,7 +409,12 @@ describe('MongoDb', function () {
describe('#_count', function () {
it('calls underlying count', function (done) {

var count = sinon.stub().returns(Q(108))
var count = function (query, callback) {
count.args = arguments
process.nextTick(function () {
callback(null, 108)
})
}
var collection = {count:count}

var q = StubQuery()
Expand All @@ -324,7 +424,7 @@ describe('MongoDb', function () {

mdb._count(collection, q).then(function (val) {
val.should.equal(108)
count.should.have.been.calledWithExactly(q.query)
count.args[0].should.equal(q.query)
})
.then(done, done)

Expand All @@ -341,9 +441,11 @@ describe('MongoDb', function () {
var mdb = new MongoDb()
mdb._count = count

mdb._exists({}, q).then(function (val) {
var collection = {}

mdb._exists(collection, q).then(function (val) {
val.should.equal(true)
count.should.have.been.calledWithExactly(q.query)
count.should.have.been.calledWithExactly(collection, q.query)

})
.then(done, done)
Expand All @@ -359,9 +461,11 @@ describe('MongoDb', function () {
var mdb = new MongoDb()
mdb._count = count

mdb._exists({}, q).then(function (val) {
var collection = {}

mdb._exists(collection, q).then(function (val) {
val.should.equal(false)
count.should.have.been.calledWithExactly(q.query)
count.should.have.been.calledWithExactly(collection, q.query)

})
.then(done, done)
Expand Down
9 changes: 4 additions & 5 deletions test/query.test.js
Expand Up @@ -18,7 +18,6 @@ describe('Query', function () {
q._.db.should.equal(db)
q._.should.have.property('collection')
q._.should.have.property('query')
q._.should.have.property('projection')
q._.should.have.property('options')

})
Expand Down Expand Up @@ -73,17 +72,17 @@ describe('Query', function () {
})

describe('#select', function () {
it('sets _.projection', function () {
it('sets _.options.fields', function () {
var q = new Query
expect(q._.projection).to.equal(null)
expect(q._.options.fields).to.equal(undefined)
q.select({_id: true, name: true})
.should.equal(q)
q._.projection.should.deep.equal({_id: true, name: true})
q._.options.fields.should.deep.equal({_id: true, name: true})
})
it('takes arrays of string field names', function () {
var q = new Query
q.select(['a','b','c'])
q._.projection.should.deep.equal({a: true, b: true, c: true})
q._.options.fields.should.deep.equal({a: true, b: true, c: true})
})
})

Expand Down

0 comments on commit 8b33129

Please sign in to comment.