Add crop manager for staging and training image lifecycle
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
48
tests/unit/test_crop_manager.py
Normal file
48
tests/unit/test_crop_manager.py
Normal file
@@ -0,0 +1,48 @@
|
||||
"""Tests for detection crop saving and staging cleanup."""
|
||||
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
import numpy as np
|
||||
|
||||
from vigilar.detection.crop_manager import CropManager
|
||||
|
||||
|
||||
class TestCropManager:
|
||||
def test_save_crop(self, tmp_path):
|
||||
manager = CropManager(staging_dir=str(tmp_path / "staging"),
|
||||
training_dir=str(tmp_path / "training"))
|
||||
crop = np.zeros((100, 80, 3), dtype=np.uint8)
|
||||
path = manager.save_staging_crop(crop, species="cat", camera_id="kitchen")
|
||||
assert Path(path).exists()
|
||||
assert "cat" in path
|
||||
assert "kitchen" in path
|
||||
|
||||
def test_promote_to_training(self, tmp_path):
|
||||
manager = CropManager(staging_dir=str(tmp_path / "staging"),
|
||||
training_dir=str(tmp_path / "training"))
|
||||
crop = np.zeros((100, 80, 3), dtype=np.uint8)
|
||||
staging_path = manager.save_staging_crop(crop, species="cat", camera_id="kitchen")
|
||||
training_path = manager.promote_to_training(staging_path, pet_name="angel")
|
||||
assert Path(training_path).exists()
|
||||
assert "angel" in training_path
|
||||
assert not Path(staging_path).exists()
|
||||
|
||||
def test_cleanup_old_crops(self, tmp_path):
|
||||
staging = tmp_path / "staging"
|
||||
staging.mkdir(parents=True)
|
||||
|
||||
old_file = staging / "old_crop.jpg"
|
||||
old_file.write_bytes(b"fake")
|
||||
old_time = time.time() - 10 * 86400
|
||||
import os
|
||||
os.utime(old_file, (old_time, old_time))
|
||||
|
||||
new_file = staging / "new_crop.jpg"
|
||||
new_file.write_bytes(b"fake")
|
||||
|
||||
manager = CropManager(staging_dir=str(staging), training_dir=str(tmp_path / "training"))
|
||||
deleted = manager.cleanup_expired(retention_days=7)
|
||||
assert deleted == 1
|
||||
assert not old_file.exists()
|
||||
assert new_file.exists()
|
||||
Reference in New Issue
Block a user