def test_persistence(self): client = TestClient() client_context = client.create_client_context("testid") 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 client_context.bot = Bot(bot_config) filename = bot_config.conversations._storage._dir + os.sep + client_context.userid + ".convo" if os.path.exists(filename): os.remove(filename) self.assertFalse(os.path.exists(filename)) conversation = client_context.bot.get_conversation(client_context) conversation.properties['name'] = "fred" client_context.bot.save_conversation(client_context.userid) self.assertTrue(os.path.exists(filename)) test_bot2 = Bot(bot_config) conversation2 = test_bot2.get_conversation(client_context) 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_bot_chat_loop(self): bot = Bot(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(self._client_context, "hello") self.assertIsNotNone(response) self.assertEqual(response, "Sorry, I don't have an answer for that right now") response = bot.ask_question(self._client_context, "hello again") self.assertIsNotNone(response) self.assertEqual(response, "Sorry, I don't have an answer for that right now") response = bot.ask_question(self._client_context, "goodbye") self.assertIsNotNone(response) self.assertEqual(response, "Sorry, I don't have an answer for that right now") conversation = bot.get_conversation(self._client_context) self.assertIsNotNone(conversation) self.assertEqual(conversation.previous_nth_question(2).sentence(0).text(), "hello") self.assertEqual(conversation.previous_nth_question(2).sentence(0).response, "Sorry, I don't have an answer for that right now") self.assertEqual(conversation.previous_nth_question(1).sentence(0).text(), "hello again") self.assertEqual(conversation.previous_nth_question(1).sentence(0).response, "Sorry, I don't have an answer for that right now") self.assertEqual(conversation.previous_nth_question(0).sentence(0).text(), "goodbye") self.assertEqual(conversation.previous_nth_question(0).sentence(0).response, "Sorry, I don't have an answer for that right now")