Add unknown_whitelist_manager/tests/test_core.py
This commit is contained in:
parent
9b99ca42f4
commit
27179d7b65
1 changed files with 60 additions and 0 deletions
60
unknown_whitelist_manager/tests/test_core.py
Normal file
60
unknown_whitelist_manager/tests/test_core.py
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
import json
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from unknown_whitelist_manager.core import update_whitelist
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def temp_whitelist_file():
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
path = Path(tmpdir) / "whitelist.json"
|
||||
# initial whitelist with one entry
|
||||
initial_data = [{"entry": "bereits bekannter Fehler"}]
|
||||
path.write_text(json.dumps(initial_data, ensure_ascii=False))
|
||||
yield path
|
||||
|
||||
|
||||
def test_add_new_entry(temp_whitelist_file):
|
||||
new_entry = {"entry": "fehlendes Output-File"}
|
||||
|
||||
update_whitelist(str(temp_whitelist_file), new_entry)
|
||||
|
||||
with open(temp_whitelist_file, encoding="utf-8") as f:
|
||||
data = json.load(f)
|
||||
|
||||
assert any(e["entry"] == "fehlendes Output-File" for e in data), "Neuer Eintrag sollte hinzugefügt werden"
|
||||
assert len(data) == 2, "Whitelist sollte nun zwei Einträge enthalten"
|
||||
|
||||
|
||||
def test_add_duplicate_entry_does_not_duplicate(temp_whitelist_file):
|
||||
duplicate_entry = {"entry": "bereits bekannter Fehler"}
|
||||
|
||||
update_whitelist(str(temp_whitelist_file), duplicate_entry)
|
||||
|
||||
with open(temp_whitelist_file, encoding="utf-8") as f:
|
||||
data = json.load(f)
|
||||
|
||||
entries = [e["entry"] for e in data]
|
||||
assert entries.count("bereits bekannter Fehler") == 1, "Doppelte Einträge dürfen nicht entstehen"
|
||||
|
||||
|
||||
def test_invalid_file_path_raises(tmp_path):
|
||||
non_existing = tmp_path / "non_existing.json"
|
||||
new_entry = {"entry": "fehlerhafte Datei"}
|
||||
|
||||
# file should be created with this new entry (graceful default)
|
||||
update_whitelist(str(non_existing), new_entry)
|
||||
|
||||
with open(non_existing, encoding="utf-8") as f:
|
||||
content = json.load(f)
|
||||
assert content == [new_entry], "Neue Datei sollte korrekt erstellt werden"
|
||||
|
||||
|
||||
def test_input_validation_requires_entry_field(temp_whitelist_file):
|
||||
invalid_entry = {"wrong_field": "oops"}
|
||||
|
||||
with pytest.raises((AssertionError, KeyError, ValueError)):
|
||||
update_whitelist(str(temp_whitelist_file), invalid_entry)
|
||||
Loading…
Reference in a new issue