-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest_cache_integration.py
More file actions
156 lines (127 loc) · 5 KB
/
Copy pathtest_cache_integration.py
File metadata and controls
156 lines (127 loc) · 5 KB
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
"""Integration tests for S2 scene caching."""
import tempfile
from pathlib import Path
import pytest
from app.services.cache_service import (
check_s2_scene_exists,
save_to_cache,
)
@pytest.fixture
def temp_cache_dir(monkeypatch):
"""Create a temporary cache directory for testing."""
with tempfile.TemporaryDirectory() as tmpdir:
# Mock get_cache_dir to use our temp directory
temp_path = Path(tmpdir) / "cache" / "scenes"
monkeypatch.setattr(
"app.services.cache_service.get_cache_dir", lambda: temp_path
)
yield temp_path
@pytest.fixture
def sample_image_file():
"""Create a sample image file for testing."""
with tempfile.NamedTemporaryFile(suffix=".tif", delete=False) as f:
f.write(b"fake image data for testing")
temp_path = Path(f.name)
yield temp_path
# Cleanup
if temp_path.exists():
temp_path.unlink()
class TestCacheIntegration:
"""Integration tests for cache functionality."""
@pytest.mark.asyncio
async def test_cache_miss_then_hit(self, temp_cache_dir, sample_image_file):
"""Test that cache miss works, then cache hit after saving."""
win_a = "2023-01-01_2023-03-31"
win_b = "2023-04-01_2023-06-30"
bbox = [10.0, 20.0, 10.5, 20.5]
# First check - should be a cache MISS
exists, path = await check_s2_scene_exists(win_a, win_b, bbox)
assert exists is False
assert path is None
# Save to cache
await save_to_cache(sample_image_file, win_a, win_b, bbox)
# Second check - should be a cache HIT
exists, path = await check_s2_scene_exists(win_a, win_b, bbox)
assert exists is True
assert path is not None
assert path.exists()
assert path.suffix == ".tif"
@pytest.mark.asyncio
async def test_cache_organizes_by_year(self, temp_cache_dir, sample_image_file):
"""Test that cache files are organized in year directories."""
win_a = "2023-01-01_2023-03-31"
win_b = "2023-04-01_2023-06-30"
bbox = [10.0, 20.0, 10.5, 20.5]
await save_to_cache(sample_image_file, win_a, win_b, bbox)
# Check that year directory was created
year_dir = temp_cache_dir / "2023"
assert year_dir.exists()
assert year_dir.is_dir()
# Check that file is in the year directory
cached_files = list(year_dir.glob("scene_*.tif"))
assert len(cached_files) == 1
@pytest.mark.asyncio
async def test_multiple_scenes_cached(self, temp_cache_dir, sample_image_file):
"""Test that multiple different scenes can be cached."""
scenes = [
(
"2023-01-01_2023-03-31",
"2023-04-01_2023-06-30",
[10.0, 20.0, 10.5, 20.5],
),
(
"2023-07-01_2023-09-30",
"2023-10-01_2023-12-31",
[11.0, 21.0, 11.5, 21.5],
),
(
"2024-01-01_2024-03-31",
"2024-04-01_2024-06-30",
[12.0, 22.0, 12.5, 22.5],
),
]
# Cache all scenes
for win_a, win_b, bbox in scenes:
await save_to_cache(sample_image_file, win_a, win_b, bbox)
# Verify all can be found
for win_a, win_b, bbox in scenes:
exists, path = await check_s2_scene_exists(win_a, win_b, bbox)
assert exists is True
assert path is not None
@pytest.mark.asyncio
async def test_cached_file_content_preserved(
self, temp_cache_dir, sample_image_file
):
"""Test that cached file content is preserved correctly."""
win_a = "2023-01-01_2023-03-31"
win_b = "2023-04-01_2023-06-30"
bbox = [10.0, 20.0, 10.5, 20.5]
# Read original content
with open(sample_image_file, "rb") as f:
original_content = f.read()
# Save to cache
await save_to_cache(sample_image_file, win_a, win_b, bbox)
# Get cached file
exists, cached_path = await check_s2_scene_exists(win_a, win_b, bbox)
assert exists is True
# Verify content matches
with open(cached_path, "rb") as f:
cached_content = f.read()
assert cached_content == original_content
@pytest.mark.asyncio
async def test_different_bbox_different_cache(
self, temp_cache_dir, sample_image_file
):
"""Test that different bboxes create different cache entries."""
win_a = "2023-01-01_2023-03-31"
win_b = "2023-04-01_2023-06-30"
bbox1 = [10.0, 20.0, 10.5, 20.5]
bbox2 = [10.0, 20.0, 10.6, 20.6] # Slightly different
# Cache first bbox
await save_to_cache(sample_image_file, win_a, win_b, bbox1)
# First bbox should hit
exists1, _ = await check_s2_scene_exists(win_a, win_b, bbox1)
assert exists1 is True
# Second bbox should miss
exists2, _ = await check_s2_scene_exists(win_a, win_b, bbox2)
assert exists2 is False