modified labels tests to pass with new metadata

This commit is contained in:
Nate Stemen 2017-10-12 17:28:11 +01:00
parent f113ba6342
commit 42412b1bb4
5 changed files with 281 additions and 254 deletions

View file

@ -1,5 +1,5 @@
ProjectEntityHandler = require "../Project/ProjectEntityHandler" ProjectEntityHandler = require "../Project/ProjectEntityHandler"
DocumentUpdaterHandler = require('../DocumentUpdater/DocumentUpdaterHandler') DocumentUpdaterHandler = require '../DocumentUpdater/DocumentUpdaterHandler'
module.exports = MetaHandler = module.exports = MetaHandler =

View file

@ -1,119 +0,0 @@
chai = require('chai')
chai.should()
expect = chai.expect
sinon = require("sinon")
modulePath = "../../../../app/js/Features/Labels/LabelsController"
SandboxedModule = require('sandboxed-module')
describe 'LabelsController', ->
beforeEach ->
@projectId = 'somekindofid'
@EditorRealTimeController = {
emitToRoom: sinon.stub()
}
@LabelsHandler = {
getAllLabelsForProject: sinon.stub()
getLabelsForDoc: sinon.stub()
}
@LabelsController = SandboxedModule.require modulePath, requires:
'logger-sharelatex': {log: sinon.stub(), err: sinon.stub()}
'../Editor/EditorRealTimeController': @EditorRealTimeController
'./LabelsHandler': @LabelsHandler
describe 'getAllLabels', ->
beforeEach ->
@fakeLabels = {'somedoc': ['a_label']}
@LabelsHandler.getAllLabelsForProject = sinon.stub().callsArgWith(1, null, @fakeLabels)
@req = {params: {project_id: @projectId}}
@res = {json: sinon.stub()}
@next = sinon.stub()
it 'should call LabelsHandler.getAllLabelsForProject', () ->
@LabelsController.getAllLabels(@req, @res, @next)
@LabelsHandler.getAllLabelsForProject.callCount.should.equal 1
@LabelsHandler.getAllLabelsForProject.calledWith(@projectId).should.equal true
it 'should call not call next with an error', () ->
@LabelsController.getAllLabels(@req, @res, @next)
@next.callCount.should.equal 0
it 'should send a json response', () ->
@LabelsController.getAllLabels(@req, @res, @next)
@res.json.callCount.should.equal 1
expect(@res.json.lastCall.args[0]).to.have.all.keys ['projectId', 'projectLabels']
describe 'when LabelsHandler.getAllLabelsForProject produces an error', ->
beforeEach ->
@LabelsHandler.getAllLabelsForProject = sinon.stub().callsArgWith(1, new Error('woops'))
@req = {params: {project_id: @projectId}}
@res = {json: sinon.stub()}
@next = sinon.stub()
it 'should call LabelsHandler.getAllLabelsForProject', () ->
@LabelsController.getAllLabels(@req, @res, @next)
@LabelsHandler.getAllLabelsForProject.callCount.should.equal 1
@LabelsHandler.getAllLabelsForProject.calledWith(@projectId).should.equal true
it 'should call next with an error', ->
@LabelsController.getAllLabels(@req, @res, @next)
@next.callCount.should.equal 1
expect(@next.lastCall.args[0]).to.be.instanceof Error
it 'should not send a json response', ->
@LabelsController.getAllLabels(@req, @res, @next)
@res.json.callCount.should.equal 0
describe 'broadcastLabelsForDoc', ->
beforeEach ->
@LabelsHandler.getLabelsForDoc = sinon.stub().callsArgWith(2, null, @fakeLabels)
@EditorRealTimeController.emitToRoom = sinon.stub()
@docId = 'somedoc'
@req = {params: {project_id: @projectId, doc_id: @docId}}
@res = {sendStatus: sinon.stub()}
@next = sinon.stub()
it 'should call LabelsHandler.getLabelsForDoc', () ->
@LabelsController.broadcastLabelsForDoc(@req, @res, @next)
@LabelsHandler.getLabelsForDoc.callCount.should.equal 1
@LabelsHandler.getLabelsForDoc.calledWith(@projectId).should.equal true
it 'should call not call next with an error', () ->
@LabelsController.broadcastLabelsForDoc(@req, @res, @next)
@next.callCount.should.equal 0
it 'should send a success response', () ->
@LabelsController.broadcastLabelsForDoc(@req, @res, @next)
@res.sendStatus.callCount.should.equal 1
@res.sendStatus.calledWith(200).should.equal true
it 'should emit a message to room', () ->
@LabelsController.broadcastLabelsForDoc(@req, @res, @next)
@EditorRealTimeController.emitToRoom.callCount.should.equal 1
lastCall = @EditorRealTimeController.emitToRoom.lastCall
expect(lastCall.args[0]).to.equal @projectId
expect(lastCall.args[1]).to.equal 'broadcastDocLabels'
expect(lastCall.args[2]).to.have.all.keys ['docId', 'labels']
describe 'when LabelsHandler.getLabelsForDoc produces an error', ->
beforeEach ->
@LabelsHandler.getLabelsForDoc = sinon.stub().callsArgWith(2, new Error('woops'))
@EditorRealTimeController.emitToRoom = sinon.stub()
@docId = 'somedoc'
@req = {params: {project_id: @projectId, doc_id: @docId}}
@res = {json: sinon.stub()}
@next = sinon.stub()
it 'should call LabelsHandler.getLabelsForDoc', () ->
@LabelsController.broadcastLabelsForDoc(@req, @res, @next)
@LabelsHandler.getLabelsForDoc.callCount.should.equal 1
@LabelsHandler.getLabelsForDoc.calledWith(@projectId).should.equal true
it 'should call next with an error', ->
@LabelsController.broadcastLabelsForDoc(@req, @res, @next)
@next.callCount.should.equal 1
expect(@next.lastCall.args[0]).to.be.instanceof Error
it 'should not send a json response', ->
@LabelsController.broadcastLabelsForDoc(@req, @res, @next)
@res.json.callCount.should.equal 0

View file

@ -1,134 +0,0 @@
chai = require('chai')
chai.should()
expect = chai.expect
sinon = require("sinon")
modulePath = "../../../../app/js/Features/Labels/LabelsHandler"
SandboxedModule = require('sandboxed-module')
describe 'LabelsHandler', ->
beforeEach ->
@projectId = 'someprojectid'
@docId = 'somedocid'
@ProjectEntityHandler = {
getAllDocs: sinon.stub()
getDoc: sinon.stub()
}
@DocumentUpdaterHandler = {
flushDocToMongo: sinon.stub()
}
@LabelsHandler = SandboxedModule.require modulePath, requires:
'../Project/ProjectEntityHandler': @ProjectEntityHandler
'../DocumentUpdater/DocumentUpdaterHandler': @DocumentUpdaterHandler
describe 'extractLabelsFromDoc', ->
beforeEach ->
@lines = [
'one',
'two',
'three \\label{aaa}',
'four five',
'\\label{bbb}',
'six seven'
]
it 'should extract all the labels', ->
docLabels = @LabelsHandler.extractLabelsFromDoc @lines
expect(docLabels).to.deep.equal ['aaa', 'bbb']
describe 'extractLabelsFromProjectDocs', ->
beforeEach ->
@docs = {
'doc_one': {
_id: 'id_one',
lines: ['one', '\\label{aaa} two', 'three']
},
'doc_two': {
_id: 'id_two',
lines: ['four']
},
'doc_three': {
_id: 'id_three',
lines: ['\\label{bbb}', 'five six', 'seven eight \\label{ccc} nine']
}
}
it 'should extract all the labels', ->
projectLabels = @LabelsHandler.extractLabelsFromProjectDocs @docs
expect(projectLabels).to.deep.equal {
'id_one': ['aaa'],
'id_two': [],
'id_three': ['bbb', 'ccc']
}
describe 'getLabelsForDoc', ->
beforeEach ->
@fakeLines = ['one', '\\label{aaa}', 'two']
@fakeLabels = ['aaa']
@DocumentUpdaterHandler.flushDocToMongo = sinon.stub().callsArgWith(2, null)
@ProjectEntityHandler.getDoc = sinon.stub().callsArgWith(2, null, @fakeLines)
@LabelsHandler.extractLabelsFromDoc = sinon.stub().returns(@fakeLabels)
@call = (callback) =>
@LabelsHandler.getLabelsForDoc @projectId, @docId, callback
it 'should not produce an error', (done) ->
@call (err, docLabels) =>
expect(err).to.equal null
done()
it 'should produce docLabels', (done) ->
@call (err, docLabels) =>
expect(docLabels).to.equal @fakeLabels
done()
it 'should call flushDocToMongo', (done) ->
@call (err, docLabels) =>
@DocumentUpdaterHandler.flushDocToMongo.callCount.should.equal 1
@DocumentUpdaterHandler.flushDocToMongo.calledWith(@projectId, @docId).should.equal true
done()
it 'should call getDoc', (done) ->
@call (err, docLabels) =>
@ProjectEntityHandler.getDoc.callCount.should.equal 1
@ProjectEntityHandler.getDoc.calledWith(@projectId, @docId).should.equal true
done()
it 'should call extractLabelsFromDoc', (done) ->
@call (err, docLabels) =>
@LabelsHandler.extractLabelsFromDoc.callCount.should.equal 1
@LabelsHandler.extractLabelsFromDoc.calledWith(@fakeLines).should.equal true
done()
describe 'getAllLabelsForProject', ->
beforeEach ->
@fakeDocs = {
'doc_one': {lines: ['\\label{aaa}']}
}
@fakeLabels = ['aaa']
@DocumentUpdaterHandler.flushProjectToMongo = sinon.stub().callsArgWith(1, null)
@ProjectEntityHandler.getAllDocs = sinon.stub().callsArgWith(1, null, @fakeDocs)
@LabelsHandler.extractLabelsFromProjectDocs = sinon.stub().returns(@fakeLabels)
@call = (callback) =>
@LabelsHandler.getAllLabelsForProject @projectId, callback
it 'should not produce an error', (done) ->
@call (err, projectLabels) =>
expect(err).to.equal null
done()
it 'should produce projectLabels', (done) ->
@call (err, projectLabels) =>
expect(projectLabels).to.equal @fakeLabels
done()
it 'should call getAllDocs', (done) ->
@call (err, projectLabels) =>
@ProjectEntityHandler.getAllDocs.callCount.should.equal 1
@ProjectEntityHandler.getAllDocs.calledWith(@projectId).should.equal true
done()
it 'should call extractLabelsFromDoc', (done) ->
@call (err, docLabels) =>
@LabelsHandler.extractLabelsFromProjectDocs.callCount.should.equal 1
@LabelsHandler.extractLabelsFromProjectDocs.calledWith(@fakeDocs).should.equal true
done()

View file

@ -0,0 +1,119 @@
chai = require('chai')
chai.should()
expect = chai.expect
sinon = require("sinon")
modulePath = "../../../../app/js/Features/Metadata/MetaController"
SandboxedModule = require('sandboxed-module')
describe 'MetaController', ->
beforeEach ->
@projectId = 'somekindofid'
@EditorRealTimeController = {
emitToRoom: sinon.stub()
}
@MetaHandler = {
getAllMetaForProject: sinon.stub()
getMetaForDoc: sinon.stub()
}
@MetadataController = SandboxedModule.require modulePath, requires:
'logger-sharelatex': {log: sinon.stub(), err: sinon.stub()}
'../Editor/EditorRealTimeController': @EditorRealTimeController
'./MetaHandler': @MetaHandler
describe 'getMetadata', ->
beforeEach ->
@fakeLabels = {'somedoc': ['a_label']}
@MetaHandler.getAllMetaForProject = sinon.stub().callsArgWith(1, null, @fakeLabels)
@req = {params: {project_id: @projectId}}
@res = {json: sinon.stub()}
@next = sinon.stub()
it 'should call MetaHandler.getAllMetaForProject', () ->
@MetadataController.getMetadata(@req, @res, @next)
@MetaHandler.getAllMetaForProject.callCount.should.equal 1
@MetaHandler.getAllMetaForProject.calledWith(@projectId).should.equal true
it 'should call not call next with an error', () ->
@MetadataController.getMetadata(@req, @res, @next)
@next.callCount.should.equal 0
it 'should send a json response', () ->
@MetadataController.getMetadata(@req, @res, @next)
@res.json.callCount.should.equal 1
expect(@res.json.lastCall.args[0]).to.have.all.keys ['projectId', 'projectMeta']
describe 'when MetaHandler.getAllMetaForProject produces an error', ->
beforeEach ->
@MetaHandler.getAllMetaForProject = sinon.stub().callsArgWith(1, new Error('woops'))
@req = {params: {project_id: @projectId}}
@res = {json: sinon.stub()}
@next = sinon.stub()
it 'should call MetaHandler.getAllMetaForProject', () ->
@MetadataController.getMetadata(@req, @res, @next)
@MetaHandler.getAllMetaForProject.callCount.should.equal 1
@MetaHandler.getAllMetaForProject.calledWith(@projectId).should.equal true
it 'should call next with an error', ->
@MetadataController.getMetadata(@req, @res, @next)
@next.callCount.should.equal 1
expect(@next.lastCall.args[0]).to.be.instanceof Error
it 'should not send a json response', ->
@MetadataController.getMetadata(@req, @res, @next)
@res.json.callCount.should.equal 0
describe 'broadcastMetadataForDoc', ->
beforeEach ->
@MetaHandler.getMetaForDoc = sinon.stub().callsArgWith(2, null, @fakeLabels)
@EditorRealTimeController.emitToRoom = sinon.stub()
@docId = 'somedoc'
@req = {params: {project_id: @projectId, doc_id: @docId}}
@res = {sendStatus: sinon.stub()}
@next = sinon.stub()
it 'should call MetaHandler.getMetaForDoc', () ->
@MetadataController.broadcastMetadataForDoc(@req, @res, @next)
@MetaHandler.getMetaForDoc.callCount.should.equal 1
@MetaHandler.getMetaForDoc.calledWith(@projectId).should.equal true
it 'should call not call next with an error', () ->
@MetadataController.broadcastMetadataForDoc(@req, @res, @next)
@next.callCount.should.equal 0
it 'should send a success response', () ->
@MetadataController.broadcastMetadataForDoc(@req, @res, @next)
@res.sendStatus.callCount.should.equal 1
@res.sendStatus.calledWith(200).should.equal true
it 'should emit a message to room', () ->
@MetadataController.broadcastMetadataForDoc(@req, @res, @next)
@EditorRealTimeController.emitToRoom.callCount.should.equal 1
lastCall = @EditorRealTimeController.emitToRoom.lastCall
expect(lastCall.args[0]).to.equal @projectId
expect(lastCall.args[1]).to.equal 'broadcastDocMeta'
expect(lastCall.args[2]).to.have.all.keys ['docId', 'meta']
describe 'when MetaHandler.getMetaForDoc produces an error', ->
beforeEach ->
@MetaHandler.getMetaForDoc = sinon.stub().callsArgWith(2, new Error('woops'))
@EditorRealTimeController.emitToRoom = sinon.stub()
@docId = 'somedoc'
@req = {params: {project_id: @projectId, doc_id: @docId}}
@res = {json: sinon.stub()}
@next = sinon.stub()
it 'should call MetaHandler.getMetaForDoc', () ->
@MetadataController.broadcastMetadataForDoc(@req, @res, @next)
@MetaHandler.getMetaForDoc.callCount.should.equal 1
@MetaHandler.getMetaForDoc.calledWith(@projectId).should.equal true
it 'should call next with an error', ->
@MetadataController.broadcastMetadataForDoc(@req, @res, @next)
@next.callCount.should.equal 1
expect(@next.lastCall.args[0]).to.be.instanceof Error
it 'should not send a json response', ->
@MetadataController.broadcastMetadataForDoc(@req, @res, @next)
@res.json.callCount.should.equal 0

View file

@ -0,0 +1,161 @@
chai = require('chai')
chai.should()
expect = chai.expect
sinon = require("sinon")
modulePath = "../../../../app/js/Features/Metadata/MetaHandler"
SandboxedModule = require('sandboxed-module')
describe 'MetaHandler', ->
beforeEach ->
@projectId = 'someprojectid'
@docId = 'somedocid'
@ProjectEntityHandler = {
getAllDocs: sinon.stub()
getDoc: sinon.stub()
}
@DocumentUpdaterHandler = {
flushDocToMongo: sinon.stub()
}
@MetaHandler = SandboxedModule.require modulePath, requires:
'../Project/ProjectEntityHandler': @ProjectEntityHandler
'../DocumentUpdater/DocumentUpdaterHandler': @DocumentUpdaterHandler
describe 'extractMetaFromDoc', ->
beforeEach ->
@lines = [
'\\usepackage{foo}'
'\\usepackage{bar, baz}'
'one'
'two'
'three \\label{aaa}'
'four five'
'\\label{bbb}'
'six seven'
]
it 'should extract all the labels and packages', ->
docMeta = @MetaHandler.extractMetaFromDoc @lines
expect(docMeta).to.deep.equal {
labels: ['aaa', 'bbb']
packages: ['foo', 'bar', 'baz']
}
describe 'extractMetaFromProjectDocs', ->
beforeEach ->
@docs =
'doc_one':
_id: 'id_one'
lines: ['one', '\\label{aaa} two', 'three']
'doc_two':
_id: 'id_two'
lines: ['four']
'doc_three':
_id: 'id_three'
lines: [
'\\label{bbb}'
'five six'
'seven eight \\label{ccc} nine'
]
'doc_four':
_id: 'id_four'
lines: [
'\\usepackage[foo=bar,baz=bat]{ddd}'
'\\usepackage[draft]{something}'
]
'doc_five':
_id: 'id_five'
lines: [
'\\usepackage{this,that}'
'\\usepackage[options=foo]{hello}'
'some text'
'\\section{this}\\label{sec:intro}'
'In Section \\ref{sec:intro} we saw'
'nothing'
]
it 'should extract all metadata', ->
projectMeta = @MetaHandler.extractMetaFromProjectDocs @docs
expect(projectMeta).to.deep.equal {
'id_one': {labels: ['aaa'], packages: []}
'id_two': {labels: [], packages: []}
'id_three': {labels: ['bbb', 'ccc'], packages: []}
'id_four': {labels: [], packages: ['ddd', 'something']}
'id_five': {labels: ['sec:intro'], packages: ['this', 'that', 'hello']}
}
describe 'getMetaForDoc', ->
beforeEach ->
@fakeLines = ['\\usepackage{abc}', 'one', '\\label{aaa}', 'two']
@fakeMeta = {labels: ['aaa'], packages: ['abc']}
@DocumentUpdaterHandler.flushDocToMongo = sinon.stub().callsArgWith 2, null
@ProjectEntityHandler.getDoc = sinon.stub().callsArgWith 2, null, @fakeLines
@MetaHandler.extractMetaFromDoc = sinon.stub().returns @fakeMeta
@call = (callback) =>
@MetaHandler.getMetaForDoc @projectId, @docId, callback
it 'should not produce an error', (done) ->
@call (err, docMeta) =>
expect(err).to.equal null
done()
it 'should produce docMeta', (done) ->
@call (err, docMeta) =>
expect(docMeta).to.equal @fakeMeta
done()
it 'should call flushDocToMongo', (done) ->
@call (err, docMeta) =>
@DocumentUpdaterHandler.flushDocToMongo.callCount.should.equal 1
@DocumentUpdaterHandler.flushDocToMongo.calledWith(@projectId, @docId).should.equal true
done()
it 'should call getDoc', (done) ->
@call (err, docMeta) =>
@ProjectEntityHandler.getDoc.callCount.should.equal 1
@ProjectEntityHandler.getDoc.calledWith(@projectId, @docId).should.equal true
done()
it 'should call extractMetaFromDoc', (done) ->
@call (err, docMeta) =>
@MetaHandler.extractMetaFromDoc.callCount.should.equal 1
@MetaHandler.extractMetaFromDoc.calledWith(@fakeLines).should.equal true
done()
describe 'getAllMetaForProject', ->
beforeEach ->
@fakeDocs =
'doc_one':
lines: [
'\\usepackage[some-options,more=foo]{pkg}'
'\\label{aaa}'
]
@fakeMeta = {labels: ['aaa'], packages: ['pkg']}
@DocumentUpdaterHandler.flushProjectToMongo = sinon.stub().callsArgWith 1, null
@ProjectEntityHandler.getAllDocs = sinon.stub().callsArgWith 1, null, @fakeDocs
@MetaHandler.extractMetaFromProjectDocs = sinon.stub().returns @fakeMeta
@call = (callback) =>
@MetaHandler.getAllMetaForProject @projectId, callback
it 'should not produce an error', (done) ->
@call (err, projectMeta) =>
expect(err).to.equal null
done()
it 'should produce projectMeta', (done) ->
@call (err, projectMeta) =>
expect(projectMeta).to.equal @fakeMeta
done()
it 'should call getAllDocs', (done) ->
@call (err, projectMeta) =>
@ProjectEntityHandler.getAllDocs.callCount.should.equal 1
@ProjectEntityHandler.getAllDocs.calledWith(@projectId).should.equal true
done()
it 'should call extractMetaFromDoc', (done) ->
@call (err, docMeta) =>
@MetaHandler.extractMetaFromProjectDocs.callCount.should.equal 1
@MetaHandler.extractMetaFromProjectDocs.calledWith(@fakeDocs).should.equal true
done()