Repository: redis/redis-py
File: tests/test_commands.py (Python)

Code before:

        assert len(res) == 16

    @skip_if_server_version_lt("8.3.224")
    @pytest.mark.parametrize(
        "value", [b"", b"abc", b"The quick brown fox jumps over the lazy dog"]
    )
    def test_local_digest_matches_server(self, r, value):
        key = "k:digest"
        r.delete(key)
        r.set(key, value)

        res_server = r.digest(key)
        res_local = r.digest_local(value)

        # got is str if decode_responses=True; ensure bytes->str for comparison
        if isinstance(res_server, bytes):
            assert isinstance(res_local, bytes)

        assert res_server is not None
        assert len(res_server) == 16
        assert res_local is not None
        assert len(res_local) == 16
        assert res_server == res_local

    @skip_if_server_version_lt("8.3.224")
    def test_pipeline_digest(self, r):
        k1, k2 = "k:d1{42}", "k:d2{42}"
        r.mset({k1: b"A", k2: b"B"})
        p = r.pipeline()
        p.digest(k1)
        p.digest(k2)
        out = p.execute()
        assert len(out) == 2
        for d in out:
            if isinstance(d, bytes):
                d = d.decode()
            assert d is None or len(d) == 16

    def test_get_and_set(self, r):
        # get and set can't be tested independently of each other
        assert r.get("a") is None
        byte_string = b"value"
        integer = 5
        unicode_string = chr(3456) + "abcd" + chr(3421)
        assert r.set("byte_string", byte_string)
        assert r.set("integer", 5)
        assert r.set("unicode_string", unicode_string)
        assert r.get("byte_string") == byte_string
        assert r.get("integer") == str(integer).encode()
        assert r.get("unicode_string").decode("utf-8") == unicode_string

Review comment (suggestion):
The test only verifies that res_local is bytes when res_server is bytes, but doesn't verify the opposite case when decode_responses=True. Consider adding an else clause to assert that both are strings when decode_responses=True, ensuring type consistency between server and local digest in both modes.

Code after:

        assert len(res) == 16

    @skip_if_server_version_lt("8.3.224")
    @pytest.mark.parametrize(
        "value",
        [
            b"",
            b"abc",
            b"The quick brown fox jumps over the lazy dog",
            "",
            "abc",
            "The quick brown fox jumps over the lazy dog",
        ],
    )
    def test_local_digest_matches_server(self, r, value):
        key = "k:digest"
        r.delete(key)
        r.set(key, value)

        res_server = r.digest(key)
        res_local = r.digest_local(value)

        # Verify type consistency between server and local digest
        if isinstance(res_server, bytes):
            assert isinstance(res_local, bytes)
        else:
            assert isinstance(res_local, str)

        assert res_server is not None
        assert len(res_server) == 16
        assert res_local is not None
        assert len(res_local) == 16
        assert res_server == res_local

    @skip_if_server_version_lt("8.3.224")
    def test_pipeline_digest(self, r):
        k1, k2 = "k:d1{42}", "k:d2{42}"
        r.mset({k1: b"A", k2: b"B"})
        p = r.pipeline()
        p.digest(k1)
        p.digest(k2)
        out = p.execute()
        assert len(out) == 2
        for d in out:
            if isinstance(d, bytes):
                d = d.decode()
            assert d is None or len(d) == 16

    def test_get_and_set(self, r):
        # get and set can't be tested independently of each other