From 86505047a3786fd23c4d4af64edc0f3362318562 Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Fri, 27 Mar 2015 17:04:38 +0000 Subject: [PATCH] added unit test (work in progress) --- .../test/unit/coffee/Compression.coffee | 100 ++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 services/document-updater/test/unit/coffee/Compression.coffee diff --git a/services/document-updater/test/unit/coffee/Compression.coffee b/services/document-updater/test/unit/coffee/Compression.coffee new file mode 100644 index 0000000000..bba1dd5c3e --- /dev/null +++ b/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() + + + +