|
| 1 | +from unittest.mock import patch |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +import app.platforms.dispatcher as dispatcher |
| 6 | +from app.platforms.base import BaseProcessingPlatform |
| 7 | +from app.schemas import ProcessType, ProcessingJobSummary |
| 8 | + |
| 9 | + |
| 10 | +class DummyPlatform(BaseProcessingPlatform): |
| 11 | + def execute_job(self, title: str, details: dict, parameters: dict) -> ProcessingJobSummary: |
| 12 | + return ProcessingJobSummary( |
| 13 | + id="dummy-job-id", |
| 14 | + title=title, |
| 15 | + status="created" |
| 16 | + ) |
| 17 | + |
| 18 | + |
| 19 | +@pytest.fixture(autouse=True) |
| 20 | +def clear_registry(): |
| 21 | + """Ensure PROCESSING_PLATFORMS is clean before each test.""" |
| 22 | + dispatcher.PROCESSING_PLATFORMS.clear() |
| 23 | + yield |
| 24 | + dispatcher.PROCESSING_PLATFORMS.clear() |
| 25 | + |
| 26 | + |
| 27 | +def test_register_processing_platform(): |
| 28 | + dispatcher.register_processing_platform(ProcessType.OPENEO, DummyPlatform) |
| 29 | + assert dispatcher.PROCESSING_PLATFORMS[ProcessType.OPENEO] is DummyPlatform |
| 30 | + |
| 31 | + |
| 32 | +def test_get_processing_platform_success(): |
| 33 | + dispatcher.PROCESSING_PLATFORMS[ProcessType.OPENEO] = DummyPlatform |
| 34 | + instance = dispatcher.get_processing_platform(ProcessType.OPENEO) |
| 35 | + assert isinstance(instance, DummyPlatform) |
| 36 | + |
| 37 | + |
| 38 | +def test_get_processing_platform_unsupported(): |
| 39 | + with pytest.raises(ValueError, match="Unsupported service type"): |
| 40 | + dispatcher.get_processing_platform(ProcessType.OPENEO) |
| 41 | + |
| 42 | + |
| 43 | +@patch("app.platforms.dispatcher.importlib.import_module") |
| 44 | +@patch("app.platforms.dispatcher.pkgutil.iter_modules") |
| 45 | +def test_load_processing_platforms(mock_iter_modules, mock_import_module): |
| 46 | + # Simulate two modules found in the implementations package |
| 47 | + mock_iter_modules.return_value = [ |
| 48 | + (None, "mod1", False), |
| 49 | + (None, "mod2", False), |
| 50 | + ] |
| 51 | + |
| 52 | + dispatcher.load_processing_platforms() |
| 53 | + |
| 54 | + mock_import_module.assert_any_call("app.platforms.implementations.mod1") |
| 55 | + mock_import_module.assert_any_call("app.platforms.implementations.mod2") |
| 56 | + assert mock_import_module.call_count == 2 |
| 57 | + |
| 58 | + |
| 59 | +@patch("app.platforms.dispatcher.pkgutil.iter_modules") |
| 60 | +def test_load_processing_platforms_no_modules(mock_iter_modules): |
| 61 | + mock_iter_modules.return_value = [] |
| 62 | + # Should not raise, just do nothing |
| 63 | + dispatcher.load_processing_platforms() |
0 commit comments