253 lines
8.0 KiB
Python
253 lines
8.0 KiB
Python
|
|
import pytest
|
||
|
|
import json
|
||
|
|
from pathlib import Path
|
||
|
|
from src.core.config import load_config, save_config, default_config
|
||
|
|
|
||
|
|
|
||
|
|
class TestConfig:
|
||
|
|
"""Testy pro config modul"""
|
||
|
|
|
||
|
|
@pytest.fixture
|
||
|
|
def temp_config_file(self, tmp_path, monkeypatch):
|
||
|
|
"""Fixture pro dočasný config soubor"""
|
||
|
|
config_path = tmp_path / "test_config.json"
|
||
|
|
# Změníme CONFIG_FILE v modulu config
|
||
|
|
import src.core.config as config_module
|
||
|
|
monkeypatch.setattr(config_module, 'CONFIG_FILE', config_path)
|
||
|
|
return config_path
|
||
|
|
|
||
|
|
def test_default_config_structure(self):
|
||
|
|
"""Test struktury defaultní konfigurace"""
|
||
|
|
assert "ignore_patterns" in default_config
|
||
|
|
assert "last_folder" in default_config
|
||
|
|
assert isinstance(default_config["ignore_patterns"], list)
|
||
|
|
assert default_config["last_folder"] is None
|
||
|
|
|
||
|
|
def test_load_config_nonexistent_file(self, temp_config_file):
|
||
|
|
"""Test načtení konfigurace když soubor neexistuje"""
|
||
|
|
config = load_config()
|
||
|
|
|
||
|
|
assert config == default_config
|
||
|
|
assert config["ignore_patterns"] == []
|
||
|
|
assert config["last_folder"] is None
|
||
|
|
|
||
|
|
def test_save_config(self, temp_config_file):
|
||
|
|
"""Test uložení konfigurace"""
|
||
|
|
test_config = {
|
||
|
|
"ignore_patterns": ["*.tmp", "*.log"],
|
||
|
|
"last_folder": "/home/user/documents"
|
||
|
|
}
|
||
|
|
|
||
|
|
save_config(test_config)
|
||
|
|
|
||
|
|
# Kontrola že soubor existuje
|
||
|
|
assert temp_config_file.exists()
|
||
|
|
|
||
|
|
# Kontrola obsahu
|
||
|
|
with open(temp_config_file, "r", encoding="utf-8") as f:
|
||
|
|
saved_data = json.load(f)
|
||
|
|
|
||
|
|
assert saved_data == test_config
|
||
|
|
|
||
|
|
def test_load_config_existing_file(self, temp_config_file):
|
||
|
|
"""Test načtení existující konfigurace"""
|
||
|
|
test_config = {
|
||
|
|
"ignore_patterns": ["*.tmp"],
|
||
|
|
"last_folder": "/test/path"
|
||
|
|
}
|
||
|
|
|
||
|
|
# Uložení
|
||
|
|
save_config(test_config)
|
||
|
|
|
||
|
|
# Načtení
|
||
|
|
loaded_config = load_config()
|
||
|
|
|
||
|
|
assert loaded_config == test_config
|
||
|
|
assert loaded_config["ignore_patterns"] == ["*.tmp"]
|
||
|
|
assert loaded_config["last_folder"] == "/test/path"
|
||
|
|
|
||
|
|
def test_save_and_load_config_cycle(self, temp_config_file):
|
||
|
|
"""Test cyklu uložení a načtení"""
|
||
|
|
original_config = {
|
||
|
|
"ignore_patterns": ["*.jpg", "*.png", "*.gif"],
|
||
|
|
"last_folder": "/home/user/pictures"
|
||
|
|
}
|
||
|
|
|
||
|
|
save_config(original_config)
|
||
|
|
loaded_config = load_config()
|
||
|
|
|
||
|
|
assert loaded_config == original_config
|
||
|
|
|
||
|
|
def test_config_json_format(self, temp_config_file):
|
||
|
|
"""Test že config je uložen ve správném JSON formátu"""
|
||
|
|
test_config = {
|
||
|
|
"ignore_patterns": ["*.tmp"],
|
||
|
|
"last_folder": "/test"
|
||
|
|
}
|
||
|
|
|
||
|
|
save_config(test_config)
|
||
|
|
|
||
|
|
# Kontrola formátování
|
||
|
|
with open(temp_config_file, "r", encoding="utf-8") as f:
|
||
|
|
content = f.read()
|
||
|
|
|
||
|
|
# Mělo by být naformátováno s indentací
|
||
|
|
assert " " in content # 2 mezery jako indent
|
||
|
|
|
||
|
|
def test_config_utf8_encoding(self, temp_config_file):
|
||
|
|
"""Test UTF-8 encoding s českými znaky"""
|
||
|
|
test_config = {
|
||
|
|
"ignore_patterns": ["*.čeština"],
|
||
|
|
"last_folder": "/cesta/s/čestnými/znaky"
|
||
|
|
}
|
||
|
|
|
||
|
|
save_config(test_config)
|
||
|
|
loaded_config = load_config()
|
||
|
|
|
||
|
|
assert loaded_config == test_config
|
||
|
|
assert loaded_config["last_folder"] == "/cesta/s/čestnými/znaky"
|
||
|
|
|
||
|
|
def test_config_empty_ignore_patterns(self, temp_config_file):
|
||
|
|
"""Test s prázdným seznamem ignore_patterns"""
|
||
|
|
test_config = {
|
||
|
|
"ignore_patterns": [],
|
||
|
|
"last_folder": "/test"
|
||
|
|
}
|
||
|
|
|
||
|
|
save_config(test_config)
|
||
|
|
loaded_config = load_config()
|
||
|
|
|
||
|
|
assert loaded_config["ignore_patterns"] == []
|
||
|
|
|
||
|
|
def test_config_null_last_folder(self, temp_config_file):
|
||
|
|
"""Test s None hodnotou pro last_folder"""
|
||
|
|
test_config = {
|
||
|
|
"ignore_patterns": ["*.tmp"],
|
||
|
|
"last_folder": None
|
||
|
|
}
|
||
|
|
|
||
|
|
save_config(test_config)
|
||
|
|
loaded_config = load_config()
|
||
|
|
|
||
|
|
assert loaded_config["last_folder"] is None
|
||
|
|
|
||
|
|
def test_config_multiple_ignore_patterns(self, temp_config_file):
|
||
|
|
"""Test s více ignore patterny"""
|
||
|
|
patterns = ["*.tmp", "*.log", "*.cache", "*/node_modules/*", "*.pyc"]
|
||
|
|
test_config = {
|
||
|
|
"ignore_patterns": patterns,
|
||
|
|
"last_folder": "/test"
|
||
|
|
}
|
||
|
|
|
||
|
|
save_config(test_config)
|
||
|
|
loaded_config = load_config()
|
||
|
|
|
||
|
|
assert loaded_config["ignore_patterns"] == patterns
|
||
|
|
assert len(loaded_config["ignore_patterns"]) == 5
|
||
|
|
|
||
|
|
def test_config_special_characters_in_patterns(self, temp_config_file):
|
||
|
|
"""Test se speciálními znaky v patterns"""
|
||
|
|
test_config = {
|
||
|
|
"ignore_patterns": ["*.tmp", "file[0-9].txt", "test?.log"],
|
||
|
|
"last_folder": "/test"
|
||
|
|
}
|
||
|
|
|
||
|
|
save_config(test_config)
|
||
|
|
loaded_config = load_config()
|
||
|
|
|
||
|
|
assert loaded_config["ignore_patterns"] == test_config["ignore_patterns"]
|
||
|
|
|
||
|
|
def test_load_config_corrupted_file(self, temp_config_file):
|
||
|
|
"""Test načtení poškozeného config souboru"""
|
||
|
|
# Vytvoření poškozeného JSON
|
||
|
|
with open(temp_config_file, "w") as f:
|
||
|
|
f.write("{ invalid json }")
|
||
|
|
|
||
|
|
# Mělo by vrátit default config
|
||
|
|
config = load_config()
|
||
|
|
assert config == default_config
|
||
|
|
|
||
|
|
def test_load_config_returns_new_dict(self, temp_config_file):
|
||
|
|
"""Test že load_config vrací nový dictionary (ne stejnou referenci)"""
|
||
|
|
config1 = load_config()
|
||
|
|
config2 = load_config()
|
||
|
|
|
||
|
|
# Měly by to být různé objekty (ne stejná reference)
|
||
|
|
assert config1 is not config2
|
||
|
|
|
||
|
|
# Ale hodnoty by měly být stejné
|
||
|
|
assert config1 == config2
|
||
|
|
|
||
|
|
def test_config_overwrite(self, temp_config_file):
|
||
|
|
"""Test přepsání existující konfigurace"""
|
||
|
|
config1 = {
|
||
|
|
"ignore_patterns": ["*.tmp"],
|
||
|
|
"last_folder": "/path1"
|
||
|
|
}
|
||
|
|
|
||
|
|
config2 = {
|
||
|
|
"ignore_patterns": ["*.log"],
|
||
|
|
"last_folder": "/path2"
|
||
|
|
}
|
||
|
|
|
||
|
|
save_config(config1)
|
||
|
|
save_config(config2)
|
||
|
|
|
||
|
|
loaded = load_config()
|
||
|
|
assert loaded == config2
|
||
|
|
|
||
|
|
def test_config_path_with_spaces(self, temp_config_file):
|
||
|
|
"""Test s cestou obsahující mezery"""
|
||
|
|
test_config = {
|
||
|
|
"ignore_patterns": [],
|
||
|
|
"last_folder": "/path/with spaces/in name"
|
||
|
|
}
|
||
|
|
|
||
|
|
save_config(test_config)
|
||
|
|
loaded_config = load_config()
|
||
|
|
|
||
|
|
assert loaded_config["last_folder"] == "/path/with spaces/in name"
|
||
|
|
|
||
|
|
def test_config_long_path(self, temp_config_file):
|
||
|
|
"""Test s dlouhou cestou"""
|
||
|
|
long_path = "/very/long/path/" + "subdir/" * 50 + "final"
|
||
|
|
test_config = {
|
||
|
|
"ignore_patterns": [],
|
||
|
|
"last_folder": long_path
|
||
|
|
}
|
||
|
|
|
||
|
|
save_config(test_config)
|
||
|
|
loaded_config = load_config()
|
||
|
|
|
||
|
|
assert loaded_config["last_folder"] == long_path
|
||
|
|
|
||
|
|
def test_config_many_patterns(self, temp_config_file):
|
||
|
|
"""Test s velkým počtem patterns"""
|
||
|
|
patterns = [f"*.ext{i}" for i in range(100)]
|
||
|
|
test_config = {
|
||
|
|
"ignore_patterns": patterns,
|
||
|
|
"last_folder": "/test"
|
||
|
|
}
|
||
|
|
|
||
|
|
save_config(test_config)
|
||
|
|
loaded_config = load_config()
|
||
|
|
|
||
|
|
assert len(loaded_config["ignore_patterns"]) == 100
|
||
|
|
assert loaded_config["ignore_patterns"] == patterns
|
||
|
|
|
||
|
|
def test_config_ensure_ascii_false(self, temp_config_file):
|
||
|
|
"""Test že ensure_ascii=False funguje správně"""
|
||
|
|
test_config = {
|
||
|
|
"ignore_patterns": ["čeština", "русский", "中文"],
|
||
|
|
"last_folder": "/cesta/čeština"
|
||
|
|
}
|
||
|
|
|
||
|
|
save_config(test_config)
|
||
|
|
|
||
|
|
# Kontrola že znaky nejsou escapovány
|
||
|
|
with open(temp_config_file, "r", encoding="utf-8") as f:
|
||
|
|
content = f.read()
|
||
|
|
|
||
|
|
assert "čeština" in content
|
||
|
|
assert "\\u" not in content # Nemělo by být escapováno
|