Add tests - part 2
This commit is contained in:
142
tests/test_config.py
Normal file
142
tests/test_config.py
Normal file
@@ -0,0 +1,142 @@
|
||||
"""Tests for configuration."""
|
||||
|
||||
import pytest
|
||||
|
||||
from i18n_guardian.config import Config, ConfigLoader, generate_default_config
|
||||
|
||||
|
||||
class TestConfig:
|
||||
"""Tests for Config class."""
|
||||
|
||||
def test_default_values(self):
|
||||
"""Test default configuration values."""
|
||||
config = Config()
|
||||
|
||||
assert config.i18n_library is None
|
||||
assert config.i18n_functions == []
|
||||
assert config.min_string_length == 3
|
||||
assert config.key_style == "snake_case"
|
||||
assert config.output_format == "text"
|
||||
assert config.fail_level == "error"
|
||||
|
||||
def test_from_dict(self):
|
||||
"""Test creating config from dictionary."""
|
||||
data = {
|
||||
"i18n_library": "react-intl",
|
||||
"min_string_length": 5,
|
||||
"key_style": "kebab-case",
|
||||
"i18n_functions": ["formatMessage", "t"],
|
||||
}
|
||||
|
||||
config = Config.from_dict(data)
|
||||
|
||||
assert config.i18n_library == "react-intl"
|
||||
assert config.min_string_length == 5
|
||||
assert config.key_style == "kebab-case"
|
||||
assert "formatMessage" in config.i18n_functions
|
||||
|
||||
def test_to_dict(self):
|
||||
"""Test converting config to dictionary."""
|
||||
config = Config(
|
||||
i18n_library="i18next",
|
||||
min_string_length=4,
|
||||
key_style="camelCase",
|
||||
)
|
||||
|
||||
data = config.to_dict()
|
||||
|
||||
assert data["i18n_library"] == "i18next"
|
||||
assert data["min_string_length"] == 4
|
||||
assert data["key_style"] == "camelCase"
|
||||
|
||||
def test_include_patterns(self):
|
||||
"""Test include patterns."""
|
||||
config = Config(
|
||||
include_patterns=["**/*.js", "**/*.ts", "**/*.jsx"],
|
||||
)
|
||||
|
||||
assert ".js" in config.include_patterns[0] or "*.js" in str(config.include_patterns)
|
||||
|
||||
def test_exclude_patterns(self):
|
||||
"""Test exclude patterns."""
|
||||
config = Config(
|
||||
exclude_patterns=["**/node_modules/**", "**/.git/**"],
|
||||
)
|
||||
|
||||
assert len(config.exclude_patterns) == 2
|
||||
|
||||
|
||||
class TestConfigLoader:
|
||||
"""Tests for ConfigLoader."""
|
||||
|
||||
def test_load_default_config(self, temp_dir):
|
||||
"""Test loading with no config file."""
|
||||
loader = ConfigLoader()
|
||||
config = loader.load(None, str(temp_dir))
|
||||
|
||||
assert config.path == str(temp_dir)
|
||||
assert isinstance(config, Config)
|
||||
|
||||
def test_load_from_file(self, sample_config_file):
|
||||
"""Test loading configuration from file."""
|
||||
loader = ConfigLoader()
|
||||
config = loader.load(str(sample_config_file), str(sample_config_file.parent))
|
||||
|
||||
assert config.i18n_library == "react-intl"
|
||||
assert config.min_string_length == 3
|
||||
|
||||
def test_load_invalid_yaml(self, temp_dir):
|
||||
"""Test handling of invalid YAML."""
|
||||
yaml_file = temp_dir / ".i18n-guardian.yaml"
|
||||
yaml_file.write_text("invalid: yaml: content: [", encoding="utf-8")
|
||||
|
||||
loader = ConfigLoader()
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
loader.load(str(yaml_file), str(temp_dir))
|
||||
|
||||
def test_save_config(self, temp_dir):
|
||||
"""Test saving configuration."""
|
||||
loader = ConfigLoader()
|
||||
config = Config(i18n_library="vue-i18n")
|
||||
output_path = temp_dir / "output.yaml"
|
||||
|
||||
loader.save(config, str(output_path))
|
||||
|
||||
assert output_path.exists()
|
||||
|
||||
def test_load_missing_file(self, temp_dir):
|
||||
"""Test loading when file doesn't exist."""
|
||||
loader = ConfigLoader()
|
||||
config = loader.load("/nonexistent/file.yaml", str(temp_dir))
|
||||
|
||||
assert config is not None
|
||||
|
||||
|
||||
class TestGenerateDefaultConfig:
|
||||
"""Tests for default config generation."""
|
||||
|
||||
def test_generate_default(self):
|
||||
"""Test generating default configuration."""
|
||||
config = generate_default_config()
|
||||
|
||||
assert "i18n_library" in config
|
||||
assert "min_string_length" in config
|
||||
assert "key_style" in config
|
||||
assert "exclude_patterns" in config
|
||||
assert isinstance(config["exclude_patterns"], list)
|
||||
|
||||
def test_default_exclude_patterns(self):
|
||||
"""Test default exclude patterns."""
|
||||
config = generate_default_config()
|
||||
|
||||
assert "**/node_modules/**" in config["exclude_patterns"]
|
||||
assert "**/.git/**" in config["exclude_patterns"]
|
||||
|
||||
def test_default_include_patterns(self):
|
||||
"""Test default include patterns."""
|
||||
config = generate_default_config()
|
||||
|
||||
assert "**/*.py" in config["include_patterns"]
|
||||
assert "**/*.js" in config["include_patterns"]
|
||||
assert "**/*.ts" in config["include_patterns"]
|
||||
Reference in New Issue
Block a user