forked from gunthercox/ChatterBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_chatbot_output.py
210 lines (157 loc) · 6.81 KB
/
test_chatbot_output.py
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
from .base_case import ChatBotTestCase, UntrainedChatBotTestCase
from chatterbot.conversation import Statement, Response
class ChatBotOutputTests(ChatBotTestCase):
def test_get_last_statement(self):
"""
Make sure that the get last statement method
returns the last statement that was issued.
"""
self.chatbot.recent_statements.append(
Statement("Test statement 1")
)
self.chatbot.recent_statements.append(
Statement("Test statement 2")
)
self.chatbot.recent_statements.append(
Statement("Test statement 3")
)
last_statement = self.chatbot.get_last_statement()
self.assertEqual(last_statement.text, "Test statement 3")
def test_get_most_frequent_response(self):
statement_list = [
Statement("What... is your quest?", in_response_to=[Response("Hello", occurrence=2)]),
Statement("This is a phone.", in_response_to=[Response("Hello", occurrence=4)]),
Statement("A what?", in_response_to=[Response("Hello", occurrence=2)]),
Statement("A phone.", in_response_to=[Response("Hello", occurrence=1)])
]
# Save each statement to the database
for statement in statement_list:
self.chatbot.storage.update(statement)
output = self.chatbot.get_most_frequent_response(
Statement("Hello"),
statement_list
)
self.assertEqual("This is a phone.", output)
def test_get_first_response(self):
statement_list = [
Statement("What... is your quest?"),
Statement("A what?"),
Statement("A quest.")
]
output = self.chatbot.get_first_response(
statement_list
)
self.assertEqual("What... is your quest?", output)
def test_get_random_response(self):
statement_list = [
Statement("This is a phone."),
Statement("A what?"),
Statement("A phone.")
]
output = self.chatbot.get_random_response(
statement_list
)
self.assertTrue(output)
def test_answer_to_known_input(self):
"""
Test that a matching response is returned
when an exact match exists.
"""
input_text = "What... is your favourite colour?"
response = self.chatbot.get_response(input_text)
self.assertIn("Blue", response)
def test_answer_close_to_known_input(self):
input_text = "What is your favourite colour?"
response = self.chatbot.get_response(input_text)
self.assertIn("Blue", response)
def test_match_has_no_response(self):
"""
Make sure that the if the last line in a file
matches the input text then a index error does
not occure.
"""
input_text = "Siri is my cat"
response = self.chatbot.get_response(input_text)
self.assertGreater(len(response), 0)
def test_empty_input(self):
"""
If empty input is provided, anything may be returned.
"""
output = self.chatbot.get_response("")
self.assertTrue(len(output) >= 0)
class ChatterBotResponseTestCase(UntrainedChatBotTestCase):
def setUp(self):
super(ChatterBotResponseTestCase, self).setUp()
response_list = [
Response("Hi")
]
self.test_statement = Statement("Hello", in_response_to=response_list)
def test_empty_database(self):
"""
If there is no statements in the database, then the
user's input is the only thing that can be returned.
"""
response = self.chatbot.get_response("How are you?")
self.assertEqual("How are you?", response)
def test_statement_saved_empty_database(self):
"""
Test that when database is empty, the first
statement is saved and returned as a response.
"""
statement_text = "Wow!"
response = self.chatbot.get_response(statement_text)
saved_statement = self.chatbot.storage.find(statement_text)
self.assertIsNotNone(saved_statement)
self.assertEqual(response, statement_text)
def test_statement_added_to_recent_response_list(self):
"""
A new input statement should be added to the recent response list.
"""
statement_text = "Wow!"
response = self.chatbot.get_response(statement_text)
self.assertIn(statement_text, self.chatbot.recent_statements)
self.assertEqual(response, statement_text)
def test_response_known(self):
self.chatbot.storage.update(self.test_statement)
response = self.chatbot.get_response("Hi")
self.assertEqual(response, self.test_statement.text)
def test_response_format(self):
self.chatbot.storage.update(self.test_statement)
response = self.chatbot.get_response("Hi")
statement_object = self.chatbot.storage.find(response)
self.assertEqual(response, self.test_statement.text)
#TODO: self.assertEqual(statement_object.get_occurrence_count(), 2)
self.assertEqual(len(statement_object.in_response_to), 1)
self.assertIn("Hi", statement_object.in_response_to)
def test_second_response_format(self):
self.chatbot.storage.update(self.test_statement)
response = self.chatbot.get_response("Hi")
# response = "Hello"
second_response = self.chatbot.get_response("How are you?")
statement_object = self.chatbot.storage.find(second_response)
self.assertEqual(second_response, self.test_statement.text)
#TODO: self.assertEqual(statement_object.get_response_count(), 2)
self.assertEqual(len(statement_object.in_response_to), 1)
self.assertIn("Hi", statement_object.in_response_to)
class ChatterBotStorageIntegrationTests(UntrainedChatBotTestCase):
def test_database_is_updated(self):
"""
Test that the database is updated when read_only is set to false.
"""
input_text = "What is the airspeed velocity of an unladen swallow?"
exists_before = self.chatbot.storage.find(input_text)
response = self.chatbot.get_response(input_text)
exists_after = self.chatbot.storage.find(input_text)
self.assertFalse(exists_before)
self.assertTrue(exists_after)
def test_database_is_not_updated_when_read_only(self):
"""
Test that the database is not updated when read_only is set to true.
"""
self.chatbot.storage.read_only = True
input_text = "Who are you? The proud lord said."
exists_before = self.chatbot.storage.find(input_text)
response = self.chatbot.get_response(input_text)
exists_after = self.chatbot.storage.find(input_text)
self.assertFalse(exists_before)
self.assertFalse(exists_after)