76 lines
2.3 KiB
Python
76 lines
2.3 KiB
Python
"""Tests for configuration loading and validation."""
|
|
|
|
import pytest
|
|
|
|
from src.config import load_config, setup_logging
|
|
|
|
|
|
class TestLoadConfig:
|
|
def test_load_valid_config(self, config_file):
|
|
config = load_config(config_file)
|
|
assert config.targets.subreddits[0].name == "pics"
|
|
assert config.targets.subreddits[0].limit == 25
|
|
assert config.targets.users[0].name == "testuser"
|
|
assert config.download.min_score == 10
|
|
assert config.download.skip_nsfw is True
|
|
assert config.rate_limit.requests_per_minute == 10
|
|
|
|
def test_missing_config_raises(self):
|
|
with pytest.raises(FileNotFoundError):
|
|
load_config("nonexistent.yaml")
|
|
|
|
def test_empty_targets_raises(self, tmp_path):
|
|
config_path = tmp_path / "empty.yaml"
|
|
config_path.write_text("targets:\n subreddits: []\n users: []\n")
|
|
with pytest.raises(ValueError, match="No targets configured"):
|
|
load_config(str(config_path))
|
|
|
|
def test_blacklist_lowercase(self, tmp_path):
|
|
config_path = tmp_path / "config.yaml"
|
|
config_path.write_text("""
|
|
targets:
|
|
subreddits:
|
|
- name: "test"
|
|
limit: 10
|
|
download: {}
|
|
rate_limit: {}
|
|
logging: {}
|
|
blacklist:
|
|
authors:
|
|
- "SpAmMeR"
|
|
subreddits:
|
|
- "BadSub"
|
|
title_keywords:
|
|
- "BUY NOW"
|
|
domains:
|
|
- "Evil.COM"
|
|
""")
|
|
config = load_config(str(config_path))
|
|
assert config.blacklist.authors == ["spammer"]
|
|
assert config.blacklist.subreddits == ["badsub"]
|
|
assert config.blacklist.title_keywords == ["buy now"]
|
|
assert config.blacklist.domains == ["evil.com"]
|
|
|
|
def test_default_values(self, tmp_path):
|
|
config_path = tmp_path / "minimal.yaml"
|
|
config_path.write_text("""
|
|
targets:
|
|
subreddits:
|
|
- name: "test"
|
|
""")
|
|
config = load_config(str(config_path))
|
|
assert config.download.output_dir == "./downloads"
|
|
assert config.download.min_score == 10
|
|
assert config.rate_limit.requests_per_minute == 10
|
|
|
|
|
|
class TestSetupLogging:
|
|
def test_creates_logger(self, sample_config):
|
|
logger = setup_logging(sample_config.logging)
|
|
assert logger.name == "reddit_collector"
|
|
|
|
def test_log_level(self, sample_config):
|
|
import logging
|
|
|
|
logger = setup_logging(sample_config.logging)
|
|
assert logger.level == logging.DEBUG
|