File size: 4,243 Bytes
c30b695
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
"""
Unit tests for profanity detector
"""

import unittest
from src.profanity_detector import ProfanityDetector


class TestProfanityDetector(unittest.TestCase):
    """Test cases for ProfanityDetector class"""

    def setUp(self):
        """Set up test fixtures"""
        self.detector = ProfanityDetector()

    def test_detects_basic_profanity(self):
        """Test detection of common profanity"""
        self.assertTrue(self.detector.is_profane("This is bullshit"))
        self.assertTrue(self.detector.is_profane("damn this"))
        self.assertFalse(self.detector.is_profane("This is great"))
        self.assertFalse(self.detector.is_profane("Hello world"))

    def test_detects_leetspeak(self):
        """Test detection of leetspeak variants"""
        self.assertTrue(self.detector.is_profane("sh1t happens"))
        self.assertTrue(self.detector.is_profane("b*tch please"))

    def test_empty_text(self):
        """Test handling of empty text"""
        self.assertFalse(self.detector.is_profane(""))
        self.assertFalse(self.detector.is_profane("   "))
        self.assertIsNone(self.detector.detect_violations(""))

    def test_violation_details(self):
        """Test detailed violation information"""
        violation = self.detector.detect_violations("damn this shit")
        self.assertIsNotNone(violation)
        self.assertEqual(violation['detected'], True)
        self.assertTrue(len(violation['violations']) > 0)
        self.assertIn('severity', violation)
        self.assertIn('censored_text', violation)
        self.assertIn('violation_count', violation)

    def test_no_violation(self):
        """Test clean text returns None"""
        violation = self.detector.detect_violations("This is a nice message")
        self.assertIsNone(violation)

    def test_whitelist(self):
        """Test whitelist functionality"""
        detector_with_whitelist = ProfanityDetector(whitelist=['arsenal', 'scunthorpe'])
        self.assertFalse(detector_with_whitelist.is_profane("I love arsenal"))
        self.assertFalse(detector_with_whitelist.is_profane("Scunthorpe is a town"))

    def test_severity_calculation(self):
        """Test severity level calculation"""
        # Single violation = low
        violation_low = self.detector.detect_violations("shit")
        self.assertIsNotNone(violation_low)
        self.assertEqual(violation_low['severity'], 'low')

        # Multiple violations = higher severity
        violation_multiple = self.detector.detect_violations("shit damn")
        self.assertIsNotNone(violation_multiple)
        self.assertIn(violation_multiple['severity'], ['low', 'medium', 'high'])

    def test_add_custom_words(self):
        """Test adding custom words at runtime"""
        custom_words = ['badword1', 'badword2']
        self.detector.add_words(custom_words)
        self.assertTrue(self.detector.is_profane("This is badword1"))
        self.assertTrue(self.detector.is_profane("badword2 here"))

    def test_get_stats(self):
        """Test statistics retrieval"""
        stats = self.detector.get_stats()
        self.assertIn('custom_words_count', stats)
        self.assertIn('whitelist_count', stats)
        self.assertIn('using_defaults', stats)


class TestProfanityDetectorWithCustomWords(unittest.TestCase):
    """Test cases for custom word lists"""

    def test_custom_word_list(self):
        """Test initialization with custom words"""
        custom_words = ['spam', 'phishing', 'scam']
        detector = ProfanityDetector(custom_words=custom_words)

        self.assertTrue(detector.is_profane("This is spam"))
        self.assertTrue(detector.is_profane("phishing attack"))
        self.assertTrue(detector.is_profane("scam alert"))

    def test_combined_default_and_custom(self):
        """Test that custom words work alongside defaults"""
        custom_words = ['custombadword']
        detector = ProfanityDetector(custom_words=custom_words)

        # Custom word should be detected (case insensitive)
        self.assertTrue(detector.is_profane("This is custombadword"))

        # Default profanity should still work
        self.assertTrue(detector.is_profane("This is shit"))


if __name__ == '__main__':
    unittest.main()