160 lines
5.3 KiB
Python
160 lines
5.3 KiB
Python
"""Unit tests for Config class"""
|
|
|
|
import unittest
|
|
import tempfile
|
|
import os
|
|
from pathlib import Path
|
|
from unittest.mock import patch, MagicMock
|
|
|
|
# Add src to path
|
|
import sys
|
|
sys.path.insert(0, str(Path(__file__).parent.parent / 'src'))
|
|
|
|
from src.connector.config import Config
|
|
|
|
|
|
class TestConfig(unittest.TestCase):
|
|
"""Test cases for Config class"""
|
|
|
|
def setUp(self):
|
|
"""Set up test fixtures"""
|
|
self.temp_dir = tempfile.mkdtemp()
|
|
self.temp_path = Path(self.temp_dir)
|
|
|
|
def tearDown(self):
|
|
"""Clean up test fixtures"""
|
|
import shutil
|
|
if self.temp_path.exists():
|
|
shutil.rmtree(self.temp_path)
|
|
|
|
@patch.dict(os.environ, {
|
|
'FLUX_API_KEY': 'test_api_key_12345',
|
|
'LOG_LEVEL': 'DEBUG',
|
|
'MAX_IMAGE_SIZE_MB': '25',
|
|
'DEFAULT_TIMEOUT': '600'
|
|
})
|
|
def test_config_initialization(self):
|
|
"""Test config initialization with environment variables"""
|
|
config = Config()
|
|
|
|
self.assertEqual(config.api_key, 'test_api_key_12345')
|
|
self.assertEqual(config.log_level, 'DEBUG')
|
|
self.assertEqual(config.max_image_size_mb, 25)
|
|
self.assertEqual(config.default_timeout, 600)
|
|
|
|
def test_config_defaults(self):
|
|
"""Test config defaults when env vars not set"""
|
|
# Clear environment
|
|
env_vars_to_clear = [
|
|
'FLUX_API_KEY', 'LOG_LEVEL', 'MAX_IMAGE_SIZE_MB',
|
|
'DEFAULT_TIMEOUT', 'POLLING_INTERVAL', 'MAX_POLLING_ATTEMPTS'
|
|
]
|
|
|
|
with patch.dict(os.environ, {}, clear=True):
|
|
config = Config()
|
|
|
|
self.assertEqual(config.api_key, '')
|
|
self.assertEqual(config.log_level, 'INFO')
|
|
self.assertEqual(config.max_image_size_mb, 20)
|
|
self.assertEqual(config.default_timeout, 300)
|
|
self.assertEqual(config.polling_interval, 2)
|
|
self.assertEqual(config.max_polling_attempts, 150)
|
|
|
|
def test_api_url_generation(self):
|
|
"""Test API URL generation"""
|
|
config = Config()
|
|
|
|
edit_url = config.get_api_url(config.EDIT_ENDPOINT)
|
|
result_url = config.get_api_url(config.RESULT_ENDPOINT)
|
|
|
|
expected_edit = f"{config.api_base_url}/flux-kontext-pro"
|
|
expected_result = f"{config.api_base_url}/v1/get_result"
|
|
|
|
self.assertEqual(edit_url, expected_edit)
|
|
self.assertEqual(result_url, expected_result)
|
|
|
|
def test_filename_generation(self):
|
|
"""Test filename generation"""
|
|
config = Config()
|
|
|
|
base_name = "fluxedit_123456_20250826_143022"
|
|
|
|
# Test different file numbers and extensions
|
|
filename_000 = config.generate_filename(base_name, 0, 'png')
|
|
filename_001 = config.generate_filename(base_name, 1, 'png')
|
|
filename_json = config.generate_filename(base_name, 1, 'json')
|
|
|
|
self.assertEqual(filename_000, "fluxedit_123456_20250826_143022_000.png")
|
|
self.assertEqual(filename_001, "fluxedit_123456_20250826_143022_001.png")
|
|
self.assertEqual(filename_json, "fluxedit_123456_20250826_143022_001.json")
|
|
|
|
def test_base_name_generation(self):
|
|
"""Test base name generation"""
|
|
config = Config()
|
|
|
|
# Test with seed
|
|
base_name_with_seed = config.generate_base_name(12345)
|
|
self.assertIn("fluxedit_12345_", base_name_with_seed)
|
|
|
|
# Test simple generation
|
|
base_name_simple = config.generate_base_name_simple()
|
|
self.assertIn("fluxedit_", base_name_simple)
|
|
self.assertNotIn("_12345_", base_name_simple) # No seed in simple
|
|
|
|
@patch.dict(os.environ, {
|
|
'FLUX_API_KEY': 'valid_key',
|
|
'MAX_IMAGE_SIZE_MB': '20',
|
|
'DEFAULT_TIMEOUT': '300'
|
|
})
|
|
def test_validation_success(self):
|
|
"""Test successful validation"""
|
|
config = Config()
|
|
self.assertTrue(config.validate())
|
|
|
|
def test_validation_failures(self):
|
|
"""Test validation failures"""
|
|
# Test missing API key
|
|
with patch.dict(os.environ, {'FLUX_API_KEY': ''}, clear=True):
|
|
config = Config()
|
|
self.assertFalse(config.validate())
|
|
|
|
# Test invalid image size
|
|
with patch.dict(os.environ, {
|
|
'FLUX_API_KEY': 'valid_key',
|
|
'MAX_IMAGE_SIZE_MB': '0'
|
|
}, clear=True):
|
|
config = Config()
|
|
self.assertFalse(config.validate())
|
|
|
|
# Test invalid timeout
|
|
with patch.dict(os.environ, {
|
|
'FLUX_API_KEY': 'valid_key',
|
|
'DEFAULT_TIMEOUT': '-1'
|
|
}, clear=True):
|
|
config = Config()
|
|
self.assertFalse(config.validate())
|
|
|
|
def test_max_image_size_bytes(self):
|
|
"""Test max image size in bytes calculation"""
|
|
config = Config()
|
|
config.max_image_size_mb = 20
|
|
|
|
expected_bytes = 20 * 1024 * 1024
|
|
self.assertEqual(config.get_max_image_size_bytes(), expected_bytes)
|
|
|
|
@patch('pathlib.Path.mkdir')
|
|
@patch('pathlib.Path.exists')
|
|
def test_directory_creation(self, mock_exists, mock_mkdir):
|
|
"""Test directory creation logic"""
|
|
mock_exists.return_value = True
|
|
|
|
# This should not raise an exception
|
|
config = Config()
|
|
|
|
# Verify mkdir was called
|
|
mock_mkdir.assert_called()
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|