Commit ·
6012c2e
1
Parent(s): e236d2e
Test: Implement complete unit test suites for frontend and backend, fix Windows encoding crash
Browse files- src/routes/assets.py +1 -1
- tests/__init__.py +11 -0
- tests/test_assets.py +58 -0
- tests/test_config.py +38 -0
- tests/test_detection.py +79 -0
- tests/test_health.py +40 -0
src/routes/assets.py
CHANGED
|
@@ -28,7 +28,7 @@ def serve_mask(filename):
|
|
| 28 |
safe_path = os.path.join(config.DOWNLOADS_DIR, filename)
|
| 29 |
|
| 30 |
if not os.path.exists(safe_path):
|
| 31 |
-
|
| 32 |
abort(404)
|
| 33 |
|
| 34 |
return send_from_directory(
|
|
|
|
| 28 |
safe_path = os.path.join(config.DOWNLOADS_DIR, filename)
|
| 29 |
|
| 30 |
if not os.path.exists(safe_path):
|
| 31 |
+
config.logger.warning(f"Asset Serving Failed: File not found at path {safe_path}")
|
| 32 |
abort(404)
|
| 33 |
|
| 34 |
return send_from_directory(
|
tests/__init__.py
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
|
| 3 |
+
# Ensure console output uses UTF-8 on Windows to prevent UnicodeEncodeErrors during piped test runs
|
| 4 |
+
if sys.platform.startswith('win'):
|
| 5 |
+
try:
|
| 6 |
+
if hasattr(sys.stdout, 'reconfigure'):
|
| 7 |
+
sys.stdout.reconfigure(encoding='utf-8')
|
| 8 |
+
if hasattr(sys.stderr, 'reconfigure'):
|
| 9 |
+
sys.stderr.reconfigure(encoding='utf-8')
|
| 10 |
+
except Exception:
|
| 11 |
+
pass
|
tests/test_assets.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import unittest
|
| 2 |
+
from unittest.mock import patch, MagicMock
|
| 3 |
+
import sys
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# Add backend directory to sys.path to enable imports
|
| 7 |
+
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
| 8 |
+
|
| 9 |
+
class TestAssets(unittest.TestCase):
|
| 10 |
+
@classmethod
|
| 11 |
+
def setUpClass(cls):
|
| 12 |
+
# Patch dependencies for offline testing
|
| 13 |
+
cls.gee_patch = patch('src.database.gee.initialize_earth_engine')
|
| 14 |
+
cls.mongo_patch = patch('src.database.mongo.initialize_mongodb')
|
| 15 |
+
cls.model_patch = patch('src.services.model.initialize_model')
|
| 16 |
+
|
| 17 |
+
cls.gee_mock = cls.gee_patch.start()
|
| 18 |
+
cls.mongo_mock = cls.mongo_patch.start()
|
| 19 |
+
cls.model_mock = cls.model_patch.start()
|
| 20 |
+
|
| 21 |
+
cls.mongo_mock.return_value = (MagicMock(), MagicMock())
|
| 22 |
+
cls.model_mock.return_value = MagicMock()
|
| 23 |
+
|
| 24 |
+
from src.app import create_app
|
| 25 |
+
cls.app = create_app()
|
| 26 |
+
cls.client = cls.app.test_client()
|
| 27 |
+
|
| 28 |
+
@classmethod
|
| 29 |
+
def tearDownClass(cls):
|
| 30 |
+
cls.gee_patch.stop()
|
| 31 |
+
cls.mongo_patch.stop()
|
| 32 |
+
cls.model_patch.stop()
|
| 33 |
+
|
| 34 |
+
def test_serve_missing_mask_returns_404(self):
|
| 35 |
+
# Mock os.path.exists to return False to simulate missing files
|
| 36 |
+
with patch('os.path.exists') as mock_exists:
|
| 37 |
+
mock_exists.return_value = False
|
| 38 |
+
response = self.client.get('/mask/nonexistent_file.png')
|
| 39 |
+
self.assertEqual(response.status_code, 404)
|
| 40 |
+
|
| 41 |
+
def test_serve_existing_mask_calls_send_from_directory(self):
|
| 42 |
+
# Mock os.path.exists to True and intercept send_from_directory
|
| 43 |
+
with patch('os.path.exists') as mock_exists, \
|
| 44 |
+
patch('src.routes.assets.send_from_directory') as mock_send:
|
| 45 |
+
mock_exists.return_value = True
|
| 46 |
+
mock_send.return_value = "mock_file_content"
|
| 47 |
+
|
| 48 |
+
response = self.client.get('/mask/session123/existing_file.png')
|
| 49 |
+
self.assertEqual(response.status_code, 200)
|
| 50 |
+
self.assertEqual(response.data.decode('utf-8'), "mock_file_content")
|
| 51 |
+
|
| 52 |
+
mock_send.assert_called_once()
|
| 53 |
+
args = mock_send.call_args[0]
|
| 54 |
+
self.assertTrue(args[0].endswith(os.path.join('server_downloads', 'session123')))
|
| 55 |
+
self.assertEqual(args[1], 'existing_file.png')
|
| 56 |
+
|
| 57 |
+
if __name__ == '__main__':
|
| 58 |
+
unittest.main()
|
tests/test_config.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import unittest
|
| 3 |
+
from unittest.mock import patch
|
| 4 |
+
import sys
|
| 5 |
+
|
| 6 |
+
# Add backend directory to sys.path to enable imports
|
| 7 |
+
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
| 8 |
+
|
| 9 |
+
class TestConfig(unittest.TestCase):
|
| 10 |
+
def test_default_config_loading(self):
|
| 11 |
+
# Patch os.environ to check default values when env vars are missing
|
| 12 |
+
with patch.dict(os.environ, {}, clear=True):
|
| 13 |
+
import importlib
|
| 14 |
+
from src import config
|
| 15 |
+
importlib.reload(config)
|
| 16 |
+
|
| 17 |
+
# Assert defaults
|
| 18 |
+
self.assertEqual(config.FLASK_PORT, 5000)
|
| 19 |
+
self.assertEqual(config.MONGO_DB_NAME, "satvision_db")
|
| 20 |
+
self.assertIsNone(config.GEMINI_API_KEY)
|
| 21 |
+
|
| 22 |
+
def test_env_override_config_loading(self):
|
| 23 |
+
env_vars = {
|
| 24 |
+
"FLASK_PORT": "8080",
|
| 25 |
+
"EE_PROJECT_ID": "test-project-123",
|
| 26 |
+
"GEMINI_API_KEY": "test-gemini-key-xyz"
|
| 27 |
+
}
|
| 28 |
+
with patch.dict(os.environ, env_vars, clear=True):
|
| 29 |
+
import importlib
|
| 30 |
+
from src import config
|
| 31 |
+
importlib.reload(config)
|
| 32 |
+
|
| 33 |
+
self.assertEqual(config.FLASK_PORT, 8080)
|
| 34 |
+
self.assertEqual(config.EE_PROJECT_ID, "test-project-123")
|
| 35 |
+
self.assertEqual(config.GEMINI_API_KEY, "test-gemini-key-xyz")
|
| 36 |
+
|
| 37 |
+
if __name__ == '__main__':
|
| 38 |
+
unittest.main()
|
tests/test_detection.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import unittest
|
| 2 |
+
from unittest.mock import patch, MagicMock
|
| 3 |
+
import sys
|
| 4 |
+
import os
|
| 5 |
+
import json
|
| 6 |
+
|
| 7 |
+
# Add backend directory to sys.path to enable imports
|
| 8 |
+
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
| 9 |
+
|
| 10 |
+
class TestDetection(unittest.TestCase):
|
| 11 |
+
@classmethod
|
| 12 |
+
def setUpClass(cls):
|
| 13 |
+
# Patch dependencies for offline testing
|
| 14 |
+
cls.gee_patch = patch('src.database.gee.initialize_earth_engine')
|
| 15 |
+
cls.mongo_patch = patch('src.database.mongo.initialize_mongodb')
|
| 16 |
+
cls.model_patch = patch('src.services.model.initialize_model')
|
| 17 |
+
|
| 18 |
+
cls.gee_mock = cls.gee_patch.start()
|
| 19 |
+
cls.mongo_mock = cls.mongo_patch.start()
|
| 20 |
+
cls.model_mock = cls.model_patch.start()
|
| 21 |
+
|
| 22 |
+
cls.mongo_mock.return_value = (MagicMock(), MagicMock())
|
| 23 |
+
cls.model_mock.return_value = MagicMock()
|
| 24 |
+
|
| 25 |
+
from src.app import create_app
|
| 26 |
+
cls.app = create_app()
|
| 27 |
+
cls.client = cls.app.test_client()
|
| 28 |
+
|
| 29 |
+
@classmethod
|
| 30 |
+
def tearDownClass(cls):
|
| 31 |
+
cls.gee_patch.stop()
|
| 32 |
+
cls.mongo_patch.stop()
|
| 33 |
+
cls.model_patch.stop()
|
| 34 |
+
|
| 35 |
+
def test_detect_stream_success(self):
|
| 36 |
+
# Setup mock payload
|
| 37 |
+
payload = {
|
| 38 |
+
"bbox": {
|
| 39 |
+
"north": 30.5,
|
| 40 |
+
"south": 30.2,
|
| 41 |
+
"east": 69.5,
|
| 42 |
+
"west": 69.2
|
| 43 |
+
},
|
| 44 |
+
"apply_buffer": True
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
# Mock run_detection_pipeline generator
|
| 48 |
+
def mock_pipeline_gen(*args, **kwargs):
|
| 49 |
+
yield json.dumps({"progress": 10, "log": "Fetching imagery..."}) + "\n"
|
| 50 |
+
yield json.dumps({"progress": 50, "log": "Running UNet++ model..."}) + "\n"
|
| 51 |
+
yield json.dumps({"progress": 100, "result": {"latest": "url1"}}) + "\n"
|
| 52 |
+
|
| 53 |
+
with patch('src.services.detection.run_detection_pipeline', side_effect=mock_pipeline_gen) as mock_pipeline:
|
| 54 |
+
response = self.client.post('/api/detect_stream', json=payload)
|
| 55 |
+
self.assertEqual(response.status_code, 200)
|
| 56 |
+
self.assertEqual(response.mimetype, 'application/json')
|
| 57 |
+
|
| 58 |
+
# Read streaming response
|
| 59 |
+
chunks = response.data.decode('utf-8').split('\n')
|
| 60 |
+
# Filter out empty string due to trailing newline
|
| 61 |
+
chunks = [c for c in chunks if c.strip()]
|
| 62 |
+
|
| 63 |
+
self.assertEqual(len(chunks), 3)
|
| 64 |
+
first_chunk = json.loads(chunks[0])
|
| 65 |
+
self.assertEqual(first_chunk['progress'], 10)
|
| 66 |
+
self.assertEqual(first_chunk['log'], "Fetching imagery...")
|
| 67 |
+
|
| 68 |
+
last_chunk = json.loads(chunks[2])
|
| 69 |
+
self.assertEqual(last_chunk['progress'], 100)
|
| 70 |
+
self.assertEqual(last_chunk['result']['latest'], "url1")
|
| 71 |
+
|
| 72 |
+
# Verify mock call arguments
|
| 73 |
+
mock_pipeline.assert_called_once()
|
| 74 |
+
kwargs = mock_pipeline.call_args[1]
|
| 75 |
+
self.assertEqual(kwargs['coords']['north'], 30.5)
|
| 76 |
+
self.assertEqual(kwargs['apply_buffer'], True)
|
| 77 |
+
|
| 78 |
+
if __name__ == '__main__':
|
| 79 |
+
unittest.main()
|
tests/test_health.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import unittest
|
| 2 |
+
from unittest.mock import patch, MagicMock
|
| 3 |
+
import sys
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# Add backend directory to sys.path to enable imports
|
| 7 |
+
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
| 8 |
+
|
| 9 |
+
class TestHealth(unittest.TestCase):
|
| 10 |
+
@classmethod
|
| 11 |
+
def setUpClass(cls):
|
| 12 |
+
# Patch initialization dependencies to execute tests offline
|
| 13 |
+
cls.gee_patch = patch('src.database.gee.initialize_earth_engine')
|
| 14 |
+
cls.mongo_patch = patch('src.database.mongo.initialize_mongodb')
|
| 15 |
+
cls.model_patch = patch('src.services.model.initialize_model')
|
| 16 |
+
|
| 17 |
+
cls.gee_mock = cls.gee_patch.start()
|
| 18 |
+
cls.mongo_mock = cls.mongo_patch.start()
|
| 19 |
+
cls.model_mock = cls.model_patch.start()
|
| 20 |
+
|
| 21 |
+
cls.mongo_mock.return_value = (MagicMock(), MagicMock())
|
| 22 |
+
cls.model_mock.return_value = MagicMock()
|
| 23 |
+
|
| 24 |
+
from src.app import create_app
|
| 25 |
+
cls.app = create_app()
|
| 26 |
+
cls.client = cls.app.test_client()
|
| 27 |
+
|
| 28 |
+
@classmethod
|
| 29 |
+
def tearDownClass(cls):
|
| 30 |
+
cls.gee_patch.stop()
|
| 31 |
+
cls.mongo_patch.stop()
|
| 32 |
+
cls.model_patch.stop()
|
| 33 |
+
|
| 34 |
+
def test_health_check(self):
|
| 35 |
+
response = self.client.get('/')
|
| 36 |
+
self.assertEqual(response.status_code, 200)
|
| 37 |
+
self.assertEqual(response.data.decode('utf-8'), "✅ SatVision Backend is alive!")
|
| 38 |
+
|
| 39 |
+
if __name__ == '__main__':
|
| 40 |
+
unittest.main()
|