|
| 1 | +import pytest |
| 2 | +import numpy as np |
| 3 | +import pandas as pd |
| 4 | +from pathlib import Path |
| 5 | +from unittest.mock import MagicMock, patch |
| 6 | + |
| 7 | +from tools.cache import CacheIO |
| 8 | +from const_utils.default_values import AppSettings |
| 9 | + |
| 10 | + |
| 11 | +@pytest.fixture |
| 12 | +def mock_settings(): |
| 13 | + """Provides mock AppSettings for testing CacheIO.""" |
| 14 | + settings = AppSettings( |
| 15 | + log_path=Path("./test_log"), |
| 16 | + cache_file_path=Path("./test_cache") |
| 17 | + ) |
| 18 | + return settings |
| 19 | + |
| 20 | + |
| 21 | +@pytest.fixture |
| 22 | +def cache_io_instance(mock_settings): |
| 23 | + """Provides a CacheIO instance with mock settings.""" |
| 24 | + return CacheIO(settings=mock_settings) |
| 25 | + |
| 26 | + |
| 27 | +@pytest.fixture |
| 28 | +def temp_cache_file(tmp_path): |
| 29 | + """Provides a temporary cache file path for testing.""" |
| 30 | + return tmp_path / "test_cache.parquet" |
| 31 | + |
| 32 | + |
| 33 | +@pytest.fixture |
| 34 | +def sample_hash_map(): |
| 35 | + """Provides a sample hash map for testing.""" |
| 36 | + return { |
| 37 | + Path("/path/to/file1.jpg"): np.array([True, False, True], dtype=bool), |
| 38 | + Path("/path/to/file2.png"): np.array([False, True, False], dtype=bool), |
| 39 | + } |
| 40 | + |
| 41 | + |
| 42 | +def test_cache_io_init(cache_io_instance): |
| 43 | + """Tests if CacheIO initializes correctly.""" |
| 44 | + assert isinstance(cache_io_instance, CacheIO) |
| 45 | + assert cache_io_instance.settings is not None |
| 46 | + assert cache_io_instance.logger is not None |
| 47 | + |
| 48 | + |
| 49 | +def test_save_empty_hash_map(cache_io_instance, temp_cache_file): |
| 50 | + """Tests saving an empty hash map, which should not create a file.""" |
| 51 | + hash_map = {} |
| 52 | + cache_io_instance.save(hash_map, temp_cache_file) |
| 53 | + assert not temp_cache_file.exists() |
| 54 | + |
| 55 | + |
| 56 | +def test_save_valid_hash_map(cache_io_instance, temp_cache_file, sample_hash_map): |
| 57 | + """Tests saving a valid hash map and checks if the file is created.""" |
| 58 | + cache_io_instance.save(sample_hash_map, temp_cache_file) |
| 59 | + assert temp_cache_file.exists() |
| 60 | + |
| 61 | + |
| 62 | +def test_load_non_existent_file(cache_io_instance, temp_cache_file): |
| 63 | + """Tests loading from a cache file that does not exist.""" |
| 64 | + loaded_data = cache_io_instance.load(temp_cache_file) |
| 65 | + assert loaded_data == {} |
| 66 | + |
| 67 | + |
| 68 | +def test_load_damaged_file(cache_io_instance, temp_cache_file): |
| 69 | + """Tests loading from a damaged cache file (simulated by EOFError).""" |
| 70 | + temp_cache_file.write_text("corrupted data") |
| 71 | + with patch('pandas.read_parquet', side_effect=EOFError): |
| 72 | + loaded_data = cache_io_instance.load(temp_cache_file) |
| 73 | + assert loaded_data == {} |
| 74 | + |
| 75 | + |
| 76 | +def test_load_valid_file(cache_io_instance, temp_cache_file, sample_hash_map): |
| 77 | + """Tests loading from a valid cache file.""" |
| 78 | + cache_io_instance.save(sample_hash_map, temp_cache_file) |
| 79 | + loaded_data = cache_io_instance.load(temp_cache_file) |
| 80 | + |
| 81 | + assert len(loaded_data) == len(sample_hash_map) |
| 82 | + for path, hash_array in sample_hash_map.items(): |
| 83 | + assert path in loaded_data |
| 84 | + assert np.array_equal(loaded_data[path], hash_array) |
| 85 | + |
| 86 | + |
| 87 | +def test_generate_cache_filename_no_custom_name(): |
| 88 | + """Tests generating a cache filename without a custom name.""" |
| 89 | + source_path = Path("/home/user/images") |
| 90 | + hash_type = "dhash" |
| 91 | + core_size = 16 |
| 92 | + filename = CacheIO.generate_cache_filename(source_path, hash_type, core_size, None) |
| 93 | + assert filename.startswith("cache_") |
| 94 | + assert "_dimages" in filename |
| 95 | + assert "dhash_s16.parquet" in filename |
| 96 | + |
| 97 | + |
| 98 | +def test_generate_cache_filename_with_custom_name(): |
| 99 | + """Tests generating a cache filename with a custom name.""" |
| 100 | + source_path = Path("/home/user/videos") |
| 101 | + hash_type = "phash" |
| 102 | + core_size = 32 |
| 103 | + custom_name = Path("my_video_cache") |
| 104 | + filename = CacheIO.generate_cache_filename(source_path, hash_type, core_size, custom_name) |
| 105 | + assert filename == "my_video_cache_phash_s32.parquet" |
| 106 | + |
| 107 | + |
| 108 | +def test_generate_cache_filename_with_custom_name_with_suffix(): |
| 109 | + """Tests generating a cache filename with a custom name that already includes the suffix.""" |
| 110 | + source_path = Path("/home/user/documents") |
| 111 | + hash_type = "ahash" |
| 112 | + core_size = 8 |
| 113 | + custom_name = Path("doc_cache.parquet") |
| 114 | + filename = CacheIO.generate_cache_filename(source_path, hash_type, core_size, custom_name) |
| 115 | + assert filename == "doc_cache_ahash_s8.parquet" |
0 commit comments