getDocVersionInMongoTests.coffee 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. modulePath = "../../../../app/js/PersistenceManager.js"
  5. SandboxedModule = require('sandboxed-module')
  6. Errors = require "../../../../app/js/Errors"
  7. {ObjectId} = require("mongojs")
  8. describe "PersistenceManager.getDocVersionInMongo", ->
  9. beforeEach ->
  10. @PersistenceManager = SandboxedModule.require modulePath, requires:
  11. "request": @request = sinon.stub()
  12. "settings-sharelatex": @Settings = {}
  13. "./Metrics": @Metrics =
  14. Timer: class Timer
  15. done: sinon.stub()
  16. "./mongojs":
  17. db: @db = { docOps: {} }
  18. ObjectId: ObjectId
  19. "logger-sharelatex": @logger = {log: sinon.stub(), err: sinon.stub()}
  20. @doc_id = ObjectId().toString()
  21. @callback = sinon.stub()
  22. describe "getDocVersionInMongo", ->
  23. describe "when the doc exists", ->
  24. beforeEach ->
  25. @doc =
  26. version: @version = 42
  27. @db.docOps.find = sinon.stub().callsArgWith(2, null, [@doc])
  28. @PersistenceManager.getDocVersionInMongo @doc_id, @callback
  29. it "should look for the doc in the database", ->
  30. @db.docOps.find
  31. .calledWith({ doc_id: ObjectId(@doc_id) }, {version: 1})
  32. .should.equal true
  33. it "should call the callback with the version", ->
  34. @callback.calledWith(null, @version).should.equal true
  35. describe "when the doc doesn't exist", ->
  36. beforeEach ->
  37. @db.docOps.find = sinon.stub().callsArgWith(2, null, [])
  38. @PersistenceManager.getDocVersionInMongo @doc_id, @callback
  39. it "should call the callback with 0", ->
  40. @callback.calledWith(null, 0).should.equal true