Skip to content

Commit a32576c

Browse files
committed
Raaise exceptions for missing required parameters
1 parent 21e0306 commit a32576c

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

chatterbot/logic/specific_response.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,19 @@ class SpecificResponseAdapter(LogicAdapter):
2020
def __init__(self, chatbot, **kwargs):
2121
super().__init__(chatbot, **kwargs)
2222

23-
self.input_text = kwargs.get('input_text')
23+
try:
24+
self.input_text = kwargs['input_text']
25+
except KeyError:
26+
raise chatbot.ChatBotException(
27+
'The SpecificResponseAdapter requires an input_text parameter.'
28+
)
29+
30+
try:
31+
self._output_text = kwargs['output_text']
32+
except KeyError:
33+
raise chatbot.ChatBotException(
34+
'The SpecificResponseAdapter requires an output_text parameter.'
35+
)
2436

2537
self.matcher = None
2638

@@ -33,8 +45,6 @@ def __init__(self, chatbot, **kwargs):
3345

3446
self.matcher.add('SpecificResponse', [self.input_text])
3547

36-
self._output_text = kwargs.get('output_text')
37-
3848
def _initialize_nlp(self, language):
3949
model = get_model_for_language(language)
4050

tests/logic/test_specific_response.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,26 @@ def setUp(self):
1717
output_text='Your sesame seed hamburger roll is now open.'
1818
)
1919

20+
def test_initialization_with_missing_input_text(self):
21+
""""
22+
Test that an exception is raised if input_text is missing.
23+
"""
24+
with self.assertRaises(self.chatbot.ChatBotException):
25+
SpecificResponseAdapter(
26+
self.chatbot,
27+
output_text='Done!'
28+
)
29+
30+
def test_initialization_with_missing_output_text(self):
31+
""""
32+
Test that an exception is raised if output_text is missing.
33+
"""
34+
with self.assertRaises(self.chatbot.ChatBotException):
35+
SpecificResponseAdapter(
36+
self.chatbot,
37+
input_text='Do something!'
38+
)
39+
2040
def test_exact_match(self):
2141
"""
2242
Test the case that an exact match is given.

0 commit comments

Comments
 (0)