Spaces:
Build error
Build error
File size: 6,262 Bytes
d545f81 3cdce90 d545f81 3cdce90 d545f81 3cdce90 d545f81 3cdce90 d545f81 3cdce90 d545f81 3cdce90 d545f81 3cdce90 d545f81 3cdce90 d545f81 3cdce90 d545f81 3cdce90 d545f81 3cdce90 d545f81 3cdce90 d545f81 3cdce90 d545f81 3cdce90 d545f81 3cdce90 d545f81 3cdce90 d545f81 3cdce90 d545f81 3cdce90 d545f81 3cdce90 d545f81 3cdce90 d545f81 3cdce90 d545f81 3cdce90 d545f81 3cdce90 d545f81 3cdce90 d545f81 3cdce90 d545f81 3cdce90 d545f81 3cdce90 d545f81 3cdce90 d545f81 3cdce90 d545f81 3cdce90 d545f81 3cdce90 d545f81 3cdce90 d545f81 3cdce90 d545f81 3cdce90 d545f81 3cdce90 d545f81 3cdce90 d545f81 3cdce90 d545f81 3cdce90 d545f81 | 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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | """
Unit tests for intent_classifier module
Tests the IntentClassifier class
"""
import unittest
from unittest.mock import Mock
from src.intent_classifier import IntentClassifier
class TestIntentClassifier(unittest.TestCase):
"""Test cases for IntentClassifier class"""
def setUp(self):
"""Set up test fixtures"""
self.classifier = IntentClassifier()
def test_initialization(self):
"""Test classifier initialization"""
self.assertIsNotNone(self.classifier.intent_patterns)
self.assertIn("greeting", self.classifier.intent_patterns)
self.assertIn("thanks", self.classifier.intent_patterns)
self.assertIn("goodbye", self.classifier.intent_patterns)
def test_classify_greeting(self):
"""Test classification of greeting messages"""
test_cases = [
"hi",
"hello",
"Hey there",
"good morning",
"Good afternoon!",
"how are you",
]
for message in test_cases:
intent, response = self.classifier.classify_intent(message)
self.assertEqual(intent, "greeting", f"Failed for message: {message}")
self.assertIsInstance(response, str)
self.assertGreater(len(response), 0)
def test_classify_thanks(self):
"""Test classification of thank you messages"""
test_cases = [
"thank you",
"thanks",
"thank u",
"thx",
"I appreciate it",
"thanks a lot",
"thank you so much",
]
for message in test_cases:
intent, response = self.classifier.classify_intent(message)
self.assertEqual(intent, "thanks", f"Failed for message: {message}")
self.assertIsInstance(response, str)
self.assertGreater(len(response), 0)
def test_classify_goodbye(self):
"""Test classification of goodbye messages"""
test_cases = [
"bye",
"goodbye",
"see you",
"farewell",
"take care",
"have a good day",
"talk to you later",
]
for message in test_cases:
intent, response = self.classifier.classify_intent(message)
self.assertEqual(intent, "goodbye", f"Failed for message: {message}")
self.assertIsInstance(response, str)
self.assertGreater(len(response), 0)
def test_classify_query(self):
"""Test classification of query messages"""
test_cases = [
"How do I open an account?",
"What are the transaction fees?",
"Can you help me with my balance?",
"Tell me about XENO services",
]
for message in test_cases:
intent, response = self.classifier.classify_intent(message)
self.assertEqual(intent, "query", f"Failed for message: {message}")
self.assertEqual(response, "")
def test_case_insensitivity(self):
"""Test that classification is case insensitive"""
messages = [
("HI", "greeting"),
("THANK YOU", "thanks"),
("BYE", "goodbye"),
("Hi There", "greeting"),
]
for message, expected_intent in messages:
intent, _ = self.classifier.classify_intent(message)
self.assertEqual(intent, expected_intent)
def test_with_timer(self):
"""Test classification with timer object"""
mock_timer = Mock()
mock_timer.time_step = Mock()
mock_timer.time_step.return_value.__enter__ = Mock()
mock_timer.time_step.return_value.__exit__ = Mock()
intent, response = self.classifier.classify_intent("hello", timer=mock_timer)
self.assertEqual(intent, "greeting")
mock_timer.time_step.assert_called_once_with("intent_classification")
def test_is_simple_intent(self):
"""Test is_simple_intent method"""
self.assertTrue(self.classifier.is_simple_intent("greeting"))
self.assertTrue(self.classifier.is_simple_intent("thanks"))
self.assertFalse(self.classifier.is_simple_intent("goodbye"))
self.assertFalse(self.classifier.is_simple_intent("query"))
def test_add_intent(self):
"""Test adding a new intent"""
patterns = [r"\b(test|testing)\b"]
responses = ["This is a test response"]
self.classifier.add_intent("test_intent", patterns, responses)
# Verify intent was added
self.assertIn("test_intent", self.classifier.intent_patterns)
self.assertEqual(
self.classifier.intent_patterns["test_intent"]["patterns"], patterns
)
self.assertEqual(
self.classifier.intent_patterns["test_intent"]["responses"], responses
)
# Test classification with new intent
intent, response = self.classifier.classify_intent("testing")
self.assertEqual(intent, "test_intent")
self.assertEqual(response, "This is a test response")
def test_response_variety(self):
"""Test that responses vary (random selection)"""
# Multiple calls might return different responses
responses = set()
for _ in range(20):
_, response = self.classifier.classify_intent("hello")
responses.add(response)
# Should have at least 1 response (could be more if random varies)
self.assertGreater(len(responses), 0)
def test_empty_message(self):
"""Test classification of empty or whitespace messages"""
test_cases = ["", " ", "\n", "\t"]
for message in test_cases:
intent, response = self.classifier.classify_intent(message)
self.assertEqual(intent, "query")
self.assertEqual(response, "")
def test_mixed_intent_message(self):
"""Test messages that might match multiple patterns"""
# "hi thank you" should match greeting (first match wins)
intent, response = self.classifier.classify_intent("hi thank you")
# Should match the first pattern it encounters
self.assertIn(intent, ["greeting", "thanks"])
if __name__ == "__main__":
unittest.main()
|