From 4f6583bbf22877dafc37d9d71acaded07604f0d8 Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Mon, 25 Nov 2019 13:28:36 +0000 Subject: [PATCH] fix getDocVersion and add tests --- .../app/coffee/RedisManager.coffee | 3 +- .../coffee/ApplyingUpdatesToADocTests.coffee | 33 ++++++++++++++ .../RedisManager/RedisManagerTests.coffee | 44 ++++++++++++++++++- 3 files changed, 78 insertions(+), 2 deletions(-) diff --git a/services/document-updater/app/coffee/RedisManager.coffee b/services/document-updater/app/coffee/RedisManager.coffee index 01e0289bee..9e2edbd99d 100644 --- a/services/document-updater/app/coffee/RedisManager.coffee +++ b/services/document-updater/app/coffee/RedisManager.coffee @@ -156,8 +156,9 @@ module.exports = RedisManager = callback null, docLines, version, ranges, pathname, projectHistoryId, unflushedTime, lastUpdatedAt, lastUpdatedBy getDocVersion: (doc_id, callback = (error, version, projectHistoryType) ->) -> - rclient.mget keys.docVersion(doc_id: doc_id), keys.projectHistoryType(doc_id:doc_id), (error, version, projectHistoryType) -> + rclient.mget keys.docVersion(doc_id: doc_id), keys.projectHistoryType(doc_id:doc_id), (error, result) -> return callback(error) if error? + [version, projectHistoryType] = result || [] version = parseInt(version, 10) callback null, version, projectHistoryType diff --git a/services/document-updater/test/acceptance/coffee/ApplyingUpdatesToADocTests.coffee b/services/document-updater/test/acceptance/coffee/ApplyingUpdatesToADocTests.coffee index 51b9cf08a9..0b28dea7a7 100644 --- a/services/document-updater/test/acceptance/coffee/ApplyingUpdatesToADocTests.coffee +++ b/services/document-updater/test/acceptance/coffee/ApplyingUpdatesToADocTests.coffee @@ -135,6 +135,39 @@ describe "Applying updates to a doc", -> done() return null + describe "when the document is loaded and is using project-history only", -> + before (done) -> + [@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()] + + MockWebApi.insertDoc @project_id, @doc_id, {lines: @lines, version: @version, projectHistoryType: 'project-history'} + DocUpdaterClient.preloadDoc @project_id, @doc_id, (error) => + throw error if error? + sinon.spy MockWebApi, "getDocument" + DocUpdaterClient.sendUpdate @project_id, @doc_id, @update, (error) -> + throw error if error? + setTimeout done, 200 + return null + + after -> + MockWebApi.getDocument.restore() + + it "should update the doc", (done) -> + DocUpdaterClient.getDoc @project_id, @doc_id, (error, res, doc) => + doc.lines.should.deep.equal @result + done() + return null + + it "should not push any applied updates to the track changes api", (done) -> + rclient_history.lrange HistoryKeys.uncompressedHistoryOps({@doc_id}), 0, -1, (error, updates) => + updates.length.should.equal 0 + done() + return null + + it "should push the applied updates to the project history changes api", (done) -> + rclient_history.lrange ProjectHistoryKeys.projectHistoryOps({@project_id}), 0, -1, (error, updates) => + JSON.parse(updates[0]).op.should.deep.equal @update.op + done() + return null describe "when the document has been deleted", -> describe "when the ops come in a single linear order", -> diff --git a/services/document-updater/test/unit/coffee/RedisManager/RedisManagerTests.coffee b/services/document-updater/test/unit/coffee/RedisManager/RedisManagerTests.coffee index 508a9ba0f7..99035a32b3 100644 --- a/services/document-updater/test/unit/coffee/RedisManager/RedisManagerTests.coffee +++ b/services/document-updater/test/unit/coffee/RedisManager/RedisManagerTests.coffee @@ -361,11 +361,12 @@ describe "RedisManager", -> describe "with a consistent version", -> beforeEach -> - @RedisManager.getDocVersion.withArgs(@doc_id).yields(null, @version - @ops.length) + describe "with project history enabled", -> beforeEach -> @settings.apis.project_history.enabled = true + @RedisManager.getDocVersion.withArgs(@doc_id).yields(null, @version - @ops.length) @RedisManager.updateDocument @project_id, @doc_id, @lines, @version, @ops, @ranges, @updateMeta, @callback it "should get the current doc version to check for consistency", -> @@ -446,6 +447,7 @@ describe "RedisManager", -> beforeEach -> @rclient.rpush = sinon.stub() @settings.apis.project_history.enabled = false + @RedisManager.getDocVersion.withArgs(@doc_id).yields(null, @version - @ops.length) @RedisManager.updateDocument @project_id, @doc_id, @lines, @version, @ops, @ranges, @updateMeta, @callback it "should not push the updates into the project history ops list", -> @@ -456,6 +458,26 @@ describe "RedisManager", -> .calledWith(null, @doc_update_list_length) .should.equal true + describe "with a doc using project history only", -> + beforeEach -> + @RedisManager.getDocVersion.withArgs(@doc_id).yields(null, @version - @ops.length, 'project-history') + @RedisManager.updateDocument @project_id, @doc_id, @lines, @version, @ops, @ranges, @updateMeta, @callback + + it "should not push the updates to the track-changes ops list", -> + @multi.rpush + .calledWith("UncompressedHistoryOps:#{@doc_id}") + .should.equal false + + it "should push the updates into the project history ops list", -> + @ProjectHistoryRedisManager.queueOps + .calledWith(@project_id, JSON.stringify(@ops[0])) + .should.equal true + + it "should call the callback with the project update count only", -> + @callback + .calledWith(null, undefined, @project_update_list_length) + .should.equal true + describe "with an inconsistent version", -> beforeEach -> @RedisManager.getDocVersion.withArgs(@doc_id).yields(null, @version - @ops.length - 1) @@ -754,3 +776,23 @@ describe "RedisManager", -> @ProjectHistoryRedisManager.queueRenameEntity .calledWithExactly(@project_id, @projectHistoryId, 'doc', @doc_id, @userId, @update, @callback) .should.equal true + + describe "getDocVersion", -> + beforeEach -> + @version = 12345 + + describe "when the document does not have a project history type set", -> + beforeEach -> + @rclient.mget = sinon.stub().withArgs("DocVersion:#{@doc_id}", "ProjectHistoryType:#{@doc_id}").callsArgWith(2, null, ["#{@version}"]) + @RedisManager.getDocVersion @doc_id, @callback + + it "should return the document version and an undefined history type", -> + @callback.calledWithExactly(null, @version, undefined).should.equal true + + describe "when the document has a project history type set", -> + beforeEach -> + @rclient.mget = sinon.stub().withArgs("DocVersion:#{@doc_id}", "ProjectHistoryType:#{@doc_id}").callsArgWith(2, null, ["#{@version}", 'project-history']) + @RedisManager.getDocVersion @doc_id, @callback + + it "should return the document version and history type", -> + @callback.calledWithExactly(null, @version, 'project-history').should.equal true