|
| 1 | +import { strict as assert } from 'assert'; |
| 2 | +import testUtils, { GLOBAL } from '../test-utils'; |
| 3 | +import { promisify } from 'util'; |
| 4 | +import { RedisLegacyClientType } from './legacy-mode'; |
| 5 | +import { ErrorReply } from '../errors'; |
| 6 | +import { RedisClientType } from '.'; |
| 7 | +import { once } from 'events'; |
| 8 | + |
| 9 | +function testWithLegacyClient(title: string, fn: (legacy: RedisLegacyClientType, client: RedisClientType) => Promise<unknown>) { |
| 10 | + testUtils.testWithClient(title, client => fn(client.legacy(), client), GLOBAL.SERVERS.OPEN); |
| 11 | +} |
| 12 | + |
| 13 | +describe.only('Legacy Mode', () => { |
| 14 | + describe('client.sendCommand', () => { |
| 15 | + testWithLegacyClient('resolve', async client => { |
| 16 | + assert.equal( |
| 17 | + await promisify(client.sendCommand).call(client, 'PING'), |
| 18 | + 'PONG' |
| 19 | + ); |
| 20 | + }); |
| 21 | + |
| 22 | + testWithLegacyClient('reject', async client => { |
| 23 | + await assert.rejects( |
| 24 | + promisify(client.sendCommand).call(client, 'ERROR'), |
| 25 | + ErrorReply |
| 26 | + ); |
| 27 | + }); |
| 28 | + |
| 29 | + testWithLegacyClient('reject without a callback', async (legacy, client) => { |
| 30 | + legacy.sendCommand('ERROR'); |
| 31 | + const [err] = await once(client, 'error'); |
| 32 | + assert.ok(err instanceof ErrorReply); |
| 33 | + }); |
| 34 | + }); |
| 35 | + |
| 36 | + describe('hGetAll (TRANSFORM_LEGACY_REPLY)', () => { |
| 37 | + testWithLegacyClient('resolve', async client => { |
| 38 | + await promisify(client.hSet).call(client, 'key', 'field', 'value'); |
| 39 | + assert.deepEqual( |
| 40 | + await promisify(client.hGetAll).call(client, 'key'), |
| 41 | + Object.create(null, { |
| 42 | + field: { |
| 43 | + value: 'value', |
| 44 | + configurable: true, |
| 45 | + enumerable: true |
| 46 | + } |
| 47 | + }) |
| 48 | + ); |
| 49 | + }); |
| 50 | + |
| 51 | + testWithLegacyClient('reject', async client => { |
| 52 | + await assert.rejects( |
| 53 | + promisify(client.hGetAll).call(client), |
| 54 | + ErrorReply |
| 55 | + ); |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + describe('client.set', () => { |
| 60 | + testWithLegacyClient('vardict', async client => { |
| 61 | + assert.equal( |
| 62 | + await promisify(client.set).call(client, 'a', 'b'), |
| 63 | + 'OK' |
| 64 | + ); |
| 65 | + }); |
| 66 | + |
| 67 | + testWithLegacyClient('array', async client => { |
| 68 | + assert.equal( |
| 69 | + await promisify(client.set).call(client, ['a', 'b']), |
| 70 | + 'OK' |
| 71 | + ); |
| 72 | + }); |
| 73 | + |
| 74 | + testWithLegacyClient('vardict & arrays', async client => { |
| 75 | + assert.equal( |
| 76 | + await promisify(client.set).call(client, ['a'], 'b', ['EX', 1]), |
| 77 | + 'OK' |
| 78 | + ); |
| 79 | + }); |
| 80 | + |
| 81 | + testWithLegacyClient('reject without a callback', async (legacy, client) => { |
| 82 | + legacy.set('ERROR'); |
| 83 | + const [err] = await once(client, 'error'); |
| 84 | + assert.ok(err instanceof ErrorReply); |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + describe('client.multi', () => { |
| 89 | + testWithLegacyClient('resolve', async client => { |
| 90 | + const multi = client.multi().ping().sendCommand('PING'); |
| 91 | + assert.deepEqual( |
| 92 | + await promisify(multi.exec).call(multi), |
| 93 | + ['PONG', 'PONG'] |
| 94 | + ); |
| 95 | + }); |
| 96 | + |
| 97 | + testWithLegacyClient('reject', async client => { |
| 98 | + const multi = client.multi().sendCommand('ERROR'); |
| 99 | + await assert.rejects( |
| 100 | + promisify(multi.exec).call(multi), |
| 101 | + ErrorReply |
| 102 | + ); |
| 103 | + }); |
| 104 | + |
| 105 | + testWithLegacyClient('reject without a callback', async (legacy, client) => { |
| 106 | + legacy.multi().sendCommand('ERROR').exec(); |
| 107 | + const [err] = await once(client, 'error'); |
| 108 | + assert.ok(err instanceof ErrorReply); |
| 109 | + }); |
| 110 | + }); |
| 111 | +}); |
0 commit comments