Skip to content

Commit

Permalink
chore: run zrange tests on real redis
Browse files Browse the repository at this point in the history
Confirms that real redis returns `string[]`, and that the `string[] | number[]` mock behaviour is indeed incorrect
  • Loading branch information
stipsan committed Apr 11, 2023
1 parent f08d264 commit d51a3bd
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 218 deletions.
5 changes: 3 additions & 2 deletions test/integration/commands/scard.js
Expand Up @@ -24,8 +24,9 @@ runTwinSuite('scard', command => {
}
)

it('should return 0 if the set does not exist', () => {
return redis[command]('foo').then(length => expect(length).toBe(0))
it('should return 0 if the set does not exist', async () => {
const length = await redis[command]('foo')
expect(length).toBe(0)
})

// @TODO Rewrite test so it runs on a real Redis instance
Expand Down
316 changes: 100 additions & 216 deletions test/integration/commands/zrange.js
Expand Up @@ -5,284 +5,168 @@ describe('zrange', () => {
afterAll(() => {
redis.disconnect()
})
beforeEach(async () => {
await redis.zadd(
'foo',
1,
'first',
2,
'second',
3,
'third',
4,
'fourth',
5,
'fifth'
)
await redis.zadd('bar', 100, 'a', 100, 'b', 100, 'c', 100, 'd', 100, 'e')
})

const data = {
foo: new Map([
['first', { score: 1, value: 'first' }],
['second', { score: 2, value: 'second' }],
['third', { score: 3, value: 'third' }],
['fourth', { score: 4, value: 'fourth' }],
['fifth', { score: 5, value: 'fifth' }],
]),
}

// @TODO Rewrite test so it runs on a real Redis instance
;(process.env.IS_E2E ? it.skip : it)(
'should return first 3 items ordered by score',
() => {
const redis = new Redis({ data })

return redis
.zrange('foo', 0, 2)
.then(res => expect(res).toEqual(['first', 'second', 'third']))
}
)

// @TODO Rewrite test so it runs on a real Redis instance
;(process.env.IS_E2E ? it.skip : it)(
'should return nothing when min > max',
() => {
const redis = new Redis({ data })

return redis.zrange('foo', 2, 0).then(res => expect(res).toEqual([]))
}
)

// @TODO Rewrite test so it runs on a real Redis instance
;(process.env.IS_E2E ? it.skip : it)(
'should return nothing if the min is greater than the max, and max is negative',
() => {
const redis = new Redis({ data })
it('should return first 3 items ordered by score', () => {
return redis
.zrange('foo', 0, 2)
.then(res => expect(res).toEqual(['first', 'second', 'third']))
})

return redis.zrange('foo', 0, -8).then(res => expect(res).toEqual([]))
}
)
it('should return nothing when min > max', () => {
return redis.zrange('foo', 2, 0).then(res => expect(res).toEqual([]))
})

// @TODO Rewrite test so it runs on a real Redis instance
;(process.env.IS_E2E ? it.skip : it)('should return last 3 items', () => {
const redis = new Redis({ data })
it('should return nothing if the min is greater than the max, and max is negative', () => {
return redis.zrange('foo', 0, -8).then(res => expect(res).toEqual([]))
})

it('should return last 3 items', () => {
return redis
.zrange('foo', -3, -1)
.then(res => expect(res).toEqual(['third', 'fourth', 'fifth']))
})

// @TODO Rewrite test so it runs on a real Redis instance
;(process.env.IS_E2E ? it.skip : it)(
'should return all items on larger ranges',
() => {
const redis = new Redis({ data })

return redis
.zrange('foo', 0, 100)
.then(res =>
expect(res).toEqual(['first', 'second', 'third', 'fourth', 'fifth'])
)
}
)

// @TODO Rewrite test so it runs on a real Redis instance
;(process.env.IS_E2E ? it.skip : it)(
'should work even if the min is negative and larger than set size',
() => {
const redis = new Redis({ data })

return redis
.zrange('foo', -200, -3)
.then(res => expect(res).toEqual(['first', 'second', 'third']))
}
)
it('should return all items on larger ranges', () => {
return redis
.zrange('foo', 0, 100)
.then(res =>
expect(res).toEqual(['first', 'second', 'third', 'fourth', 'fifth'])
)
})

// @TODO Rewrite test so it runs on a real Redis instance
;(process.env.IS_E2E ? it.skip : it)(
'should return empty array if out-of-range',
() => {
const redis = new Redis({ data })
it('should work even if the min is negative and larger than set size', () => {
return redis
.zrange('foo', -200, -3)
.then(res => expect(res).toEqual(['first', 'second', 'third']))
})

return redis.zrange('foo', 10, 100).then(res => expect(res).toEqual([]))
}
)
it('should return empty array if out-of-range', () => {
return redis.zrange('foo', 10, 100).then(res => expect(res).toEqual([]))
})

it('should throw WRONGTYPE if the key contains something other than a list', async () => {
expect.assertions(1)

await redis.set('foo', 'not a list')
await redis.set('baz', 'not a list')

return redis
.zrange('foo', 0, 2)
.zrange('baz', 0, 2)
.catch(err =>
expect(err.message).toBe(
'WRONGTYPE Operation against a key holding the wrong kind of value'
)
)
})

// @TODO Rewrite test so it runs on a real Redis instance
;(process.env.IS_E2E ? it.skip : it)(
'should include scores if WITHSCORES is specified',
() => {
const redis = new Redis({ data })
return redis
.zrange('foo', 0, 2, 'WITHSCORES')
.then(res =>
expect(res).toEqual(['first', '1', 'second', '2', 'third', '3'])
)
}
)

// @TODO Rewrite test so it runs on a real Redis instance
;(process.env.IS_E2E ? it.skip : it)(
'should sort items with the same score lexicographically',
() => {
const redis = new Redis({
data: {
foo: new Map([
['aaa', { score: 5, value: 'aaa' }],
['ccc', { score: 4, value: 'ccc' }],
['ddd', { score: 4, value: 'ddd' }],
['bbb', { score: 4, value: 'bbb' }],
]),
},
})
it('should include scores if WITHSCORES is specified', () => {
return redis
.zrange('foo', 0, 2, 'WITHSCORES')
.then(res =>
expect(res).toEqual(['first', '1', 'second', '2', 'third', '3'])
)
})

return redis
.zrange('foo', 0, 100)
.then(res => expect(res).toEqual(['bbb', 'ccc', 'ddd', 'aaa']))
}
)
it('should sort items with the same score lexicographically', async () => {
await redis.zadd('foobar', 5, 'aaa', 4, 'ccc', 4, 'ddd', 4, 'bbb')

// @TODO Rewrite test so it runs on a real Redis instance
;(process.env.IS_E2E ? it.skip : it)('should handle REV', () => {
const redis = new Redis({ data })
return redis
.zrange('foobar', 0, 100)
.then(res => expect(res).toEqual(['bbb', 'ccc', 'ddd', 'aaa']))
})

it('should handle REV', () => {
return redis.zrange('foo', 0, 2, 'REV').then(res => {
expect(res).toEqual(['fifth', 'fourth', 'third'])
})
})
// @TODO Rewrite test so it runs on a real Redis instance
;(process.env.IS_E2E ? it.skip : it)('should handle REV WITHSCORES', () => {
const redis = new Redis({ data })

it('should handle REV WITHSCORES', () => {
return redis.zrange('foo', 0, 2, 'REV', 'WITHSCORES').then(res => {
expect(res).toEqual(['fifth', '5', 'fourth', '4', 'third', '3'])
})
})
// @TODO Rewrite test so it runs on a real Redis instance
;(process.env.IS_E2E ? it.skip : it)('should handle BYSCORE', () => {
const redis = new Redis({ data })

it('should handle BYSCORE', () => {
return redis
.zrange('foo', 2, '(4', 'BYSCORE')
.then(res => expect(res).toEqual(['second', 'third']))
})

// @TODO Rewrite test so it runs on a real Redis instance
;(process.env.IS_E2E ? it.skip : it)(
'should handle BYSCORE with LIMIT',
() => {
const redis = new Redis({ data })

return redis
.zrange('foo', 2, '(4', 'BYSCORE', 'LIMIT', 1, 2)
.then(res => expect(res).toEqual(['third']))
}
)

// @TODO Rewrite test so it runs on a real Redis instance
;(process.env.IS_E2E ? it.skip : it)(
'should handle BYSCORE with LIMIT WITHSCORES',
() => {
const redis = new Redis({ data })

return redis
.zrange('foo', 2, '(4', 'BYSCORE', 'LIMIT', 1, 2, 'WITHSCORES')
.then(res => expect(res).toEqual(['third', '3']))
}
)
it('should handle BYSCORE with LIMIT', () => {
return redis
.zrange('foo', 2, '(4', 'BYSCORE', 'LIMIT', 1, 2)
.then(res => expect(res).toEqual(['third']))
})

// @TODO Rewrite test so it runs on a real Redis instance
;(process.env.IS_E2E ? it.skip : it)('should handle BYSCORE REV', () => {
const redis = new Redis({ data })
it('should handle BYSCORE with LIMIT WITHSCORES', () => {
return redis
.zrange('foo', 2, '(4', 'BYSCORE', 'LIMIT', 1, 2, 'WITHSCORES')
.then(res => expect(res).toEqual(['third', '3']))
})

it('should handle BYSCORE REV', () => {
return redis
.zrange('foo', '3', '1', 'BYSCORE', 'REV')
.then(res => expect(res).toEqual(['third', 'second', 'first']))
})

// @TODO Rewrite test so it runs on a real Redis instance
;(process.env.IS_E2E ? it.skip : it)(
'should handle BYSCORE REV WITHSCORES',
() => {
const redis = new Redis({ data })

return redis
.zrange('foo', '3', '1', 'BYSCORE', 'REV', 'WITHSCORES')
.then(res =>
expect(res).toEqual(['third', '3', 'second', '2', 'first', '1'])
)
}
)

// @TODO Rewrite test so it runs on a real Redis instance
;(process.env.IS_E2E ? it.skip : it)(
'should handle BYSCORE REV with LIMIT',
() => {
const redis = new Redis({ data })

return redis
.zrange('foo', '3', '1', 'BYSCORE', 'REV', 'LIMIT', 0, 1)
.then(res => expect(res).toEqual(['third']))
}
)

// @TODO Rewrite test so it runs on a real Redis instance
;(process.env.IS_E2E ? it.skip : it)(
'should handle BYSCORE REV with LIMIT WITHSCORES',
() => {
const redis = new Redis({ data })

return redis
.zrange('foo', '3', '1', 'BYSCORE', 'REV', 'LIMIT', 0, 1, 'WITHSCORES')
.then(res => expect(res).toEqual(['third', '3']))
}
)
it('should handle BYSCORE REV WITHSCORES', () => {
return redis
.zrange('foo', '3', '1', 'BYSCORE', 'REV', 'WITHSCORES')
.then(res =>
expect(res).toEqual(['third', '3', 'second', '2', 'first', '1'])
)
})

const lexData = {
foo: new Map([
['a', { score: 100, value: 'a' }],
['b', { score: 100, value: 'b' }],
['c', { score: 100, value: 'c' }],
['d', { score: 100, value: 'd' }],
['e', { score: 100, value: 'e' }],
]),
}
it('should handle BYSCORE REV with LIMIT', () => {
return redis
.zrange('foo', '3', '1', 'BYSCORE', 'REV', 'LIMIT', 0, 1)
.then(res => expect(res).toEqual(['third']))
})

// @TODO Rewrite test so it runs on a real Redis instance
;(process.env.IS_E2E ? it.skip : it)('should handle BYLEX', () => {
const redis = new Redis({ data: lexData })
it('should handle BYSCORE REV with LIMIT WITHSCORES', () => {
return redis
.zrange('foo', '3', '1', 'BYSCORE', 'REV', 'LIMIT', 0, 1, 'WITHSCORES')
.then(res => expect(res).toEqual(['third', '3']))
})

it('should handle BYLEX', () => {
return redis
.zrange('foo', '[b', '(d', 'BYLEX')
.zrange('bar', '[b', '(d', 'BYLEX')
.then(res => expect(res).toEqual(['b', 'c']))
})

// @TODO Rewrite test so it runs on a real Redis instance
;(process.env.IS_E2E ? it.skip : it)('should handle BYLEX with LIMIT', () => {
const redis = new Redis({ data: lexData })

it('should handle BYLEX with LIMIT', () => {
return redis
.zrange('foo', '[b', '(d', 'BYLEX', 'LIMIT', 1, 2)
.zrange('bar', '[b', '(d', 'BYLEX', 'LIMIT', 1, 2)
.then(res => expect(res).toEqual(['c']))
})

// @TODO Rewrite test so it runs on a real Redis instance
;(process.env.IS_E2E ? it.skip : it)('should handle BYLEX REV', () => {
const redis = new Redis({ data: lexData })

it('should handle BYLEX REV', () => {
return redis
.zrange('foo', '[c', '[a', 'BYLEX', 'REV')
.zrange('bar', '[c', '[a', 'BYLEX', 'REV')
.then(res => expect(res).toEqual(['c', 'b', 'a']))
})

// @TODO Rewrite test so it runs on a real Redis instance
;(process.env.IS_E2E ? it.skip : it)(
'should handle BYLEX REV with LIMIT',
() => {
const redis = new Redis({ data: lexData })

return redis
.zrange('foo', '[c', '[a', 'BYLEX', 'REV', 'LIMIT', 0, 1)
.then(res => expect(res).toEqual(['c']))
}
)
it('should handle BYLEX REV with LIMIT', () => {
return redis
.zrange('bar', '[c', '[a', 'BYLEX', 'REV', 'LIMIT', 0, 1)
.then(res => expect(res).toEqual(['c']))
})
})

0 comments on commit d51a3bd

Please sign in to comment.