migrate to gitea
This commit is contained in:
141
tests/test_config.py
Normal file
141
tests/test_config.py
Normal file
@@ -0,0 +1,141 @@
|
||||
"""
|
||||
Tests for configuration module
|
||||
"""
|
||||
|
||||
import unittest
|
||||
import tempfile
|
||||
import os
|
||||
import yaml
|
||||
from unittest.mock import patch
|
||||
from config import Config
|
||||
|
||||
|
||||
class TestConfig(unittest.TestCase):
|
||||
"""Test cases for Config class"""
|
||||
|
||||
def setUp(self):
|
||||
"""Set up test fixtures"""
|
||||
self.test_config_data = {
|
||||
'llm': {
|
||||
'base_url': 'http://localhost:1234',
|
||||
'api_key': 'test-key',
|
||||
'model': 'test-model'
|
||||
},
|
||||
'android': {
|
||||
'input_folder': 'app/src/main/res',
|
||||
'base_values_folder': 'values',
|
||||
'target_folders': ['values-de-rDE'],
|
||||
'files_to_translate': ['strings.xml']
|
||||
},
|
||||
'translation': {
|
||||
'batch_size': 5,
|
||||
'interactive_approval': True
|
||||
}
|
||||
}
|
||||
|
||||
def test_load_valid_config(self):
|
||||
"""Test loading a valid configuration file"""
|
||||
with tempfile.NamedTemporaryFile(mode='w', suffix='.yaml', delete=False) as f:
|
||||
yaml.dump(self.test_config_data, f)
|
||||
temp_path = f.name
|
||||
|
||||
try:
|
||||
config = Config(temp_path)
|
||||
self.assertEqual(config.llm_config['base_url'], 'http://localhost:1234')
|
||||
self.assertEqual(config.android_config['input_folder'], 'app/src/main/res')
|
||||
self.assertEqual(config.translation_config['batch_size'], 5)
|
||||
finally:
|
||||
os.unlink(temp_path)
|
||||
|
||||
def test_missing_file_error(self):
|
||||
"""Test error handling for missing configuration file"""
|
||||
with patch('sys.exit') as mock_exit:
|
||||
Config('nonexistent.yaml')
|
||||
mock_exit.assert_called_once_with(1)
|
||||
|
||||
def test_missing_required_section(self):
|
||||
"""Test error handling for missing required sections"""
|
||||
incomplete_data = {'llm': self.test_config_data['llm']} # Missing android and translation
|
||||
|
||||
with tempfile.NamedTemporaryFile(mode='w', suffix='.yaml', delete=False) as f:
|
||||
yaml.dump(incomplete_data, f)
|
||||
temp_path = f.name
|
||||
|
||||
try:
|
||||
with patch('sys.exit') as mock_exit:
|
||||
Config(temp_path)
|
||||
mock_exit.assert_called_with(1) # Remove assert_called_once
|
||||
finally:
|
||||
os.unlink(temp_path)
|
||||
|
||||
def test_output_config_default(self):
|
||||
"""Test default output configuration"""
|
||||
with tempfile.NamedTemporaryFile(mode='w', suffix='.yaml', delete=False) as f:
|
||||
yaml.dump(self.test_config_data, f)
|
||||
temp_path = f.name
|
||||
|
||||
try:
|
||||
config = Config(temp_path)
|
||||
output_config = config.output_config
|
||||
self.assertEqual(output_config, {}) # Should be empty dict when not specified
|
||||
finally:
|
||||
os.unlink(temp_path)
|
||||
|
||||
def test_examples_config_missing(self):
|
||||
"""Test examples config when not specified"""
|
||||
with tempfile.NamedTemporaryFile(mode='w', suffix='.yaml', delete=False) as f:
|
||||
yaml.dump(self.test_config_data, f)
|
||||
temp_path = f.name
|
||||
|
||||
try:
|
||||
config = Config(temp_path)
|
||||
self.assertFalse(config.has_examples_config())
|
||||
self.assertEqual(config.examples_config, {})
|
||||
finally:
|
||||
os.unlink(temp_path)
|
||||
|
||||
def test_examples_config_present(self):
|
||||
"""Test examples config when specified"""
|
||||
config_with_examples = self.test_config_data.copy()
|
||||
config_with_examples['examples'] = {
|
||||
'input_folder': 'examples',
|
||||
'base_folder': 'assets/hints',
|
||||
'target_folders': ['assets/hints-de-rDE'],
|
||||
'file_extension': '.md'
|
||||
}
|
||||
|
||||
with tempfile.NamedTemporaryFile(mode='w', suffix='.yaml', delete=False) as f:
|
||||
yaml.dump(config_with_examples, f)
|
||||
temp_path = f.name
|
||||
|
||||
try:
|
||||
config = Config(temp_path)
|
||||
self.assertTrue(config.has_examples_config())
|
||||
self.assertEqual(config.examples_config['input_folder'], 'examples')
|
||||
self.assertEqual(config.examples_config['base_folder'], 'assets/hints')
|
||||
self.assertEqual(config.examples_config['target_folders'], ['assets/hints-de-rDE'])
|
||||
self.assertEqual(config.examples_config['file_extension'], '.md')
|
||||
finally:
|
||||
os.unlink(temp_path)
|
||||
|
||||
def test_examples_config_incomplete(self):
|
||||
"""Test has_examples_config returns False for incomplete config"""
|
||||
config_with_examples = self.test_config_data.copy()
|
||||
config_with_examples['examples'] = {
|
||||
'input_folder': 'examples',
|
||||
# Missing base_folder and target_folders
|
||||
}
|
||||
|
||||
with tempfile.NamedTemporaryFile(mode='w', suffix='.yaml', delete=False) as f:
|
||||
yaml.dump(config_with_examples, f)
|
||||
temp_path = f.name
|
||||
|
||||
try:
|
||||
config = Config(temp_path)
|
||||
self.assertFalse(config.has_examples_config())
|
||||
finally:
|
||||
os.unlink(temp_path)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user