Video-Summary/tests/test_config.py

66 lines
2.1 KiB
Python

"""Tests for Config module."""
import json
import os
import tempfile
import pytest
from Application.Config import Config
class TestConfig:
"""Test suite for Config class."""
def test_default_config(self):
"""Test that default config is loaded when no file provided."""
config = Config(None)
assert config["min_area"] == 300
assert config["max_area"] == 900000
assert config["threshold"] == 7
def test_load_config_from_file(self):
"""Test loading config from a JSON file."""
test_config = {"min_area": 500, "max_area": 1000000, "threshold": 10}
with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as f:
json.dump(test_config, f)
temp_path = f.name
try:
config = Config(temp_path)
assert config["min_area"] == 500
assert config["max_area"] == 1000000
assert config["threshold"] == 10
finally:
os.unlink(temp_path)
def test_config_with_invalid_file(self):
"""Test that default config is used when file doesn't exist."""
config = Config("/nonexistent/path/config.json")
assert config["min_area"] == 300 # Should use defaults
def test_config_getitem(self):
"""Test __getitem__ method."""
config = Config(None)
assert config["min_area"] is not None
assert config["nonexistent_key"] is None
def test_config_setitem(self):
"""Test __setitem__ method."""
config = Config(None)
config["new_key"] = "new_value"
assert config["new_key"] == "new_value"
def test_config_with_malformed_json(self):
"""Test handling of malformed JSON file."""
with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as f:
f.write("{invalid json content")
temp_path = f.name
try:
config = Config(temp_path)
# Should fall back to defaults
assert config["min_area"] == 300
finally:
os.unlink(temp_path)