You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-06 02:15:48 +03:00
chore: refactor codebase to promises
This commit is contained in:
@@ -11,104 +11,60 @@ describe('The \'select\' method', () => {
|
||||
describe('when not connected', () => {
|
||||
let client
|
||||
|
||||
beforeEach((done) => {
|
||||
beforeEach(() => {
|
||||
client = redis.createClient.apply(null, args)
|
||||
client.once('ready', () => {
|
||||
client.quit()
|
||||
})
|
||||
client.on('end', done)
|
||||
return client.quit()
|
||||
})
|
||||
|
||||
it('returns an error if redis is not connected', (done) => {
|
||||
client.select(1, (err, res) => {
|
||||
assert(err.message.match(/The connection is already closed/))
|
||||
done()
|
||||
})
|
||||
it('returns an error if redis is not connected', () => {
|
||||
return client.select(1).then(assert, helper.isError(/The connection is already closed/))
|
||||
})
|
||||
})
|
||||
|
||||
describe('when connected', () => {
|
||||
let client
|
||||
|
||||
beforeEach((done) => {
|
||||
beforeEach(() => {
|
||||
client = redis.createClient.apply(null, args)
|
||||
client.once('ready', () => {
|
||||
client.flushdb(done)
|
||||
})
|
||||
return client.flushdb()
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
client.end(true)
|
||||
})
|
||||
|
||||
it('changes the database and calls the callback', (done) => {
|
||||
it('changes the database', () => {
|
||||
// default value of null means database 0 will be used.
|
||||
assert.strictEqual(client.selectedDb, undefined, 'default db should be undefined')
|
||||
client.select(1, (err, res) => {
|
||||
helper.isNotError()(err, res)
|
||||
return client.select(1).then((res) => {
|
||||
assert.strictEqual(client.selectedDb, 1, 'db should be 1 after select')
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
describe('and a callback is specified', () => {
|
||||
describe('with a valid db index', () => {
|
||||
it('selects the appropriate database', (done) => {
|
||||
assert.strictEqual(client.selectedDb, undefined, 'default db should be undefined')
|
||||
client.select(1, (err) => {
|
||||
assert.strictEqual(err, null)
|
||||
assert.strictEqual(client.selectedDb, 1, 'we should have selected the new valid DB')
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('with an invalid db index', () => {
|
||||
it('returns an error', (done) => {
|
||||
assert.strictEqual(client.selectedDb, undefined, 'default db should be undefined')
|
||||
client.select(9999, (err) => {
|
||||
assert.strictEqual(err.code, 'ERR')
|
||||
assert.strictEqual(err.message, 'ERR invalid DB index')
|
||||
done()
|
||||
})
|
||||
describe('with a valid db index', () => {
|
||||
it('selects the appropriate database', () => {
|
||||
assert.strictEqual(client.selectedDb, undefined, 'default db should be undefined')
|
||||
return client.select(1).then(() => {
|
||||
assert.strictEqual(client.selectedDb, 1, 'we should have selected the new valid DB')
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('and no callback is specified', () => {
|
||||
describe('with a valid db index', () => {
|
||||
it('selects the appropriate database', (done) => {
|
||||
assert.strictEqual(client.selectedDb, undefined, 'default db should be undefined')
|
||||
client.select(1)
|
||||
setTimeout(() => {
|
||||
assert.strictEqual(client.selectedDb, 1, 'we should have selected the new valid DB')
|
||||
done()
|
||||
}, 25)
|
||||
})
|
||||
})
|
||||
|
||||
describe('with an invalid db index', () => {
|
||||
it('emits an error when callback not provided', (done) => {
|
||||
assert.strictEqual(client.selectedDb, undefined, 'default db should be undefined')
|
||||
|
||||
client.on('error', (err) => {
|
||||
assert.strictEqual(err.command, 'SELECT')
|
||||
assert.strictEqual(err.message, 'ERR invalid DB index')
|
||||
done()
|
||||
})
|
||||
|
||||
client.select(9999)
|
||||
describe('with an invalid db index', () => {
|
||||
it('returns an error', () => {
|
||||
assert.strictEqual(client.selectedDb, undefined, 'default db should be undefined')
|
||||
return client.select(9999).then(assert, (err) => {
|
||||
assert.strictEqual(err.code, 'ERR')
|
||||
assert.strictEqual(err.message, 'ERR invalid DB index')
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('reconnection occurs', () => {
|
||||
describe('reconnecting', () => {
|
||||
it('selects the appropriate database after a reconnect', (done) => {
|
||||
assert.strictEqual(client.selectedDb, undefined, 'default db should be undefined')
|
||||
client.select(3)
|
||||
client.set('foo', 'bar', () => {
|
||||
client.stream.destroy()
|
||||
})
|
||||
client.set('foo', 'bar').then(() => client.stream.destroy())
|
||||
client.once('ready', () => {
|
||||
assert.strictEqual(client.selectedDb, 3)
|
||||
assert(typeof client.serverInfo.db3 === 'object')
|
||||
|
Reference in New Issue
Block a user