|
| 1 | +import { test } from 'node:test'; |
| 2 | +import * as Assert from 'node:assert'; |
| 3 | +import { cmdCli } from '../common'; |
| 4 | + |
| 5 | +const TEST_KEY_PREFIX = 'test_zrange_'; |
| 6 | + |
| 7 | +test('Command For zRangeWithScores', async (t) => { |
| 8 | + |
| 9 | + await cmdCli.del([...await cmdCli.keys(`${TEST_KEY_PREFIX}*`), 'any']); |
| 10 | + |
| 11 | + await t.test('Add multiple members to sorted set', async () => { |
| 12 | + |
| 13 | + Assert.strictEqual(await cmdCli.zAdd(`${TEST_KEY_PREFIX}myset`, 1, 'one'), true); |
| 14 | + Assert.strictEqual(await cmdCli.zAdd(`${TEST_KEY_PREFIX}myset`, 2, 'two'), true); |
| 15 | + Assert.strictEqual(await cmdCli.zAdd(`${TEST_KEY_PREFIX}myset`, 3, 'three'), true); |
| 16 | + Assert.strictEqual(await cmdCli.zAdd(`${TEST_KEY_PREFIX}myset`, 4, 'four'), true); |
| 17 | + Assert.strictEqual(await cmdCli.zAdd(`${TEST_KEY_PREFIX}myset`, 5, 'five'), true); |
| 18 | + |
| 19 | + }); |
| 20 | + |
| 21 | + await t.test('Get range by index with scores', async () => { |
| 22 | + |
| 23 | + const result = await cmdCli.zRangeWithScores(`${TEST_KEY_PREFIX}myset`, 0, 2); |
| 24 | + |
| 25 | + Assert.deepEqual(result, [ |
| 26 | + { 'member': 'one', 'score': 1 }, |
| 27 | + { 'member': 'two', 'score': 2 }, |
| 28 | + { 'member': 'three', 'score': 3 } |
| 29 | + ]); |
| 30 | + |
| 31 | + }); |
| 32 | + |
| 33 | + await t.test('Get range by index with scores in reverse order', async () => { |
| 34 | + |
| 35 | + const result = await cmdCli.zRangeWithScores(`${TEST_KEY_PREFIX}myset`, 0, 2, { 'rev': true }); |
| 36 | + |
| 37 | + Assert.deepEqual(result, [ |
| 38 | + { 'member': 'five', 'score': 5 }, |
| 39 | + { 'member': 'four', 'score': 4 }, |
| 40 | + { 'member': 'three', 'score': 3 } |
| 41 | + ]); |
| 42 | + |
| 43 | + }); |
| 44 | + |
| 45 | + await t.test('Get range by score', async () => { |
| 46 | + |
| 47 | + const result = await cmdCli.zRangeWithScores(`${TEST_KEY_PREFIX}myset`, 2, 4, { 'by': 'SCORE' }); |
| 48 | + |
| 49 | + Assert.deepEqual(result, [ |
| 50 | + { 'member': 'two', 'score': 2 }, |
| 51 | + { 'member': 'three', 'score': 3 }, |
| 52 | + { 'member': 'four', 'score': 4 } |
| 53 | + ]); |
| 54 | + |
| 55 | + }); |
| 56 | + |
| 57 | + await t.test('Get range by score with LIMIT', async () => { |
| 58 | + |
| 59 | + const result = await cmdCli.zRangeWithScores(`${TEST_KEY_PREFIX}myset`, 1, 5, { |
| 60 | + 'by': 'SCORE', |
| 61 | + 'offset': 1, |
| 62 | + 'count': 2 |
| 63 | + }); |
| 64 | + |
| 65 | + Assert.deepEqual(result, [ |
| 66 | + { 'member': 'two', 'score': 2 }, |
| 67 | + { 'member': 'three', 'score': 3 } |
| 68 | + ]); |
| 69 | + |
| 70 | + }); |
| 71 | + |
| 72 | + await t.test('Get range by score in reverse order with LIMIT', async () => { |
| 73 | + |
| 74 | + const result = await cmdCli.zRangeWithScores(`${TEST_KEY_PREFIX}myset`, 5, 1, { |
| 75 | + 'by': 'SCORE', |
| 76 | + 'rev': true, |
| 77 | + 'offset': 0, |
| 78 | + 'count': 2 |
| 79 | + }); |
| 80 | + |
| 81 | + Assert.deepEqual(result, [ |
| 82 | + { 'member': 'five', 'score': 5 }, |
| 83 | + { 'member': 'four', 'score': 4 } |
| 84 | + ]); |
| 85 | + |
| 86 | + }); |
| 87 | + |
| 88 | + await t.test('Get range with negative indices', async () => { |
| 89 | + |
| 90 | + const result = await cmdCli.zRangeWithScores(`${TEST_KEY_PREFIX}myset`, -3, -1); |
| 91 | + |
| 92 | + Assert.deepEqual(result, [ |
| 93 | + { 'member': 'three', 'score': 3 }, |
| 94 | + { 'member': 'four', 'score': 4 }, |
| 95 | + { 'member': 'five', 'score': 5 } |
| 96 | + ]); |
| 97 | + |
| 98 | + }); |
| 99 | + |
| 100 | + await t.test('Get all members with scores', async () => { |
| 101 | + |
| 102 | + const result = await cmdCli.zRangeWithScores(`${TEST_KEY_PREFIX}myset`, 0, -1); |
| 103 | + |
| 104 | + Assert.deepEqual(result, [ |
| 105 | + { 'member': 'one', 'score': 1 }, |
| 106 | + { 'member': 'two', 'score': 2 }, |
| 107 | + { 'member': 'three', 'score': 3 }, |
| 108 | + { 'member': 'four', 'score': 4 }, |
| 109 | + { 'member': 'five', 'score': 5 } |
| 110 | + ]); |
| 111 | + |
| 112 | + }); |
| 113 | + |
| 114 | + await t.test('Get range with Buffer version', async () => { |
| 115 | + |
| 116 | + const result = await cmdCli.zRangeWithScores$(`${TEST_KEY_PREFIX}myset`, 0, 1); |
| 117 | + |
| 118 | + Assert.strictEqual(result.length, 2); |
| 119 | + Assert.strictEqual(result[0].member.toString(), 'one'); |
| 120 | + Assert.strictEqual(result[0].score, 1); |
| 121 | + Assert.strictEqual(result[1].member.toString(), 'two'); |
| 122 | + Assert.strictEqual(result[1].score, 2); |
| 123 | + |
| 124 | + }); |
| 125 | + |
| 126 | + await t.test('Get empty range', async () => { |
| 127 | + |
| 128 | + const result = await cmdCli.zRangeWithScores(`${TEST_KEY_PREFIX}myset`, 10, 20); |
| 129 | + |
| 130 | + Assert.deepEqual(result, []); |
| 131 | + |
| 132 | + }); |
| 133 | + |
| 134 | + await t.test('Clean up test data', async () => { |
| 135 | + |
| 136 | + const deleted = await cmdCli.del(`${TEST_KEY_PREFIX}myset`); |
| 137 | + Assert.strictEqual(deleted, 1); |
| 138 | + |
| 139 | + }); |
| 140 | + |
| 141 | +}); |
| 142 | + |
| 143 | +test.after(async () => { |
| 144 | + |
| 145 | + await cmdCli.close(); |
| 146 | +}); |
0 commit comments