48 lines
1.3 KiB
Python
48 lines
1.3 KiB
Python
import importlib
|
|
import os
|
|
import sys
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
|
|
PROJECT_ROOT = Path(__file__).resolve().parents[1]
|
|
if str(PROJECT_ROOT) not in sys.path:
|
|
sys.path.insert(0, str(PROJECT_ROOT))
|
|
|
|
|
|
def _reload_analysis(backend: str):
|
|
os.environ["JOURNAL_NLP_BACKEND"] = backend
|
|
import journal.core.config as config
|
|
import journal.ai.analysis as analysis
|
|
|
|
importlib.reload(config)
|
|
importlib.reload(analysis)
|
|
return analysis
|
|
|
|
|
|
class AiBackendTests(unittest.TestCase):
|
|
def test_auto_backend_resolves_without_crashing(self):
|
|
analysis = _reload_analysis("auto")
|
|
backend = analysis.get_nlp_backend()
|
|
self.assertIn(backend, {"spacy", "fallback"})
|
|
|
|
def test_fallback_backend_forced(self):
|
|
analysis = _reload_analysis("fallback")
|
|
self.assertEqual(analysis.get_nlp_backend(), "fallback")
|
|
themes = analysis.extract_themes(
|
|
"rain rain rain over city streets and rain over old memories"
|
|
)
|
|
self.assertIsInstance(themes, list)
|
|
|
|
def test_spacy_forced_is_explicit(self):
|
|
analysis = _reload_analysis("spacy")
|
|
try:
|
|
backend = analysis.get_nlp_backend()
|
|
except RuntimeError:
|
|
return
|
|
self.assertEqual(backend, "spacy")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|