getDocTests.coffee 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. modulePath = "../../../../app/js/DocumentManager.js"
  5. SandboxedModule = require('sandboxed-module')
  6. describe "DocumentUpdater - getDoc", ->
  7. beforeEach ->
  8. @DocumentManager = SandboxedModule.require modulePath, requires:
  9. "./RedisManager": @RedisManager = {}
  10. "./PersistenceManager": @PersistenceManager = {}
  11. "./DocOpsManager": @DocOpsManager = {}
  12. "logger-sharelatex": @logger = {log: sinon.stub()}
  13. "./Metrics": @Metrics =
  14. Timer: class Timer
  15. done: sinon.stub()
  16. @project_id = "project-id-123"
  17. @doc_id = "doc-id-123"
  18. @lines = ["one", "two", "three"]
  19. @version = 42
  20. @callback = sinon.stub()
  21. describe "when the doc exists in Redis", ->
  22. beforeEach ->
  23. @RedisManager.getDoc = sinon.stub().callsArgWith(1, null, @lines, @version)
  24. @DocumentManager.getDoc @project_id, @doc_id, @callback
  25. it "should get the doc from Redis", ->
  26. @RedisManager.getDoc
  27. .calledWith(@doc_id)
  28. .should.equal true
  29. it "should call the callback with the doc info", ->
  30. @callback.calledWith(null, @lines, @version).should.equal true
  31. it "should time the execution", ->
  32. @Metrics.Timer::done.called.should.equal true
  33. describe "when the doc does not exist in Redis", ->
  34. beforeEach ->
  35. @RedisManager.getDoc = sinon.stub().callsArgWith(1, null, null, null)
  36. @PersistenceManager.getDoc = sinon.stub().callsArgWith(2, null, @lines, @version)
  37. @RedisManager.putDocInMemory = sinon.stub().callsArg(4)
  38. @DocumentManager.getDoc @project_id, @doc_id, @callback
  39. it "should try to get the doc from Redis", ->
  40. @RedisManager.getDoc
  41. .calledWith(@doc_id)
  42. .should.equal true
  43. it "should get the doc from the PersistenceManager", ->
  44. @PersistenceManager.getDoc
  45. .calledWith(@project_id, @doc_id)
  46. .should.equal true
  47. it "should set the doc in Redis", ->
  48. @RedisManager.putDocInMemory
  49. .calledWith(@project_id, @doc_id, @lines, @version)
  50. .should.equal true
  51. it "should call the callback with the doc info", ->
  52. @callback.calledWith(null, @lines, @version).should.equal true
  53. it "should time the execution", ->
  54. @Metrics.Timer::done.called.should.equal true