[misc] decaffeinate tests: decaffeinate unit tests

This commit is contained in:
Jakob Ackermann 2020-11-09 16:42:41 +00:00
parent 7210b3dfed
commit 7b69cc42c9
2 changed files with 134 additions and 75 deletions

View file

@ -9,7 +9,7 @@
"lint": "eslint --max-warnings 0 .", "lint": "eslint --max-warnings 0 .",
"format": "prettier-eslint $PWD'/**/*.js' --list-different", "format": "prettier-eslint $PWD'/**/*.js' --list-different",
"format:fix": "prettier-eslint $PWD'/**/*.js' --write", "format:fix": "prettier-eslint $PWD'/**/*.js' --write",
"test": "mocha --require coffee-script/register test/unit/src/*.coffee" "test": "mocha --recursive test/unit/src/"
}, },
"dependencies": { "dependencies": {
"coffee-script": "1.8.0", "coffee-script": "1.8.0",

View file

@ -1,88 +1,147 @@
should = require('chai').should() /*
SandboxedModule = require('sandboxed-module') * decaffeinate suggestions:
assert = require('assert') * DS102: Remove unnecessary code created because of implicit returns
path = require('path') * DS206: Consider reworking classes to avoid initClass
sinon = require('sinon') * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
modulePath = path.join __dirname, "./../../../index.js" */
expect = require("chai").expect const should = require('chai').should()
const SandboxedModule = require('sandboxed-module')
const assert = require('assert')
const path = require('path')
const sinon = require('sinon')
const modulePath = path.join(__dirname, './../../../index.js')
const { expect } = require('chai')
describe "index", -> describe('index', function () {
beforeEach(function () {
let Cluster, IoRedis, ioredisConstructor
this.settings = {}
this.ioredisConstructor = ioredisConstructor = sinon.stub()
beforeEach -> this.ioredis = IoRedis = (function () {
let createIoRedis
IoRedis = class IoRedis {
static initClass() {
this.prototype.on = sinon.stub()
createIoRedis = ioredisConstructor
}
@settings = {} constructor() {
@ioredisConstructor = ioredisConstructor = sinon.stub() return createIoRedis.apply(this, arguments)
}
}
IoRedis.initClass()
return IoRedis
})()
this.ioredis.Cluster = Cluster = (function () {
Cluster = class Cluster {
static initClass() {
this.prototype.on = sinon.stub()
}
@ioredis = class IoRedis constructor(config, options) {
constructor: ioredisConstructor this.config = config
on: sinon.stub() this.options = options
@ioredis.Cluster = class Cluster }
constructor: (@config, @options) -> }
on: sinon.stub() Cluster.initClass()
@redis = SandboxedModule.require modulePath, requires: return Cluster
"ioredis": @ioredis })()
@auth_pass = "1234 pass" this.redis = SandboxedModule.require(modulePath, {
requires: {
ioredis: this.ioredis,
},
})
return (this.auth_pass = '1234 pass')
})
describe "single node redis", -> describe('single node redis', function () {
beforeEach -> beforeEach(function () {
@standardOpts = return (this.standardOpts = {
auth_pass: @auth_pass auth_pass: this.auth_pass,
port: 1234 port: 1234,
host: "redis.mysite.env" host: 'redis.mysite.env',
})
})
it "should use the ioredis driver in single-instance mode if a non array is passed", -> it('should use the ioredis driver in single-instance mode if a non array is passed', function () {
client = @redis.createClient @standardOpts const client = this.redis.createClient(this.standardOpts)
assert.equal(client.constructor, @ioredis) return assert.equal(client.constructor, this.ioredis)
})
it "should call createClient for the ioredis driver in single-instance mode if a non array is passed", -> return it('should call createClient for the ioredis driver in single-instance mode if a non array is passed', function () {
client = @redis.createClient @standardOpts const client = this.redis.createClient(this.standardOpts)
@ioredisConstructor.calledWith(@standardOpts).should.equal true return this.ioredisConstructor
.calledWith(this.standardOpts)
.should.equal(true)
})
})
describe "cluster", -> describe('cluster', function () {
beforeEach -> beforeEach(function () {
@cluster = [{"mock": "cluster"}, { "mock": "cluster2"}] this.cluster = [{ mock: 'cluster' }, { mock: 'cluster2' }]
@extraOptions = {keepAlive:100} this.extraOptions = { keepAlive: 100 }
@settings = return (this.settings = {
cluster: @cluster cluster: this.cluster,
redisOptions: @extraOptions redisOptions: this.extraOptions,
key_schema: {foo: (x) -> "#{x}"} key_schema: {
foo(x) {
return `${x}`
},
},
})
})
it "should pass the options correctly though with no options", -> it('should pass the options correctly though with no options', function () {
client = @redis.createClient cluster: @cluster const client = this.redis.createClient({ cluster: this.cluster })
assert(client instanceof @ioredis.Cluster) assert(client instanceof this.ioredis.Cluster)
client.config.should.deep.equal @cluster return client.config.should.deep.equal(this.cluster)
})
it "should not pass the key_schema through to the driver", -> it('should not pass the key_schema through to the driver', function () {
client = @redis.createClient cluster: @cluster, key_schema: "foobar" const client = this.redis.createClient({
assert(client instanceof @ioredis.Cluster) cluster: this.cluster,
client.config.should.deep.equal @cluster key_schema: 'foobar',
expect(client.options).to.deep.equal {retry_max_delay: 5000} })
assert(client instanceof this.ioredis.Cluster)
client.config.should.deep.equal(this.cluster)
return expect(client.options).to.deep.equal({ retry_max_delay: 5000 })
})
it "should pass the options correctly though with additional options", -> return it('should pass the options correctly though with additional options', function () {
client = @redis.createClient @settings const client = this.redis.createClient(this.settings)
assert(client instanceof @ioredis.Cluster) assert(client instanceof this.ioredis.Cluster)
client.config.should.deep.equal @cluster client.config.should.deep.equal(this.cluster)
# need to use expect here because of _.clone in sandbox // need to use expect here because of _.clone in sandbox
expect(client.options).to.deep.equal {redisOptions: @extraOptions, retry_max_delay: 5000} return expect(client.options).to.deep.equal({
redisOptions: this.extraOptions,
retry_max_delay: 5000,
})
})
})
describe "monkey patch ioredis exec", -> return describe('monkey patch ioredis exec', function () {
beforeEach -> beforeEach(function () {
@callback = sinon.stub() this.callback = sinon.stub()
@results = [] this.results = []
@multiOrig = { exec: sinon.stub().yields(null, @results)} this.multiOrig = { exec: sinon.stub().yields(null, this.results) }
@client = { multi: sinon.stub().returns(@multiOrig) } this.client = { multi: sinon.stub().returns(this.multiOrig) }
@redis._monkeyPatchIoredisExec(@client) this.redis._monkeyPatchIoredisExec(this.client)
@multi = @client.multi() return (this.multi = this.client.multi())
})
it "should return the old redis format for an array", -> it('should return the old redis format for an array', function () {
@results[0] = [null, 42] this.results[0] = [null, 42]
@results[1] = [null, "foo"] this.results[1] = [null, 'foo']
@multi.exec @callback this.multi.exec(this.callback)
@callback.calledWith(null, [42, "foo"]).should.equal true return this.callback.calledWith(null, [42, 'foo']).should.equal(true)
})
it "should return the old redis format when there is an error", ->
@results[0] = [null, 42]
@results[1] = ["error", "foo"]
@multi.exec @callback
@callback.calledWith("error").should.equal true
return it('should return the old redis format when there is an error', function () {
this.results[0] = [null, 42]
this.results[1] = ['error', 'foo']
this.multi.exec(this.callback)
return this.callback.calledWith('error').should.equal(true)
})
})
})