def setUp(self): self.bot = Bot(test=True) self.bot.students = {'11': BotUserHandler()} self.student = self.bot.students['11'] self.update = Update() self.prep = Preparations() self.prep.create_langs() self.update.effective_user.id = 11
def setUp(self): prep = Preparations() prep.create_langs() prep.create_cats() prep.create_user() prep.create_global_words() self.bot = Bot(test=True) self.update = Update()
class TestRegister(TestCase): def setUp(self): self.bot = Bot(test=True) self.bot.students = {'11': BotUserHandler()} self.student = self.bot.students['11'] self.update = Update() self.prep = Preparations() self.prep.create_langs() self.update.effective_user.id = 11 def test_1_register_first_name(self): self.student.destination = 'Register first name' self.update.message.text = 'User' self.bot.echo(self.update, context) self.assertEqual(self.update.message.test_text, 'Okay User. Now choose your home language') def test_2_register_home_language(self): self.student.destination = 'Register home language' self.student.callback_data = ['English'] self.update.callback_query.data = '0' self.bot.echo(self.update, context) self.assertEqual(self.update.callback_query.message.test_text, ' -:- ') def test_3_register_current_language(self): self.student.destination = 'Register current language' self.student.callback_data = ['Japanese'] self.update.callback_query.data = '0' self.bot.dispatch_destination(self.update, context, self.student, 'Register current language') self.assertEqual( self.update.callback_query.message.test_text, 'To use our service you need to accept Privacy Policy.' ' You can read it at https://linguint.pro/privacy_policy . ' 'Do you accept Privacy Policy?') def test_4_accept_privacy_policy(self): self.student.destination = 'Accept privacy policy' self.student.callback_data = ['Yes', 'No'] self.update.callback_query.data = '0' self.student.temp_data = { 'username': 11, 'first_name': 'User', 'home_language': 'English', 'current_language': 'Japanese', } self.bot.echo(self.update, context) self.assertEqual(self.update.message.test_text, 'Menu[You can always go back to /menu]') self.student.destination = 'Accept privacy policy' self.student.callback_data = ['Yes', 'No'] self.update.callback_query.data = '1' self.bot.echo(self.update, context) self.assertEqual( self.update.callback_query.message.test_text, 'Registration aborted. Write /start for other' ' options.')
def setUp(self): self.prep = Preparations() self.prep.create_cats() self.prep.create_langs() self.prep.create_global_words() self.prep.create_user() self.update = Update() self.bot = Bot(test=True) self.bot.start(self.update, context) self.student = self.bot.students['42']
class TestTelegramBotCore(TestCase): def setUp(self): prep = Preparations() prep.create_langs() prep.create_cats() prep.create_user() prep.create_global_words() self.bot = Bot(test=True) self.update = Update() def change_id(self): self.update.effective_user.id = 11 def restore_id(self): self.update.effective_user.id = 42 def test_start(self): self.bot.start(self.update, context) self.assertEqual(self.update.message.test_text, 'Welcome test user. Type anything or /menu to show variants!') self.change_id() self.bot.start(self.update, context) self.assertEqual(self.update.message.test_text, 'Welcome stranger!' \ 'If you have account on web site type /link ' \ 'If you new one type /register') self.restore_id() def test_register(self): self.change_id() self.bot.students['11'] = BotUserHandler() self.bot.register(self.update, context) self.assertEqual(self.update.message.test_text, 'To start learning tell a little bit more about yourself.' \ 'What is your name?') self.assertEqual(self.bot.students['11'].destination, 'Register first name') self.restore_id() def test_link_telegram(self): self.change_id() self.bot.students['11'] = BotUserHandler() self.bot.link_telegram(self.update, context) self.assertEqual(self.update.message.test_text, 'Enter your username:'******'11'].destination, 'Take username') self.restore_id() def test_help(self): self.bot.help(self.update, context, None) self.assertEqual(self.update.message.test_text, 'Have any questions? Write it to [email protected]') def test_echo(self): self.bot.echo(self.update, context) self.assertEqual(self.update.message.test_text, 'Welcome test user. Type anything or /menu to show variants!') self.bot.students['42'].destination = 'Help' self.bot.echo(self.update, context) self.assertEqual(self.update.message.test_text, 'Have any questions? Write it to [email protected]') self.bot.students['42'].destination = 'jjhsdf' self.bot.echo(self.update, context) self.assertEqual(self.update.message.test_text, 'Menu[You can always go back to /menu]') def test_menu(self): self.bot.students['42'] = BotUserHandler() self.bot.menu(self.update, context, self.bot.students['42']) self.assertEqual(self.update.message.test_text, 'Menu[You can always go back to /menu]') menu_list = [ 'Add words', 'My words', 'Learn words', 'Play matching', 'Play reversed matching', 'Play typing', 'Play reversed typing', 'Help', 'Change learn language', 'Add more learn language', ] self.assertEqual(self.bot.students['42'].callback_data, menu_list) def test_callback_handler(self): self.bot.students['42'] = BotUserHandler() self.bot.students['42'].destination = 'Help' self.bot.callback_handler(self.update, context) self.assertEqual(self.update.message.test_text, 'Have any questions? Write it to [email protected]') self.bot.students['42'].destination = 'jjhsdf' self.bot.callback_handler(self.update, context) self.assertEqual(self.update.message.test_text, 'Menu[You can always go back to /menu]') def test_delete(self): self.bot.start(self.update, context) self.bot.delete(self.update, context) self.assertEqual(self.update.message.test_text, 'User deleted') with self.assertRaises(ObjectDoesNotExist): Student.objects.get(username="******") Preparations().create_user() def test_menu_action(self): self.bot.start(self.update, context) self.bot.students['42'].callback_data = ['Help'] self.bot.menu_action(self.update, context) self.assertEqual(self.update.message.test_text, 'Have any questions? Write it to [email protected]') self.bot.students['42'].callback_data = ['dsfsd'] self.bot.menu_action(self.update, context) self.assertEqual(self.update.message.test_text, 'Menu[You can always go back to /menu]') def test_change_learn_language(self): self.bot.start(self.update, context) self.bot.change_learn_language(self.update, context) self.assertEqual(self.update.callback_query.message.test_text, 'Choose your learn language[You can always go back to /menu]') def test_add_more_learn_language(self): self.bot.start(self.update, context) self.bot.add_more_learn_language(self.update, context) self.assertEqual(self.update.callback_query.message.test_text, 'Choose your new learn language[You can always go back to /menu]') def test_change_language(self): self.bot.start(self.update, context) self.bot.students['42'].callback_data = ['English', 'Japanese'] self.bot.change_language(self.update, context) self.assertEqual(self.update.message.test_text, 'Alright! Good luck![You can always go back to /menu]')
class TestAddwords(TestCase): def setUp(self): self.prep = Preparations() self.prep.create_cats() self.prep.create_langs() self.prep.create_global_words() self.prep.create_user() self.update = Update() self.bot = Bot(test=True) self.bot.start(self.update, context) self.student = self.bot.students['42'] def test_add_words(self): self.student.destination = 'Add words' self.bot.echo(self.update, context) self.assertEqual( self.update.message.test_text, 'How do you want to add word?[You can always go back to /menu]') # self.assertEqual(self.update.message.test_text, '[You can always go back to /menu]') def test_add_words_option(self): self.student.destination = 'Add words option' self.student.callback_data = ['Add word by typing'] self.update.callback_query.data = '0' self.bot.echo(self.update, context) self.assertEqual( self.update.message.test_text, 'Enter foreign word[You can always go back to /menu]') self.student.destination = 'Add words option' self.student.callback_data = ['Choose word from presets'] self.bot.echo(self.update, context) self.assertEqual(self.update.message.test_text, 'Choose category[You can always go back to /menu]') def test_add_custom_word(self): self.student.destination = 'Add custom word' self.student.temp_data = { 'word_name': 'おはよう', 'translation': 'good morning', 'pronunciation': 'ohayo' } self.student.callback_data = ['Default'] self.update.callback_query.data = '0' self.bot.echo(self.update, context) self.assertEqual( self.update.message.test_text, 'Word has been added[You can always go back to /menu]') def test_add_word_translation(self): self.student.destination = 'Add word translation' self.bot.echo(self.update, context) self.assertEqual(self.student.temp_data['translation'], self.update.message.text) self.assertEqual( self.update.message.test_text, 'Now choose category.[You can always go back to /menu]') def test_search_word(self): self.update.message.text = 'ありがとう' self.student.destination = 'Search word' self.bot.echo(self.update, context) self.assertEqual(self.update.message.test_text, 'Menu[You can always go back to /menu]') def test_translation_option(self): self.student.destination = 'Translation option' self.student.callback_data = ['Yes', 'No'] self.update.callback_query.data = '0' self.bot.echo(self.update, context) self.assertEqual( self.update.message.test_text, 'Enter your translation:[You can always go back to /menu]') self.student.destination = 'Translation option' self.update.callback_query.data = '1' self.bot.echo(self.update, context) self.assertEqual( self.update.message.test_text, 'Now choose category.[You can always go back to /menu]') def test_show_category_words(self): self.student.destination = 'Show category words' self.student.callback_data = ['Default'] self.update.callback_query.data = '0' self.bot.echo(self.update, context) self.assertEqual( self.update.message.test_text, 'Word [ありがとう]' '\nPronunciation [No pronunciation]' '\nTranslation [Thank you]') def test_add_from_global_word(self): self.student.destination = 'Add from global word' self.update.callback_query.data = '0' self.student.temp_data = {'words': GlobalWord.objects.all()} self.bot.echo(self.update, context) self.assertEqual(self.update.message.test_text, 'Word(s) added[You can always go back to /menu]')
class TestLearnwords(TestCase): def setUp(self): self.prep = Preparations() self.prep.create_cats() self.prep.create_langs() self.prep.create_global_words() self.prep.create_user() self.update = Update() self.bot = Bot(test=True) self.bot.start(self.update, context) self.student = self.bot.students['42'] def test_learn_words(self): self.student.destination = 'Learn words' self.bot.echo(self.update, context) self.assertEqual(self.update.message.test_text, 'No words to learn') self.prep.create_user_words() self.student.destination = 'Learn words' self.bot.echo(self.update, context) self.assertEqual( self.update.message.test_text, 'Word [ありがとう]' '\nPronunciation [No pronunciation]' '\nTranslation [Thank you]\n[You can always go back to /menu]') def test_learn_word_status(self): self.prep.create_user_words() self.student.destination = 'Learn word status' self.student.temp_data = {'word': self.prep.get_user_word()} self.update.callback_query.data = '0' self.bot.echo(self.update, context) self.assertEqual( self.update.message.test_text, 'Word [ありがとう]' '\nPronunciation [No pronunciation]' '\nTranslation [Thank you]\n[You can always go back to /menu]') def test_play_matching(self): self.prep.delete_user_words() self.student.destination = 'Play matching' self.bot.echo(self.update, context) self.assertEqual(self.update.message.test_text, 'Menu[You can always go back to /menu]') self.student.destination = 'Play reversed matching' self.bot.echo(self.update, context) self.assertEqual(self.update.message.test_text, 'Menu[You can always go back to /menu]') self.prep.create_user_words() self.student.destination = 'Play matching' self.bot.echo(self.update, context) self.assertEqual(self.update.message.test_text, 'ありがとう[You can always go back to /menu]') self.student.destination = 'Play reversed matching' self.bot.echo(self.update, context) self.assertEqual(self.update.message.test_text, 'Thank you[You can always go back to /menu]') def test_play_matching_result(self): self.prep.delete_user_words() self.prep.create_user_words() self.student.destination = 'Play matching result' self.student.temp_data = {'answer': self.prep.get_user_word()} self.student.callback_data = ['Thank you'] self.update.callback_query.data = 0 self.bot.echo(self.update, context) self.assertEqual(self.update.message.test_text, 'Right[You can always go back to /menu]') self.student.destination = 'Play matching result' self.student.temp_data = {'answer': self.prep.get_user_word()} self.student.callback_data = ['sdgdrfgdsf'] self.update.callback_query.data = 0 self.bot.echo(self.update, context) self.assertEqual(self.update.message.test_text, 'Wrong: Thank you[You can always go back to /menu]') def test_typing(self): self.prep.delete_user_words() self.prep.create_user_words() self.student.destination = 'Play typing' self.bot.echo(self.update, context) self.assertEqual(self.update.message.test_text, 'ありがとう[You can always go back to /menu]') self.student.destination = 'Play reversed typing' self.bot.echo(self.update, context) self.assertEqual(self.update.message.test_text, 'Thank you[You can always go back to /menu]') def test_play_typing_result(self): self.prep.delete_user_words() self.prep.create_user_words() self.student.destination = 'Play typing result' self.student.temp_data = {'answer': self.prep.get_user_word()} self.update.message.text = 'Thank you' self.bot.echo(self.update, context) self.assertEqual(self.update.message.test_text, 'Right[You can always go back to /menu]') self.student.destination = 'Play matching result' self.student.temp_data = {'answer': self.prep.get_user_word()} self.update.message.text = 'kjkdjf' self.bot.echo(self.update, context) self.assertEqual(self.update.message.test_text, 'Wrong: Thank you[You can always go back to /menu]')
from telegram_bot.bot import Bot from default_config import DefaultConfig cfg = DefaultConfig.load_default_config() TelegramBot = Bot.load_from_config(cfg.telegram_bot)
def handle(self, *args, **options): print('Running bot') Bot()