Refactor and improve acceptance tests

This commit is contained in:
James Allen 2014-02-26 12:38:47 +00:00
parent d27872c9bd
commit 80af34895b

View file

@ -9,12 +9,25 @@ Settings = require "settings-sharelatex"
request = require "request" request = require "request"
rclient = require("redis").createClient() # Only works locally for now rclient = require("redis").createClient() # Only works locally for now
flushAndGetCompressedUpdates = (doc_id, callback = (error, updates) ->) ->
request.post {
url: "http://localhost:#{Settings.port}/doc/#{doc_id}/flush"
}, (error, response, body) =>
response.statusCode.should.equal 204
db.docHistory
.find(doc_id: ObjectId(doc_id))
.sort("meta.end_ts": 1)
.toArray callback
pushRawUpdates = (doc_id, updates, callback = (error) ->) ->
rclient.rpush "UncompressedHistoryOps:#{doc_id}", (JSON.stringify(u) for u in updates)..., callback
describe "Appending doc ops to the history", -> describe "Appending doc ops to the history", ->
describe "when the history does not exist yet", -> describe "when the history does not exist yet", ->
before (done) -> before (done) ->
@doc_id = ObjectId().toString() @doc_id = ObjectId().toString()
@user_id = ObjectId().toString() @user_id = ObjectId().toString()
updates = [{ pushRawUpdates @doc_id, [{
op: [{ i: "f", p: 3 }] op: [{ i: "f", p: 3 }]
meta: { ts: Date.now(), user_id: @user_id } meta: { ts: Date.now(), user_id: @user_id }
v: 3 v: 3
@ -26,106 +39,96 @@ describe "Appending doc ops to the history", ->
op: [{ i: "o", p: 5 }] op: [{ i: "o", p: 5 }]
meta: { ts: Date.now(), user_id: @user_id } meta: { ts: Date.now(), user_id: @user_id }
v: 5 v: 5
}] }], (error) =>
throw error if error?
rclient.rpush "UncompressedHistoryOps:#{@doc_id}", (JSON.stringify(u) for u in updates)... flushAndGetCompressedUpdates @doc_id, (error, @updates) =>
throw error if error?
request.post {
url: "http://localhost:#{Settings.port}/doc/#{@doc_id}/flush"
}, (@error, @response, @body) =>
db.docHistory
.find(doc_id: ObjectId(@doc_id))
.sort("meta.end_ts": -1)
.toArray (error, updates) =>
@update = updates[0]
done() done()
it "should return a successful response", ->
@response.statusCode.should.equal 204
it "should insert the compressed op into mongo", -> it "should insert the compressed op into mongo", ->
expect(@update.op).to.deep.equal { expect(@updates[0].op).to.deep.equal {
p: 3, i: "foo" p: 3, i: "foo"
} }
it "should insert the correct version number into mongo", -> it "should insert the correct version number into mongo", ->
expect(@update.v).to.equal 5 expect(@updates[0].v).to.equal 5
###
describe "when the history has already been started", -> describe "when the history has already been started", ->
beforeEach (done) -> beforeEach (done) ->
@doc_id = ObjectId().toString() @doc_id = ObjectId().toString()
@user_id = ObjectId().toString() @user_id = ObjectId().toString()
updates = [{ pushRawUpdates @doc_id, [{
op: [{ i: "f", p: 3 }] op: [{ i: "f", p: 3 }]
meta: { ts: Date.now(), user_id: @user_id } meta: { ts: Date.now(), user_id: @user_id }
v: 3
}, { }, {
op: [{ i: "o", p: 4 }] op: [{ i: "o", p: 4 }]
meta: { ts: Date.now(), user_id: @user_id } meta: { ts: Date.now(), user_id: @user_id }
v: 4
}, { }, {
op: [{ i: "o", p: 5 }] op: [{ i: "o", p: 5 }]
meta: { ts: Date.now(), user_id: @user_id } meta: { ts: Date.now(), user_id: @user_id }
}] v: 5
@version = 3 }], (error) =>
throw error if error?
request.post { flushAndGetCompressedUpdates @doc_id, (error, updates) =>
url: "http://localhost:#{Settings.port}/doc/#{@doc_id}/history" throw error if error?
json:
version: @version
docOps: updates
}, (@error, @response, @body) =>
done() done()
describe "when the updates are recent and from the same user", -> describe "when the updates are recent and from the same user", ->
beforeEach (done) -> beforeEach (done) ->
updates = [{ pushRawUpdates @doc_id, [{
op: [{ i: "b", p: 6 }] op: [{ i: "b", p: 6 }]
meta: { ts: Date.now(), user_id: @user_id } meta: { ts: Date.now(), user_id: @user_id }
v: 6
}, { }, {
op: [{ i: "a", p: 7 }] op: [{ i: "a", p: 7 }]
meta: { ts: Date.now(), user_id: @user_id } meta: { ts: Date.now(), user_id: @user_id }
v: 7
}, { }, {
op: [{ i: "r", p: 8 }] op: [{ i: "r", p: 8 }]
meta: { ts: Date.now(), user_id: @user_id } meta: { ts: Date.now(), user_id: @user_id }
}] v: 8
@version = 6 }], (error) =>
throw error if error?
request.post { flushAndGetCompressedUpdates @doc_id, (error, @updates) =>
url: "http://localhost:#{Settings.port}/doc/#{@doc_id}/history" throw error if error?
json:
version: @version
docOps: updates
}, (@error, @response, @body) =>
done() done()
it "should return a successful response", -> it "should combine all the updates into one", ->
@response.statusCode.should.equal 204 expect(@updates[0].op).to.deep.equal {
p: 3, i: "foobar"
}
it "should insert the correct version number into mongo", ->
expect(@updates[0].v).to.equal 8
describe "when the updates are far apart", -> describe "when the updates are far apart", ->
beforeEach (done) -> beforeEach (done) ->
oneDay = 24 * 60 * 60 * 1000 oneDay = 24 * 60 * 60 * 1000
updates = [{ pushRawUpdates @doc_id, [{
op: [{ i: "b", p: 6 }] op: [{ i: "b", p: 6 }]
meta: { ts: Date.now() + oneDay, user_id: @user_id } meta: { ts: Date.now() + oneDay, user_id: @user_id }
v: 6
}, { }, {
op: [{ i: "a", p: 7 }] op: [{ i: "a", p: 7 }]
meta: { ts: Date.now() + oneDay, user_id: @user_id } meta: { ts: Date.now() + oneDay, user_id: @user_id }
v: 7
}, { }, {
op: [{ i: "r", p: 8 }] op: [{ i: "r", p: 8 }]
meta: { ts: Date.now() + oneDay, user_id: @user_id } meta: { ts: Date.now() + oneDay, user_id: @user_id }
}] v: 8
@version = 6 }], (error) =>
throw error if error?
request.post { flushAndGetCompressedUpdates @doc_id, (error, @updates) =>
url: "http://localhost:#{Settings.port}/doc/#{@doc_id}/history" throw error if error?
json:
version: @version
docOps: updates
}, (@error, @response, @body) =>
done() done()
it "should return a successful response", -> it "should keep the updates separate", ->
@response.statusCode.should.equal 204 expect(@updates[0].op).to.deep.equal {
p: 3, i: "foo"
### }
expect(@updates[1].op).to.deep.equal {
p: 6, i: "bar"
}