Skip to content

Commit

Permalink
Merge pull request #2253 from Maayanshani25/tests_range
Browse files Browse the repository at this point in the history
Adding tests for unique chars in setrange and getrange commands. Changing documentation to byte offset/position
  • Loading branch information
Maayanshani25 authored Sep 9, 2024
2 parents e21b50b + e868da8 commit b3b91cc
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
10 changes: 5 additions & 5 deletions node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1201,7 +1201,7 @@ export class BaseClient {
}

/**
* Returns the substring of the string value stored at `key`, determined by the offsets
* Returns the substring of the string value stored at `key`, determined by the byte offsets
* `start` and `end` (both are inclusive). Negative offsets can be used in order to provide
* an offset starting from the end of the string. So `-1` means the last character, `-2` the
* penultimate and so forth. If `key` does not exist, an empty string is returned. If `start`
Expand All @@ -1210,8 +1210,8 @@ export class BaseClient {
* @see {@link https://valkey.io/commands/getrange/|valkey.io} for details.
*
* @param key - The key of the string.
* @param start - The starting offset.
* @param end - The ending offset.
* @param start - The starting byte offset.
* @param end - The ending byte offset.
* @param options - (Optional) See {@link DecoderOption}.
* @returns A substring extracted from the value stored at `key`.
*
Expand Down Expand Up @@ -6865,14 +6865,14 @@ export class BaseClient {
}

/**
* Overwrites part of the string stored at `key`, starting at the specified `offset`,
* Overwrites part of the string stored at `key`, starting at the specified byte `offset`,
* for the entire length of `value`. If the `offset` is larger than the current length of the string at `key`,
* the string is padded with zero bytes to make `offset` fit. Creates the `key` if it doesn't exist.
*
* @see {@link https://valkey.io/commands/setrange/|valkey.io} for more details.
*
* @param key - The key of the string to update.
* @param offset - The position in the string where `value` should be written.
* @param offset - The byte position in the string where `value` should be written.
* @param value - The string written with `offset`.
* @returns The length of the string stored at `key` after it was modified.
*
Expand Down
14 changes: 7 additions & 7 deletions node/src/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import {
GlideRecord, // eslint-disable-line @typescript-eslint/no-unused-vars
GlideString,
HashDataType,
ReadFrom, // eslint-disable-line @typescript-eslint/no-unused-vars
SortedSetDataType,
convertGlideRecord,
convertHashDataType,
ReadFrom, // eslint-disable-line @typescript-eslint/no-unused-vars
SortedSetDataType, // eslint-disable-line @typescript-eslint/no-unused-vars
} from "./BaseClient";

import {
Expand Down Expand Up @@ -365,7 +365,7 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
}

/**
* Returns the substring of the string value stored at `key`, determined by the offsets
* Returns the substring of the string value stored at `key`, determined by the byte offsets
* `start` and `end` (both are inclusive). Negative offsets can be used in order to provide
* an offset starting from the end of the string. So `-1` means the last character, `-2` the
* penultimate and so forth. If `key` does not exist, an empty string is returned. If `start`
Expand All @@ -374,8 +374,8 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
* @see {@link https://valkey.io/commands/getrange/|valkey.io} for details.
*
* @param key - The key of the string.
* @param start - The starting offset.
* @param end - The ending offset.
* @param start - The starting byte offset.
* @param end - The ending byte offset.
*
* Command Response - substring extracted from the value stored at `key`.
*/
Expand Down Expand Up @@ -3835,14 +3835,14 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
}

/**
* Overwrites part of the string stored at `key`, starting at the specified `offset`,
* Overwrites part of the string stored at `key`, starting at the specified byte `offset`,
* for the entire length of `value`. If the `offset` is larger than the current length of the string at `key`,
* the string is padded with zero bytes to make `offset` fit. Creates the `key` if it doesn't exist.
*
* @see {@link https://valkey.io/commands/setrange/|valkey.io} for details.
*
* @param key - The key of the string to update.
* @param offset - The position in the string where `value` should be written.
* @param offset - The byte position in the string where `value` should be written.
* @param value - The string written with `offset`.
*
* Command Response - The length of the string stored at `key` after it was modified.
Expand Down
21 changes: 21 additions & 0 deletions node/tests/SharedTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1343,6 +1343,13 @@ export function runBaseTests(config: {
await expect(
client.getrange(nonStringKey, 0, -1),
).rejects.toThrow(RequestError);

// unique chars key
expect(await client.set(key, "爱和美力")).toEqual("OK");
expect(await client.getrange(key, 0, 5)).toEqual("爱和");

expect(await client.set(key, "😊🌸")).toEqual("OK");
expect(await client.getrange(key, 4, 7)).toEqual("🌸");
}, protocol);
},
config.timeout,
Expand Down Expand Up @@ -7724,6 +7731,8 @@ export function runBaseTests(config: {
async (protocol) => {
await runTest(async (client: BaseClient) => {
const key = uuidv4();
const key_2 = uuidv4();
const key_3 = uuidv4();
const nonStringKey = uuidv4();

// new key
Expand All @@ -7733,6 +7742,18 @@ export function runBaseTests(config: {
expect(await client.setrange(key, 6, "GLIDE")).toBe(11);
expect(await client.get(key)).toEqual("Hello GLIDE");

// unique chars keys, size of 3 bytes each
expect(await client.setrange(key_2, 0, "爱和美力")).toBe(12);

expect(await client.setrange(key_2, 3, "abc")).toBe(12);
expect(await client.get(key_2)).toEqual("爱abc美力");

// unique char key, size of 4 bytes
expect(await client.setrange(key_3, 0, "😊")).toBe(4);

expect(await client.setrange(key_3, 4, "GLIDE")).toBe(9);
expect(await client.get(key_3)).toEqual("😊GLIDE");

// offset > len
expect(await client.setrange(key, 15, "GLIDE")).toBe(20);
expect(await client.get(key)).toEqual(
Expand Down

0 comments on commit b3b91cc

Please sign in to comment.