Exemplo n.º 1
0
    def test_rate(self):
        ''' Tests rating of video. '''

        configure_for_unittest()
        from command import rate
        from model import Chat, Show, Season, Episode, Video

        chat = Chat(id=1).save()

        show = Show(title='House of Cards').save()
        season1 = Season(show=show, number=1).save()
        season2 = Season(show=show, number=2).save()
        episode1 = Episode(season=season1,
                           number=1,
                           release_date=dt(2010, 1, 1)).save()
        episode2 = Episode(season=season1, number=2).save()
        episode3 = Episode(season=season2, number=1).save()
        video1 = Video(link='link to video').save()
        video2 = Video(link='one more link').save()

        show.seasons.connect(season1)
        show.seasons.connect(season2)
        season1.episodes.connect(episode1)
        season1.episodes.connect(episode2)
        season2.episodes.connect(episode3)
        episode1.videos.connect(video1)
        episode1.videos.connect(video2)
        chat.rated_videos.connect(video1, {'value': 1})

        bot = FakeBot()
        upd = FakeUpdate(message=FakeMessage(chat=FakeChat(id=1)))

        # positive review
        upd.message.text = ' '.join(
            ['/rate link to video',
             unicode(Emoji.THUMBS_UP_SIGN, 'utf-8')])
        rate(bot, upd)

        self.assertEqual(bot.sended_message['text'],
                         _('Thanks for the feedback!'))
        self.assertEqual(bot.sended_message['chat_id'], 1)
        self.assertEqual(type(bot.sended_message['reply_markup']),
                         ReplyKeyboardHide)

        # negative review
        upd.message.text = ' '.join(
            ['/rate link to video',
             unicode(Emoji.THUMBS_DOWN_SIGN, 'utf-8')])
        rate(bot, upd)

        self.assertEqual(bot.sended_message['text'], 'one more link')
        self.assertEqual(bot.sended_message['chat_id'], 1)
        self.assertEqual(type(bot.sended_message['reply_markup']),
                         ReplyKeyboardMarkup)
        self.assertEqual(bot.sended_message['reply_markup'].keyboard,
                         [[Emoji.THUMBS_UP_SIGN, Emoji.THUMBS_DOWN_SIGN]])

        # unknown review
        upd.message.text = ' '.join(['/rate link to video', 'unknown'])
        rate(bot, upd)

        self.assertEqual(bot.sended_message['text'],
                         _('Please rate the video.'))
        self.assertEqual(bot.sended_message['chat_id'], 1)
        self.assertEqual(type(bot.sended_message['reply_markup']),
                         ReplyKeyboardMarkup)
        self.assertEqual(bot.sended_message['reply_markup'].keyboard,
                         [[Emoji.THUMBS_UP_SIGN, Emoji.THUMBS_DOWN_SIGN]])

        chat.referer = ''
        chat.save()
Exemplo n.º 2
0
    def test_default(self):
        ''' Tests responses on messages that are not commands. '''

        configure_for_unittest()
        from command import default, subscribe, unsubscribe, setlanguage, watch
        from model import Chat, Show, Season, Episode, Video

        chat = Chat(id=1).save()

        show = Show(title='House of Cards').save()
        season1 = Season(show=show, number=1).save()
        season2 = Season(show=show, number=2).save()
        episode1 = Episode(season=season1,
                           number=1,
                           release_date=dt(2010, 1, 1)).save()
        episode2 = Episode(season=season1, number=2).save()
        episode3 = Episode(season=season2, number=1).save()
        video1 = Video(link='link to video').save()
        video2 = Video(link='one more link').save()

        show.seasons.connect(season1)
        show.seasons.connect(season2)
        season1.episodes.connect(episode1)
        season1.episodes.connect(episode2)
        season2.episodes.connect(episode3)
        episode1.videos.connect(video1)
        episode1.videos.connect(video2)
        chat.rated_videos.connect(video1, {'value': 1})

        bot = FakeBot()
        upd = FakeUpdate(message=FakeMessage(chat=FakeChat(id=1)))

        # random input
        upd.message.text = 'random input'
        default(bot, upd)

        self.assertTrue('You can control' in bot.sended_message['text'])

        # subscribe in 2 steps
        upd.message.text = '/subscribe'
        subscribe(bot, upd)

        upd.message.text = 'house of cards'
        default(bot, upd)

        self.assertEqual(bot.sended_message['text'],
                         _('You have subscribed to the show.'))

        # unsubscribe in 2 steps
        upd.message.text = '/unsubscribe'
        unsubscribe(bot, upd)

        upd.message.text = 'house of cards'
        default(bot, upd)

        self.assertEqual(bot.sended_message['text'],
                         _('You have unsubscribed from the show.'))

        # setlanguage in 2 steps
        upd.message.text = '/setlanguage'
        setlanguage(bot, upd)

        upd.message.text = 'en'
        default(bot, upd)

        self.assertEqual(bot.sended_message['text'],
                         _('You are already using this language!'))

        # watch in 4 steps
        upd.message.text = '/watch'
        watch(bot, upd)

        self.assertEqual(bot.sended_message['text'],
                         _('Which TV show would you like to see?'))

        upd.message.text = 'house of cards'
        default(bot, upd)

        self.assertEqual(bot.sended_message['text'],
                         _('Which season would you like to see?'))

        upd.message.text = '1'
        default(bot, upd)

        self.assertEqual(bot.sended_message['text'],
                         _('Which episode would you like to see?'))

        upd.message.text = '1'
        default(bot, upd)

        self.assertEqual(bot.sended_message['text'], 'link to video')

        chat.referer = ''
        chat.save()

        # positive review - say 'thank you'
        upd.message.text = '/watch house of cards s1 e1'
        watch(bot, upd)

        upd.message.text = Emoji.THUMBS_UP_SIGN
        default(bot, upd)

        self.assertEqual(bot.sended_message['text'],
                         _('Thanks for the feedback!'))
Exemplo n.º 3
0
    def test_watch(self):
        ''' Tests responses on watch request. '''

        configure_for_unittest()
        from command import watch
        from model import Chat, Show, Season, Episode, Video

        chat = Chat(id=1).save()

        show = Show(title='House of Cards').save()
        season1 = Season(show=show, number=1).save()
        season2 = Season(show=show, number=2).save()
        episode1 = Episode(season=season1,
                           number=1,
                           release_date=dt(2010, 1, 1)).save()
        episode2 = Episode(season=season1, number=2).save()
        episode3 = Episode(season=season2, number=1).save()
        video1 = Video(link='link to video').save()
        video2 = Video(link='one more link').save()

        show.seasons.connect(season1)
        show.seasons.connect(season2)
        season1.episodes.connect(episode1)
        season1.episodes.connect(episode2)
        season2.episodes.connect(episode3)
        episode1.videos.connect(video1)
        episode1.videos.connect(video2)
        chat.rated_videos.connect(video1, {'value': 1})

        bot = FakeBot()
        upd = FakeUpdate(message=FakeMessage(chat=FakeChat(id=1)))

        # valid request
        upd.message.text = '/watch house of cards s1 e1'
        watch(bot, upd)

        self.assertEqual(bot.sended_message['text'], 'link to video')
        self.assertEqual(bot.sended_message['chat_id'], 1)
        self.assertEqual(type(bot.sended_message['reply_markup']),
                         ReplyKeyboardMarkup)
        self.assertEqual(bot.sended_message['reply_markup'].keyboard,
                         [[Emoji.THUMBS_UP_SIGN, Emoji.THUMBS_DOWN_SIGN]])

        chat.referer = ''  # bot is waiting for feedback
        chat.save()

        # valid request - without arguments
        upd.message.text = '/watch'
        watch(bot, upd)

        chat.refresh()

        self.assertEqual('/watch', chat.referer)
        self.assertEqual(bot.sended_message['text'],
                         _('Which TV show would you like to see?'))
        self.assertEqual(bot.sended_message['chat_id'], 1)
        self.assertEqual(type(bot.sended_message['reply_markup']),
                         ReplyKeyboardHide)

        # valid request - continue with show_title
        upd.message.text = ' '.join([chat.referer, 'house of cards'])
        watch(bot, upd)

        chat.refresh()

        self.assertEqual(chat.referer, '/watch house of cards')
        self.assertEqual(bot.sended_message['text'],
                         _('Which season would you like to see?'))
        self.assertEqual(bot.sended_message['chat_id'], 1)
        self.assertEqual(type(bot.sended_message['reply_markup']),
                         ReplyKeyboardMarkup)
        self.assertEqual(bot.sended_message['reply_markup'].keyboard, [['1']])

        # valid request - continue with seasons number
        upd.message.text = ' '.join([chat.referer, 's1'])
        watch(bot, upd)

        chat.refresh()

        self.assertEqual(chat.referer, '/watch house of cards s1')
        self.assertEqual(bot.sended_message['text'],
                         _('Which episode would you like to see?'))
        self.assertEqual(bot.sended_message['chat_id'], 1)
        self.assertEqual(type(bot.sended_message['reply_markup']),
                         ReplyKeyboardMarkup)
        self.assertEqual(bot.sended_message['reply_markup'].keyboard, [['1']])

        # valid request - continue with episode number
        upd.message.text = ' '.join([chat.referer, 'e1'])
        watch(bot, upd)

        chat.refresh()

        self.assertEqual(chat.referer, '/rate link to video')
        self.assertEqual(bot.sended_message['text'], 'link to video')
        self.assertEqual(bot.sended_message['chat_id'], 1)
        self.assertEqual(type(bot.sended_message['reply_markup']),
                         ReplyKeyboardMarkup)
        self.assertEqual(bot.sended_message['reply_markup'].keyboard,
                         [[Emoji.THUMBS_UP_SIGN, Emoji.THUMBS_DOWN_SIGN]])

        chat.referer = ''
        chat.save()

        # trying to watch non-existent TV show
        upd.message.text = '/watch kitchen'
        watch(bot, upd)

        self.assertEqual(bot.sended_message['text'],
                         _('This TV show is not available.'))
        self.assertEqual(bot.sended_message['chat_id'], 1)
        self.assertEqual(type(bot.sended_message['reply_markup']),
                         ReplyKeyboardHide)

        # trying to watch non-existent season
        upd.message.text = '/watch house of cards s5'
        watch(bot, upd)

        self.assertEqual(bot.sended_message['text'],
                         _('This season is not available.'))
        self.assertEqual(bot.sended_message['chat_id'], 1)
        self.assertEqual(type(bot.sended_message['reply_markup']),
                         ReplyKeyboardHide)

        # trying to watch unavailable season
        upd.message.text = '/watch house of cards s2 e1'
        watch(bot, upd)

        self.assertEqual(bot.sended_message['text'],
                         _('This season is not available.'))
        self.assertEqual(bot.sended_message['chat_id'], 1)
        self.assertEqual(type(bot.sended_message['reply_markup']),
                         ReplyKeyboardHide)

        # trying to watch non-existent episode
        upd.message.text = '/watch house of cards s1 e5'
        watch(bot, upd)

        self.assertEqual(bot.sended_message['text'],
                         _('This episode is not available.'))
        self.assertEqual(bot.sended_message['chat_id'], 1)
        self.assertEqual(type(bot.sended_message['reply_markup']),
                         ReplyKeyboardHide)

        # trying to watch unavailable episode
        upd.message.text = '/watch house of cards s1 e2'
        watch(bot, upd)

        self.assertEqual(bot.sended_message['text'],
                         _('This episode is not available.'))
        self.assertEqual(bot.sended_message['chat_id'], 1)
        self.assertEqual(type(bot.sended_message['reply_markup']),
                         ReplyKeyboardHide)