Clean up and speed up unit tests by making sure requires are mocked

This commit is contained in:
James Allen 2016-01-20 17:36:06 +00:00
parent af5d01e440
commit 3c7c318ea0
36 changed files with 53 additions and 67 deletions

View file

@ -4,7 +4,7 @@ should = chai.should()
modulePath = "../../../../app/js/DocumentManager.js"
SandboxedModule = require('sandboxed-module')
describe "DocumentUpdater - flushAndDeleteDoc", ->
describe "DocumentUpdater.flushAndDeleteDoc", ->
beforeEach ->
@DocumentManager = SandboxedModule.require modulePath, requires:
"./RedisManager": @RedisManager = {}

View file

@ -4,7 +4,7 @@ should = chai.should()
modulePath = "../../../../app/js/DocumentManager.js"
SandboxedModule = require('sandboxed-module')
describe "DocumentUpdater - flushDocIfLoaded", ->
describe "DocumentManager.flushDocIfLoaded", ->
beforeEach ->
@DocumentManager = SandboxedModule.require modulePath, requires:
"./RedisManager": @RedisManager = {}
@ -14,6 +14,7 @@ describe "DocumentUpdater - flushDocIfLoaded", ->
"./Metrics": @Metrics =
Timer: class Timer
done: sinon.stub()
"./TrackChangesManager": {}
@project_id = "project-id-123"
@doc_id = "doc-id-123"
@lines = ["one", "two", "three"]

View file

@ -4,7 +4,7 @@ should = chai.should()
modulePath = "../../../../app/js/DocumentManager.js"
SandboxedModule = require('sandboxed-module')
describe "DocumentUpdater - getDocAndRecentOps", ->
describe "DocumentUpdater.getDocAndRecentOps", ->
beforeEach ->
@DocumentManager = SandboxedModule.require modulePath, requires:
"./RedisManager": @RedisManager = {}
@ -14,6 +14,7 @@ describe "DocumentUpdater - getDocAndRecentOps", ->
"./Metrics": @Metrics =
Timer: class Timer
done: sinon.stub()
"./TrackChangesManager": {}
@project_id = "project-id-123"
@doc_id = "doc-id-123"

View file

@ -4,7 +4,7 @@ should = chai.should()
modulePath = "../../../../app/js/DocumentManager.js"
SandboxedModule = require('sandboxed-module')
describe "DocumentUpdater - getDoc", ->
describe "DocumentUpdater.getDoc", ->
beforeEach ->
@DocumentManager = SandboxedModule.require modulePath, requires:
"./RedisManager": @RedisManager = {}
@ -14,6 +14,7 @@ describe "DocumentUpdater - getDoc", ->
"./Metrics": @Metrics =
Timer: class Timer
done: sinon.stub()
"./TrackChangesManager": {}
@project_id = "project-id-123"
@doc_id = "doc-id-123"

View file

@ -4,7 +4,7 @@ should = chai.should()
modulePath = "../../../../app/js/DocumentManager.js"
SandboxedModule = require('sandboxed-module')
describe "DocumentManager - setDoc", ->
describe "DocumentManager.setDoc", ->
beforeEach ->
@DocumentManager = SandboxedModule.require modulePath, requires:
"./RedisManager": @RedisManager = {}
@ -16,6 +16,7 @@ describe "DocumentManager - setDoc", ->
"./Metrics": @Metrics =
Timer: class Timer
done: sinon.stub()
"./TrackChangesManager": {}
@project_id = "project-id-123"
@doc_id = "doc-id-123"

View file

@ -1,42 +0,0 @@
assert = require('assert')
should = require('chai').should()
path = require('path')
modulePath = path.join __dirname, '../../../app/js/RedisManager.js'
_ = require('underscore')
SandboxedModule = require('sandboxed-module')
keys = require(path.join __dirname, '../../../app/js/RedisKeyBuilder.js')
describe 'getting entire list of pending updates', ()->
doc_id = 123
redisMemory = {}
correctUpdates = [{"update1"}, {"update2"}, {"update3"}]
jsonCorrectUpdates = _.map correctUpdates, (d)-> JSON.stringify d
redisMemory[keys.pendingUpdates(doc_id:doc_id)] = jsonCorrectUpdates
redisMemory[keys.pendingUpdates(doc_id:"notThis")] = JSON.stringify([{"updatex"}, {"updatez"}])
redisReturn = []
mocks =
"redis-sharelatex":
createClient: ()->
auth:->
multi: ()->
lrange:(key, start, end)->
key.should.equal(keys.pendingUpdates(doc_id:doc_id))
start.should.equal(0)
end.should.equal(-1)
redisReturn.push(redisMemory[key])
del : (key)->
key.should.equal(keys.pendingUpdates(doc_id:doc_id))
redisReturn.push(1)
exec: (callback)->
callback(null, redisReturn)
redisManager = SandboxedModule.require(modulePath, requires: mocks)
it 'should have 3 elements in array', (done)->
redisManager.getPendingUpdatesForDoc doc_id, (err, listOfUpdates)->
listOfUpdates.length.should.equal(3)
done()

View file

@ -5,7 +5,7 @@ modulePath = "../../../../app/js/HttpController.js"
SandboxedModule = require('sandboxed-module')
Errors = require "../../../../app/js/Errors.js"
describe "HttpController - deleteProject", ->
describe "HttpController.deleteProject", ->
beforeEach ->
@HttpController = SandboxedModule.require modulePath, requires:
"./DocumentManager": @DocumentManager = {}

View file

@ -5,7 +5,7 @@ modulePath = "../../../../app/js/HttpController.js"
SandboxedModule = require('sandboxed-module')
Errors = require "../../../../app/js/Errors.js"
describe "HttpController - flushAndDeleteDoc", ->
describe "HttpController.flushAndDeleteDoc", ->
beforeEach ->
@HttpController = SandboxedModule.require modulePath, requires:
"./DocumentManager": @DocumentManager = {}

View file

@ -5,7 +5,7 @@ modulePath = "../../../../app/js/HttpController.js"
SandboxedModule = require('sandboxed-module')
Errors = require "../../../../app/js/Errors.js"
describe "HttpController - flushDocIfLoaded", ->
describe "HttpController.flushDocIfLoaded", ->
beforeEach ->
@HttpController = SandboxedModule.require modulePath, requires:
"./DocumentManager": @DocumentManager = {}

View file

@ -5,7 +5,7 @@ modulePath = "../../../../app/js/HttpController.js"
SandboxedModule = require('sandboxed-module')
Errors = require "../../../../app/js/Errors.js"
describe "HttpController - flushProject", ->
describe "HttpController.flushProject", ->
beforeEach ->
@HttpController = SandboxedModule.require modulePath, requires:
"./DocumentManager": @DocumentManager = {}

View file

@ -5,7 +5,7 @@ modulePath = "../../../../app/js/HttpController.js"
SandboxedModule = require('sandboxed-module')
Errors = require "../../../../app/js/Errors.js"
describe "HttpController - getDoc", ->
describe "HttpController.getDoc", ->
beforeEach ->
@HttpController = SandboxedModule.require modulePath, requires:
"./DocumentManager": @DocumentManager = {}

View file

@ -5,7 +5,7 @@ modulePath = "../../../../app/js/HttpController.js"
SandboxedModule = require('sandboxed-module')
Errors = require "../../../../app/js/Errors.js"
describe "HttpController - setDoc", ->
describe "HttpController.setDoc", ->
beforeEach ->
@HttpController = SandboxedModule.require modulePath, requires:
"./DocumentManager": @DocumentManager = {}

View file

@ -27,6 +27,7 @@ describe 'Lock Manager - checking the lock', ()->
expire: exireStub
set: setStub
exec: execStub
"./Metrics": {inc: () ->}
LockManager = SandboxedModule.require(modulePath, requires: mocks)
it 'should check if lock exists but not set or expire', (done)->

View file

@ -18,6 +18,7 @@ describe 'LockManager - releasing the lock', ()->
createClient : ()->
auth:->
del:deleteStub
"./Metrics": {inc: () ->}
LockManager = SandboxedModule.require(modulePath, requires: mocks)

View file

@ -11,6 +11,7 @@ describe 'LockManager - getting the lock', ->
"redis-sharelatex":
createClient : () =>
auth:->
"./Metrics": {inc: () ->}
@callback = sinon.stub()
@doc_id = "doc-id-123"

View file

@ -12,6 +12,8 @@ describe 'LockManager - trying the lock', ->
createClient : () =>
auth:->
set: @set = sinon.stub()
"./Metrics": {inc: () ->}
@callback = sinon.stub()
@doc_id = "doc-id-123"

View file

@ -13,6 +13,7 @@ describe "PersistenceManager.getDocFromWeb", ->
"./Metrics": @Metrics =
Timer: class Timer
done: sinon.stub()
"logger-sharelatex": @logger = {log: sinon.stub(), err: sinon.stub()}
@project_id = "project-id-123"
@doc_id = "doc-id-123"
@lines = ["one", "two", "three"]

View file

@ -17,6 +17,7 @@ describe "PersistenceManager.getDocVersionInMongo", ->
"./mongojs":
db: @db = { docOps: {} }
ObjectId: ObjectId
"logger-sharelatex": @logger = {log: sinon.stub(), err: sinon.stub()}
@doc_id = ObjectId().toString()
@callback = sinon.stub()

View file

@ -13,6 +13,7 @@ describe "PersistenceManager.setDocInWeb", ->
"./Metrics": @Metrics =
Timer: class Timer
done: sinon.stub()
"logger-sharelatex": @logger = {log: sinon.stub(), err: sinon.stub()}
@project_id = "project-id-123"
@doc_id = "doc-id-123"
@lines = ["one", "two", "three"]

View file

@ -17,6 +17,7 @@ describe "PersistenceManager.getDocVersionInMongo", ->
"./mongojs":
db: @db = { docOps: {} }
ObjectId: ObjectId
"logger-sharelatex": @logger = {log: sinon.stub(), err: sinon.stub()}
@doc_id = ObjectId().toString()
@callback = sinon.stub()

View file

@ -13,6 +13,7 @@ describe "RedisManager.clearDocFromPendingUpdatesSet", ->
"redis-sharelatex" : createClient: () =>
@rclient ?= auth:-> # only assign one rclient
"logger-sharelatex": {}
"./ZipManager": {}
@rclient.srem = sinon.stub().callsArg(2)
@RedisManager.clearDocFromPendingUpdatesSet(@project_id, @doc_id, @callback)

View file

@ -2,11 +2,11 @@ require('coffee-script')
assert = require('assert')
should = require('chai').should()
path = require('path')
modulePath = path.join __dirname, '../../../app/js/RedisManager.js'
keys = require(path.join __dirname, '../../../app/js/RedisKeyBuilder.js')
modulePath = path.join __dirname, '../../../../app/js/RedisManager.js'
keys = require(path.join __dirname, '../../../../app/js/RedisKeyBuilder.js')
SandboxedModule = require('sandboxed-module')
describe 'getting cound of docs from memory', ()->
describe 'RedisManager.getCountOfDocsInMemory', ()->
project_id = "12345"
doc_id1 = "docid1"
@ -18,6 +18,7 @@ describe 'getting cound of docs from memory', ()->
beforeEach (done)->
mocks =
"./ZipManager": {}
"logger-sharelatex": log:->
"redis-sharelatex":
createClient : ()->

View file

@ -1,7 +1,7 @@
sinon = require('sinon')
chai = require('chai')
should = chai.should()
modulePath = "../../../app/js/RedisManager.js"
modulePath = "../../../../app/js/RedisManager.js"
SandboxedModule = require('sandboxed-module')
describe 'RedisManager.getDoc', ->
@ -11,6 +11,7 @@ describe 'RedisManager.getDoc', ->
@rclient.multi = () => @rclient
@RedisManager = SandboxedModule.require modulePath, requires:
"logger-sharelatex": {}
"redis-sharelatex": @redis =
createClient: () => @rclient

View file

@ -8,6 +8,7 @@ describe "RedisManager.getDocsWithPendingUpdates", ->
beforeEach ->
@callback = sinon.stub()
@RedisManager = SandboxedModule.require modulePath, requires:
"./ZipManager": {}
"redis-sharelatex" : createClient: () =>
@rclient ?= auth:->
"logger-sharelatex": {}

View file

@ -7,6 +7,7 @@ SandboxedModule = require('sandboxed-module')
describe "RedisManager.getPendingUpdatesForDoc", ->
beforeEach ->
@RedisManager = SandboxedModule.require modulePath, requires:
"./ZipManager": {}
"redis-sharelatex": createClient: () =>
@rclient =
auth: () ->

View file

@ -8,6 +8,7 @@ describe "RedisManager.getPreviousDocOpsTests", ->
beforeEach ->
@callback = sinon.stub()
@RedisManager = SandboxedModule.require modulePath, requires:
"./ZipManager": {}
"redis-sharelatex" : createClient: () =>
@rclient ?=
auth: ->

View file

@ -2,17 +2,19 @@ assert = require('chai').assert
sinon = require('sinon')
chai = require('chai')
should = chai.should()
modulePath = "../../../app/js/RedisManager.js"
modulePath = "../../../../app/js/RedisManager.js"
SandboxedModule = require('sandboxed-module')
doc_id = "1234"
describe 'Document Manager - getUpdatesLength ', ->
describe 'Redis Manager.getUpdatesLength ', ->
beforeEach ->
@llenStub = sinon.stub()
@redisManager = SandboxedModule.require modulePath, requires:
"./ZipManager": {}
"logger-sharelatex": {}
"redis-sharelatex":
createClient:=>
auth:->

View file

@ -7,6 +7,7 @@ SandboxedModule = require('sandboxed-module')
describe "RedisManager.pushDocOp", ->
beforeEach ->
@RedisManager = SandboxedModule.require modulePath, requires:
"./ZipManager": {}
"redis-sharelatex": createClient: () =>
@rclient ?=
auth: () ->

View file

@ -7,6 +7,7 @@ SandboxedModule = require('sandboxed-module')
describe "RedisManager.pushUncompressedHistoryOp", ->
beforeEach ->
@RedisManager = SandboxedModule.require modulePath, requires:
"./ZipManager": {}
"redis-sharelatex": createClient: () =>
@rclient ?=
auth: () ->

View file

@ -1,13 +1,13 @@
require('coffee-script')
assert = require('assert')
path = require('path')
modulePath = path.join __dirname, '../../../app/js/RedisManager.js'
keys = require(path.join __dirname, '../../../app/js/RedisKeyBuilder.js')
modulePath = path.join __dirname, '../../../../app/js/RedisManager.js'
keys = require(path.join __dirname, '../../../../app/js/RedisKeyBuilder.js')
project_id = 1234
doc_id = 5678
SandboxedModule = require('sandboxed-module')
describe 'putting a doc into memory', ()->
describe 'RedisManager.putDocInMemory', ()->
lines = ["this is one line", "and another line"]
version = 42
@ -21,6 +21,7 @@ describe 'putting a doc into memory', ()->
potentialSAdds[keys.docsInProject(project_id:project_id)] = doc_id
mocks =
"./ZipManager": {}
"logger-sharelatex": log:->
"redis-sharelatex":
createClient : ()->

View file

@ -3,11 +3,11 @@ _ = require("underscore")
assert = require('assert')
sinon = require('sinon')
path = require('path')
modulePath = path.join __dirname, '../../../app/js/RedisManager.js'
keys = require(path.join __dirname, '../../../app/js/RedisKeyBuilder.js')
modulePath = path.join __dirname, '../../../../app/js/RedisManager.js'
keys = require(path.join __dirname, '../../../../app/js/RedisKeyBuilder.js')
SandboxedModule = require('sandboxed-module')
describe 'removing single doc from memory', ()->
describe 'RedisManager.removeDocFromMemory', ()->
project_id = "12345"
doc_id1 = "docid1"
@ -21,6 +21,7 @@ describe 'removing single doc from memory', ()->
redisMemory = {}
mocks =
"./ZipManager": {}
"logger-sharelatex":
error:->
log:->

View file

@ -16,6 +16,7 @@ describe "ShareJsDB.getOps", ->
"./RedisManager": @RedisManager = {}
"./DocOpsManager": @DocOpsManager = {}
"./DocumentManager":{}
"logger-sharelatex": {}
describe "with start == end", ->
beforeEach ->

View file

@ -16,6 +16,7 @@ describe "ShareJsDB.getSnapshot", ->
"./DocumentManager": @DocumentManager = {}
"./RedisManager": {}
"./DocOpsManager": {}
"logger-sharelatex": {}
@version = 42

View file

@ -17,6 +17,7 @@ describe "ShareJsDB.writeOps", ->
"./RedisManager": @RedisManager = {}
"./DocOpsManager": @DocOpsManager = {}
"./DocumentManager": {}
"logger-sharelatex": @logger = {error: sinon.stub()}
describe "writing an op", ->
beforeEach ->

View file

@ -1,7 +1,7 @@
sinon = require('sinon')
chai = require('chai')
should = chai.should()
modulePath = "../../../app/js/ShareJsUpdateManager.js"
modulePath = "../../../../app/js/ShareJsUpdateManager.js"
SandboxedModule = require('sandboxed-module')
describe "ShareJsUpdateManager", ->

View file

@ -101,7 +101,7 @@ describe "ZipManager with RedisManager", ->
@docLines = []
while @docLines.join('').length <= MIN_SIZE
@docLines.push "this is a long line in a long document"
console.log "length of doclines", @docLines.join('').length
# console.log "length of doclines", @docLines.join('').length
@callback = sinon.stub()
@RedisManager.setDocument @doc_id, @docLines, @version, () =>
@callback()