From 0c16fbed8808c5e74f6207d298aa6c697472b0cb Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Wed, 2 Sep 2015 15:39:19 -0300 Subject: [PATCH] add more unit tests --- .../DocArchive/DocArchiveManager.coffee | 64 ++++++++++++++++++- .../unit/coffee/DocArchive/MongoAWS.coffee | 33 ++++++++++ 2 files changed, 95 insertions(+), 2 deletions(-) diff --git a/services/track-changes/test/unit/coffee/DocArchive/DocArchiveManager.coffee b/services/track-changes/test/unit/coffee/DocArchive/DocArchiveManager.coffee index c76afab988..ac266132c3 100644 --- a/services/track-changes/test/unit/coffee/DocArchive/DocArchiveManager.coffee +++ b/services/track-changes/test/unit/coffee/DocArchive/DocArchiveManager.coffee @@ -30,9 +30,7 @@ describe "DocArchiveManager", -> @callback = sinon.stub() describe "archiveAllDocsChanges", -> - it "should archive all project docs change", (done)-> - @DocstoreHandler.getAllDocs = sinon.stub().callsArgWith(1, null, @mongoDocs) @DocArchiveManager.archiveDocChangesWithLock = sinon.stub().callsArgWith(2, null) @@ -42,3 +40,65 @@ describe "DocArchiveManager", -> @DocArchiveManager.archiveDocChangesWithLock.calledWith(@project_id, @mongoDocs[2]._id).should.equal true should.not.exist err done() + + describe "archiveDocChangesWithLock", -> + beforeEach -> + @DocArchiveManager.archiveDocChanges = sinon.stub().callsArg(2) + @LockManager.runWithLock = sinon.stub().callsArg(2) + @DocArchiveManager.archiveDocChangesWithLock @project_id, @doc_id, @callback + + it "should run archiveDocChangesWithLock with the lock", -> + @LockManager.runWithLock + .calledWith( + "HistoryArchiveLock:#{@doc_id}" + ) + .should.equal true + + it "should call the callback", -> + @callback.called.should.equal true + + describe "archiveDocChanges", -> + beforeEach -> + @update = { _id: ObjectId(), op: "op", meta: "meta", v: "v"} + @MongoManager.getDocChangesCount = sinon.stub().callsArg(1) + @MongoManager.getLastCompressedUpdate = sinon.stub().callsArgWith(1, null, @update) + @MongoAWS.archiveDocHistory = sinon.stub().callsArg(2) + @MongoManager.markDocHistoryAsArchived = sinon.stub().callsArg(2) + @DocArchiveManager.archiveDocChanges @project_id, @doc_id, @callback + + it "should run markDocHistoryAsArchived with doc_id and update", -> + @MongoManager.markDocHistoryAsArchived + .calledWith( + @doc_id, @update + ) + .should.equal true + it "should call the callback", -> + @callback.called.should.equal true + + describe "unArchiveAllDocsChanges", -> + it "should unarchive all project docs change", (done)-> + @DocstoreHandler.getAllDocs = sinon.stub().callsArgWith(1, null, @mongoDocs) + @DocArchiveManager.unArchiveDocChanges = sinon.stub().callsArgWith(2, null) + + @DocArchiveManager.unArchiveAllDocsChanges @project_id, (err)=> + @DocArchiveManager.unArchiveDocChanges.calledWith(@project_id, @mongoDocs[0]._id).should.equal true + @DocArchiveManager.unArchiveDocChanges.calledWith(@project_id, @mongoDocs[1]._id).should.equal true + @DocArchiveManager.unArchiveDocChanges.calledWith(@project_id, @mongoDocs[2]._id).should.equal true + should.not.exist err + done() + + describe "unArchiveDocChanges", -> + beforeEach -> + @MongoManager.getArchivedDocChanges = sinon.stub().callsArg(1) + @MongoAWS.unArchiveDocHistory = sinon.stub().callsArg(2) + @MongoManager.markDocHistoryAsUnarchived = sinon.stub().callsArg(1) + @DocArchiveManager.unArchiveDocChanges @project_id, @doc_id, @callback + + it "should run markDocHistoryAsUnarchived with doc_id", -> + @MongoManager.markDocHistoryAsUnarchived + .calledWith( + @doc_id + ) + .should.equal true + it "should call the callback", -> + @callback.called.should.equal true diff --git a/services/track-changes/test/unit/coffee/DocArchive/MongoAWS.coffee b/services/track-changes/test/unit/coffee/DocArchive/MongoAWS.coffee index 88c9f4b7da..180562dea7 100644 --- a/services/track-changes/test/unit/coffee/DocArchive/MongoAWS.coffee +++ b/services/track-changes/test/unit/coffee/DocArchive/MongoAWS.coffee @@ -76,3 +76,36 @@ describe "MongoAWS", -> it "should call handleBulk", -> @MongoAWS.handleBulk.calledWith([],@callback).should.equal true + + describe "handleBulk", -> + beforeEach -> + @ops = [{ + _id: ObjectId() + doc_id: ObjectId() + project_id: ObjectId() + }, { + _id: ObjectId() + doc_id: ObjectId() + project_id: ObjectId() + }, { + _id: ObjectId() + doc_id: ObjectId() + project_id: ObjectId() + }] + @bulk = + find: sinon.stub().returns + upsert: sinon.stub().returns + updateOne: sinon.stub() + execute: sinon.stub().callsArgWith(0, null, {}) + @db.docHistory = {} + @db.docHistory.initializeUnorderedBulkOp = sinon.stub().returns @bulk + @MongoAWS.handleBulk @ops, @callback + + it "should call updateOne for each operation", -> + @bulk.find.calledWith({_id:@ops[0]._id}).should.equal true + @bulk.find.calledWith({_id:@ops[1]._id}).should.equal true + @bulk.find.calledWith({_id:@ops[2]._id}).should.equal true + + it "should call the callback", -> + @callback.calledWith(null).should.equal true +