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
|
// TODO: Add a more restrictive options validation
|
||||||
const cnxOptions = {}
|
const cnxOptions = {}
|
||||||
for (const tlsOption in options.tls) {
|
for (const tlsOption in options.tls) {
|
||||||
|
/* istanbul ignore else */
|
||||||
if (options.tls.hasOwnProperty(tlsOption)) {
|
if (options.tls.hasOwnProperty(tlsOption)) {
|
||||||
cnxOptions[tlsOption] = options.tls[tlsOption]
|
cnxOptions[tlsOption] = options.tls[tlsOption]
|
||||||
// Copy the tls options into the general options to make sure the address is set right
|
// 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 {
|
} else {
|
||||||
cnxOptions.port = +options.port || 6379
|
cnxOptions.port = +options.port || 6379
|
||||||
cnxOptions.host = options.host || '127.0.0.1'
|
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}`
|
this.address = `${cnxOptions.host}:${cnxOptions.port}`
|
||||||
}
|
}
|
||||||
// TODO: Properly fix typo
|
// TODO: Properly fix typo
|
||||||
@@ -64,6 +65,7 @@ class RedisClient extends EventEmitter {
|
|||||||
options.socketKeepalive = true
|
options.socketKeepalive = true
|
||||||
}
|
}
|
||||||
for (const command in options.renameCommands) {
|
for (const command in options.renameCommands) {
|
||||||
|
/* istanbul ignore else */
|
||||||
if (options.renameCommands.hasOwnProperty(command)) {
|
if (options.renameCommands.hasOwnProperty(command)) {
|
||||||
options.renameCommands[command.toLowerCase()] = options.renameCommands[command]
|
options.renameCommands[command.toLowerCase()] = options.renameCommands[command]
|
||||||
}
|
}
|
||||||
@@ -234,7 +236,7 @@ class RedisClient extends EventEmitter {
|
|||||||
this._stream.unref()
|
this._stream.unref()
|
||||||
} else {
|
} else {
|
||||||
debug('Not connected yet, will unref later')
|
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)
|
const existingOptions = utils.clone(this._options)
|
||||||
options = utils.clone(options)
|
options = utils.clone(options)
|
||||||
for (const elem in options) {
|
for (const elem in options) {
|
||||||
|
/* istanbul ignore else */
|
||||||
if (options.hasOwnProperty(elem)) {
|
if (options.hasOwnProperty(elem)) {
|
||||||
existingOptions[elem] = options[elem]
|
existingOptions[elem] = options[elem]
|
||||||
}
|
}
|
||||||
@@ -280,11 +283,11 @@ class RedisClient extends EventEmitter {
|
|||||||
|
|
||||||
// Note: this overrides a native function!
|
// Note: this overrides a native function!
|
||||||
multi (args) {
|
multi (args) {
|
||||||
return new Multi(this, args, 'multi')
|
return new Multi(this, 'multi', args)
|
||||||
}
|
}
|
||||||
|
|
||||||
batch (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 !== '') {
|
if (parsed.search !== '') {
|
||||||
for (var elem in parsed.query) {
|
for (var elem in parsed.query) {
|
||||||
|
/* istanbul ignore if */
|
||||||
if (!Object.prototype.hasOwnProperty.call(parsed.query, elem)) {
|
if (!Object.prototype.hasOwnProperty.call(parsed.query, elem)) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
11
lib/multi.js
11
lib/multi.js
@@ -80,10 +80,7 @@ function execTransaction (multi) {
|
|||||||
err.command = 'EXEC'
|
err.command = 'EXEC'
|
||||||
err.code = 'EXECABORT'
|
err.code = 'EXECABORT'
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
utils.replyInOrder(client, (err, res) => {
|
utils.replyInOrder(client, reject, err)
|
||||||
if (err) return reject(err)
|
|
||||||
resolve(res)
|
|
||||||
}, null, [])
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
const len = queue.length
|
const len = queue.length
|
||||||
@@ -145,14 +142,14 @@ class Multi {
|
|||||||
/**
|
/**
|
||||||
* Creates an instance of Multi.
|
* Creates an instance of Multi.
|
||||||
* @param {RedisClient} client
|
* @param {RedisClient} client
|
||||||
* @param {any[]} [args]
|
|
||||||
* @param {string} [type]
|
* @param {string} [type]
|
||||||
|
* @param {any[]} [args]
|
||||||
*
|
*
|
||||||
* @memberof Multi
|
* @memberof Multi
|
||||||
*/
|
*/
|
||||||
constructor (client, args, type) {
|
constructor (client, type, args) {
|
||||||
this._client = client
|
this._client = client
|
||||||
this._type = typeof type === 'string' ? type : 'multi'
|
this._type = type
|
||||||
this._queue = new Queue()
|
this._queue = new Queue()
|
||||||
// Either undefined or an array. Fail hard if it's not an array
|
// Either undefined or an array. Fail hard if it's not an array
|
||||||
if (args) {
|
if (args) {
|
||||||
|
@@ -112,7 +112,7 @@ function toString (arg) {
|
|||||||
|
|
||||||
function returnErr (client, command) {
|
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')
|
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.args = command.args
|
||||||
err.issues = errors
|
err.issues = errors
|
||||||
errors = null
|
errors = null
|
||||||
|
@@ -71,6 +71,14 @@ describe('The \'client\' method', () => {
|
|||||||
promises.push(client.get('foo').then(helper.isString('bar')))
|
promises.push(client.get('foo').then(helper.isString('bar')))
|
||||||
return Promise.all(promises)
|
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', () => {
|
describe('in a batch context', () => {
|
||||||
@@ -112,6 +120,14 @@ describe('The \'client\' method', () => {
|
|||||||
assert.deepStrictEqual(res, ['OK', undefined, undefined, 'bar'])
|
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') {
|
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) => {
|
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')
|
client = Redis.createClient('redis:///3?db=3')
|
||||||
|
@@ -198,9 +198,12 @@ describe('The \'multi\' method', () => {
|
|||||||
client = redis.createClient({
|
client = redis.createClient({
|
||||||
host: 'somewhere',
|
host: 'somewhere',
|
||||||
port: 6379,
|
port: 6379,
|
||||||
|
family: 'IPv6',
|
||||||
retryStrategy () {}
|
retryStrategy () {}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
assert.strictEqual(client._connectionOptions.family, 6)
|
||||||
|
|
||||||
client.on('error', (err) => {
|
client.on('error', (err) => {
|
||||||
assert.strictEqual(err.code, 'NR_CLOSED')
|
assert.strictEqual(err.code, 'NR_CLOSED')
|
||||||
done()
|
done()
|
||||||
|
@@ -57,6 +57,26 @@ describe('The nodeRedis client', () => {
|
|||||||
client._stream.destroy()
|
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) => {
|
helper.allTests((ip, args) => {
|
||||||
describe(`using ${ip}`, () => {
|
describe(`using ${ip}`, () => {
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
@@ -69,6 +89,22 @@ describe('The nodeRedis client', () => {
|
|||||||
return client.flushdb()
|
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', () => {
|
describe('duplicate', () => {
|
||||||
it('check if all options got copied properly', (done) => {
|
it('check if all options got copied properly', (done) => {
|
||||||
client.selectedDb = 2
|
client.selectedDb = 2
|
||||||
|
Reference in New Issue
Block a user