Files
2026-11-09 08:43:30 +03:00

105 lines
4.4 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import json
import pytest
from estimator.__main__ import main
from estimator.config import get_settings
@pytest.fixture
def env(monkeypatch, dsn):
monkeypatch.setenv("EST_DATABASE__DSN", dsn)
monkeypatch.setenv("EST_LLM__ANTHROPIC__API_KEY", "sk-test")
monkeypatch.setenv("EST_RAG__PROVIDER", "hash")
get_settings.cache_clear()
yield
get_settings.cache_clear()
def write_csv(tmp_path, rows, name="refs.csv"):
path = tmp_path / name
lines = ["project,title,description,role,hours,hours_kind"] + [",".join(r) for r in rows]
path.write_text("\n".join(lines) + "\n", encoding="utf-8")
return path
GOOD = [
["P1", "Интеграция amoCRM", "сделки и контакты", "backend", "18", "actual"],
["P1", "Адаптер MAX", "", "backend", "12", "estimate"],
["P2", "Тестирование сценариев", "", "qa", "20", "actual"],
]
def test_import_stats_search_delete_cycle(env, tmp_path, capsys):
path = write_csv(tmp_path, GOOD)
assert main(["rag", "import", str(path)]) == 0
out = capsys.readouterr().out
assert "rows 3, valid 3, rejected 0" in out and "inserted 3" in out and "embedded 3 with hash-v1" in out
assert main(["rag", "stats"]) == 0
out = capsys.readouterr().out
assert "tasks 3 in 2 projects (2 with actual hours, 1 estimates)" in out
assert "need (re)indexing: 0" in out and "retrieval is OFF" in out
assert main(["rag", "search", "интеграция с CRM", "--min-similarity", "0.3"]) == 0
out = capsys.readouterr().out
assert "Интеграция amoCRM" in out and "18 h actual" in out and "P1" in out
assert main(["rag", "search", "ничего общего", "--min-similarity", "0.9"]) == 0
assert "nothing above the similarity threshold" in capsys.readouterr().out
assert main(["rag", "search", "тестирование", "--role", "backend", "--min-similarity", "0.2"]) == 0
assert "Тестирование сценариев" not in capsys.readouterr().out # a QA task is not shown for a backend query
assert main(["rag", "delete", "--project", "P2"]) == 0
assert "deleted 1 tasks of 'P2'" in capsys.readouterr().out
main(["rag", "stats"])
assert "tasks 2 in 1 projects" in capsys.readouterr().out
def test_dry_run_validates_without_writing(env, tmp_path, capsys):
path = write_csv(tmp_path, GOOD)
assert main(["rag", "import", str(path), "--dry-run"]) == 0
assert "dry run: nothing was written" in capsys.readouterr().out
main(["rag", "stats"])
assert "tasks 0 in 0 projects" in capsys.readouterr().out
def test_bad_rows_are_listed_and_do_not_block_the_good_ones(env, tmp_path, capsys):
path = write_csv(tmp_path, [*GOOD, ["P1", "Плохая", "", "wizard", "5", ""], ["P1", "", "", "", "5", ""]])
assert main(["rag", "import", str(path)]) == 0
out = capsys.readouterr().out
assert "row 4: unknown role 'wizard'" in out and "row 5: title" in out and "valid 3, rejected 2" in out
def test_a_file_with_only_bad_rows_fails(env, tmp_path, capsys):
path = write_csv(tmp_path, [["P1", "", "", "", "x", ""]])
assert main(["rag", "import", str(path)]) == 1
def test_default_project_and_json_input(env, tmp_path, capsys):
path = tmp_path / "refs.json"
path.write_text(json.dumps([{"title": "Задача без проекта", "hours": 4}]), encoding="utf-8")
assert main(["rag", "import", str(path)]) == 1 # no project in the file and no default: nothing valid
assert "project is required" in capsys.readouterr().out
assert main(["rag", "import", str(path), "--project", "Старый проект"]) == 0
assert "inserted 1" in capsys.readouterr().out
def test_without_an_embeddings_key_the_openai_provider_is_refused(monkeypatch, dsn, capsys):
monkeypatch.setenv("EST_DATABASE__DSN", dsn)
monkeypatch.setenv("EST_LLM__ANTHROPIC__API_KEY", "sk-test")
get_settings.cache_clear()
try:
assert main(["rag", "stats"]) == 2
finally:
get_settings.cache_clear()
assert "No embeddings API key" in capsys.readouterr().err
def test_the_commands_work_while_retrieval_is_still_switched_off(env, tmp_path, capsys):
"""An administrator loads the corpus first and enables retrieval afterwards."""
main(["rag", "import", str(write_csv(tmp_path, GOOD))])
capsys.readouterr()
main(["rag", "stats"])
assert "retrieval is OFF" in capsys.readouterr().out