1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-03 04:01:40 +03:00
Files
node-redis/packages/client/lib/commands/FUNCTION_LOAD.spec.ts
Shaya Potter 4708736f3b new "transform arguments" API for better key and metadata extraction (#2733)
* Parser support with all commands

* remove "dist" from all imports for consistency

* address most of my review comments

* small tweak to multi type mapping handling

* tweak multi commands / fix addScript cases

* nits

* addressed all in person review comments

* revert addCommand/addScript changes to multi-commands

addCommand needs to be there for sendCommand like ability within a multi.

If its there, it might as well be used by createCommand() et al, to avoid repeating code.

addScript is there (even though only used once), but now made private to keep the logic for bookkeeping near each other.
2024-10-31 12:16:59 -04:00

80 lines
1.9 KiB
TypeScript

import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../test-utils';
import FUNCTION_LOAD from './FUNCTION_LOAD';
import { RedisClientType } from '../client';
import { NumberReply, RedisFunctions, RedisModules, RedisScripts, RespVersions } from '../RESP/types';
import { parseArgs } from './generic-transformers';
import { CommandParser } from '../client/parser';
export const MATH_FUNCTION = {
name: 'math',
engine: 'LUA',
code:
`#!LUA name=math
redis.register_function {
function_name = "square",
callback = function(keys, args)
local number = redis.call('GET', keys[1])
return number * number
end,
flags = { "no-writes" }
}`,
library: {
square: {
NAME: 'square',
IS_READ_ONLY: true,
NUMBER_OF_KEYS: 1,
FIRST_KEY_INDEX: 0,
parseCommand(parser: CommandParser, key: string) {
parser.pushKey(key);
},
transformReply: undefined as unknown as () => NumberReply
}
}
};
export function loadMathFunction<
M extends RedisModules,
F extends RedisFunctions,
S extends RedisScripts,
RESP extends RespVersions
>(
client: RedisClientType<M, F, S, RESP>
) {
return client.functionLoad(
MATH_FUNCTION.code,
{ REPLACE: true }
);
}
describe('FUNCTION LOAD', () => {
testUtils.isVersionGreaterThanHook([7]);
describe('transformArguments', () => {
it('simple', () => {
assert.deepEqual(
parseArgs(FUNCTION_LOAD, 'code'),
['FUNCTION', 'LOAD', 'code']
);
});
it('with REPLACE', () => {
assert.deepEqual(
parseArgs(FUNCTION_LOAD, 'code', {
REPLACE: true
}),
['FUNCTION', 'LOAD', 'REPLACE', 'code']
);
});
});
testUtils.testWithClient('client.functionLoad', async client => {
assert.equal(
await loadMathFunction(client),
MATH_FUNCTION.name
);
}, GLOBAL.SERVERS.OPEN);
});