-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a4e344e
commit d520cbf
Showing
5 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { keygen, newHttpClient, randomID } from "../test-utils"; | ||
|
||
import { afterAll, describe, expect, test } from "bun:test"; | ||
import { CopyCommand } from "./copy"; | ||
import { SetCommand } from "./set"; | ||
import { LPushCommand } from "./lpush"; | ||
|
||
const client = newHttpClient(); | ||
|
||
const { newKey, cleanup } = keygen(); | ||
afterAll(cleanup); | ||
|
||
describe("COPY test", () => { | ||
test("should copy key-value to another key", async () => { | ||
const key = newKey(); | ||
const destinationKey = newKey(); | ||
const value = randomID(); | ||
await new SetCommand([key, value]).exec(client); | ||
const res = await new CopyCommand([key, destinationKey]).exec(client); | ||
expect(res).toEqual("COPIED"); | ||
}); | ||
|
||
test("should not override existing destination", async () => { | ||
const key = newKey(); | ||
const destinationKey = newKey(); | ||
const value = randomID(); | ||
await new SetCommand([key, value]).exec(client); | ||
await new SetCommand([destinationKey, value]).exec(client); | ||
const res = await new CopyCommand([key, destinationKey]).exec(client); | ||
expect(res).toEqual("NOT_COPIED"); | ||
}); | ||
|
||
test("should override existing destination with replace", async () => { | ||
const key = newKey(); | ||
const destinationKey = newKey(); | ||
const value = randomID(); | ||
await new SetCommand([key, value]).exec(client); | ||
await new SetCommand([destinationKey, value]).exec(client); | ||
const res = await new CopyCommand([key, destinationKey, { replace: true }]).exec(client); | ||
expect(res).toEqual("COPIED"); | ||
}); | ||
|
||
test("should handle non-existent source key", async () => { | ||
const key = newKey(); | ||
const destinationKey = newKey(); | ||
const res = await new CopyCommand([key, destinationKey]).exec(client); | ||
expect(res).toEqual("NOT_COPIED"); | ||
}); | ||
|
||
test("should handle same source and destination keys", async () => { | ||
const key = newKey(); | ||
const value = randomID(); | ||
await new SetCommand([key, value]).exec(client); | ||
const res = await new CopyCommand([key, key]).exec(client); | ||
expect(res).toEqual("NOT_COPIED"); | ||
}); | ||
|
||
test("should copy list data type", async () => { | ||
const key = newKey(); | ||
const destinationKey = newKey(); | ||
await new LPushCommand([key, "value1", "value2"]).exec(client); | ||
const res = await new CopyCommand([key, destinationKey]).exec(client); | ||
expect(res).toEqual("COPIED"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Command, CommandOptions } from "./command"; | ||
|
||
/** | ||
* @see https://redis.io/commands/copy | ||
*/ | ||
export class CopyCommand extends Command<number, "COPIED" | "NOT_COPIED"> { | ||
constructor( | ||
[key, destinationKey, opts]: [key: string, destinationKey: string, opts?: { replace: boolean }], | ||
commandOptions?: CommandOptions<number, "COPIED" | "NOT_COPIED"> | ||
) { | ||
super(["COPY", key, destinationKey, ...(opts?.replace ? ["REPLACE"] : [])], { | ||
...commandOptions, | ||
deserialize(result) { | ||
if (result > 0) { | ||
return "COPIED"; | ||
} | ||
return "NOT_COPIED"; | ||
}, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters