Fix ShareJsDB tests

This commit is contained in:
James Allen
2016-12-01 17:14:40 +00:00
parent b6c93c718d
commit 889f5fdf9f
5 changed files with 93 additions and 183 deletions
@@ -1,8 +1,6 @@
Keys = require('./UpdateKeys')
Settings = require('settings-sharelatex')
RedisManager = require "./RedisManager"
Errors = require "./Errors"
logger = require "logger-sharelatex"
module.exports = class ShareJsDB
constructor: (@project_id, @doc_id, @lines, @version) ->
@@ -1,55 +0,0 @@
sinon = require('sinon')
chai = require('chai')
should = chai.should()
modulePath = "../../../../app/js/ShareJsDB.js"
SandboxedModule = require('sandboxed-module')
describe "ShareJsDB.getOps", ->
beforeEach ->
@doc_id = "document-id"
@project_id = "project-id"
@doc_key = "#{@project_id}:#{@doc_id}"
@callback = sinon.stub()
@ops = [{p: 20, t: "foo"}]
@redis_ops = (JSON.stringify(op) for op in @ops)
@ShareJsDB = SandboxedModule.require modulePath, requires:
"./RedisManager": @RedisManager = {}
"./DocumentManager":{}
"logger-sharelatex": {}
@db = new @ShareJsDB()
describe "with start == end", ->
beforeEach ->
@start = @end = 42
@db.getOps @doc_key, @start, @end, @callback
it "should return an empty array", ->
@callback.calledWith(null, []).should.equal true
describe "with a non empty range", ->
beforeEach ->
@start = 35
@end = 42
@RedisManager.getPreviousDocOps = sinon.stub().callsArgWith(3, null, @ops)
@db.getOps @doc_key, @start, @end, @callback
it "should get the range from redis", ->
@RedisManager.getPreviousDocOps
.calledWith(@doc_id, @start, @end-1)
.should.equal true
it "should return the ops", ->
@callback.calledWith(null, @ops).should.equal true
describe "with no specified end", ->
beforeEach ->
@start = 35
@end = null
@RedisManager.getPreviousDocOps = sinon.stub().callsArgWith(3, null, @ops)
@db.getOps @doc_key, @start, @end, @callback
it "should get until the end of the list", ->
@RedisManager.getPreviousDocOps
.calledWith(@doc_id, @start, -1)
.should.equal true
@@ -1,87 +0,0 @@
sinon = require('sinon')
chai = require('chai')
should = chai.should()
expect = chai.expect
modulePath = "../../../../app/js/ShareJsDB.js"
SandboxedModule = require('sandboxed-module')
Errors = require "../../../../app/js/Errors"
describe "ShareJsDB.getSnapshot", ->
beforeEach ->
@doc_id = "document-id"
@project_id = "project-id"
@doc_key = "#{@project_id}:#{@doc_id}"
@callback = sinon.stub()
@ShareJsDB = SandboxedModule.require modulePath, requires:
"./DocumentManager": @DocumentManager = {}
"./RedisManager": {}
"./DocOpsManager": {}
"logger-sharelatex": {}
@db = new @ShareJsDB()
@version = 42
describe "with a text document", ->
beforeEach ->
@lines = ["one", "two", "three"]
describe "successfully", ->
beforeEach ->
@DocumentManager.getDoc = sinon.stub().callsArgWith(2, null, @lines, @version)
@db.getSnapshot @doc_key, @callback
it "should get the doc", ->
@DocumentManager.getDoc
.calledWith(@project_id, @doc_id)
.should.equal true
it "should return the doc lines", ->
@callback.args[0][1].snapshot.should.equal @lines.join("\n")
it "should return the doc version", ->
@callback.args[0][1].v.should.equal @version
it "should return the type as text", ->
@callback.args[0][1].type.should.equal "text"
describe "when the doclines do not exist", ->
beforeEach ->
@DocumentManager.getDoc = sinon.stub().callsArgWith(2, null, null, null)
@db.getSnapshot @doc_key, @callback
it "should return the callback with a NotFoundError", ->
@callback.calledWith(new Errors.NotFoundError("not found")).should.equal true
describe "when getDoc returns an error", ->
beforeEach ->
@DocumentManager.getDoc = sinon.stub().callsArgWith(2, @error = new Error("oops"), null, null)
@db.getSnapshot @doc_key, @callback
it "should return the callback with an error", ->
@callback.calledWith(@error).should.equal true
describe "with a JSON document", ->
beforeEach ->
@lines = [{text: "one"}, {text:"two"}, {text:"three"}]
describe "successfully", ->
beforeEach ->
@DocumentManager.getDoc = sinon.stub().callsArgWith(2, null, @lines, @version)
@db.getSnapshot @doc_key, @callback
it "should get the doc", ->
@DocumentManager.getDoc
.calledWith(@project_id, @doc_id)
.should.equal true
it "should return the doc lines", ->
expect(@callback.args[0][1].snapshot).to.deep.equal lines: @lines
it "should return the doc version", ->
@callback.args[0][1].v.should.equal @version
it "should return the type as text", ->
@callback.args[0][1].type.should.equal "json"
@@ -0,0 +1,93 @@
sinon = require('sinon')
chai = require('chai')
should = chai.should()
expect = chai.expect
modulePath = "../../../../app/js/ShareJsDB.js"
SandboxedModule = require('sandboxed-module')
Errors = require "../../../../app/js/Errors"
describe "ShareJsDB", ->
beforeEach ->
@doc_id = "document-id"
@project_id = "project-id"
@doc_key = "#{@project_id}:#{@doc_id}"
@callback = sinon.stub()
@ShareJsDB = SandboxedModule.require modulePath, requires:
"./RedisManager": @RedisManager = {}
@version = 42
@lines = ["one", "two", "three"]
@db = new @ShareJsDB(@project_id, @doc_id, @lines, @version)
describe "getSnapshot", ->
describe "successfully", ->
beforeEach ->
@db.getSnapshot @doc_key, @callback
it "should return the doc lines", ->
@callback.args[0][1].snapshot.should.equal @lines.join("\n")
it "should return the doc version", ->
@callback.args[0][1].v.should.equal @version
it "should return the type as text", ->
@callback.args[0][1].type.should.equal "text"
describe "when the key does not match", ->
beforeEach ->
@db.getSnapshot "bad:key", @callback
it "should return the callback with a NotFoundError", ->
@callback.calledWith(new Errors.NotFoundError("not found")).should.equal true
describe "getOps", ->
describe "with start == end", ->
beforeEach ->
@start = @end = 42
@db.getOps @doc_key, @start, @end, @callback
it "should return an empty array", ->
@callback.calledWith(null, []).should.equal true
describe "with a non empty range", ->
beforeEach ->
@start = 35
@end = 42
@RedisManager.getPreviousDocOps = sinon.stub().callsArgWith(3, null, @ops)
@db.getOps @doc_key, @start, @end, @callback
it "should get the range from redis", ->
@RedisManager.getPreviousDocOps
.calledWith(@doc_id, @start, @end-1)
.should.equal true
it "should return the ops", ->
@callback.calledWith(null, @ops).should.equal true
describe "with no specified end", ->
beforeEach ->
@start = 35
@end = null
@RedisManager.getPreviousDocOps = sinon.stub().callsArgWith(3, null, @ops)
@db.getOps @doc_key, @start, @end, @callback
it "should get until the end of the list", ->
@RedisManager.getPreviousDocOps
.calledWith(@doc_id, @start, -1)
.should.equal true
describe "writeOps", ->
describe "writing an op", ->
beforeEach ->
@opData =
op: {p: 20, t: "foo"}
meta: {source: "bar"}
v: @version
@db.writeOp @doc_key, @opData, @callback
it "should write into appliedOps", ->
expect(@db.appliedOps[@doc_key]).to.deep.equal [@opData]
it "should call the callback without an error", ->
@callback.called.should.equal true
(@callback.args[0][0]?).should.equal false
@@ -1,39 +0,0 @@
sinon = require('sinon')
chai = require('chai')
expect = chai.expect
should = chai.should()
modulePath = "../../../../app/js/ShareJsDB.js"
SandboxedModule = require('sandboxed-module')
describe "ShareJsDB.writeOps", ->
beforeEach ->
@project_id = "project-id"
@doc_id = "document-id"
@doc_key = "#{@project_id}:#{@doc_id}"
@callback = sinon.stub()
@opData =
op: {p: 20, t: "foo"}
meta: {source: "bar"}
@ShareJsDB = SandboxedModule.require modulePath, requires:
"./RedisManager": @RedisManager = {}
"./DocOpsManager": @DocOpsManager = {}
"./DocumentManager": {}
"logger-sharelatex": @logger = {error: sinon.stub()}
@db = new @ShareJsDB()
describe "writing an op", ->
beforeEach ->
@version = 42
@opData.v = @version
@db.writeOp @doc_key, @opData, @callback
it "should write into appliedOps", ->
expect(@db.appliedOps[@doc_key]).to.deep.equal [@opData]
it "should call the callback without an error", ->
@callback.called.should.equal true
(@callback.args[0][0]?).should.equal false