fix: RENAMENX according to spec #229 (#230)

fix: RENAMENX according to spec #226

Signed-off-by: odedponcz <oded@poncz.com>
This commit is contained in:
odedponcz 2022-08-07 22:49:21 +03:00 committed by GitHub
parent d19fa58216
commit 3b04801cd6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 1 deletions

View File

@ -443,7 +443,14 @@ void GenericFamily::Rename(CmdArgList args, ConnectionContext* cntx) {
void GenericFamily::RenameNx(CmdArgList args, ConnectionContext* cntx) {
OpResult<void> st = RenameGeneric(args, true, cntx);
(*cntx)->SendError(st.status());
OpStatus status = st.status();
if (status == OpStatus::OK) {
(*cntx)->SendLong(1);
} else if (status == OpStatus::KEY_EXISTS) {
(*cntx)->SendLong(0);
} else {
(*cntx)->SendError(status);
}
}
void GenericFamily::Ttl(CmdArgList args, ConnectionContext* cntx) {

View File

@ -153,6 +153,19 @@ TEST_F(GenericFamilyTest, RenameBinary) {
EXPECT_EQ(Run({"get", kKey2}), "bar");
}
TEST_F(GenericFamilyTest, RenameNx) {
// Set two keys
string b_val(32, 'b');
string x_val(32, 'x');
Run({"mset", "x", x_val, "b", b_val});
ASSERT_THAT(Run({"renamenx", "z", "b"}), ErrArg("no such key"));
ASSERT_THAT(Run({"renamenx", "x", "b"}), IntArg(0)); // b already exists
ASSERT_THAT(Run({"renamenx", "x", "y"}), IntArg(1));
ASSERT_EQ(Run({"get", "y"}), x_val);
}
using testing::AnyOf;
using testing::Each;
using testing::StartsWith;