From 3ea2e079938424ac52c2b3ee7470f32422f7ac63 Mon Sep 17 00:00:00 2001 From: James Allen Date: Fri, 2 Dec 2016 12:01:23 +0000 Subject: [PATCH] Add tests for fetching and flushing track changes entries to persistence layer --- .../app/coffee/TrackChangesManager.coffee | 13 ++++- .../coffee/TrackChangesTests.coffee | 50 ++++++++++++++++++- .../coffee/helpers/DocUpdaterClient.coffee | 15 ------ .../coffee/helpers/MockWebApi.coffee | 5 +- 4 files changed, 63 insertions(+), 20 deletions(-) diff --git a/services/document-updater/app/coffee/TrackChangesManager.coffee b/services/document-updater/app/coffee/TrackChangesManager.coffee index 65f1931bb4..126b9ec7e0 100644 --- a/services/document-updater/app/coffee/TrackChangesManager.coffee +++ b/services/document-updater/app/coffee/TrackChangesManager.coffee @@ -8,5 +8,14 @@ module.exports = TrackChangesManager = changesTracker.track_changes = !!update.meta.tc for op in update.op changesTracker.applyOp(op, { user_id: update.meta?.user_id }) - {changes, comments} = changesTracker - callback null, {changes, comments} \ No newline at end of file + + # Return the minimal data structure needed, since most documents won't have any + # changes or comments + response = null + if changesTracker.changes?.length > 0 + response ?= {} + response.changes = changesTracker.changes + if changesTracker.comments?.length > 0 + response ?= {} + response.comments = changesTracker.comments + callback null, response \ No newline at end of file diff --git a/services/document-updater/test/acceptance/coffee/TrackChangesTests.coffee b/services/document-updater/test/acceptance/coffee/TrackChangesTests.coffee index e3577fd6d7..88250f82a7 100644 --- a/services/document-updater/test/acceptance/coffee/TrackChangesTests.coffee +++ b/services/document-updater/test/acceptance/coffee/TrackChangesTests.coffee @@ -46,7 +46,7 @@ describe "Track changes", -> throw error if error? setTimeout done, 200 - it "should set the updated track changes entries in redis", (done) -> + it "should update the tracked entries", (done) -> DocUpdaterClient.getDoc @project_id, @doc.id, (error, res, data) => throw error if error? entries = data.track_changes_entries @@ -55,3 +55,51 @@ describe "Track changes", -> change.metadata.user_id.should.equal @user_id done() + describe "Loading changes from persistence layer", -> + before (done) -> + @project_id = DocUpdaterClient.randomId() + @user_id = DocUpdaterClient.randomId() + @doc = { + id: DocUpdaterClient.randomId() + lines: ["a123aa"] + } + @update = { + doc: @doc.id + op: [{ i: "456", p: 5 }] + v: 0 + meta: { user_id: @user_id, tc: 1 } + } + MockWebApi.insertDoc @project_id, @doc.id, { + lines: @doc.lines + version: 0 + track_changes_entries: { + changes: [{ + op: { i: "123", p: 1 } + metadata: + user_id: @user_id + ts: new Date() + }] + } + } + DocUpdaterClient.preloadDoc @project_id, @doc.id, (error) => + throw error if error? + DocUpdaterClient.sendUpdate @project_id, @doc.id, @update, (error) -> + throw error if error? + setTimeout done, 200 + + it "should have preloaded the existing changes", (done) -> + DocUpdaterClient.getDoc @project_id, @doc.id, (error, res, data) => + throw error if error? + {changes} = data.track_changes_entries + changes[0].op.should.deep.equal { i: "123", p: 1 } + changes[1].op.should.deep.equal { i: "456", p: 5 } + done() + + it "should flush the changes to the persistence layer again", (done) -> + DocUpdaterClient.flushDoc @project_id, @doc.id, (error) => + throw error if error? + MockWebApi.getDocument @project_id, @doc.id, (error, doc) => + {changes} = doc.track_changes_entries + changes[0].op.should.deep.equal { i: "123", p: 1 } + changes[1].op.should.deep.equal { i: "456", p: 5 } + done() diff --git a/services/document-updater/test/acceptance/coffee/helpers/DocUpdaterClient.coffee b/services/document-updater/test/acceptance/coffee/helpers/DocUpdaterClient.coffee index b90e7ea82e..d704daefd1 100644 --- a/services/document-updater/test/acceptance/coffee/helpers/DocUpdaterClient.coffee +++ b/services/document-updater/test/acceptance/coffee/helpers/DocUpdaterClient.coffee @@ -72,18 +72,3 @@ module.exports = DocUpdaterClient = deleteProject: (project_id, callback = () ->) -> request.del "http://localhost:3003/project/#{project_id}", callback - - setTrackChangesOn: (project_id, callback = () ->) -> - request.post { - url: "http://localhost:3003/project/#{project_id}/track_changes" - json: - on: true - }, callback - - setTrackChangesOff: (project_id, callback = () ->) -> - request.post { - url: "http://localhost:3003/project/#{project_id}/track_changes" - json: - on: false - }, callback - diff --git a/services/document-updater/test/acceptance/coffee/helpers/MockWebApi.coffee b/services/document-updater/test/acceptance/coffee/helpers/MockWebApi.coffee index e77a18c0ea..4e9d073cc4 100644 --- a/services/document-updater/test/acceptance/coffee/helpers/MockWebApi.coffee +++ b/services/document-updater/test/acceptance/coffee/helpers/MockWebApi.coffee @@ -11,10 +11,11 @@ module.exports = MockWebApi = doc.lines ?= [] @docs["#{project_id}:#{doc_id}"] = doc - setDocument: (project_id, doc_id, lines, version, callback = (error) ->) -> + setDocument: (project_id, doc_id, lines, version, track_changes_entries, callback = (error) ->) -> doc = @docs["#{project_id}:#{doc_id}"] ||= {} doc.lines = lines doc.version = version + doc.track_changes_entries = track_changes_entries callback null getDocument: (project_id, doc_id, callback = (error, doc) ->) -> @@ -31,7 +32,7 @@ module.exports = MockWebApi = res.send 404 app.post "/project/:project_id/doc/:doc_id", express.bodyParser(), (req, res, next) => - MockWebApi.setDocument req.params.project_id, req.params.doc_id, req.body.lines, req.body.version, (error) -> + MockWebApi.setDocument req.params.project_id, req.params.doc_id, req.body.lines, req.body.version, req.body.track_changes_entries, (error) -> if error? res.send 500 else