/tests_django/test_chatbot.py

https://github.com/gunthercox/ChatterBot · Python · 309 lines · 302 code · 4 blank · 3 comment · 10 complexity · d301217529c6cb478dcdf6df2372ad81 MD5 · raw file

  1. from tests_django.base_case import ChatterBotTestCase
  2. from chatterbot.conversation import Statement
  3. class ChatBotTests(ChatterBotTestCase):
  4. def test_get_response_text(self):
  5. self.chatbot.get_response(text='Test')
  6. def test_no_statements_known(self):
  7. """
  8. If there is no statements in the database, then the
  9. user's input is the only thing that can be returned.
  10. """
  11. statement_text = 'How are you?'
  12. response = self.chatbot.get_response(statement_text)
  13. results = list(self.chatbot.storage.filter(text=statement_text))
  14. self.assertEqual(response.text, statement_text)
  15. self.assertEqual(response.confidence, 0)
  16. # Make sure that the input and output were saved
  17. self.assertEqual(len(results), 2)
  18. self.assertEqual(results[0].text, statement_text)
  19. self.assertEqual(results[1].text, statement_text)
  20. def test_one_statement_known_no_response(self):
  21. """
  22. Test the case where a single statement is known, but
  23. it is not in response to any other statement.
  24. """
  25. self.chatbot.storage.create(text='Hello', in_response_to=None)
  26. response = self.chatbot.get_response('Hi')
  27. self.assertEqual(response.confidence, 0)
  28. self.assertEqual(response.text, 'Hello')
  29. def test_one_statement_one_response_known(self):
  30. """
  31. Test the case that one response is known and there is a response
  32. entry for it in the database.
  33. """
  34. self.chatbot.storage.create(text='Hello', in_response_to='Hi')
  35. response = self.chatbot.get_response('Hi')
  36. self.assertEqual(response.confidence, 0)
  37. self.assertEqual(response.text, 'Hello')
  38. def test_two_statements_one_response_known(self):
  39. """
  40. Test the case that one response is known and there is a response
  41. entry for it in the database.
  42. """
  43. self.chatbot.storage.create(text='Hi', in_response_to=None)
  44. self.chatbot.storage.create(text='Hello', in_response_to='Hi')
  45. response = self.chatbot.get_response('Hi')
  46. self.assertEqual(response.confidence, 1)
  47. self.assertEqual(response.text, 'Hello')
  48. def test_three_statements_two_responses_known(self):
  49. self.chatbot.storage.create(text='Hi', in_response_to=None)
  50. self.chatbot.storage.create(text='Hello', in_response_to='Hi')
  51. self.chatbot.storage.create(text='How are you?', in_response_to='Hello')
  52. first_response = self.chatbot.get_response('Hi')
  53. second_response = self.chatbot.get_response('How are you?')
  54. self.assertEqual(first_response.confidence, 1)
  55. self.assertEqual(first_response.text, 'Hello')
  56. self.assertEqual(second_response.confidence, 0)
  57. def test_four_statements_three_responses_known(self):
  58. self.chatbot.storage.create(text='Hi', in_response_to=None)
  59. self.chatbot.storage.create(text='Hello', in_response_to='Hi')
  60. self.chatbot.storage.create(text='How are you?', in_response_to='Hello')
  61. self.chatbot.storage.create(text='I am well.', in_response_to='How are you?')
  62. first_response = self.chatbot.get_response('Hi')
  63. second_response = self.chatbot.get_response('How are you?')
  64. self.assertEqual(first_response.confidence, 1)
  65. self.assertEqual(first_response.text, 'Hello')
  66. self.assertEqual(second_response.confidence, 1)
  67. self.assertEqual(second_response.text, 'I am well.')
  68. def test_second_response_unknown(self):
  69. self.chatbot.storage.create(text='Hi', in_response_to=None)
  70. self.chatbot.storage.create(text='Hello', in_response_to='Hi')
  71. first_response = self.chatbot.get_response(
  72. text='Hi',
  73. conversation='test'
  74. )
  75. second_response = self.chatbot.get_response(
  76. text='How are you?',
  77. conversation='test'
  78. )
  79. results = list(self.chatbot.storage.filter(text='How are you?'))
  80. self.assertEqual(first_response.confidence, 1)
  81. self.assertEqual(first_response.text, 'Hello')
  82. self.assertEqual(first_response.in_response_to, 'Hi')
  83. self.assertEqual(second_response.confidence, 0)
  84. self.assertEqual(second_response.in_response_to, 'How are you?')
  85. # Make sure that the second response was saved to the database
  86. self.assertEqual(len(results), 1)
  87. self.assertEqual(results[0].in_response_to, 'Hi')
  88. def test_statement_added_to_conversation(self):
  89. """
  90. An input statement should be added to the recent response list.
  91. """
  92. statement = Statement(text='Wow!', conversation='test')
  93. response = self.chatbot.get_response(statement)
  94. self.assertEqual(statement.text, response.text)
  95. self.assertEqual(response.conversation, 'test')
  96. def test_get_response_additional_response_selection_parameters(self):
  97. self.chatbot.storage.create_many([
  98. Statement('A', conversation='test_1'),
  99. Statement('B', conversation='test_1', in_response_to='A'),
  100. Statement('A', conversation='test_2'),
  101. Statement('C', conversation='test_2', in_response_to='A'),
  102. ])
  103. statement = Statement(text='A', conversation='test_3')
  104. response = self.chatbot.get_response(statement, additional_response_selection_parameters={
  105. 'conversation': 'test_2'
  106. })
  107. self.assertEqual(response.text, 'C')
  108. self.assertEqual(response.conversation, 'test_3')
  109. def test_get_response_unicode(self):
  110. """
  111. Test the case that a unicode string is passed in.
  112. """
  113. response = self.chatbot.get_response(u'سلام')
  114. self.assertGreater(len(response.text), 0)
  115. def test_get_response_emoji(self):
  116. """
  117. Test the case that the input string contains an emoji.
  118. """
  119. response = self.chatbot.get_response(u'💩 ')
  120. self.assertGreater(len(response.text), 0)
  121. def test_get_response_non_whitespace(self):
  122. """
  123. Test the case that a non-whitespace C1 control string is passed in.
  124. """
  125. response = self.chatbot.get_response(u'€Ž‘’')
  126. self.assertGreater(len(response.text), 0)
  127. def test_get_response_two_byte_characters(self):
  128. """
  129. Test the case that a string containing two-byte characters is passed in.
  130. """
  131. response = self.chatbot.get_response(u'田中さんにあげて下さい')
  132. self.assertGreater(len(response.text), 0)
  133. def test_get_response_corrupted_text(self):
  134. """
  135. Test the case that a string contains "corrupted" text.
  136. """
  137. response = self.chatbot.get_response(u'Ṱ̺̺̕h̼͓̲̦̳̘̲e͇̣̰̦̬͎ ̢̼̻̱̘h͚͎͙̜̣̲ͅi̦̲̣̰̤v̻͍e̺̭̳̪̰-m̢iͅn̖̺̞̲̯̰d̵̼̟͙̩̼̘̳.̨̹͈̣')
  138. self.assertGreater(len(response.text), 0)
  139. def test_response_with_tags_added(self):
  140. """
  141. If an input statement has tags added to it,
  142. that data should saved with the input statement.
  143. """
  144. self.chatbot.get_response(Statement(
  145. text='Hello',
  146. in_response_to='Hi',
  147. tags=['test']
  148. ))
  149. results = list(self.chatbot.storage.filter(text='Hello'))
  150. self.assertEqual(len(results), 2)
  151. self.assertIn('test', results[0].get_tags())
  152. self.assertEqual(results[1].get_tags(), [])
  153. def test_get_response_with_text_and_kwargs(self):
  154. self.chatbot.get_response('Hello', conversation='greetings')
  155. results = list(self.chatbot.storage.filter(text='Hello'))
  156. self.assertEqual(len(results), 2)
  157. self.assertEqual(results[0].conversation, 'greetings')
  158. self.assertEqual(results[1].conversation, 'greetings')
  159. def test_get_response_missing_text(self):
  160. with self.assertRaises(self.chatbot.ChatBotException):
  161. self.chatbot.get_response()
  162. def test_get_response_missing_text_with_conversation(self):
  163. with self.assertRaises(self.chatbot.ChatBotException):
  164. self.chatbot.get_response(conversation='test')
  165. def test_generate_response(self):
  166. statement = Statement(text='Many insects adopt a tripedal gait for rapid yet stable walking.')
  167. response = self.chatbot.generate_response(statement)
  168. self.assertEqual(response.text, statement.text)
  169. self.assertEqual(response.confidence, 0)
  170. def test_learn_response(self):
  171. previous_response = Statement(text='Define Hemoglobin.')
  172. statement = Statement(text='Hemoglobin is an oxygen-transport metalloprotein.')
  173. self.chatbot.learn_response(statement, previous_response)
  174. results = list(self.chatbot.storage.filter(text=statement.text))
  175. self.assertEqual(len(results), 1)
  176. def test_get_response_does_not_add_new_statement(self):
  177. """
  178. Test that a new statement is not learned if `read_only` is set to True.
  179. """
  180. self.chatbot.read_only = True
  181. self.chatbot.get_response('Hi!')
  182. results = list(self.chatbot.storage.filter(text='Hi!'))
  183. self.assertEqual(len(results), 0)
  184. def test_get_latest_response_from_zero_responses(self):
  185. response = self.chatbot.get_latest_response('invalid')
  186. self.assertIsNone(response)
  187. def test_get_latest_response_from_one_responses(self):
  188. self.chatbot.storage.create(text='A', conversation='test')
  189. self.chatbot.storage.create(text='B', conversation='test', in_response_to='A')
  190. response = self.chatbot.get_latest_response('test')
  191. self.assertEqual(response.text, 'A')
  192. def test_get_latest_response_from_two_responses(self):
  193. self.chatbot.storage.create(text='A', conversation='test')
  194. self.chatbot.storage.create(text='B', conversation='test', in_response_to='A')
  195. self.chatbot.storage.create(text='C', conversation='test', in_response_to='B')
  196. response = self.chatbot.get_latest_response('test')
  197. self.assertEqual(response.text, 'B')
  198. def test_get_latest_response_from_three_responses(self):
  199. self.chatbot.storage.create(text='A', conversation='test')
  200. self.chatbot.storage.create(text='B', conversation='test', in_response_to='A')
  201. self.chatbot.storage.create(text='C', conversation='test', in_response_to='B')
  202. self.chatbot.storage.create(text='D', conversation='test', in_response_to='C')
  203. response = self.chatbot.get_latest_response('test')
  204. self.assertEqual(response.text, 'C')
  205. def test_search_text_results_after_training(self):
  206. """
  207. ChatterBot should return close matches to an input
  208. string when filtering using the search_text parameter.
  209. """
  210. self.chatbot.storage.create_many([
  211. Statement('Example A for search.'),
  212. Statement('Another example.'),
  213. Statement('Example B for search.'),
  214. Statement(text='Another statement.'),
  215. ])
  216. results = list(self.chatbot.storage.filter(
  217. search_text=self.chatbot.storage.tagger.get_text_index_string(
  218. 'Example A for search.'
  219. )
  220. ))
  221. self.assertEqual(len(results), 1, msg=[r.text for r in results])
  222. self.assertEqual('Example A for search.', results[0].text)
  223. def test_search_text_contains_results_after_training(self):
  224. """
  225. ChatterBot should return close matches to an input
  226. string when filtering using the search_text parameter.
  227. """
  228. self.chatbot.storage.create_many([
  229. Statement('Example A for search.'),
  230. Statement('Another example.'),
  231. Statement('Example B for search.'),
  232. Statement(text='Another statement.'),
  233. ])
  234. results = list(self.chatbot.storage.filter(
  235. search_text_contains=self.chatbot.storage.tagger.get_text_index_string(
  236. 'Example A for search.'
  237. )
  238. ))
  239. self.assertEqual(len(results), 2, msg=[r.text for r in results])
  240. self.assertEqual('Example A for search.', results[0].text)
  241. self.assertEqual('Example B for search.', results[1].text)