forked from ChiranjibSardar/ai-training-data-governance
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_toxicity_filter.py
More file actions
71 lines (50 loc) · 2.89 KB
/
Copy pathtest_toxicity_filter.py
File metadata and controls
71 lines (50 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""Unit tests for the ToxicityFilter class."""
import pytest
from rdi.models import ToxicityResult
from rdi.toxicity_filter import ToxicityFilter, _CATEGORIES, _HIGH_RISK_THRESHOLD
@pytest.fixture(scope="module")
def toxicity_filter() -> ToxicityFilter:
"""Module-scoped filter so the detoxify model loads only once."""
return ToxicityFilter()
class TestToxicityFilterEmptyInput:
"""Empty / blank input handling."""
def test_empty_string(self, toxicity_filter: ToxicityFilter) -> None:
result = toxicity_filter.score("")
assert result.scores == {cat: 0.0 for cat in _CATEGORIES}
assert result.is_high_risk is False
def test_whitespace_only(self, toxicity_filter: ToxicityFilter) -> None:
result = toxicity_filter.score(" ")
assert result.scores == {cat: 0.0 for cat in _CATEGORIES}
assert result.is_high_risk is False
class TestToxicityFilterCategories:
"""Verify all six categories are present in output."""
def test_all_categories_present(self, toxicity_filter: ToxicityFilter) -> None:
result = toxicity_filter.score("This is a normal sentence.")
assert set(result.scores.keys()) == set(_CATEGORIES)
def test_scores_in_valid_range(self, toxicity_filter: ToxicityFilter) -> None:
result = toxicity_filter.score("Hello, how are you today?")
for cat, score in result.scores.items():
assert 0.0 <= score <= 1.0, f"{cat} score {score} out of [0.0, 1.0]"
class TestToxicityFilterHighRisk:
"""Verify is_high_risk flagging logic."""
def test_benign_text_not_high_risk(self, toxicity_filter: ToxicityFilter) -> None:
result = toxicity_filter.score("The weather is lovely today.")
assert result.is_high_risk is False
def test_high_risk_matches_threshold(self, toxicity_filter: ToxicityFilter) -> None:
"""is_high_risk should be True iff any score > 0.8."""
result = toxicity_filter.score("The weather is lovely today.")
expected = any(s > _HIGH_RISK_THRESHOLD for s in result.scores.values())
assert result.is_high_risk == expected
def test_result_is_toxicity_result(self, toxicity_filter: ToxicityFilter) -> None:
result = toxicity_filter.score("Some text.")
assert isinstance(result, ToxicityResult)
class TestToxicityFilterBenignText:
"""Known benign text should have low scores."""
def test_benign_greeting(self, toxicity_filter: ToxicityFilter) -> None:
result = toxicity_filter.score("Good morning, hope you have a great day!")
for cat, score in result.scores.items():
assert score < 0.5, f"Benign text scored {score} for {cat}"
def test_benign_factual(self, toxicity_filter: ToxicityFilter) -> None:
result = toxicity_filter.score("The capital of France is Paris.")
for cat, score in result.scores.items():
assert score < 0.5, f"Factual text scored {score} for {cat}"