File size: 6,232 Bytes
5b7ed07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# 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


@pytest.mark.parametrize(
    "algorithm,expected_digest",
    [
        ("sha256", hashlib.sha256(b"test_data").hexdigest()),
        ("md5", hashlib.md5(b"test_data").hexdigest()),  # noqa: S324
    ],
)
def test_configurable_hash_method_parametrized(algorithm, expected_digest):
    """Parametrized test for ConfigurableHashMethod with different algorithms."""
    mock_app = MagicMock()
    mock_app.config = {"HASH_ALGORITHM": algorithm}

    with patch("superset.utils.cache_manager.current_app", mock_app):
        hash_obj = configurable_hash_method(b"test_data")
        assert hash_obj.hexdigest() == expected_digest