Skip to content

Commit

Permalink
fix(commands): add lmoveBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
stipsan committed Jul 14, 2023
1 parent 85726d0 commit 9cb31eb
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 23 deletions.
7 changes: 7 additions & 0 deletions src/commands/lmove.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { convertStringToBuffer } from '../commands-utils/convertStringToBuffer'

export function lmove(listKey1, listKey2, position1, position2) {
if (this.data.has(listKey1) && !(this.data.get(listKey1) instanceof Array)) {
throw new Error(
Expand Down Expand Up @@ -48,3 +50,8 @@ export function lmove(listKey1, listKey2, position1, position2) {

return value
}

export function lmoveBuffer(...args) {
const val = lmove.apply(this, args)
return convertStringToBuffer(val)
}
72 changes: 49 additions & 23 deletions test/integration/commands/lmove.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ runTwinSuite('lmove', command => {
})

it('should move the value from LEFT of list1 to LEFT of list2', async () => {
const result = await redis.lmove(listId1, listId2, 'LEFT', 'LEFT')
expect(result).toEqual('one')
const result = await redis[command](listId1, listId2, 'LEFT', 'LEFT')
expect(Buffer.isBuffer(result) ? result.toString() : result).toEqual(
'one'
)

const current1 = await redis.lrange(listId1, 0, -1)
const current2 = await redis.lrange(listId2, 0, -1)
Expand All @@ -45,8 +47,10 @@ runTwinSuite('lmove', command => {
})

it('should move the value from RIGHT of list1 to LEFT of list2', async () => {
const result = await redis.lmove(listId1, listId2, 'RIGHT', 'LEFT')
expect(result).toEqual('two')
const result = await redis[command](listId1, listId2, 'RIGHT', 'LEFT')
expect(Buffer.isBuffer(result) ? result.toString() : result).toEqual(
'two'
)

const current1 = await redis.lrange(listId1, 0, -1)
const current2 = await redis.lrange(listId2, 0, -1)
Expand All @@ -55,8 +59,10 @@ runTwinSuite('lmove', command => {
})

it('should move the value from LEFT of list1 to RIGHT of list2', async () => {
const result = await redis.lmove(listId1, listId2, 'LEFT', 'RIGHT')
expect(result).toEqual('one')
const result = await redis[command](listId1, listId2, 'LEFT', 'RIGHT')
expect(Buffer.isBuffer(result) ? result.toString() : result).toEqual(
'one'
)

const current1 = await redis.lrange(listId1, 0, -1)
const current2 = await redis.lrange(listId2, 0, -1)
Expand All @@ -65,8 +71,10 @@ runTwinSuite('lmove', command => {
})

it('should move the value from RIGHT of list1 to RIGHT of list2', async () => {
const result = await redis.lmove(listId1, listId2, 'RIGHT', 'RIGHT')
expect(result).toEqual('two')
const result = await redis[command](listId1, listId2, 'RIGHT', 'RIGHT')
expect(Buffer.isBuffer(result) ? result.toString() : result).toEqual(
'two'
)

const current1 = await redis.lrange(listId1, 0, -1)
const current2 = await redis.lrange(listId2, 0, -1)
Expand All @@ -75,8 +83,10 @@ runTwinSuite('lmove', command => {
})

it('should rotate the list if the source and destination are the same', async () => {
const result = await redis.lmove(listId2, listId2, 'LEFT', 'RIGHT')
expect(result).toEqual('three')
const result = await redis[command](listId2, listId2, 'LEFT', 'RIGHT')
expect(Buffer.isBuffer(result) ? result.toString() : result).toEqual(
'three'
)

const current1 = await redis.lrange(listId1, 0, -1)
const current2 = await redis.lrange(listId2, 0, -1)
Expand All @@ -85,46 +95,62 @@ runTwinSuite('lmove', command => {
})

it('should perform no operation if the source is an empty list', async () => {
const result = await redis.lmove(emptyList, listId2, 'LEFT', 'LEFT')
const result = await redis[command](emptyList, listId2, 'LEFT', 'LEFT')
expect(result).toEqual(null)

const current1 = await redis.lrange(listId1, 0, -1)
const current2 = await redis.lrange(listId2, 0, -1)
expect(current1).toEqual(['one', 'two'])
expect(current2).toEqual(['three', 'four'])
expect(
current1.map(v => (Buffer.isBuffer(v) ? v.toString() : v))
).toEqual(['one', 'two'])
expect(
current2.map(v => (Buffer.isBuffer(v) ? v.toString() : v))
).toEqual(['three', 'four'])
})

it('should perform no operation if the source and destination are the same and both positions are LEFT', async () => {
const result = await redis.lmove(listId2, listId2, 'LEFT', 'LEFT')
expect(result).toEqual('three')
const result = await redis[command](listId2, listId2, 'LEFT', 'LEFT')
expect(Buffer.isBuffer(result) ? result.toString() : result).toEqual(
'three'
)

const current1 = await redis.lrange(listId1, 0, -1)
const current2 = await redis.lrange(listId2, 0, -1)
expect(current1).toEqual(['one', 'two'])
expect(current2).toEqual(['three', 'four'])
expect(
current1.map(v => (Buffer.isBuffer(v) ? v.toString() : v))
).toEqual(['one', 'two'])
expect(
current2.map(v => (Buffer.isBuffer(v) ? v.toString() : v))
).toEqual(['three', 'four'])
})

it('should perform no operation if the source and destination are the same and both positions are RIGHT', async () => {
const result = await redis.lmove(listId2, listId2, 'RIGHT', 'RIGHT')
expect(result).toEqual('four')
const result = await redis[command](listId2, listId2, 'RIGHT', 'RIGHT')
expect(Buffer.isBuffer(result) ? result.toString() : result).toEqual(
'four'
)

const current1 = await redis.lrange(listId1, 0, -1)
const current2 = await redis.lrange(listId2, 0, -1)
expect(current1).toEqual(['one', 'two'])
expect(current2).toEqual(['three', 'four'])
expect(
current1.map(v => (Buffer.isBuffer(v) ? v.toString() : v))
).toEqual(['one', 'two'])
expect(
current2.map(v => (Buffer.isBuffer(v) ? v.toString() : v))
).toEqual(['three', 'four'])
})

it('should perform no operation and return nil when source does not exist', async () => {
const value = await redis.get('unknown')
expect(value).toEqual(null) // Ensures nil is being represented by null

const result = await redis.lmove('unknown', listId2, 'LEFT', 'LEFT')
const result = await redis[command]('unknown', listId2, 'LEFT', 'LEFT')
expect(result).toEqual(null)
})

it('should error if the value is not a list', async () => {
expect(async () => {
await redis.lmove(notalist, listId2, 'LEFT', 'LEFT')
await redis[command](notalist, listId2, 'LEFT', 'LEFT')
}).rejects.toThrow(
'WRONGTYPE Operation against a key holding the wrong kind of value'
)
Expand Down

0 comments on commit 9cb31eb

Please sign in to comment.