106 lines
3.5 KiB
Python
106 lines
3.5 KiB
Python
"""CLI execution tests covering flags, arguments, stdout, and error handling."""
|
|
|
|
import json
|
|
from pathlib import Path
|
|
import pytest
|
|
from classify import main
|
|
|
|
|
|
def test_cli_success_stdout(tmp_path, capsys):
|
|
ecp_file = tmp_path / "ecp.json"
|
|
ecp_file.write_text(json.dumps({
|
|
"target_entity_id": "ent_1",
|
|
"target_name": "Petrobras",
|
|
"aliases": ["Petrobras"],
|
|
"domain": "Oil & Gas",
|
|
"anchors": ["petróleo", "pré-sal"]
|
|
}), encoding="utf-8")
|
|
|
|
content_file = tmp_path / "content.md"
|
|
content_file.write_text("# Notícia\n\nA Petrobras bateu recorde de extração de petróleo no pré-sal este mês.", encoding="utf-8")
|
|
|
|
exit_code = main(["--ecp", str(ecp_file), "--content", str(content_file)])
|
|
assert exit_code == 0
|
|
|
|
captured = capsys.readouterr()
|
|
result = json.loads(captured.out)
|
|
assert result["decision"] == "DIRECT_INHERENT"
|
|
assert result["is_inherent"] is True
|
|
assert result["detected_language"] == "pt"
|
|
|
|
|
|
def test_cli_output_file(tmp_path):
|
|
ecp_file = tmp_path / "ecp.json"
|
|
ecp_file.write_text(json.dumps({
|
|
"target_entity_id": "ent_1",
|
|
"target_name": "Petrobras",
|
|
"aliases": ["Petrobras"],
|
|
"domain": "Oil & Gas",
|
|
"anchors": ["petróleo", "pré-sal"]
|
|
}), encoding="utf-8")
|
|
|
|
content_file = tmp_path / "content.md"
|
|
content_file.write_text("Petrobras anunciou investimentos bilionários em novas refinarias de petróleo.", encoding="utf-8")
|
|
|
|
output_file = tmp_path / "out" / "result.json"
|
|
|
|
exit_code = main(["--ecp", str(ecp_file), "--content", str(content_file), "--output", str(output_file)])
|
|
assert exit_code == 0
|
|
assert output_file.exists()
|
|
|
|
result = json.loads(output_file.read_text(encoding="utf-8"))
|
|
assert result["decision"] == "DIRECT_INHERENT"
|
|
assert result["is_inherent"] is True
|
|
|
|
|
|
def test_cli_missing_ecp_file(tmp_path, capsys):
|
|
content_file = tmp_path / "content.md"
|
|
content_file.write_text("Algum conteúdo válido aqui.", encoding="utf-8")
|
|
|
|
exit_code = main(["--ecp", str(tmp_path / "non_existent.json"), "--content", str(content_file)])
|
|
assert exit_code == 1
|
|
|
|
captured = capsys.readouterr()
|
|
err = json.loads(captured.err)
|
|
assert err["error_code"] == "invalid_ecp_json"
|
|
|
|
|
|
def test_cli_empty_content_file(tmp_path, capsys):
|
|
ecp_file = tmp_path / "ecp.json"
|
|
ecp_file.write_text(json.dumps({
|
|
"target_entity_id": "ent_1",
|
|
"target_name": "Petrobras",
|
|
"aliases": ["Petrobras"],
|
|
"domain": "Oil & Gas",
|
|
"anchors": ["petróleo"]
|
|
}), encoding="utf-8")
|
|
|
|
content_file = tmp_path / "empty.md"
|
|
content_file.write_text(" \n\n ", encoding="utf-8")
|
|
|
|
exit_code = main(["--ecp", str(ecp_file), "--content", str(content_file)])
|
|
assert exit_code == 1
|
|
|
|
captured = capsys.readouterr()
|
|
err = json.loads(captured.err)
|
|
assert err["error_code"] == "empty_content"
|
|
|
|
|
|
def test_cli_missing_required_ecp_field(tmp_path, capsys):
|
|
ecp_file = tmp_path / "bad_ecp.json"
|
|
ecp_file.write_text(json.dumps({
|
|
"target_entity_id": "ent_1",
|
|
"domain": "Oil & Gas",
|
|
"anchors": ["petróleo"]
|
|
}), encoding="utf-8")
|
|
|
|
content_file = tmp_path / "content.md"
|
|
content_file.write_text("Algum conteúdo válido para testar o erro.", encoding="utf-8")
|
|
|
|
exit_code = main(["--ecp", str(ecp_file), "--content", str(content_file)])
|
|
assert exit_code == 1
|
|
|
|
captured = capsys.readouterr()
|
|
err = json.loads(captured.err)
|
|
assert err["error_code"] == "missing_required_field"
|