"""Tests for config validation.""" import pytest from config import ( AppConfig, BatchSettings, Environment, MySQLSettings, SAPSettings, validate_config, ) def _make_config() -> AppConfig: """Build a default (dev) AppConfig instance.""" cfg = AppConfig() cfg.env = Environment.DEV return cfg def test_validate_config_passes_for_defaults() -> None: """Test that the default dev configuration is valid.""" validate_config(_make_config()) def test_validate_config_rejects_batch_size_out_of_range() -> None: """Test that MAX_BATCH_SIZE outside [50, 500] fails validation.""" cfg = _make_config() cfg.batch = BatchSettings(MAX_BATCH_SIZE=49) with pytest.raises(ValueError, match='MAX_BATCH_SIZE'): validate_config(cfg) cfg.batch = BatchSettings(MAX_BATCH_SIZE=501) with pytest.raises(ValueError, match='MAX_BATCH_SIZE'): validate_config(cfg) def test_validate_config_rejects_stale_sync_minutes_out_of_range() -> None: """Test that STALE_SYNC_MINUTES outside [1, 240] fails validation.""" cfg = _make_config() cfg.batch = BatchSettings(STALE_SYNC_MINUTES=0) with pytest.raises(ValueError, match='STALE_SYNC_MINUTES'): validate_config(cfg) cfg.batch = BatchSettings(STALE_SYNC_MINUTES=241) with pytest.raises(ValueError, match='STALE_SYNC_MINUTES'): validate_config(cfg) def test_validate_config_rejects_non_https_sap_api() -> None: """Test that a non-https SAP API URL fails validation.""" cfg = _make_config() cfg.sap = SAPSettings(API='http://connect-tsme.sonymusic.com') with pytest.raises(ValueError, match='sap.API'): validate_config(cfg) def test_validate_config_requires_mysql_credentials_in_managed_env() -> None: """Test that managed environments require MySQL host, user, and password.""" cfg = _make_config() cfg.env = Environment.QA cfg.mysql = MySQLSettings(host='', user='', password='') with pytest.raises(ValueError) as exc_info: validate_config(cfg) message = str(exc_info.value) assert 'mysql.host' in message assert 'mysql.user' in message assert 'mysql.password' in message def test_validate_config_collects_multiple_errors() -> None: """Test that all validation errors are reported together.""" cfg = _make_config() cfg.batch = BatchSettings(MAX_BATCH_SIZE=0, STALE_SYNC_MINUTES=0) with pytest.raises(ValueError) as exc_info: validate_config(cfg) message = str(exc_info.value) assert 'MAX_BATCH_SIZE' in message assert 'STALE_SYNC_MINUTES' in message