hatespeech-detection / tests /test_export_utils.py
jl
feat: downloadable results
04013dc
Raw
History Blame Contribute Delete
2.18 kB
from pathlib import Path
import sys
PROJECT_ROOT = Path(__file__).resolve().parents[1]
SRC_PATH = PROJECT_ROOT / "src"
if str(SRC_PATH) not in sys.path:
sys.path.insert(0, str(SRC_PATH))
from export_utils import generate_results_pdf
def test_generate_single_pdf_bytes():
payload = {
"mode": "single",
"input_text": "You are not welcome here.",
"rationale": "Contains targeted exclusionary language.",
"models": {
"base": {
"prediction": 1,
"confidence": 0.91,
"probabilities": [0.09, 0.91],
"processing_time": 0.124,
"technical_details": {
"prediction": 1,
"confidence": 0.91,
},
},
"enhanced": {
"prediction": 1,
"confidence": 0.95,
"probabilities": [0.05, 0.95],
"processing_time": 0.141,
"token_importance": [
{"Token": "welcome", "Importance": 0.7321},
{"Token": "not", "Importance": 0.6112},
],
"technical_details": {
"prediction": 1,
"confidence": 0.95,
},
},
},
}
pdf_bytes = generate_results_pdf(payload)
assert pdf_bytes
assert pdf_bytes.startswith(b"%PDF")
def test_generate_batch_pdf_bytes():
payload = {
"mode": "batch",
"filename": "sample.csv",
"rows": 25,
"models": {
"base": {
"f1_score": 0.8012,
"precision": 0.8301,
"accuracy": 0.8400,
"recall": 0.7750,
"confusion_matrix": [[11, 2], [2, 10]],
"cpu_usage": 27.3,
"peak_cpu_usage": 48.1,
"memory_usage": 312.2,
"peak_memory_usage": 402.9,
"runtime": 1.24,
"avg_time_per_sample": 0.0496,
}
},
}
pdf_bytes = generate_results_pdf(payload)
assert pdf_bytes
assert pdf_bytes.startswith(b"%PDF")