1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-17 19:41:06 +03:00
Files
node-redis/test/commands/sort.spec.js
Ruben Bridgewater b2613b2270 test fixup
2017-05-06 08:16:19 +02:00

128 lines
3.8 KiB
JavaScript

'use strict'
const assert = require('assert')
const config = require('../lib/config')
const helper = require('../helper')
const redis = config.redis
function setupData (client, done) {
client.rpush('y', 'd')
client.rpush('y', 'b')
client.rpush('y', 'a')
client.rpush('y', 'c')
client.rpush('x', '3')
client.rpush('x', '9')
client.rpush('x', '2')
client.rpush('x', '4')
client.set('w3', '4')
client.set('w9', '5')
client.set('w2', '12')
client.set('w4', '6')
client.set('o2', 'buz')
client.set('o3', 'foo')
client.set('o4', 'baz')
client.set('o9', 'bar')
client.set('p2', 'qux')
client.set('p3', 'bux')
client.set('p4', 'lux')
client.set('p9', 'tux', done)
}
describe('The \'sort\' method', () => {
helper.allTests((ip, args) => {
describe(`using ${ip}`, () => {
let client
beforeEach((done) => {
client = redis.createClient.apply(null, args)
client.once('error', done)
client.once('connect', () => {
client.flushdb()
setupData(client, done)
})
})
describe('alphabetical', () => {
it('sorts in ascending alphabetical order', (done) => {
client.sort('y', 'asc', 'alpha', (err, sorted) => {
assert.deepEqual(sorted, ['a', 'b', 'c', 'd'])
return done(err)
})
})
it('sorts in descending alphabetical order', (done) => {
client.sort('y', 'desc', 'alpha', (err, sorted) => {
assert.deepEqual(sorted, ['d', 'c', 'b', 'a'])
return done(err)
})
})
})
describe('numeric', () => {
it('sorts in ascending numeric order', (done) => {
client.sort('x', 'asc', (err, sorted) => {
assert.deepEqual(sorted, [2, 3, 4, 9])
return done(err)
})
})
it('sorts in descending numeric order', (done) => {
client.sort('x', 'desc', (err, sorted) => {
assert.deepEqual(sorted, [9, 4, 3, 2])
return done(err)
})
})
})
describe('pattern', () => {
it('handles sorting with a pattern', (done) => {
client.sort('x', 'by', 'w*', 'asc', (err, sorted) => {
assert.deepEqual(sorted, [3, 9, 4, 2])
return done(err)
})
})
it('handles sorting with a \'by\' pattern and 1 \'get\' pattern', (done) => {
client.sort('x', 'by', 'w*', 'asc', 'get', 'o*', (err, sorted) => {
assert.deepEqual(sorted, ['foo', 'bar', 'baz', 'buz'])
return done(err)
})
})
it('handles sorting with a \'by\' pattern and 2 \'get\' patterns', (done) => {
client.sort('x', 'by', 'w*', 'asc', 'get', 'o*', 'get', 'p*', (err, sorted) => {
assert.deepEqual(sorted, ['foo', 'bux', 'bar', 'tux', 'baz', 'lux', 'buz', 'qux'])
return done(err)
})
})
it('handles sorting with a \'by\' pattern and 2 \'get\' patterns with the array syntax', (done) => {
client.sort(['x', 'by', 'w*', 'asc', 'get', 'o*', 'get', 'p*'], (err, sorted) => {
assert.deepEqual(sorted, ['foo', 'bux', 'bar', 'tux', 'baz', 'lux', 'buz', 'qux'])
return done(err)
})
})
it('sorting with a \'by\' pattern and 2 \'get\' patterns and stores results', (done) => {
client.sort('x', 'by', 'w*', 'asc', 'get', 'o*', 'get', 'p*', 'store', 'bacon', (err) => {
if (err) return done(err)
})
client.lrange('bacon', 0, -1, (err, values) => {
assert.deepEqual(values, ['foo', 'bux', 'bar', 'tux', 'baz', 'lux', 'buz', 'qux'])
return done(err)
})
})
})
afterEach(() => {
client.end(true)
})
})
})
})