MetaHandlerTests.coffee 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. chai = require('chai')
  2. chai.should()
  3. expect = chai.expect
  4. sinon = require("sinon")
  5. modulePath = "../../../../app/js/Features/Metadata/MetaHandler"
  6. SandboxedModule = require('sandboxed-module')
  7. describe 'MetaHandler', ->
  8. beforeEach ->
  9. @projectId = 'someprojectid'
  10. @docId = 'somedocid'
  11. @ProjectEntityHandler = {
  12. getAllDocs: sinon.stub()
  13. getDoc: sinon.stub()
  14. }
  15. @DocumentUpdaterHandler = {
  16. flushDocToMongo: sinon.stub()
  17. }
  18. @MetaHandler = SandboxedModule.require modulePath, requires:
  19. '../Project/ProjectEntityHandler': @ProjectEntityHandler
  20. '../DocumentUpdater/DocumentUpdaterHandler': @DocumentUpdaterHandler
  21. describe 'extractMetaFromDoc', ->
  22. beforeEach ->
  23. @lines = [
  24. '\\usepackage{foo}'
  25. '\\usepackage{bar, baz}'
  26. 'one'
  27. 'two'
  28. 'three \\label{aaa}'
  29. 'four five'
  30. '\\label{bbb}'
  31. 'six seven'
  32. ]
  33. it 'should extract all the labels and packages', ->
  34. docMeta = @MetaHandler.extractMetaFromDoc @lines
  35. expect(docMeta).to.deep.equal {
  36. labels: ['aaa', 'bbb']
  37. packages: ['foo', 'bar', 'baz']
  38. }
  39. describe 'extractMetaFromProjectDocs', ->
  40. beforeEach ->
  41. @docs =
  42. 'doc_one':
  43. _id: 'id_one'
  44. lines: ['one', '\\label{aaa} two', 'three']
  45. 'doc_two':
  46. _id: 'id_two'
  47. lines: ['four']
  48. 'doc_three':
  49. _id: 'id_three'
  50. lines: [
  51. '\\label{bbb}'
  52. 'five six'
  53. 'seven eight \\label{ccc} nine'
  54. ]
  55. 'doc_four':
  56. _id: 'id_four'
  57. lines: [
  58. '\\usepackage[foo=bar,baz=bat]{ddd}'
  59. '\\usepackage[draft]{something}'
  60. ]
  61. 'doc_five':
  62. _id: 'id_five'
  63. lines: [
  64. '\\usepackage{this,that}'
  65. '\\usepackage[options=foo]{hello}'
  66. 'some text'
  67. '\\section{this}\\label{sec:intro}'
  68. 'In Section \\ref{sec:intro} we saw'
  69. 'nothing'
  70. ]
  71. it 'should extract all metadata', ->
  72. projectMeta = @MetaHandler.extractMetaFromProjectDocs @docs
  73. expect(projectMeta).to.deep.equal {
  74. 'id_one': {labels: ['aaa'], packages: []}
  75. 'id_two': {labels: [], packages: []}
  76. 'id_three': {labels: ['bbb', 'ccc'], packages: []}
  77. 'id_four': {labels: [], packages: ['ddd', 'something']}
  78. 'id_five': {labels: ['sec:intro'], packages: ['this', 'that', 'hello']}
  79. }
  80. describe 'getMetaForDoc', ->
  81. beforeEach ->
  82. @fakeLines = ['\\usepackage{abc}', 'one', '\\label{aaa}', 'two']
  83. @fakeMeta = {labels: ['aaa'], packages: ['abc']}
  84. @DocumentUpdaterHandler.flushDocToMongo = sinon.stub().callsArgWith 2, null
  85. @ProjectEntityHandler.getDoc = sinon.stub().callsArgWith 2, null, @fakeLines
  86. @MetaHandler.extractMetaFromDoc = sinon.stub().returns @fakeMeta
  87. @call = (callback) =>
  88. @MetaHandler.getMetaForDoc @projectId, @docId, callback
  89. it 'should not produce an error', (done) ->
  90. @call (err, docMeta) =>
  91. expect(err).to.equal null
  92. done()
  93. it 'should produce docMeta', (done) ->
  94. @call (err, docMeta) =>
  95. expect(docMeta).to.equal @fakeMeta
  96. done()
  97. it 'should call flushDocToMongo', (done) ->
  98. @call (err, docMeta) =>
  99. @DocumentUpdaterHandler.flushDocToMongo.callCount.should.equal 1
  100. @DocumentUpdaterHandler.flushDocToMongo.calledWith(@projectId, @docId).should.equal true
  101. done()
  102. it 'should call getDoc', (done) ->
  103. @call (err, docMeta) =>
  104. @ProjectEntityHandler.getDoc.callCount.should.equal 1
  105. @ProjectEntityHandler.getDoc.calledWith(@projectId, @docId).should.equal true
  106. done()
  107. it 'should call extractMetaFromDoc', (done) ->
  108. @call (err, docMeta) =>
  109. @MetaHandler.extractMetaFromDoc.callCount.should.equal 1
  110. @MetaHandler.extractMetaFromDoc.calledWith(@fakeLines).should.equal true
  111. done()
  112. describe 'getAllMetaForProject', ->
  113. beforeEach ->
  114. @fakeDocs =
  115. 'doc_one':
  116. lines: [
  117. '\\usepackage[some-options,more=foo]{pkg}'
  118. '\\label{aaa}'
  119. ]
  120. @fakeMeta = {labels: ['aaa'], packages: ['pkg']}
  121. @DocumentUpdaterHandler.flushProjectToMongo = sinon.stub().callsArgWith 1, null
  122. @ProjectEntityHandler.getAllDocs = sinon.stub().callsArgWith 1, null, @fakeDocs
  123. @MetaHandler.extractMetaFromProjectDocs = sinon.stub().returns @fakeMeta
  124. @call = (callback) =>
  125. @MetaHandler.getAllMetaForProject @projectId, callback
  126. it 'should not produce an error', (done) ->
  127. @call (err, projectMeta) =>
  128. expect(err).to.equal null
  129. done()
  130. it 'should produce projectMeta', (done) ->
  131. @call (err, projectMeta) =>
  132. expect(projectMeta).to.equal @fakeMeta
  133. done()
  134. it 'should call getAllDocs', (done) ->
  135. @call (err, projectMeta) =>
  136. @ProjectEntityHandler.getAllDocs.callCount.should.equal 1
  137. @ProjectEntityHandler.getAllDocs.calledWith(@projectId).should.equal true
  138. done()
  139. it 'should call extractMetaFromDoc', (done) ->
  140. @call (err, docMeta) =>
  141. @MetaHandler.extractMetaFromProjectDocs.callCount.should.equal 1
  142. @MetaHandler.extractMetaFromProjectDocs.calledWith(@fakeDocs).should.equal true
  143. done()