import sqlite3 from pathlib import Path import pytest from mka.application.glossary import GlossaryConflictError, GlossaryRepository def repository(tmp_path: Path) -> GlossaryRepository: result = GlossaryRepository(tmp_path / "database" / "glossary.sqlite3") result.initialize() return result def test_initialization_creates_empty_versioned_database(tmp_path: Path) -> None: glossary = repository(tmp_path) assert glossary.database_path.is_file() assert glossary.list() == [] with sqlite3.connect(glossary.database_path) as connection: assert connection.execute("PRAGMA user_version").fetchone()[0] == 1 tables = { row[0] for row in connection.execute("SELECT name FROM sqlite_master WHERE type = 'table'") } assert {"glossary_entries", "glossary_aliases"} <= tables def test_create_read_update_and_deactivate_with_multiple_aliases(tmp_path: Path) -> None: glossary = repository(tmp_path) created = glossary.create( "Secugrid HS", "product", aliases=("Sikirgut", "Secugrid H S"), description="Canonical core product name", ) assert glossary.get(created.id).aliases == ("Secugrid H S", "Sikirgut") assert glossary.list("sikir")[0].canonical_term == "Secugrid HS" updated = glossary.update( created.id, "Secugrid HS", "technical_term", aliases=("Sekugrid HS",), description="Updated", is_active=False, ) assert updated.category == "technical_term" assert updated.aliases == ("Sekugrid HS",) assert not updated.is_active assert glossary.list(active_only=True) == [] def test_terms_and_aliases_are_unique_case_insensitively_across_entries( tmp_path: Path, ) -> None: glossary = repository(tmp_path) glossary.create("PBAT", "acronym", aliases=("Polybutylene adipate terephthalate",)) with pytest.raises(GlossaryConflictError): glossary.create("pbat", "material") with pytest.raises(GlossaryConflictError): glossary.create("Other", "other", aliases=("PBAT",)) with pytest.raises(GlossaryConflictError): glossary.create("Polybutylene adipate terephthalate", "material") def test_delete_removes_entry_and_aliases(tmp_path: Path) -> None: glossary = repository(tmp_path) entry = glossary.create("Luminy", "product", aliases=("Lumini",)) glossary.delete(entry.id) assert glossary.list() == [] with sqlite3.connect(glossary.database_path) as connection: assert connection.execute("SELECT COUNT(*) FROM glossary_aliases").fetchone()[0] == 0