2014-02-24 12:43:27 -05:00
|
|
|
sinon = require('sinon')
|
|
|
|
chai = require('chai')
|
|
|
|
should = chai.should()
|
|
|
|
expect = chai.expect
|
|
|
|
modulePath = "../../../../app/js/MongoManager.js"
|
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
{ObjectId} = require("mongojs")
|
|
|
|
|
|
|
|
describe "MongoManager", ->
|
|
|
|
beforeEach ->
|
|
|
|
@MongoManager = SandboxedModule.require modulePath, requires:
|
|
|
|
"./mongojs" : { db: @db = {}, ObjectId: ObjectId }
|
|
|
|
@callback = sinon.stub()
|
|
|
|
@doc_id = ObjectId().toString()
|
2014-03-19 12:40:55 -04:00
|
|
|
@project_id = ObjectId().toString()
|
2014-02-24 12:43:27 -05:00
|
|
|
|
|
|
|
describe "getLastCompressedUpdate", ->
|
|
|
|
beforeEach ->
|
|
|
|
@update = "mock-update"
|
|
|
|
@db.docHistory = {}
|
|
|
|
@db.docHistory.find = sinon.stub().returns @db.docHistory
|
|
|
|
@db.docHistory.sort = sinon.stub().returns @db.docHistory
|
|
|
|
@db.docHistory.limit = sinon.stub().returns @db.docHistory
|
|
|
|
@db.docHistory.toArray = sinon.stub().callsArgWith(0, null, [@update])
|
|
|
|
|
|
|
|
@MongoManager.getLastCompressedUpdate @doc_id, @callback
|
|
|
|
|
|
|
|
it "should find the updates for the doc", ->
|
|
|
|
@db.docHistory.find
|
|
|
|
.calledWith(doc_id: ObjectId(@doc_id))
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should limit to one result", ->
|
|
|
|
@db.docHistory.limit
|
|
|
|
.calledWith(1)
|
|
|
|
.should.equal true
|
|
|
|
|
2014-03-07 09:02:16 -05:00
|
|
|
it "should sort in descending version order", ->
|
2014-02-24 12:43:27 -05:00
|
|
|
@db.docHistory.sort
|
2014-03-07 09:02:16 -05:00
|
|
|
.calledWith(v: -1)
|
2014-02-24 12:43:27 -05:00
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should call the call back with the update", ->
|
|
|
|
@callback.calledWith(null, @update).should.equal true
|
|
|
|
|
|
|
|
describe "deleteCompressedUpdate", ->
|
|
|
|
beforeEach ->
|
|
|
|
@update_id = ObjectId().toString()
|
|
|
|
@db.docHistory =
|
|
|
|
remove: sinon.stub().callsArg(1)
|
|
|
|
@MongoManager.deleteCompressedUpdate(@update_id, @callback)
|
|
|
|
|
|
|
|
it "should remove the update", ->
|
|
|
|
@db.docHistory.remove
|
|
|
|
.calledWith(_id: ObjectId(@update_id))
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should call the callback", ->
|
|
|
|
@callback.called.should.equal true
|
|
|
|
|
|
|
|
describe "popLastCompressedUpdate", ->
|
|
|
|
describe "when there is no last update", ->
|
|
|
|
beforeEach ->
|
|
|
|
@MongoManager.getLastCompressedUpdate = sinon.stub().callsArgWith(1, null, null)
|
|
|
|
@MongoManager.deleteCompressedUpdate = sinon.stub()
|
|
|
|
@MongoManager.popLastCompressedUpdate @doc_id, @callback
|
|
|
|
|
|
|
|
it "should get the last update", ->
|
|
|
|
@MongoManager.getLastCompressedUpdate
|
|
|
|
.calledWith(@doc_id)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should not try to delete the last update", ->
|
|
|
|
@MongoManager.deleteCompressedUpdate.called.should.equal false
|
|
|
|
|
|
|
|
it "should call the callback with no update", ->
|
|
|
|
@callback.calledWith(null, null).should.equal true
|
|
|
|
|
|
|
|
describe "when there is an update", ->
|
|
|
|
beforeEach ->
|
|
|
|
@update = { _id: Object() }
|
|
|
|
@MongoManager.getLastCompressedUpdate = sinon.stub().callsArgWith(1, null, @update)
|
|
|
|
@MongoManager.deleteCompressedUpdate = sinon.stub().callsArgWith(1, null)
|
|
|
|
@MongoManager.popLastCompressedUpdate @doc_id, @callback
|
|
|
|
|
|
|
|
it "should get the last update", ->
|
|
|
|
@MongoManager.getLastCompressedUpdate
|
|
|
|
.calledWith(@doc_id)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should delete the last update", ->
|
|
|
|
@MongoManager.deleteCompressedUpdate
|
|
|
|
.calledWith(@update._id)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should call the callback with the update", ->
|
|
|
|
@callback.calledWith(null, @update).should.equal true
|
|
|
|
|
|
|
|
describe "insertCompressedUpdate", ->
|
|
|
|
beforeEach ->
|
|
|
|
@update = { op: "op", meta: "meta", v: "v"}
|
|
|
|
@db.docHistory =
|
|
|
|
insert: sinon.stub().callsArg(1)
|
2014-03-19 12:40:55 -04:00
|
|
|
@MongoManager.insertCompressedUpdate @project_id, @doc_id, @update, @callback
|
2014-02-24 12:43:27 -05:00
|
|
|
|
|
|
|
it "should insert the update", ->
|
|
|
|
@db.docHistory.insert
|
|
|
|
.calledWith({
|
2014-03-19 12:40:55 -04:00
|
|
|
project_id: ObjectId(@project_id),
|
2014-02-24 12:43:27 -05:00
|
|
|
doc_id: ObjectId(@doc_id),
|
|
|
|
op: @update.op,
|
|
|
|
meta: @update.meta,
|
|
|
|
v: @update.v
|
|
|
|
})
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should call the callback", ->
|
|
|
|
@callback.called.should.equal true
|
|
|
|
|
|
|
|
describe "insertCompressedUpdates", ->
|
|
|
|
beforeEach (done) ->
|
|
|
|
@updates = [ "mock-update-1", "mock-update-2" ]
|
2014-03-19 12:40:55 -04:00
|
|
|
@MongoManager.insertCompressedUpdate = sinon.stub().callsArg(3)
|
|
|
|
@MongoManager.insertCompressedUpdates @project_id, @doc_id, @updates, (args...) =>
|
2014-02-24 12:43:27 -05:00
|
|
|
@callback(args...)
|
|
|
|
done()
|
|
|
|
|
|
|
|
it "should insert each update", ->
|
|
|
|
for update in @updates
|
|
|
|
@MongoManager.insertCompressedUpdate
|
2014-03-19 12:40:55 -04:00
|
|
|
.calledWith(@project_id, @doc_id, update)
|
2014-02-24 12:43:27 -05:00
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should call the callback", ->
|
|
|
|
@callback.called.should.equal true
|
2014-03-04 08:02:48 -05:00
|
|
|
|
2014-03-19 13:44:16 -04:00
|
|
|
describe "getDocUpdates", ->
|
2014-03-04 08:02:48 -05:00
|
|
|
beforeEach ->
|
|
|
|
@updates = ["mock-update"]
|
|
|
|
@db.docHistory = {}
|
|
|
|
@db.docHistory.find = sinon.stub().returns @db.docHistory
|
|
|
|
@db.docHistory.sort = sinon.stub().returns @db.docHistory
|
2014-03-05 10:59:40 -05:00
|
|
|
@db.docHistory.limit = sinon.stub().returns @db.docHistory
|
2014-03-04 08:02:48 -05:00
|
|
|
@db.docHistory.toArray = sinon.stub().callsArgWith(0, null, @updates)
|
|
|
|
|
2014-03-06 05:45:51 -05:00
|
|
|
@from = 42
|
|
|
|
@to = 55
|
2014-03-04 08:02:48 -05:00
|
|
|
|
2014-03-06 05:45:51 -05:00
|
|
|
describe "with a to version", ->
|
2014-03-04 09:05:17 -05:00
|
|
|
beforeEach ->
|
2014-03-19 13:44:16 -04:00
|
|
|
@MongoManager.getDocUpdates @doc_id, from: @from, to: @to, @callback
|
2014-03-04 09:05:17 -05:00
|
|
|
|
2014-03-06 05:45:51 -05:00
|
|
|
it "should find the all updates between the to and from versions", ->
|
2014-03-04 09:05:17 -05:00
|
|
|
@db.docHistory.find
|
|
|
|
.calledWith({
|
|
|
|
doc_id: ObjectId(@doc_id)
|
2014-03-06 05:45:51 -05:00
|
|
|
v: { $gte: @from, $lte: @to }
|
2014-03-04 09:05:17 -05:00
|
|
|
})
|
|
|
|
.should.equal true
|
2014-03-04 08:02:48 -05:00
|
|
|
|
2014-03-06 05:45:51 -05:00
|
|
|
it "should sort in descending version order", ->
|
2014-03-04 09:05:17 -05:00
|
|
|
@db.docHistory.sort
|
2014-03-06 05:45:51 -05:00
|
|
|
.calledWith("v": -1)
|
2014-03-04 09:05:17 -05:00
|
|
|
.should.equal true
|
2014-03-04 08:02:48 -05:00
|
|
|
|
2014-03-05 10:59:40 -05:00
|
|
|
it "should not limit the results", ->
|
|
|
|
@db.docHistory.limit
|
|
|
|
.called.should.equal false
|
|
|
|
|
2014-03-04 09:05:17 -05:00
|
|
|
it "should call the call back with the updates", ->
|
|
|
|
@callback.calledWith(null, @updates).should.equal true
|
|
|
|
|
2014-03-06 05:45:51 -05:00
|
|
|
describe "without a to version", ->
|
2014-03-04 09:05:17 -05:00
|
|
|
beforeEach ->
|
2014-03-19 13:44:16 -04:00
|
|
|
@MongoManager.getDocUpdates @doc_id, from: @from, @callback
|
2014-03-04 09:05:17 -05:00
|
|
|
|
2014-03-06 05:45:51 -05:00
|
|
|
it "should find the all updates after the from version", ->
|
2014-03-04 09:05:17 -05:00
|
|
|
@db.docHistory.find
|
|
|
|
.calledWith({
|
|
|
|
doc_id: ObjectId(@doc_id)
|
2014-03-06 05:45:51 -05:00
|
|
|
v: { $gte: @from }
|
2014-03-04 09:05:17 -05:00
|
|
|
})
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should call the call back with the updates", ->
|
|
|
|
@callback.calledWith(null, @updates).should.equal true
|
2014-03-04 08:02:48 -05:00
|
|
|
|
2014-03-05 10:59:40 -05:00
|
|
|
describe "with a limit", ->
|
|
|
|
beforeEach ->
|
2014-03-19 13:44:16 -04:00
|
|
|
@MongoManager.getDocUpdates @doc_id, from: @from, limit: @limit = 10, @callback
|
2014-03-05 10:59:40 -05:00
|
|
|
|
|
|
|
it "should limit the results", ->
|
|
|
|
@db.docHistory.limit
|
|
|
|
.calledWith(@limit)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
|
2014-03-19 13:44:16 -04:00
|
|
|
describe "getDocUpdates", ->
|
|
|
|
beforeEach ->
|
|
|
|
@updates = ["mock-update"]
|
|
|
|
@db.docHistory = {}
|
|
|
|
@db.docHistory.find = sinon.stub().returns @db.docHistory
|
|
|
|
@db.docHistory.sort = sinon.stub().returns @db.docHistory
|
|
|
|
@db.docHistory.limit = sinon.stub().returns @db.docHistory
|
|
|
|
@db.docHistory.toArray = sinon.stub().callsArgWith(0, null, @updates)
|
|
|
|
|
|
|
|
@before = Date.now()
|
|
|
|
|
|
|
|
describe "with a before timestamp", ->
|
|
|
|
beforeEach ->
|
|
|
|
@MongoManager.getProjectUpdates @project_id, before: @before, @callback
|
|
|
|
|
|
|
|
it "should find the all updates before the timestamp", ->
|
|
|
|
@db.docHistory.find
|
|
|
|
.calledWith({
|
|
|
|
project_id: ObjectId(@project_id)
|
|
|
|
"meta.end_ts": { $lt: @before }
|
|
|
|
})
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should sort in descending version order", ->
|
|
|
|
@db.docHistory.sort
|
|
|
|
.calledWith("meta.end_ts": -1)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should not limit the results", ->
|
|
|
|
@db.docHistory.limit
|
|
|
|
.called.should.equal false
|
|
|
|
|
|
|
|
it "should call the call back with the updates", ->
|
|
|
|
@callback.calledWith(null, @updates).should.equal true
|
|
|
|
|
|
|
|
describe "without a before timestamp", ->
|
|
|
|
beforeEach ->
|
|
|
|
@MongoManager.getProjectUpdates @project_id, {}, @callback
|
|
|
|
|
|
|
|
it "should find the all updates", ->
|
|
|
|
@db.docHistory.find
|
|
|
|
.calledWith({
|
|
|
|
project_id: ObjectId(@project_id)
|
|
|
|
})
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should call the call back with the updates", ->
|
|
|
|
@callback.calledWith(null, @updates).should.equal true
|
|
|
|
|
|
|
|
describe "with a limit", ->
|
|
|
|
beforeEach ->
|
|
|
|
@MongoManager.getProjectUpdates @project_id, before: @before, limit: @limit = 10, @callback
|
|
|
|
|
|
|
|
it "should limit the results", ->
|
|
|
|
@db.docHistory.limit
|
|
|
|
.calledWith(@limit)
|
|
|
|
.should.equal true
|