OFPBadWord / tests /test_profanity.py
BladeSzaSza's picture
initial commit
c30b695
"""
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()