Add tests for RangeManager

This commit is contained in:
James Allen 2016-12-05 15:38:38 +00:00
parent b8a97731e4
commit 2985cb587a
2 changed files with 164 additions and 0 deletions

View file

@ -22,6 +22,8 @@ module.exports = RangeManager =
change.id = @_safeObjectId(change.id)
if change.metadata?.ts?
change.metadata.ts = new Date(change.metadata.ts)
if change.metadata?.user_id?
change.metadata.user_id = @_safeObjectId(change.metadata.user_id)
for comment in ranges.comments or []
comment.id = @_safeObjectId(comment.id)
if comment.metadata?.ts?

View file

@ -0,0 +1,162 @@
SandboxedModule = require('sandboxed-module')
sinon = require('sinon')
require('chai').should()
modulePath = require('path').join __dirname, '../../../app/js/RangeManager'
ObjectId = require("mongojs").ObjectId
assert = require("chai").assert
_ = require "underscore"
describe "RangeManager", ->
beforeEach ->
@RangeManager = SandboxedModule.require modulePath, requires:
"./mongojs":
ObjectId: ObjectId
describe "jsonRangesToMongo", ->
it "should convert ObjectIds and dates to proper objects", ->
change_id = ObjectId().toString()
comment_id = ObjectId().toString()
user_id = ObjectId().toString()
ts = new Date().toJSON()
@RangeManager.jsonRangesToMongo({
changes: [{
id: change_id
op: { i: "foo", p: 3 }
metadata:
user_id: user_id
ts: ts
}]
comments: [{
id: comment_id
offset: 42
length: 5
metadata:
ts: ts
}]
}).should.deep.equal {
changes: [{
id: ObjectId(change_id)
op: { i: "foo", p: 3 }
metadata:
user_id: ObjectId(user_id)
ts: new Date(ts)
}]
comments: [{
id: ObjectId(comment_id)
offset: 42
length: 5
metadata:
ts: new Date(ts)
}]
}
it "should leave malformed ObjectIds as they are", ->
change_id = "foo"
comment_id = "bar"
user_id = "baz"
@RangeManager.jsonRangesToMongo({
changes: [{
id: change_id
metadata:
user_id: user_id
}]
comments: [{
id: comment_id
}]
}).should.deep.equal {
changes: [{
id: change_id
metadata:
user_id: user_id
}]
comments: [{
id: comment_id
}]
}
it "should be consistent when transformed through json -> mongo -> json", ->
change_id = ObjectId().toString()
comment_id = ObjectId().toString()
user_id = ObjectId().toString()
ts = new Date().toJSON()
ranges1 = {
changes: [{
id: change_id
op: { i: "foo", p: 3 }
metadata:
user_id: user_id
ts: ts
}]
comments: [{
id: comment_id
offset: 42
length: 5
metadata:
ts: ts
}]
}
ranges1_copy = JSON.parse(JSON.stringify(ranges1)) # jsonRangesToMongo modifies in place
ranges2 = JSON.parse(JSON.stringify(@RangeManager.jsonRangesToMongo(ranges1_copy)))
ranges1.should.deep.equal ranges2
describe "shouldUpdateRanges", ->
beforeEach () ->
@ranges = {
changes: [{
id: ObjectId()
op: { i: "foo", p: 3 }
metadata:
user_id: ObjectId()
ts: new Date()
}]
comments: [{
id: ObjectId()
offset: 42
length: 5
metadata:
ts: new Date()
}]
}
@ranges_copy = @RangeManager.jsonRangesToMongo(JSON.parse(JSON.stringify(@ranges)))
describe "with a blank new range", ->
it "should return false", ->
@RangeManager.shouldUpdateRanges(@ranges, null).should.equal false
describe "with a blank old range", ->
it "should treat it like {}", ->
@RangeManager.shouldUpdateRanges(null, {}).should.equal false
@RangeManager.shouldUpdateRanges(null, @ranges).should.equal true
describe "with no changes", ->
it "should return false", ->
@RangeManager.shouldUpdateRanges(@ranges, @ranges_copy).should.equal false
describe "with changes", ->
it "should return true when the change id changes", ->
@ranges_copy.changes[0].id = ObjectId()
@RangeManager.shouldUpdateRanges(@ranges, @ranges_copy).should.equal true
it "should return true when the change user id changes", ->
@ranges_copy.changes[0].metadata.user_id = ObjectId()
@RangeManager.shouldUpdateRanges(@ranges, @ranges_copy).should.equal true
it "should return true when the change ts changes", ->
@ranges_copy.changes[0].metadata.ts = new Date(Date.now() + 1000)
@RangeManager.shouldUpdateRanges(@ranges, @ranges_copy).should.equal true
it "should return true when the change op changes", ->
@ranges_copy.changes[0].op.i = "bar"
@RangeManager.shouldUpdateRanges(@ranges, @ranges_copy).should.equal true
it "should return true when the comment id changes", ->
@ranges_copy.comments[0].id = ObjectId()
@RangeManager.shouldUpdateRanges(@ranges, @ranges_copy).should.equal true
it "should return true when the comment ts changes", ->
@ranges_copy.comments[0].metadata.ts = new Date(Date.now() + 1000)
@RangeManager.shouldUpdateRanges(@ranges, @ranges_copy).should.equal true
it "should return true when the comment offset changes", ->
@ranges_copy.comments[0].offset = 17
@RangeManager.shouldUpdateRanges(@ranges, @ranges_copy).should.equal true