1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-17 19:41:06 +03:00

add tests

This commit is contained in:
leibale
2021-06-11 16:37:04 -04:00
parent 10e9cbc005
commit 71242304bc
19 changed files with 270 additions and 19 deletions

View File

@@ -14,7 +14,7 @@ export interface RedisClientOptions<M = RedisModules, S = RedisLuaScripts> {
modules?: M;
scripts?: S;
commandsQueueMaxLength?: number;
readOnly?: boolean;
readonly?: boolean;
legacyMode?: boolean;
}
@@ -102,8 +102,8 @@ export default class RedisClient<M extends RedisModules = RedisModules, S extend
promises.push((this as any).select(RedisClient.commandOptions({ asap: true }), this.#selectedDB));
}
if (this.#options?.readOnly) {
promises.push((this as any).readOnly(RedisClient.commandOptions({ asap: true })));
if (this.#options?.readonly) {
promises.push((this as any).readonly(RedisClient.commandOptions({ asap: true })));
}
if (this.#options?.socket?.password) {

View File

@@ -91,7 +91,7 @@ export default class RedisClusterSlots {
}
}
#initiateClientForNode(node: RedisClusterMasterNode | RedisClusterReplicaNode, readOnly: boolean, clientsInUse: Set<string>, promises: Array<Promise<void>>): RedisClient {
#initiateClientForNode(node: RedisClusterMasterNode | RedisClusterReplicaNode, readonly: boolean, clientsInUse: Set<string>, promises: Array<Promise<void>>): RedisClient {
clientsInUse.add(node.url);
let client = this.#clientByKey.get(node.url);
@@ -101,7 +101,7 @@ export default class RedisClusterSlots {
host: node.host,
port: node.port
},
readOnly
readonly
});
promises.push(client.connect());
this.#clientByKey.set(node.url, client);
@@ -158,13 +158,13 @@ export default class RedisClusterSlots {
return value;
}
getClient(firstKey?: string, isReadOnly?: boolean): RedisClient {
getClient(firstKey?: string, isReadonly?: boolean): RedisClient {
if (!firstKey) {
return this.#getRandomClient();
}
const slot = calculateSlot(firstKey);
if (!isReadOnly || !this.#options.useReplicas) {
if (!isReadonly || !this.#options.useReplicas) {
return this.#getSlotMaster(slot);
}

View File

@@ -51,9 +51,9 @@ export default class RedisCluster {
return this.#slots.connect();
}
async sendCommand<T = unknown>(args: Array<string>, firstKeyIndex?: number, isReadOnly?: boolean, redirections: number = 0): Promise<T> {
async sendCommand<T = unknown>(args: Array<string>, firstKeyIndex?: number, isReadonly?: boolean, redirections: number = 0): Promise<T> {
const firstKey = firstKeyIndex ? args[firstKeyIndex] : undefined,
client = this.#slots.getClient(firstKey, isReadOnly);
client = this.#slots.getClient(firstKey, isReadonly);
try {
return await client.sendCommand(args);
@@ -64,7 +64,7 @@ export default class RedisCluster {
await this.#slots.discover();
if (redirections < (this.#options.maxCommandRedirections ?? 16)) {
return this.sendCommand(args, firstKeyIndex, isReadOnly, redirections + 1);
return this.sendCommand(args, firstKeyIndex, isReadonly, redirections + 1);
}
}

28
lib/commands/HDEL.spec.ts Normal file
View File

@@ -0,0 +1,28 @@
import { strict as assert } from 'assert';
import { TestRedisServers, itWithClient } from '../test-utils';
import { transformArguments } from './HDEL';
describe('HDEL', () => {
describe('transformArguments', () => {
it('string', () => {
assert.deepEqual(
transformArguments('key', 'field'),
['HDEL', 'key', 'field']
);
});
it('array', () => {
assert.deepEqual(
transformArguments('key', ['1', '2']),
['HDEL', 'key', '1', '2']
);
});
});
itWithClient(TestRedisServers.OPEN, 'client.hDel', async client => {
assert.equal(
await client.hDel('key', 'field'),
0
);
});
});

View File

@@ -2,12 +2,16 @@ import { transformReplyNumber } from './generic-transformers';
export const FIRST_KEY_INDEX = 1;
export function transformArguments(key: string, ...fields: Array<string>): Array<string> {
return [
'HDEL',
key,
...fields
];
export function transformArguments(key: string, field: string | Array<string>): Array<string> {
const args = ['HDEL', key];
if (typeof field === 'string') {
args.push(field);
} else {
args.push(...field);
}
return args;
}
export const transformReply = transformReplyNumber;

View File

@@ -0,0 +1,19 @@
import { strict as assert } from 'assert';
import { TestRedisServers, itWithClient } from '../test-utils';
import { transformArguments } from './HEXISTS';
describe('HEXISTS', () => {
it('transformArguments', () => {
assert.deepEqual(
transformArguments('key', 'field'),
['HEXISTS', 'key', 'field']
);
});
itWithClient(TestRedisServers.OPEN, 'client.hExists', async client => {
assert.equal(
await client.hExists('key', 'field'),
false
);
});
});

19
lib/commands/HGET.spec.ts Normal file
View File

@@ -0,0 +1,19 @@
import { strict as assert } from 'assert';
import { TestRedisServers, itWithClient } from '../test-utils';
import { transformArguments } from './HGET';
describe('HGET', () => {
it('transformArguments', () => {
assert.deepEqual(
transformArguments('key', 'field'),
['HGET', 'key', 'field']
);
});
itWithClient(TestRedisServers.OPEN, 'client.hGet', async client => {
assert.equal(
await client.hGet('key', 'field'),
null
);
});
});

View File

@@ -1,4 +1,5 @@
import { strict as assert } from 'assert';
import { TestRedisServers, itWithClient } from '../test-utils';
import { transformReply } from './HGETALL';
describe('HGETALL', () => {
@@ -30,4 +31,11 @@ describe('HGETALL', () => {
);
});
});
itWithClient(TestRedisServers.OPEN, 'client.hGetAll', async client => {
assert.deepEqual(
await client.hGetAll('key'),
Object.create(null)
);
});
});

View File

@@ -0,0 +1,19 @@
import { strict as assert } from 'assert';
import { TestRedisServers, itWithClient } from '../test-utils';
import { transformArguments } from './HINCRBY';
describe('HINCRBY', () => {
it('transformArguments', () => {
assert.deepEqual(
transformArguments('key', 'field', 1),
['HINCRBY', 'key', 'field', '1']
);
});
itWithClient(TestRedisServers.OPEN, 'client.hIncrBy', async client => {
assert.equal(
await client.hIncrBy('key', 'field', 1),
1
);
});
});

View File

@@ -0,0 +1,19 @@
import { strict as assert } from 'assert';
import { TestRedisServers, itWithClient } from '../test-utils';
import { transformArguments } from './HINCRBYFLOAT';
describe('HINCRBYFLOAT', () => {
it('transformArguments', () => {
assert.deepEqual(
transformArguments('key', 'field', 1.5),
['HINCRBYFLOAT', 'key', 'field', '1.5']
);
});
itWithClient(TestRedisServers.OPEN, 'client.hIncrByFloat', async client => {
assert.equal(
await client.hIncrByFloat('key', 'field', 1.5),
'1.5'
);
});
});

View File

@@ -0,0 +1,19 @@
import { strict as assert } from 'assert';
import { TestRedisServers, itWithClient } from '../test-utils';
import { transformArguments } from './HKEYS';
describe('HKEYS', () => {
it('transformArguments', () => {
assert.deepEqual(
transformArguments('key'),
['HKEYS', 'key']
);
});
itWithClient(TestRedisServers.OPEN, 'client.hKeys', async client => {
assert.deepEqual(
await client.hKeys('key'),
[]
);
});
});

19
lib/commands/HLEN.spec.ts Normal file
View File

@@ -0,0 +1,19 @@
import { strict as assert } from 'assert';
import { TestRedisServers, itWithClient } from '../test-utils';
import { transformArguments } from './HLEN';
describe('HLEN', () => {
it('transformArguments', () => {
assert.deepEqual(
transformArguments('key'),
['HLEN', 'key']
);
});
itWithClient(TestRedisServers.OPEN, 'client.hLen', async client => {
assert.equal(
await client.hLen('key'),
0
);
});
});

19
lib/commands/INCR.spec.ts Normal file
View File

@@ -0,0 +1,19 @@
import { strict as assert } from 'assert';
import { TestRedisServers, itWithClient } from '../test-utils';
import { transformArguments } from './INCR';
describe('INCR', () => {
it('transformArguments', () => {
assert.deepEqual(
transformArguments('key'),
['INCR', 'key']
);
});
itWithClient(TestRedisServers.OPEN, 'client.incr', async client => {
assert.equal(
await client.incr('key'),
1
);
});
});

View File

@@ -0,0 +1,19 @@
import { strict as assert } from 'assert';
import { TestRedisServers, itWithClient } from '../test-utils';
import { transformArguments } from './INCRBY';
describe('INCR', () => {
it('transformArguments', () => {
assert.deepEqual(
transformArguments('key', 1),
['INCRBY', 'key', '1']
);
});
itWithClient(TestRedisServers.OPEN, 'client.incrBy', async client => {
assert.equal(
await client.incrBy('key', 1),
1
);
});
});

View File

@@ -0,0 +1,19 @@
import { strict as assert } from 'assert';
import { TestRedisServers, itWithClient } from '../test-utils';
import { transformArguments } from './INCRBYFLOAT';
describe('INCRBYFLOAT', () => {
it('transformArguments', () => {
assert.deepEqual(
transformArguments('key', 1.5),
['INCRBYFLOAT', 'key', '1.5']
);
});
itWithClient(TestRedisServers.OPEN, 'client.incrByFloat', async client => {
assert.equal(
await client.incrByFloat('key', 1.5),
'1.5'
);
});
});

View File

@@ -1,4 +1,4 @@
import { transformReplyNumber } from './generic-transformers';
import { transformReplyString } from './generic-transformers';
export const FIRST_KEY_INDEX = 1;
@@ -6,4 +6,4 @@ export function transformArguments(key: string, increment: number): Array<string
return ['INCRBYFLOAT', key, increment.toString()];
}
export const transformReply = transformReplyNumber;
export const transformReply = transformReplyString;

View File

@@ -0,0 +1,28 @@
import { strict as assert } from 'assert';
import { TestRedisServers, itWithClient } from '../test-utils';
import { transformArguments } from './LPUSH';
describe('LPUSH', () => {
describe('transformArguments', () => {
it('string', () => {
assert.deepEqual(
transformArguments('key', 'field'),
['LPUSH', 'key', 'field']
);
});
it('array', () => {
assert.deepEqual(
transformArguments('key', ['1', '2']),
['LPUSH', 'key', '1', '2']
);
});
});
itWithClient(TestRedisServers.OPEN, 'client.lPush', async client => {
assert.equal(
await client.lPush('key', 'field'),
1
);
});
});

View File

@@ -0,0 +1,12 @@
import { strict as assert } from 'assert';
import { TestRedisServers, itWithClient } from '../test-utils';
import { transformArguments } from './READONLY';
describe('READONLY', () => {
it('transformArguments', () => {
assert.deepEqual(
transformArguments(),
['READONLY']
);
});
});

View File

@@ -166,7 +166,7 @@ export default {
RANDOMKEY,
randomKey: RANDOMKEY,
READONLY,
readOnly: READONLY,
readonly: READONLY,
RENAME,
rename: RENAME,
RENAMENX,