|
1 | 1 | import pytest |
2 | | -from unittest.mock import patch |
| 2 | +from unittest.mock import patch, MagicMock |
3 | 3 | from fastapi import status |
4 | 4 | from fastapi.testclient import TestClient |
5 | 5 |
|
@@ -83,3 +83,96 @@ def test_detect_invalid_request(client): |
83 | 83 | json={}, # Missing required field 'text' |
84 | 84 | ) |
85 | 85 | assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY |
| 86 | + |
| 87 | + |
| 88 | +@patch("babeltron.app.models.detection.factory.get_detection_model") |
| 89 | +@patch("babeltron.app.routers.detect.detection_model", new_callable=MagicMock) |
| 90 | +def test_detect_with_cache_disabled(mock_detection_model, mock_get_model, client): |
| 91 | + # Create a mock model |
| 92 | + mock_model = MagicMock() |
| 93 | + mock_model.is_loaded = True |
| 94 | + mock_model.architecture = "lingua" |
| 95 | + mock_model.detect.return_value = ("fr", 0.95) |
| 96 | + |
| 97 | + # Make the factory return our mock model |
| 98 | + mock_get_model.return_value = mock_model |
| 99 | + |
| 100 | + # Configure the detection_model mock |
| 101 | + mock_detection_model.is_loaded = True |
| 102 | + mock_detection_model.architecture = "lingua" |
| 103 | + mock_detection_model.detect.return_value = ("fr", 0.95) |
| 104 | + |
| 105 | + # Test data with cache disabled |
| 106 | + test_data = { |
| 107 | + "text": "Bonjour, comment ça va?", |
| 108 | + "cache": False |
| 109 | + } |
| 110 | + |
| 111 | + # Mock the cache service to return a cached result |
| 112 | + with patch("babeltron.app.routers.detect.cache_service") as mock_cache: |
| 113 | + mock_cache.get_detection.return_value = { |
| 114 | + "language": "en", |
| 115 | + "confidence": 0.98, |
| 116 | + "cached": True |
| 117 | + } |
| 118 | + |
| 119 | + response = client.post("/api/v1/detect", json=test_data) |
| 120 | + assert response.status_code == status.HTTP_200_OK |
| 121 | + data = response.json() |
| 122 | + assert data["language"] == "fr" # Should use fresh detection, not cached |
| 123 | + assert data["confidence"] == 0.95 |
| 124 | + assert data["cached"] is False |
| 125 | + |
| 126 | + # Verify the model was called correctly |
| 127 | + mock_detection_model.detect.assert_called_once() |
| 128 | + args, kwargs = mock_detection_model.detect.call_args |
| 129 | + assert args[0] == "Bonjour, comment ça va?" |
| 130 | + |
| 131 | + # Verify cache was not used |
| 132 | + mock_cache.get_detection.assert_not_called() |
| 133 | + mock_cache.save_detection.assert_not_called() |
| 134 | + |
| 135 | + |
| 136 | +@patch("babeltron.app.models.detection.factory.get_detection_model") |
| 137 | +@patch("babeltron.app.routers.detect.detection_model", new_callable=MagicMock) |
| 138 | +def test_detect_with_cache_enabled(mock_detection_model, mock_get_model, client): |
| 139 | + # Create a mock model |
| 140 | + mock_model = MagicMock() |
| 141 | + mock_model.is_loaded = True |
| 142 | + mock_model.architecture = "lingua" |
| 143 | + mock_model.detect.return_value = ("fr", 0.95) |
| 144 | + |
| 145 | + # Make the factory return our mock model |
| 146 | + mock_get_model.return_value = mock_model |
| 147 | + |
| 148 | + # Configure the detection_model mock |
| 149 | + mock_detection_model.is_loaded = True |
| 150 | + mock_detection_model.architecture = "lingua" |
| 151 | + mock_detection_model.detect.return_value = ("fr", 0.95) |
| 152 | + |
| 153 | + # Test data with cache enabled (default) |
| 154 | + test_data = { |
| 155 | + "text": "Bonjour, comment ça va?" |
| 156 | + } |
| 157 | + |
| 158 | + # Mock the cache service to return a cached result |
| 159 | + with patch("babeltron.app.routers.detect.cache_service") as mock_cache: |
| 160 | + mock_cache.get_detection.return_value = { |
| 161 | + "language": "en", |
| 162 | + "confidence": 0.98, |
| 163 | + "cached": True |
| 164 | + } |
| 165 | + |
| 166 | + response = client.post("/api/v1/detect", json=test_data) |
| 167 | + assert response.status_code == status.HTTP_200_OK |
| 168 | + data = response.json() |
| 169 | + assert data["language"] == "en" # Should use cached result |
| 170 | + assert data["confidence"] == 0.98 |
| 171 | + assert data["cached"] is True |
| 172 | + |
| 173 | + # Verify the model was not called (using cached result) |
| 174 | + mock_detection_model.detect.assert_not_called() |
| 175 | + |
| 176 | + # Verify cache was used |
| 177 | + mock_cache.get_detection.assert_called_once() |
| 178 | + mock_cache.save_detection.assert_not_called() # Should not save since we used cached result |
0 commit comments