fix: ZMSCORE return value if key does not exist (#4697)

* tests were added

* bug has been fixed

* formatting was fixed

* review comments have been fixed

* outdated comment has been removed
This commit is contained in:
Volodymyr Yavdoshenko 2025-03-05 12:27:59 +02:00 committed by GitHub
parent 2644d69239
commit 28e1a48781
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 19 additions and 0 deletions

View file

@ -1360,6 +1360,13 @@ OpResult<unsigned> OpRem(const OpArgs& op_args, string_view key, facade::ArgRang
OpResult<MScoreResponse> OpMScore(const OpArgs& op_args, string_view key,
facade::ArgRange members) {
auto res_it = op_args.GetDbSlice().FindReadOnly(op_args.db_cntx, key, OBJ_ZSET);
if (res_it.status() == OpStatus::KEY_NOTFOUND) {
// If the key doesn't exist return an array of NIL values
MScoreResponse result(members.Size(), std::nullopt);
return result;
}
if (!res_it)
return res_it.status();

View file

@ -287,6 +287,18 @@ TEST_F(ZSetFamilyTest, ZMScore) {
EXPECT_THAT(resp.GetVec(), ElementsAre("42", "3.14", ArgType(RespExpr::NIL)));
}
// Test for ZMSCORE with member on a non-existent keys
TEST_F(ZSetFamilyTest, ZMScoreNonExistentKeys) {
// Case 1: Single member with non-existent key (ZMSCORE abc x)
auto resp = Run({"zmscore", "abc", "x"});
EXPECT_THAT(resp, ArgType(RespExpr::NIL));
// Case 2: Multiple members with non-existent key (ZMSCORE abc x y z)
resp = Run({"zmscore", "abc", "x", "y", "z"});
EXPECT_THAT(resp.GetVec(),
ElementsAre(ArgType(RespExpr::NIL), ArgType(RespExpr::NIL), ArgType(RespExpr::NIL)));
}
TEST_F(ZSetFamilyTest, ZRangeRank) {
Run({"zadd", "x", "1.1", "a", "2.1", "b"});
EXPECT_THAT(Run({"zrangebyscore", "x", "0", "(1.1"}), ArrLen(0));