File size: 2,336 Bytes
6012c2e | 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 | import unittest
from unittest.mock import patch, MagicMock
import sys
import os
# Add backend directory to sys.path to enable imports
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
class TestAssets(unittest.TestCase):
@classmethod
def setUpClass(cls):
# Patch dependencies for offline testing
cls.gee_patch = patch('src.database.gee.initialize_earth_engine')
cls.mongo_patch = patch('src.database.mongo.initialize_mongodb')
cls.model_patch = patch('src.services.model.initialize_model')
cls.gee_mock = cls.gee_patch.start()
cls.mongo_mock = cls.mongo_patch.start()
cls.model_mock = cls.model_patch.start()
cls.mongo_mock.return_value = (MagicMock(), MagicMock())
cls.model_mock.return_value = MagicMock()
from src.app import create_app
cls.app = create_app()
cls.client = cls.app.test_client()
@classmethod
def tearDownClass(cls):
cls.gee_patch.stop()
cls.mongo_patch.stop()
cls.model_patch.stop()
def test_serve_missing_mask_returns_404(self):
# Mock os.path.exists to return False to simulate missing files
with patch('os.path.exists') as mock_exists:
mock_exists.return_value = False
response = self.client.get('/mask/nonexistent_file.png')
self.assertEqual(response.status_code, 404)
def test_serve_existing_mask_calls_send_from_directory(self):
# Mock os.path.exists to True and intercept send_from_directory
with patch('os.path.exists') as mock_exists, \
patch('src.routes.assets.send_from_directory') as mock_send:
mock_exists.return_value = True
mock_send.return_value = "mock_file_content"
response = self.client.get('/mask/session123/existing_file.png')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data.decode('utf-8'), "mock_file_content")
mock_send.assert_called_once()
args = mock_send.call_args[0]
self.assertTrue(args[0].endswith(os.path.join('server_downloads', 'session123')))
self.assertEqual(args[1], 'existing_file.png')
if __name__ == '__main__':
unittest.main()
|