test.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # -*- coding: utf-8 -*-
  2. import pytest
  3. import random
  4. from bot import *
  5. random.seed(0)
  6. def test_random_utterance():
  7. """An utterance which is unparsable should return one of the random responses"""
  8. sent = "abcd" # Something unparseable
  9. resp = broback(sent)
  10. assert resp == NONE_RESPONSES[-2]
  11. def test_basic_greetings():
  12. """The bot should respond sensibly to common greeting words"""
  13. sent = "hello"
  14. resp = broback(sent)
  15. assert resp == GREETING_RESPONSES[1]
  16. def test_contains_reference_to_user():
  17. """An utterance where the user mentions themselves generally should specifically return a phrase starting with 'You'"""
  18. sent = "I went to dinner"
  19. resp = broback(sent)
  20. assert resp.startswith("You ")
  21. def test_negs_user():
  22. """An utterance where the user says 'I am' <something> should specifically tell them they aren't that thing"""
  23. sent = "I am good at Python programming"
  24. resp = broback(sent)
  25. assert resp.startswith("You aren't really")
  26. sent = "I'm good at Python programming"
  27. resp = broback(sent)
  28. assert resp.startswith("You aren't really")
  29. sent = "i'm good at Python programming"
  30. resp = broback(sent)
  31. assert resp.startswith("You aren't really")
  32. def test_contains_reference_to_bot():
  33. """An utterance where the user directs something at the bot itself should return a canned response"""
  34. sent = "You are lame"
  35. resp = broback(sent)
  36. assert 'lame' in resp
  37. def test_reuses_subject():
  38. """If the user tells us about some kind of subject, we should mention it in our response"""
  39. sent = "I am a capable programmer"
  40. resp = broback(sent)
  41. assert "programmer" in resp
  42. def test_strip_offensive_words():
  43. """Don't allow the bot to respond with anything obviously offensive"""
  44. # To avoid including an offensive word in the test set, add a harmless word temporarily
  45. from config import FILTER_WORDS
  46. FILTER_WORDS.add('snakeperson')
  47. sent = "I am a snakeperson"
  48. with pytest.raises(UnacceptableUtteranceException):
  49. broback(sent)
  50. def test_strip_punctuation():
  51. """Removing most punctuation is one way to ensure that the bot doesn't include hashtags or @-signs, which are potential vectors for harrassment"""
  52. sent = "I am a #snakeperson"
  53. with pytest.raises(UnacceptableUtteranceException):
  54. broback(sent)
  55. sent = "@you are funny"
  56. with pytest.raises(UnacceptableUtteranceException):
  57. broback(sent)
  58. def test_unicode():
  59. """Bros love internationalization"""
  60. broback(u"☃") # Unicode snowman