Skip to content

Commit

Permalink
feat: add stubs for replicaof
Browse files Browse the repository at this point in the history
  • Loading branch information
stipsan committed Jan 31, 2022
1 parent ba8d481 commit c84d199
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 67 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# ioredis-mock · [![npm](https://img.shields.io/npm/dm/ioredis-mock.svg?style=flat-square)](https://npm-stat.com/charts.html?package=ioredis-mock) [![npm version](https://img.shields.io/npm/v/ioredis-mock.svg?style=flat-square)](https://www.npmjs.com/package/ioredis-mock) [![Redis Compatibility: 52%](https://img.shields.io/badge/redis-52%25-red.svg?style=flat-square)](compat.md) [![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg?style=flat-square)](https://github.com/semantic-release/semantic-release)
# ioredis-mock · [![npm](https://img.shields.io/npm/dm/ioredis-mock.svg?style=flat-square)](https://npm-stat.com/charts.html?package=ioredis-mock) [![npm version](https://img.shields.io/npm/v/ioredis-mock.svg?style=flat-square)](https://www.npmjs.com/package/ioredis-mock) [![Redis Compatibility: 53%](https://img.shields.io/badge/redis-53%25-red.svg?style=flat-square)](compat.md) [![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg?style=flat-square)](https://github.com/semantic-release/semantic-release)

This library emulates [ioredis](https://github.com/luin/ioredis) by performing
all operations in-memory. The best way to do integration testing against redis
Expand Down
6 changes: 3 additions & 3 deletions compat.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## Supported commands ![Commands Coverage: 52%](https://img.shields.io/badge/coverage-52%25-red.svg)
## Supported commands ![Commands Coverage: 53%](https://img.shields.io/badge/coverage-53%25-red.svg)

| redis | ioredis | ioredis-mock |
| ---------------------- | :----------------: | :----------------: |
Expand Down Expand Up @@ -117,7 +117,7 @@
| [rename] | :white_check_mark: | :white_check_mark: |
| [renamenx] | :white_check_mark: | :white_check_mark: |
| [replconf] | :white_check_mark: | :white_check_mark: |
| [replicaof] | :white_check_mark: | :x: |
| [replicaof] | :white_check_mark: | :white_check_mark: |
| [reset] | :white_check_mark: | :x: |
| [role] | :white_check_mark: | :white_check_mark: |
| [rpop] | :white_check_mark: | :white_check_mark: |
Expand All @@ -141,7 +141,7 @@
| [sinter] | :white_check_mark: | :white_check_mark: |
| [sinterstore] | :white_check_mark: | :white_check_mark: |
| [sismember] | :white_check_mark: | :white_check_mark: |
| [slaveof] | :white_check_mark: | :x: |
| [slaveof] | :white_check_mark: | :white_check_mark: |
| [smembers] | :white_check_mark: | :white_check_mark: |
| [smismember] | :white_check_mark: | :white_check_mark: |
| [smove] | :white_check_mark: | :white_check_mark: |
Expand Down
1 change: 1 addition & 0 deletions src/commands/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export * from './readwrite'
export * from './rename'
export * from './renamenx'
export * from './replconf'
export * from './replicaof'
export * from './role'
export * from './rpop'
export * from './rpoplpush'
Expand Down
20 changes: 20 additions & 0 deletions src/commands/replicaof.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export function replicaof(host, port) {
if (!host || !port) {
throw new Error("ERR wrong number of arguments for 'replicaof' command")
}

if (host.toUpperCase() === 'NO' && port.toUpperCase() === 'ONE') {
return 'OK'
}

throw new Error('ERR Unsupported operation, PRs welcome ;-)')
}

export function replicaofBuffer(...args) {
const val = replicaof.apply(this, args)
return val ? Buffer.from(val) : val
}

// As of Redis version 5 the Redis project doesn't use the word slave, but it's included for backwards compatibility
export const slaveof = replicaof
export const slaveofBuffer = replicaofBuffer
63 changes: 36 additions & 27 deletions test/integration/commands/getrange.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,43 @@ import Redis from 'ioredis'
// eslint-disable-next-line import/no-relative-parent-imports
import { runTwinSuite } from '../../../test-utils'

const suite = (command, equals) => {
const redis = new Redis()

afterAll(() => {
redis.disconnect()
})

it('should return "This"', async () => {
await redis.set('foo', 'This is a string')
expect(equals(await redis[command]('foo', 0, 3), 'This')).toBe(true)
})

it('should return "ing"', async () => {
await redis.set('foo', 'This is a string')
expect(equals(await redis[command]('foo', -3, -1), 'ing')).toBe(true)
})

it('should return "This is a string"', async () => {
await redis.set('foo', 'This is a string')
expect(equals(await redis[command]('foo', 0, -1), 'This is a string')).toBe(
true
)
})

it('should return "string"', async () => {
await redis.set('foo', 'This is a string')
expect(equals(await redis[command]('foo', 10, 100), 'string')).toBe(true)
})
}

runTwinSuite('getrange', (command, equals) => {
describe(command, () => {
const redis = new Redis()

afterAll(() => {
redis.disconnect()
})

it('should return "This"', async () => {
await redis.set('foo', 'This is a string')
expect(equals(await redis[command]('foo', 0, 3), 'This')).toBe(true)
})

it('should return "ing"', async () => {
await redis.set('foo', 'This is a string')
expect(equals(await redis[command]('foo', -3, -1), 'ing')).toBe(true)
})

it('should return "This is a string"', async () => {
await redis.set('foo', 'This is a string')
expect(
equals(await redis[command]('foo', 0, -1), 'This is a string')
).toBe(true)
})

it('should return "string"', async () => {
await redis.set('foo', 'This is a string')
expect(equals(await redis[command]('foo', 10, 100), 'string')).toBe(true)
})
suite(command, equals)
})
})
runTwinSuite('substr', (command, equals) => {
describe(command, () => {
suite(command, equals)
})
})
54 changes: 54 additions & 0 deletions test/integration/commands/replicaof.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import Redis from 'ioredis'

// eslint-disable-next-line import/no-relative-parent-imports
import { runTwinSuite } from '../../../test-utils'

const suite = (command, equals) => {
const redis = new Redis()

afterAll(() => {
redis.disconnect()
})

it('should throw if incorrect number of arguments', async () => {
expect.assertions(2)
try {
await redis[command]()
} catch (err) {
expect(err.message).toMatch('ERR wrong number of arguments')
}

try {
await redis[command]('NO')
} catch (err) {
expect(err.message).toMatch('ERR wrong number of arguments')
}
})

it('should return "OK" when setting to NO ONE', async () => {
expect(equals(await redis[command]('no', 'ONE'), 'OK')).toBe(true)
expect(equals(await redis[command]('NO', 'one'), 'OK')).toBe(true)
})
;(process.env.IS_E2E ? it.skip : it)(
'should throw an error about unsupported functionality',
async () => {
expect.hasAssertions()
try {
await redis[command]('127.0.0.1', 6799)
} catch (err) {
expect(err.message).toMatch('ERR Unsupported operation')
}
}
)
}

runTwinSuite('replicaof', (command, equals) => {
describe(command, () => {
suite(command, equals)
})
})
runTwinSuite('slaveof', (command, equals) => {
describe(command, () => {
suite(command, equals)
})
})
36 changes: 0 additions & 36 deletions test/integration/commands/substr.js

This file was deleted.

0 comments on commit c84d199

Please sign in to comment.