def test_bot_chat_loop(self): test_brain = Brain(BrainConfiguration()) self.assertIsNotNone(test_brain) self.assertIsInstance(test_brain, Brain) bot = Bot(test_brain, BotConfiguration()) self.assertIsNotNone(bot) self.assertIsInstance(bot, Bot) bot.configuration._default_response = "Sorry, I don't have an answer for that right now" response = bot.ask_question("testid", "hello") self.assertIsNotNone(response) self.assertEqual(response, "Sorry, I don't have an answer for that right now") response = bot.ask_question("testid", "hello again") self.assertIsNotNone(response) self.assertEqual(response, "Sorry, I don't have an answer for that right now") response = bot.ask_question("testid", "goodbye") self.assertIsNotNone(response) self.assertEqual(response, "Sorry, I don't have an answer for that right now") conversation = bot.get_conversation("testid") self.assertIsNotNone(conversation) self.assertEqual(conversation.nth_question(3).sentence(0).text(), "hello") self.assertEqual(conversation.nth_question(3).sentence(0).response, "Sorry, I don't have an answer for that right now") self.assertEqual(conversation.nth_question(2).sentence(0).text(), "hello again") self.assertEqual(conversation.nth_question(2).sentence(0).response, "Sorry, I don't have an answer for that right now") self.assertEqual(conversation.nth_question(1).sentence(0).text(), "goodbye") self.assertEqual(conversation.nth_question(1).sentence(0).response, "Sorry, I don't have an answer for that right now")
def test_authorise_exception(self): testbot = Bot(Brain(BrainConfiguration()), BotConfiguration()) service = MockClientIdAuthenticationService(BrainSecurityConfiguration("authentication")) service.should_authorised = True service.raise_exception = True self.assertFalse(service.authenticate(testbot, "unknown"))
def test_with_data(self): yaml = YamlConfigurationFile() self.assertIsNotNone(yaml) yaml.load_from_text(""" bot: license_keys: $BOT_ROOT/config/license.keys prompt: ">>>" initial_question: Hi, how can I help you today? default_response: Sorry, I don't have an answer for that! empty_string: YEMPTY exit_response: So long, and thanks for the fish! override_predicates: true max_recursion: 10 spelling: classname: programy.utils.spelling.checker.SpellingChecker alphabet: 'abcdefghijklmnopqrstuvwxyz' corpus: $BOT_ROOT/corpus.txt check_before: true check_and_retry: true """, ConsoleConfiguration(), ".") bot_config = BotConfiguration() bot_config.load_config_section(yaml, ".") self.assertEqual("./config/license.keys", bot_config.license_keys) self.assertEqual(">>>", bot_config.prompt) self.assertEqual("Hi, how can I help you today?", bot_config.initial_question) self.assertEqual("Sorry, I don't have an answer for that!", bot_config.default_response) self.assertEqual("YEMPTY", bot_config.empty_string) self.assertEqual(10, bot_config.max_recursion) self.assertTrue(bot_config.override_predicates) self.assertIsNotNone(bot_config.spelling) self.assertEqual("programy.utils.spelling.checker.SpellingChecker", bot_config.spelling.classname)
def test_log_question_and_answer(self): brain_config = BrainConfiguration() if os.name == 'posix': brain_config.files.aiml_files._conversation = "/tmp/tmp-conversation.txt" elif os.name == 'nt': brain_config.files.aiml_files._conversation = 'C:\Windows\Temp\\tmp-conversation.txt' else: raise Exception("Unknown os [%s]" % os.name) test_brain = Brain(brain_config) bot = Bot(test_brain, BotConfiguration()) if os.path.exists(brain_config.files.aiml_files._conversation): os.remove(brain_config.files.aiml_files._conversation) self.assertFalse( os.path.exists(brain_config.files.aiml_files._conversation)) bot.log_question_and_answer("testid", "question", "answer") self.assertTrue( os.path.exists(brain_config.files.aiml_files._conversation))
def test_bot_init_with_config(self): test_brain = Brain(BrainConfiguration()) bot_config = BotConfiguration() bot_config._license_keys = None bot_config._bot_root = BotConfiguration.DEFAULT_ROOT bot_config._prompt = BotConfiguration.DEFAULT_PROMPT bot_config._default_response = BotConfiguration.DEFAULT_RESPONSE bot_config._exit_response = BotConfiguration.DEFAULT_EXIT_RESPONSE bot_config._initial_question = BotConfiguration.DEFAULT_INITIAL_QUESTION bot_config._empty_string = BotConfiguration.DEFAULT_EMPTY_STRING bot_config._override_predicates = BotConfiguration.DEFAULT_OVERRIDE_PREDICATES bot_config._max_recursion = 10 bot = Bot(test_brain, bot_config) self.assertIsNone(bot.spell_checker) self.assertIsNotNone(bot.brain) self.assertIsNotNone(bot.conversations) self.assertIsNotNone(bot.license_keys) self.assertIsNotNone(bot.prompt) self.assertIsNotNone(bot.default_response) self.assertIsNotNone(bot.exit_response) self.assertIsNotNone(bot.initial_question) self.assertTrue(bot.override_predicates) self.assertIsNotNone(bot.get_version_string)
def test_bot_init_with_license_keys(self): test_brain = Brain(BrainConfiguration()) bot_config = BotConfiguration() bot_config._license_keys = os.path.dirname( __file__) + os.sep + "test_license.keys" bot = Bot(test_brain, bot_config) self.assertIsNotNone(bot)
def test_without_data(self): yaml = YamlConfigurationFile() self.assertIsNotNone(yaml) yaml.load_from_text(""" bot: """, ConsoleConfiguration(), ".") bot_config = BotConfiguration() bot_config.load_configuration(yaml, ".") self.assertIsNone(bot_config.license_keys) self.assertEqual(">>> ", bot_config.prompt) self.assertEqual("Hello", bot_config.initial_question) self.assertEqual("", bot_config.initial_question_srai) self.assertEqual("", bot_config.default_response) self.assertEqual("", bot_config.default_response_srai) self.assertEqual("Bye!", bot_config.exit_response) self.assertEqual("", bot_config.exit_response_srai) self.assertEqual("", bot_config.empty_string) self.assertEqual(bot_config.max_question_recursion, 100) self.assertEqual(bot_config.max_question_timeout, -1) self.assertEqual(bot_config.max_search_depth, 100) self.assertEqual(bot_config.max_search_timeout, -1) self.assertTrue(bot_config.override_properties) self.assertIsNotNone(bot_config.spelling)
def test_persistence(self): brain_config = BrainConfiguration() test_brain = Brain(brain_config) bot_config = BotConfiguration() bot_config.conversations._type = "file" bot_config.conversations._storage = BotConversationsFileStorageConfiguration( "test") bot_config.conversations._storage._dir = os.path.dirname(__file__) bot_config.conversations._max_histories = 3 test_bot = Bot(test_brain, bot_config) clientid = "test" filename = bot_config.conversations._storage._dir + os.sep + clientid + ".convo" if os.path.exists(filename): os.remove(filename) self.assertFalse(os.path.exists(filename)) conversation = test_bot.get_conversation(clientid) conversation.properties['name'] = "fred" test_bot.save_conversation(clientid) self.assertTrue(os.path.exists(filename)) test_bot2 = Bot(test_brain, bot_config) conversation2 = test_bot2.get_conversation(clientid) self.assertIsNotNone(conversation2.property('name')) self.assertEqual('fred', conversation2.property('name')) self.assertTrue(os.path.exists(filename)) if os.path.exists(filename): os.remove(filename) self.assertFalse(os.path.exists(filename))
def test_authenticator_with_empty_config(self): testbot = Bot(Brain(BrainConfiguration()), BotConfiguration()) service = Authenticator(BrainSecurityConfiguration("authentication")) self.assertIsNotNone(service) self.assertIsNotNone(service.configuration) self.assertIsNone(service.get_default_denied_srai()) self.assertFalse(service.authenticate(testbot, "console"))
def test_init(self): testbot = Bot(Brain(BrainConfiguration()), BotConfiguration()) service = ClientIdAuthenticationService(BrainSecurityConfiguration("authentication")) self.assertIsNotNone(service) self.assertTrue(service.authenticate(testbot, "console")) self.assertFalse(service.authenticate(testbot, "anyone"))
def test_bot_defaultresponses(self): test_brain = Brain(BrainConfiguration()) bot = Bot(test_brain, BotConfiguration()) self.assertIsNotNone(bot) self.assertEqual(bot.prompt, ">>> ") self.assertEqual(bot.default_response, "") self.assertEqual(bot.exit_response, "Bye!")
def setUp(self): self.bot = Bot(Brain(BrainConfiguration()), config=BotConfiguration()) self.bot.brain.denormals.process_splits([" dot com ", ".com"]) self.bot.brain.denormals.process_splits([" atsign ", "@"]) self.denormalize = DenormalizePostProcessor() self.punctuation = FormatPunctuationProcessor() self.numbers = FormatNumbersPostProcessor() self.multispaces = RemoveMultiSpacePostProcessor()
def test_service(self): testbot = Bot(Brain(BrainConfiguration()), BotConfiguration()) service = BasicPassThroughAuthenticationService( BrainServiceConfiguration("authentication")) self.assertIsNotNone(service) self.assertIsNotNone(service.configuration) self.assertTrue(service.authenticate(testbot, "console")) self.assertTrue(service.authenticate(testbot, "anyone"))
def test_authorise_success(self): testbot = Bot(Brain(BrainConfiguration()), BotConfiguration()) service = MockClientIdAuthenticationService(BrainSecurityConfiguration("authentication")) service.should_authorised = True self.assertTrue("console" in service.authorised) self.assertTrue(service.authenticate(testbot, "console")) self.assertFalse("unknown" in service.authorised) self.assertTrue(service.authenticate(testbot, "unknown")) self.assertTrue("unknown" in service.authorised)
def test_conversation(self): brain_config = BrainConfiguration() test_brain = Brain(brain_config) bot_config = BotConfiguration() bot_config.conversations._max_histories = 3 test_bot = Bot(test_brain, bot_config) conversation = Conversation("test", test_bot) self.assertIsNotNone(conversation) self.assertIsNotNone(conversation._bot) self.assertIsNotNone(conversation._clientid) self.assertEqual(conversation._clientid, "test") self.assertEqual(0, len(conversation._questions)) self.assertEqual(3, conversation._max_histories) self.assertEqual(1, len(conversation._properties)) with self.assertRaises(Exception): conversation.current_question() with self.assertRaises(Exception): conversation.previous_nth_question(0) question1 = Question.create_from_text(test_bot.brain.tokenizer, "Hello There") conversation.record_dialog(question1) self.assertEqual(question1, conversation.current_question()) with self.assertRaises(Exception): conversation.previous_nth_question(1) question2 = Question.create_from_text(test_bot.brain.tokenizer, "Hello There Again") conversation.record_dialog(question2) self.assertEqual(question2, conversation.current_question()) self.assertEqual(question1, conversation.previous_nth_question(1)) with self.assertRaises(Exception): conversation.previous_nth_question(3) question3 = Question.create_from_text(test_bot.brain.tokenizer, "Hello There Again Again") conversation.record_dialog(question3) self.assertEqual(question3, conversation.current_question()) self.assertEqual(question2, conversation.previous_nth_question(1)) with self.assertRaises(Exception): conversation.previous_nth_question(4) # Max Histories for this test is 3 # Therefore we should see the first question, pop of the stack question4 = Question.create_from_text(test_bot.brain.tokenizer, "Hello There Again Again Again") conversation.record_dialog(question4) self.assertEqual(question4, conversation.current_question()) self.assertEqual(question3, conversation.previous_nth_question(1)) with self.assertRaises(Exception): conversation.previous_nth_question(5)
def test_max_recusion(self): test_brain = Brain(BrainConfiguration()) self.assertIsNotNone(test_brain) bot = Bot(test_brain, BotConfiguration()) self.assertIsNotNone(bot) bot.configuration._default_response = "Sorry, I don't have an answer for that right now" bot.configuration._max_recursion = 0 with self.assertRaises(Exception): response = bot.ask_question("testid", "hello")
def test_get_exit_response_empty_string(self): brain_config = BrainConfiguration() self.assertIsNotNone(brain_config) test_brain = Brain(brain_config) self.assertIsNotNone(test_brain) bot_config = BotConfiguration() self.assertIsNotNone(bot_config) bot = Bot(test_brain, bot_config) self.assertIsNotNone(bot) self.assertEquals("Bye!", bot.get_exit_response("testid"))
def test_get_initial_question_empty_string(self): brain_config = BrainConfiguration() self.assertIsNotNone(brain_config) test_brain = Brain(brain_config) self.assertIsNotNone(test_brain) bot_config = BotConfiguration() self.assertIsNotNone(bot_config) bot = Bot(test_brain, bot_config) self.assertIsNotNone(bot) self.assertEquals("Hello", bot.get_initial_question("testid"))
def test_with_data(self): yaml = YamlConfigurationFile() self.assertIsNotNone(yaml) yaml.load_from_text( """ bot: license_keys: $BOT_ROOT/config/license.keys prompt: ">>>" initial_question: Hi, how can I help you today? initial_question_srai: YINITIALQUESTION default_response: Sorry, I don't have an answer for that! default_response_srai: YDEFAULTRESPONSE empty_string: YEMPTY exit_response: So long, and thanks for the fish! exit_response_srai: YEXITRESPONSE override_properties: true max_question_recursion: 1000 max_question_timeout: 60 max_search_depth: 100 max_search_timeout: 60 spelling: classname: programy.spelling.norvig.NorvigSpellingChecker alphabet: 'abcdefghijklmnopqrstuvwxyz' corpus: $BOT_ROOT/corpus.txt check_before: true check_and_retry: true """, ConsoleConfiguration(), ".") bot_config = BotConfiguration() bot_config.load_configuration(yaml, ".") self.assertEqual("./config/license.keys", bot_config.license_keys) self.assertEqual(">>>", bot_config.prompt) self.assertEqual("Hi, how can I help you today?", bot_config.initial_question) self.assertEqual("YINITIALQUESTION", bot_config.initial_question_srai) self.assertEqual("Sorry, I don't have an answer for that!", bot_config.default_response) self.assertEqual("YDEFAULTRESPONSE", bot_config.default_response_srai) self.assertEqual("So long, and thanks for the fish!", bot_config.exit_response) self.assertEqual("YEXITRESPONSE", bot_config.exit_response_srai) self.assertEqual("YEMPTY", bot_config.empty_string) self.assertEqual(bot_config.max_question_recursion, 1000) self.assertEqual(bot_config.max_question_timeout, 60) self.assertEqual(bot_config.max_search_depth, 100) self.assertEqual(bot_config.max_search_timeout, 60) self.assertTrue(bot_config.override_properties) self.assertIsNotNone(bot_config.spelling) self.assertEqual("programy.spelling.norvig.NorvigSpellingChecker", bot_config.spelling.classname)
def test_get_exit_response_exit_response_only(self): brain_config = BrainConfiguration() self.assertIsNotNone(brain_config) test_brain = Brain(brain_config) self.assertIsNotNone(test_brain) bot_config = BotConfiguration() self.assertIsNotNone(bot_config) bot_config.exit_response = "Default response!" bot = Bot(test_brain, bot_config) self.assertIsNotNone(bot) self.assertEquals("Default response!", bot.get_exit_response("testid"))
def test_get_initial_question_initial_question_only(self): brain_config = BrainConfiguration() self.assertIsNotNone(brain_config) test_brain = Brain(brain_config) self.assertIsNotNone(test_brain) bot_config = BotConfiguration() self.assertIsNotNone(bot_config) bot_config.initial_question = "Default response!" bot = Bot(test_brain, bot_config) self.assertIsNotNone(bot) self.assertEquals("Default response!", bot.get_initial_question("testid"))
def test_get_exit_response_exit_response_srai_no_match(self): brain_config = BrainConfiguration() self.assertIsNotNone(brain_config) test_brain = Brain(brain_config) self.assertIsNotNone(test_brain) bot_config = BotConfiguration() self.assertIsNotNone(bot_config) bot_config.exit_response_srai = "YDEFAULTRESPONSE" bot_config.exit_response = "Default response!" bot = Bot(test_brain, bot_config) self.assertIsNotNone(bot) self.assertEquals("Default response!", bot.get_exit_response("testid"))
def __init__(self, client_configuration, brain_config=None, bot_config=None): if brain_config is None: self._brain_config = BrainConfiguration() else: self._brain_config = brain_config if bot_config is None: self._bot_config = BotConfiguration() else: self._bot_config = bot_config self._client_config = client_configuration
def test_get_initial_question_initial_question_srai_match(self): brain_config = BrainConfiguration() self.assertIsNotNone(brain_config) test_brain = MockBrain(brain_config) test_brain._response = "Y DEFAULT RESPONSE" self.assertIsNotNone(test_brain) bot_config = BotConfiguration() self.assertIsNotNone(bot_config) bot_config.initial_question_srai = "YDEFAULTRESPONSE" bot_config.initial_question = "Default response!" bot = Bot(test_brain, bot_config) self.assertIsNotNone(bot) self.assertEquals("Y DEFAULT RESPONSE", bot.get_initial_question("testid"))
def test_total_search_time(self): test_brain = Brain(BrainConfiguration()) self.assertIsNotNone(test_brain) bot = Bot(test_brain, BotConfiguration()) self.assertIsNotNone(bot) bot._question_start_time = datetime.datetime.now() self.assertTrue(bot.total_search_time() >= 0) bot.configuration._max_question_timeout = -1 bot.check_max_timeout() bot.configuration._max_question_timeout = 0 with self.assertRaises(Exception): bot.check_max_timeout()
def test_bot_init_with_spellchecker(self): test_brain = Brain(BrainConfiguration()) bot_config = BotConfiguration() bot_config.spelling._classname = "programy.utils.spelling.checker.SpellingChecker" bot_config.spelling._corpus = os.path.dirname(__file__) + os.sep + "test_corpus.txt" bot_config.spelling._check_before = True bot_config.spelling._check_and_retry = True bot = Bot(test_brain, bot_config) self.assertIsNotNone(bot) test_sentence = Sentence("locetion") bot.check_spelling_before(test_sentence) self.assertIsNotNone(test_sentence) self.assertEqual("LOCATION", test_sentence.text()) test_sentence = Sentence("locetion") response = bot.check_spelling_and_retry("testid", test_sentence) self.assertIsNone(None)
def test_match_sentence(self): self.parser.parse_from_text( """<?xml version="1.0" encoding="UTF-8"?> <aiml> <category> <pattern>HELLO</pattern> <template>Hiya</template> </category> </aiml> """) self.parser.pattern_parser.dump() bot = Bot(Brain(BrainConfiguration()), config=BotConfiguration()) context = self.parser.match_sentence(bot, "test", Sentence("HELLO"), "*", "*") self.assertIsNotNone(context) self.assertEqual("Hiya", context.template_node().template.resolve(None, None))
def test_without_data(self): yaml = YamlConfigurationFile() self.assertIsNotNone(yaml) yaml.load_from_text(""" bot: """, ConsoleConfiguration(), ".") bot_config = BotConfiguration() bot_config.load_config_section(yaml, ".") self.assertIsNone(bot_config.license_keys) self.assertEqual(">>> ", bot_config.prompt) self.assertEqual("Hello", bot_config.initial_question) self.assertEqual("", bot_config.default_response) self.assertEqual("", bot_config.empty_string) self.assertEqual(10, bot_config.max_recursion) self.assertTrue(bot_config.override_predicates) self.assertIsNotNone(bot_config.spelling)
def test_bot_with_conversation(self): test_brain = Brain(BrainConfiguration()) self.assertIsNotNone(test_brain) bot = Bot(test_brain, BotConfiguration()) self.assertIsNotNone(bot) self.assertFalse(bot.has_conversation("testid")) response = bot.ask_question("testid", "hello") self.assertIsNotNone(response) self.assertTrue(bot.has_conversation("testid")) response = bot.ask_question("testid", "hello") self.assertIsNotNone(response) self.assertTrue(bot.has_conversation("testid")) response = bot.ask_question("testid2", "hello") self.assertIsNotNone(response) self.assertTrue(bot.has_conversation("testid2"))
def setUp(self): self.bot = Bot(Brain(BrainConfiguration()), config=BotConfiguration())