Просмотр исходного кода

added unit test (work in progress)

Brian Gough 11 лет назад
Родитель
Сommit
86505047a3
1 измененных файлов с 100 добавлено и 0 удалено
  1. 100 0
      services/document-updater/test/unit/coffee/Compression.coffee

+ 100 - 0
services/document-updater/test/unit/coffee/Compression.coffee

@@ -0,0 +1,100 @@
+sinon = require('sinon')
+chai = require('chai')
+should = chai.should()
+modulePath = "../../../app/js/RedisManager"
+SandboxedModule = require('sandboxed-module')
+
+describe "RedisManager.setDocument and getDocument", ->
+	beforeEach ->
+		@zip_opts =
+			writesEnabled: true
+			minSize: 1000
+		@doc_id = "document-id"
+		@version = 123
+		@RedisManager = SandboxedModule.require modulePath, requires:
+			"settings-sharelatex" :
+				redis:
+					web:
+						host: 'none'
+						port: 'none'
+					zip: @zip_opts
+			"redis-sharelatex" : createClient: () =>
+				@rclient ?=
+					auth:-> # only assign one rclient
+					multi: () => @rclient
+					set: (key, value) => @rclient.store[key] = value
+					get: (key) => @rclient.results.push @rclient.store[key]
+					incr: (key) => @rclient.store[key]++
+					exec: (callback) =>
+						callback.apply(null, [null, @rclient.results])
+						@rclient.results = []
+					store: {}
+					results: []
+			"logger-sharelatex": {}
+
+		@RedisManager.setDocument(@doc_id, @docLines, @version, @callback)
+
+	describe "for a small document (uncompressed)", ->
+		before ->
+			@docLines = ["hello", "world"]
+			@callback = sinon.stub()
+
+		it "should set the document", ->
+			@rclient.store['doclines:document-id']
+				.should.equal JSON.stringify(@docLines)
+
+		it "should return the callback", ->
+			@callback.called.should.equal true
+
+		it "should get the document back again", (done) ->
+			@RedisManager.getDoc @doc_id, (err, lines, version) =>
+				@docLines.should.eql lines
+				done()
+
+	describe "for a large document (with compression enabled)", ->
+		before ->
+			@zip_opts =
+				writesEnabled: true
+				minSize: 1000
+			@docLines = []
+			while @docLines.join('').length <= @zip_opts.minSize
+				@docLines.push "this is a long line in a long document"
+			@callback = sinon.stub()
+		
+		it "should set the document as a gzipped blob", ->
+			@rclient.store['doclines:document-id']
+				.should.not.equal JSON.stringify(@docLines)
+
+		it "should return the callback", ->
+			@callback.called.should.equal true
+
+		it "should get the uncompressed document back again", (done) ->
+			@RedisManager.getDoc @doc_id, (err, lines, version) =>
+				@docLines.should.eql lines
+				done()
+
+	describe "for a large document (with compression disabled)", ->
+		before ->
+			@zip_opts =
+				writesEnabled: false
+				minSize: 1000
+			@docLines = []
+			while @docLines.join('').length <= @zip_opts.minSize
+				@docLines.push "this is a long line in a long document"
+			@callback = sinon.stub()
+		
+		it "should set the document", ->
+			@rclient.store['doclines:document-id']
+				.should.equal JSON.stringify(@docLines)
+
+		it "should return the callback", ->
+			@callback.called.should.equal true
+
+		it "should get the document back again", (done) ->
+			@RedisManager.getDoc @doc_id, (err, lines, version) =>
+				@docLines.should.eql lines
+				done()
+
+
+
+