You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-04 15:02:09 +03:00
chore: improve coverage
This commit is contained in:
11
index.js
11
index.js
@@ -38,6 +38,7 @@ class RedisClient extends EventEmitter {
|
||||
// TODO: Add a more restrictive options validation
|
||||
const cnxOptions = {}
|
||||
for (const tlsOption in options.tls) {
|
||||
/* istanbul ignore else */
|
||||
if (options.tls.hasOwnProperty(tlsOption)) {
|
||||
cnxOptions[tlsOption] = options.tls[tlsOption]
|
||||
// Copy the tls options into the general options to make sure the address is set right
|
||||
@@ -56,7 +57,7 @@ class RedisClient extends EventEmitter {
|
||||
} else {
|
||||
cnxOptions.port = +options.port || 6379
|
||||
cnxOptions.host = options.host || '127.0.0.1'
|
||||
cnxOptions.family = (!options.family && net.isIP(cnxOptions.host)) || (options.family === 'IPv6' ? 6 : 4)
|
||||
cnxOptions.family = (!options.family && net.isIP(cnxOptions.host)) || (options.host && options.family === 'IPv6' ? 6 : 4)
|
||||
this.address = `${cnxOptions.host}:${cnxOptions.port}`
|
||||
}
|
||||
// TODO: Properly fix typo
|
||||
@@ -64,6 +65,7 @@ class RedisClient extends EventEmitter {
|
||||
options.socketKeepalive = true
|
||||
}
|
||||
for (const command in options.renameCommands) {
|
||||
/* istanbul ignore else */
|
||||
if (options.renameCommands.hasOwnProperty(command)) {
|
||||
options.renameCommands[command.toLowerCase()] = options.renameCommands[command]
|
||||
}
|
||||
@@ -234,7 +236,7 @@ class RedisClient extends EventEmitter {
|
||||
this._stream.unref()
|
||||
} else {
|
||||
debug('Not connected yet, will unref later')
|
||||
this.once('connect', () => this._stream.unref())
|
||||
this.once('connect', () => this.unref())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -251,6 +253,7 @@ class RedisClient extends EventEmitter {
|
||||
const existingOptions = utils.clone(this._options)
|
||||
options = utils.clone(options)
|
||||
for (const elem in options) {
|
||||
/* istanbul ignore else */
|
||||
if (options.hasOwnProperty(elem)) {
|
||||
existingOptions[elem] = options[elem]
|
||||
}
|
||||
@@ -280,11 +283,11 @@ class RedisClient extends EventEmitter {
|
||||
|
||||
// Note: this overrides a native function!
|
||||
multi (args) {
|
||||
return new Multi(this, args, 'multi')
|
||||
return new Multi(this, 'multi', args)
|
||||
}
|
||||
|
||||
batch (args) {
|
||||
return new Multi(this, args, 'batch')
|
||||
return new Multi(this, 'batch', args)
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -43,6 +43,7 @@ function unifyOptions (portArg, hostArg, options) {
|
||||
}
|
||||
if (parsed.search !== '') {
|
||||
for (var elem in parsed.query) {
|
||||
/* istanbul ignore if */
|
||||
if (!Object.prototype.hasOwnProperty.call(parsed.query, elem)) {
|
||||
continue
|
||||
}
|
||||
|
11
lib/multi.js
11
lib/multi.js
@@ -80,10 +80,7 @@ function execTransaction (multi) {
|
||||
err.command = 'EXEC'
|
||||
err.code = 'EXECABORT'
|
||||
return new Promise((resolve, reject) => {
|
||||
utils.replyInOrder(client, (err, res) => {
|
||||
if (err) return reject(err)
|
||||
resolve(res)
|
||||
}, null, [])
|
||||
utils.replyInOrder(client, reject, err)
|
||||
})
|
||||
}
|
||||
const len = queue.length
|
||||
@@ -145,14 +142,14 @@ class Multi {
|
||||
/**
|
||||
* Creates an instance of Multi.
|
||||
* @param {RedisClient} client
|
||||
* @param {any[]} [args]
|
||||
* @param {string} [type]
|
||||
* @param {any[]} [args]
|
||||
*
|
||||
* @memberof Multi
|
||||
*/
|
||||
constructor (client, args, type) {
|
||||
constructor (client, type, args) {
|
||||
this._client = client
|
||||
this._type = typeof type === 'string' ? type : 'multi'
|
||||
this._type = type
|
||||
this._queue = new Queue()
|
||||
// Either undefined or an array. Fail hard if it's not an array
|
||||
if (args) {
|
||||
|
@@ -112,7 +112,7 @@ function toString (arg) {
|
||||
|
||||
function returnErr (client, command) {
|
||||
const err = new TypeError('NodeRedis can not handle the provided arguments (see "error.issues" property).\n\nFurther information https://github.com/asd')
|
||||
err.command = command.name.toUpperCase()
|
||||
err.command = command.command.toUpperCase()
|
||||
err.args = command.args
|
||||
err.issues = errors
|
||||
errors = null
|
||||
|
@@ -71,6 +71,14 @@ describe('The \'client\' method', () => {
|
||||
promises.push(client.get('foo').then(helper.isString('bar')))
|
||||
return Promise.all(promises)
|
||||
})
|
||||
|
||||
it('weird', function () {
|
||||
helper.serverVersionAtLeast.call(this, client, [3, 2, 0])
|
||||
assert.strictEqual(client._reply, 'ON')
|
||||
const promise = client.client('REPLY', 'WEIRD').then(helper.fail, helper.isError())
|
||||
assert.strictEqual(client._reply, 'ON')
|
||||
return promise
|
||||
})
|
||||
})
|
||||
|
||||
describe('in a batch context', () => {
|
||||
@@ -112,6 +120,14 @@ describe('The \'client\' method', () => {
|
||||
assert.deepStrictEqual(res, ['OK', undefined, undefined, 'bar'])
|
||||
})
|
||||
})
|
||||
|
||||
it('weird', function () {
|
||||
helper.serverVersionAtLeast.call(this, client, [3, 2, 0])
|
||||
assert.strictEqual(client._reply, 'ON')
|
||||
const promise = client.batch().client('REPLY', 'WEIRD').exec().then(helper.fail, helper.isError())
|
||||
assert.strictEqual(client._reply, 'ON')
|
||||
return promise
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
|
@@ -413,6 +413,20 @@ describe('connection tests', () => {
|
||||
})
|
||||
})
|
||||
|
||||
it('connects correctly even if the info command returns a empty string', (done) => {
|
||||
client = Redis.createClient.apply(null, args)
|
||||
const end = helper.callFuncAfter(done, 2)
|
||||
client.info = function () {
|
||||
// Mock the result
|
||||
end()
|
||||
return Promise.resolve('')
|
||||
}
|
||||
client.once('ready', () => {
|
||||
assert.strictEqual(Object.keys(client.serverInfo).length, 0)
|
||||
end()
|
||||
})
|
||||
})
|
||||
|
||||
if (ip === 'IPv4') {
|
||||
it('allows connecting with the redis url to the default host and port, select db 3 and warn about duplicate db option', (done) => {
|
||||
client = Redis.createClient('redis:///3?db=3')
|
||||
|
@@ -198,9 +198,12 @@ describe('The \'multi\' method', () => {
|
||||
client = redis.createClient({
|
||||
host: 'somewhere',
|
||||
port: 6379,
|
||||
family: 'IPv6',
|
||||
retryStrategy () {}
|
||||
})
|
||||
|
||||
assert.strictEqual(client._connectionOptions.family, 6)
|
||||
|
||||
client.on('error', (err) => {
|
||||
assert.strictEqual(err.code, 'NR_CLOSED')
|
||||
done()
|
||||
|
@@ -57,6 +57,26 @@ describe('The nodeRedis client', () => {
|
||||
client._stream.destroy()
|
||||
})
|
||||
|
||||
describe('arguments validation', () => {
|
||||
it('no boolean for enableOfflineQueue', () => {
|
||||
try {
|
||||
redis.createClient({ enableOfflineQueue: 'test' })
|
||||
throw new Error('failed')
|
||||
} catch (err) {
|
||||
assert.strictEqual(err.name, 'TypeError')
|
||||
}
|
||||
})
|
||||
|
||||
it('no boolean for enableOfflineQueue', () => {
|
||||
try {
|
||||
redis.createClient({ password: 'test', authPass: 'foobar' })
|
||||
throw new Error('failed')
|
||||
} catch (err) {
|
||||
assert.strictEqual(err.name, 'TypeError')
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
helper.allTests((ip, args) => {
|
||||
describe(`using ${ip}`, () => {
|
||||
afterEach(() => {
|
||||
@@ -69,6 +89,22 @@ describe('The nodeRedis client', () => {
|
||||
return client.flushdb()
|
||||
})
|
||||
|
||||
describe('argument errors', () => {
|
||||
it('should return an error in case of "null" argument', () => {
|
||||
return client.set('foo', null).then(helper.fail, (err) => {
|
||||
assert.deepStrictEqual(err.issues, [null])
|
||||
})
|
||||
})
|
||||
|
||||
it('should return an error in case of unknown types', () => {
|
||||
class Foo {}
|
||||
const tmp = new Foo()
|
||||
return client.set(tmp, undefined).then(helper.fail, (err) => {
|
||||
assert.deepStrictEqual(err.issues, [tmp, undefined])
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('duplicate', () => {
|
||||
it('check if all options got copied properly', (done) => {
|
||||
client.selectedDb = 2
|
||||
|
Reference in New Issue
Block a user