bito-code-review[bot] commented on code in PR #37361:
URL: https://github.com/apache/superset/pull/37361#discussion_r2716978648


##########
tests/unit_tests/utils/test_cache_manager.py:
##########
@@ -0,0 +1,171 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+import hashlib
+from unittest.mock import MagicMock, patch
+
+import pytest
+
+from superset.utils.cache_manager import (
+    configurable_hash_method,
+    ConfigurableHashMethod,
+    SupersetCache,
+)
+
+
+def test_configurable_hash_method_uses_sha256():
+    """Test ConfigurableHashMethod uses sha256 when configured."""
+    mock_app = MagicMock()
+    mock_app.config = {"HASH_ALGORITHM": "sha256"}
+
+    with patch("superset.utils.cache_manager.current_app", mock_app):
+        hash_obj = configurable_hash_method(b"test")
+        # Verify it returns a sha256 hash object
+        assert hash_obj.hexdigest() == hashlib.sha256(b"test").hexdigest()
+
+
+def test_configurable_hash_method_uses_md5():
+    """Test ConfigurableHashMethod uses md5 when configured."""
+    mock_app = MagicMock()
+    mock_app.config = {"HASH_ALGORITHM": "md5"}
+
+    with patch("superset.utils.cache_manager.current_app", mock_app):
+        hash_obj = configurable_hash_method(b"test")
+        # Verify it returns a md5 hash object
+        assert hash_obj.hexdigest() == hashlib.md5(b"test").hexdigest()  # 
noqa: S324
+
+
+def test_configurable_hash_method_empty_data():
+    """Test ConfigurableHashMethod with empty data."""
+    mock_app = MagicMock()
+    mock_app.config = {"HASH_ALGORITHM": "sha256"}
+
+    with patch("superset.utils.cache_manager.current_app", mock_app):
+        hash_obj = configurable_hash_method()
+        assert hash_obj.hexdigest() == hashlib.sha256(b"").hexdigest()
+
+
+def test_configurable_hash_method_is_callable():
+    """Test that ConfigurableHashMethod instance is callable."""
+    method = ConfigurableHashMethod()
+    assert callable(method)
+
+
+def test_superset_cache_memoize_uses_configurable_hash():
+    """Test that SupersetCache.memoize uses configurable_hash_method by 
default."""
+    cache = SupersetCache()
+
+    with patch.object(
+        cache.__class__.__bases__[0], "memoize", return_value=lambda f: f
+    ) as mock_memoize:
+        cache.memoize(timeout=300)
+
+        mock_memoize.assert_called_once()
+        call_kwargs = mock_memoize.call_args[1]
+        assert call_kwargs["hash_method"] is configurable_hash_method
+
+
+def test_superset_cache_memoize_allows_explicit_hash_method():
+    """Test that SupersetCache.memoize allows explicit hash_method override."""
+    cache = SupersetCache()
+
+    with patch.object(
+        cache.__class__.__bases__[0], "memoize", return_value=lambda f: f
+    ) as mock_memoize:
+        cache.memoize(timeout=300, hash_method=hashlib.md5)
+
+        mock_memoize.assert_called_once()
+        call_kwargs = mock_memoize.call_args[1]
+        assert call_kwargs["hash_method"] == hashlib.md5
+
+
+def test_superset_cache_cached_uses_configurable_hash():
+    """Test that SupersetCache.cached uses configurable_hash_method by 
default."""
+    cache = SupersetCache()
+
+    with patch.object(
+        cache.__class__.__bases__[0], "cached", return_value=lambda f: f
+    ) as mock_cached:
+        cache.cached(timeout=300)
+
+        mock_cached.assert_called_once()
+        call_kwargs = mock_cached.call_args[1]
+        assert call_kwargs["hash_method"] is configurable_hash_method
+
+
+def test_superset_cache_cached_allows_explicit_hash_method():
+    """Test that SupersetCache.cached allows explicit hash_method override."""
+    cache = SupersetCache()
+
+    with patch.object(
+        cache.__class__.__bases__[0], "cached", return_value=lambda f: f
+    ) as mock_cached:
+        cache.cached(timeout=300, hash_method=hashlib.md5)
+
+        mock_cached.assert_called_once()
+        call_kwargs = mock_cached.call_args[1]
+        assert call_kwargs["hash_method"] == hashlib.md5
+
+
+def test_superset_cache_memoize_make_cache_key_uses_configurable_hash():
+    """Test _memoize_make_cache_key uses configurable_hash_method by 
default."""
+    cache = SupersetCache()
+
+    with patch.object(
+        cache.__class__.__bases__[0],
+        "_memoize_make_cache_key",
+        return_value=lambda *args, **kwargs: "cache_key",
+    ) as mock_make_key:
+        cache._memoize_make_cache_key(make_name=None, timeout=300)
+
+        mock_make_key.assert_called_once()
+        call_kwargs = mock_make_key.call_args[1]
+        assert call_kwargs["hash_method"] is configurable_hash_method
+
+
+def test_superset_cache_memoize_make_cache_key_allows_explicit_hash():
+    """Test _memoize_make_cache_key allows explicit hash_method override."""
+    cache = SupersetCache()
+
+    with patch.object(
+        cache.__class__.__bases__[0],
+        "_memoize_make_cache_key",
+        return_value=lambda *args, **kwargs: "cache_key",
+    ) as mock_make_key:
+        cache._memoize_make_cache_key(
+            make_name=None, timeout=300, hash_method=hashlib.md5
+        )
+
+        mock_make_key.assert_called_once()
+        call_kwargs = mock_make_key.call_args[1]
+        assert call_kwargs["hash_method"] == hashlib.md5
+
+
[email protected](
+    "algorithm,expected_digest",

Review Comment:
   <div>
   
   
   <div id="suggestion">
   <div id="issue"><b>Wrong type for pytest parametrize</b></div>
   <div id="fix">
   
   Change the first argument of `pytest.mark.parametrize` from a string to a 
tuple: `("algorithm", "expected_digest")` instead of 
`"algorithm,expected_digest"`.
   </div>
   
   
   <details>
   <summary>
   <b>Code suggestion</b>
   </summary>
   <blockquote>Check the AI-generated fix before applying</blockquote>
   <div id="code">
   
   
   ````suggestion
       ("algorithm", "expected_digest"),
   ````
   
   </div>
   </details>
   
   
   
   </div>
   
   
   
   
   <small><i>Code Review Run #3a5c30</i></small>
   </div>
   
   ---
   Should Bito avoid suggestions like this for future reviews? (<a 
href=https://alpha.bito.ai/home/ai-agents/review-rules>Manage Rules</a>)
   - [ ] Yes, avoid them



##########
superset/utils/cache.py:
##########
@@ -273,7 +274,7 @@ def wrapper(*args: Any, **kwargs: Any) -> Response:  # 
noqa: C901
         wrapper.uncached = f  # type: ignore
         wrapper.cache_timeout = timeout  # type: ignore
         wrapper.make_cache_key = cache._memoize_make_cache_key(  # type: 
ignore # pylint: disable=protected-access

Review Comment:
   <div>
   
   
   <div id="suggestion">
   <div id="issue"><b>Private member access and blanket type ignore</b></div>
   <div id="fix">
   
   Line 276 accesses private method `_memoize_make_cache_key` (SLF001) and uses 
blanket `# type: ignore` (PGH003). Replace with `# type: 
ignore[attr-defined,misc]` and consider if private API access is necessary.
   </div>
   
   
   <details>
   <summary>
   <b>Code suggestion</b>
   </summary>
   <blockquote>Check the AI-generated fix before applying</blockquote>
   <div id="code">
   
   
   ````suggestion
           wrapper.make_cache_key = cache._memoize_make_cache_key(  # type: 
ignore[attr-defined,misc] # pylint: disable=protected-access
   ````
   
   </div>
   </details>
   
   
   
   </div>
   
   
   
   
   <small><i>Code Review Run #3a5c30</i></small>
   </div>
   
   ---
   Should Bito avoid suggestions like this for future reviews? (<a 
href=https://alpha.bito.ai/home/ai-agents/review-rules>Manage Rules</a>)
   - [ ] Yes, avoid them



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to