Spaces:
Running
Running
File size: 2,648 Bytes
352a4b6 |
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 |
# Copyright 2025 Google LLC
#
# Licensed 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 unittest
from unittest.mock import patch
import importlib
import os
# Import module under test. Initial load uses actual or globally patched dependencies.
import cache_store
class TestCacheStore(unittest.TestCase):
@patch('diskcache.Cache')
@patch('os.getenv')
def test_cache_directory_env_var_set(self, mock_os_getenv, mock_DiskCache_class):
expected_dir = '/custom/cache/path'
# Mock os.getenv for this test.
def getenv_side_effect_set(key, default=None):
if key == 'CACHE_DIR':
return expected_dir
return os.environ.get(key, default) # Fallback for other env var calls.
mock_os_getenv.side_effect = getenv_side_effect_set
# Reload cache_store to apply method-specific mocks.
importlib.reload(cache_store)
# Check os.getenv call by cache_store.py.
mock_os_getenv.assert_any_call('CACHE_DIR', '/app/cache_dir')
# Check diskcache.Cache initialization with the expected directory.
mock_DiskCache_class.assert_called_once_with(expected_dir)
@patch('diskcache.Cache')
@patch('os.getenv')
def test_cache_directory_env_var_not_set(self, mock_os_getenv, mock_DiskCache_class):
# Mock os.getenv: simulate 'CACHE_DIR' not set, so it returns the default.
def getenv_side_effect_not_set(key, default=None):
if key == 'CACHE_DIR':
# 'default' will be '/app/cache_dir' from cache_store.py's call
return default
return os.environ.get(key, default) # Fallback for other env vars
mock_os_getenv.side_effect = getenv_side_effect_not_set
# Reload cache_store to apply method-specific mocks.
importlib.reload(cache_store)
# Check os.getenv call by cache_store.py.
mock_os_getenv.assert_any_call('CACHE_DIR', '/app/cache_dir')
# Check diskcache.Cache initialization with the default directory.
mock_DiskCache_class.assert_called_once_with('/app/cache_dir')
if __name__ == '__main__':
unittest.main()
|