Export .promises from MongoManager

This commit is contained in:
Simon Detheridge 2020-07-31 16:20:07 +01:00
parent 268f436461
commit 9724407e31
4 changed files with 56 additions and 61 deletions

View file

@ -1,5 +1,5 @@
const { promisify, callbackify } = require('util') const { callbackify } = require('util')
const MongoManager = require('./MongoManager') const MongoManager = require('./MongoManager').promises
const Errors = require('./Errors') const Errors = require('./Errors')
const logger = require('logger-sharelatex') const logger = require('logger-sharelatex')
const settings = require('settings-sharelatex') const settings = require('settings-sharelatex')
@ -29,7 +29,7 @@ module.exports = {
} }
async function archiveAllDocs(projectId) { async function archiveAllDocs(projectId) {
const docs = await promisify(MongoManager.getProjectsDocs)( const docs = await MongoManager.getProjectsDocs(
projectId, projectId,
{ include_deleted: true }, { include_deleted: true },
{ lines: true, ranges: true, rev: true, inS3: true } { lines: true, ranges: true, rev: true, inS3: true }
@ -76,11 +76,11 @@ async function archiveDoc(projectId, doc) {
await PersistorManager.sendStream(settings.docstore.bucket, key, stream, { await PersistorManager.sendStream(settings.docstore.bucket, key, stream, {
sourceMd5: md5 sourceMd5: md5
}) })
await promisify(MongoManager.markDocAsArchived)(doc._id, doc.rev) await MongoManager.markDocAsArchived(doc._id, doc.rev)
} }
async function unArchiveAllDocs(projectId) { async function unArchiveAllDocs(projectId) {
const docs = await promisify(MongoManager.getArchivedProjectDocs)(projectId) const docs = await MongoManager.getArchivedProjectDocs(projectId)
if (!docs) { if (!docs) {
throw new Errors.NotFoundError(`No docs for project ${projectId}`) throw new Errors.NotFoundError(`No docs for project ${projectId}`)
} }
@ -131,16 +131,12 @@ async function unarchiveDoc(projectId, docId) {
} else { } else {
throw new Error("I don't understand the doc format in s3") throw new Error("I don't understand the doc format in s3")
} }
await promisify(MongoManager.upsertIntoDocCollection)( await MongoManager.upsertIntoDocCollection(projectId, docId, mongoDoc)
projectId,
docId,
mongoDoc
)
await PersistorManager.deleteObject(settings.docstore.bucket, key) await PersistorManager.deleteObject(settings.docstore.bucket, key)
} }
async function destroyAllDocs(projectId) { async function destroyAllDocs(projectId) {
const docs = await promisify(MongoManager.getProjectsDocs)( const docs = await MongoManager.getProjectsDocs(
projectId, projectId,
{ include_deleted: true }, { include_deleted: true },
{ _id: 1 } { _id: 1 }
@ -157,7 +153,7 @@ async function destroyDoc(projectId, docId) {
{ project_id: projectId, doc_id: docId }, { project_id: projectId, doc_id: docId },
'removing doc from mongo and persistor' 'removing doc from mongo and persistor'
) )
const doc = await promisify(MongoManager.findDoc)(projectId, docId, { const doc = await MongoManager.findDoc(projectId, docId, {
inS3: 1 inS3: 1
}) })
if (!doc) { if (!doc) {
@ -170,7 +166,7 @@ async function destroyDoc(projectId, docId) {
`${projectId}/${docId}` `${projectId}/${docId}`
) )
} }
await promisify(MongoManager.destroyDoc)(docId) await MongoManager.destroyDoc(docId)
} }
async function _streamToString(stream) { async function _streamToString(stream) {

View file

@ -14,6 +14,7 @@ let MongoManager
const { db, ObjectId } = require('./mongojs') const { db, ObjectId } = require('./mongojs')
const logger = require('logger-sharelatex') const logger = require('logger-sharelatex')
const metrics = require('metrics-sharelatex') const metrics = require('metrics-sharelatex')
const { promisify } = require('util')
module.exports = MongoManager = { module.exports = MongoManager = {
findDoc(project_id, doc_id, filter, callback) { findDoc(project_id, doc_id, filter, callback) {
@ -162,14 +163,11 @@ module.exports = MongoManager = {
) )
} }
} }
;[
'findDoc', const methods = Object.getOwnPropertyNames(MongoManager)
'getProjectsDocs',
'getArchivedProjectDocs', module.exports.promises = {}
'upsertIntoDocCollection', for (const method of methods) {
'markDocAsArchived',
'getDocVersion',
'setDocVersion'
].map((method) =>
metrics.timeAsyncMethod(MongoManager, method, 'mongo.MongoManager', logger) metrics.timeAsyncMethod(MongoManager, method, 'mongo.MongoManager', logger)
) module.exports.promises[method] = promisify(module.exports[method])
}

View file

@ -123,17 +123,19 @@ describe('DocArchiveManager', function () {
} }
MongoManager = { MongoManager = {
markDocAsArchived: sinon.stub().yields(), promises: {
upsertIntoDocCollection: sinon.stub().yields(), markDocAsArchived: sinon.stub().resolves(),
getProjectsDocs: sinon.stub().yields(null, mongoDocs), upsertIntoDocCollection: sinon.stub().resolves(),
getArchivedProjectDocs: sinon.stub().yields(null, archivedDocs), getProjectsDocs: sinon.stub().resolves(mongoDocs),
findDoc: sinon.stub().yields(), getArchivedProjectDocs: sinon.stub().resolves(archivedDocs),
destroyDoc: sinon.stub().yields() findDoc: sinon.stub().resolves(),
destroyDoc: sinon.stub().resolves()
}
} }
for (const mongoDoc of mongoDocs) { for (const mongoDoc of mongoDocs) {
MongoManager.findDoc MongoManager.promises.findDoc
.withArgs(projectId, mongoDoc._id, sinon.match.any) .withArgs(projectId, mongoDoc._id)
.yields(null, mongoDoc) .resolves(mongoDoc)
} }
DocArchiveManager = SandboxedModule.require(modulePath, { DocArchiveManager = SandboxedModule.require(modulePath, {
@ -223,7 +225,7 @@ describe('DocArchiveManager', function () {
it('should mark the doc as archived', async function () { it('should mark the doc as archived', async function () {
await DocArchiveManager.promises.archiveDoc(projectId, mongoDocs[0]) await DocArchiveManager.promises.archiveDoc(projectId, mongoDocs[0])
expect(MongoManager.markDocAsArchived).to.have.been.calledWith( expect(MongoManager.promises.markDocAsArchived).to.have.been.calledWith(
mongoDocs[0]._id, mongoDocs[0]._id,
mongoDocs[0].rev mongoDocs[0].rev
) )
@ -270,7 +272,7 @@ describe('DocArchiveManager', function () {
it('should update the doc lines in mongo', async function () { it('should update the doc lines in mongo', async function () {
await DocArchiveManager.promises.unarchiveDoc(projectId, docId) await DocArchiveManager.promises.unarchiveDoc(projectId, docId)
expect( expect(
MongoManager.upsertIntoDocCollection MongoManager.promises.upsertIntoDocCollection
).to.have.been.calledWith(projectId, docId, { lines: mongoDocs[0].lines }) ).to.have.been.calledWith(projectId, docId, { lines: mongoDocs[0].lines })
}) })
@ -297,11 +299,9 @@ describe('DocArchiveManager', function () {
it('should return the docs lines', async function () { it('should return the docs lines', async function () {
await DocArchiveManager.promises.unarchiveDoc(projectId, docId) await DocArchiveManager.promises.unarchiveDoc(projectId, docId)
expect(MongoManager.upsertIntoDocCollection).to.have.been.calledWith( expect(
projectId, MongoManager.promises.upsertIntoDocCollection
docId, ).to.have.been.calledWith(projectId, docId, mongoDoc)
mongoDoc
)
}) })
}) })
@ -322,11 +322,9 @@ describe('DocArchiveManager', function () {
it('should return the doc lines and ranges', async function () { it('should return the doc lines and ranges', async function () {
await DocArchiveManager.promises.unarchiveDoc(projectId, docId) await DocArchiveManager.promises.unarchiveDoc(projectId, docId)
expect(MongoManager.upsertIntoDocCollection).to.have.been.calledWith( expect(
projectId, MongoManager.promises.upsertIntoDocCollection
docId, ).to.have.been.calledWith(projectId, docId, mongoDoc)
mongoDoc
)
}) })
}) })
@ -345,11 +343,9 @@ describe('DocArchiveManager', function () {
it('should return only the doc lines', async function () { it('should return only the doc lines', async function () {
await DocArchiveManager.promises.unarchiveDoc(projectId, docId) await DocArchiveManager.promises.unarchiveDoc(projectId, docId)
expect(MongoManager.upsertIntoDocCollection).to.have.been.calledWith( expect(
projectId, MongoManager.promises.upsertIntoDocCollection
docId, ).to.have.been.calledWith(projectId, docId, mongoDoc)
mongoDoc
)
}) })
}) })
@ -435,27 +431,27 @@ describe('DocArchiveManager', function () {
it('should archive all project docs which are not in s3', async function () { it('should archive all project docs which are not in s3', async function () {
await DocArchiveManager.promises.archiveAllDocs(projectId) await DocArchiveManager.promises.archiveAllDocs(projectId)
// not inS3 // not inS3
expect(MongoManager.markDocAsArchived).to.have.been.calledWith( expect(MongoManager.promises.markDocAsArchived).to.have.been.calledWith(
mongoDocs[0]._id mongoDocs[0]._id
) )
expect(MongoManager.markDocAsArchived).to.have.been.calledWith( expect(MongoManager.promises.markDocAsArchived).to.have.been.calledWith(
mongoDocs[1]._id mongoDocs[1]._id
) )
expect(MongoManager.markDocAsArchived).to.have.been.calledWith( expect(MongoManager.promises.markDocAsArchived).to.have.been.calledWith(
mongoDocs[4]._id mongoDocs[4]._id
) )
// inS3 // inS3
expect(MongoManager.markDocAsArchived).not.to.have.been.calledWith( expect(
mongoDocs[2]._id MongoManager.promises.markDocAsArchived
) ).not.to.have.been.calledWith(mongoDocs[2]._id)
expect(MongoManager.markDocAsArchived).not.to.have.been.calledWith( expect(
mongoDocs[3]._id MongoManager.promises.markDocAsArchived
) ).not.to.have.been.calledWith(mongoDocs[3]._id)
}) })
it('should return error if the project has no docs', async function () { it('should return error if the project has no docs', async function () {
MongoManager.getProjectsDocs.yields(null, null) MongoManager.promises.getProjectsDocs.resolves(null)
await expect( await expect(
DocArchiveManager.promises.archiveAllDocs(projectId) DocArchiveManager.promises.archiveAllDocs(projectId)
@ -481,7 +477,7 @@ describe('DocArchiveManager', function () {
}) })
it('should return error if the project has no docs', async function () { it('should return error if the project has no docs', async function () {
MongoManager.getArchivedProjectDocs.yields(null, null) MongoManager.promises.getArchivedProjectDocs.resolves(null)
await expect( await expect(
DocArchiveManager.promises.unArchiveAllDocs(projectId) DocArchiveManager.promises.unArchiveAllDocs(projectId)
@ -519,7 +515,9 @@ describe('DocArchiveManager', function () {
await DocArchiveManager.promises.destroyAllDocs(projectId) await DocArchiveManager.promises.destroyAllDocs(projectId)
for (const mongoDoc of mongoDocs) { for (const mongoDoc of mongoDocs) {
expect(MongoManager.destroyDoc).to.have.been.calledWith(mongoDoc._id) expect(MongoManager.promises.destroyDoc).to.have.been.calledWith(
mongoDoc._id
)
} }
}) })
}) })

View file

@ -29,6 +29,9 @@ describe('MongoManager', function () {
}, },
'metrics-sharelatex': { timeAsyncMethod: sinon.stub() }, 'metrics-sharelatex': { timeAsyncMethod: sinon.stub() },
'logger-sharelatex': { log() {} } 'logger-sharelatex': { log() {} }
},
globals: {
console
} }
}) })
this.project_id = ObjectId().toString() this.project_id = ObjectId().toString()