<s> from django . test import TestCase <EOL> from telegrambot . models import Update <EOL> from telegrambot . test import factories <EOL> from django . core . urlresolvers import reverse <EOL> from rest_framework import status <EOL> from telegram . replykeyboardhide import ReplyKeyboardHide <EOL> try : <EOL> from unittest import mock <EOL> except ImportError : <EOL> import mock <EOL> class BaseTestBot ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . bot = factories . BotFactory ( ) <EOL> self . webhook_url = reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : self . bot . token } ) <EOL> self . auth_url = reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : self . bot . user_api . username } ) <EOL> self . update = factories . UpdateLibFactory ( ) <EOL> self . kwargs = { '<STR_LIT>' : '<STR_LIT:application/json>' , } <EOL> def assertUser ( self , model_user , user ) : <EOL> self . assertEqual ( model_user . id , user . id ) <EOL> self . assertEqual ( model_user . first_name , user . first_name ) <EOL> self . assertEqual ( model_user . last_name , user . last_name ) <EOL> self . assertEqual ( model_user . username , user . username ) <EOL> def assertChat ( self , model_chat , chat ) : <EOL> self . assertEqual ( model_chat . id , chat . id ) <EOL> self . assertEqual ( model_chat . type , chat . type ) <EOL> self . assertEqual ( model_chat . title , chat . title ) <EOL> self . assertEqual ( model_chat . username , chat . username ) <EOL> self . assertEqual ( model_chat . first_name , chat . first_name ) <EOL> self . assertEqual ( model_chat . last_name , chat . last_name ) <EOL> def assertMessage ( self , model_message , message ) : <EOL> self . assertEqual ( model_message . message_id , message . message_id ) <EOL> self . assertUser ( model_message . from_user , message . from_user ) <EOL> self . assertChat ( model_message . chat , message . chat ) <EOL> self . assertEqual ( model_message . text , message . text ) <EOL> def assertUpdate ( self , model_update , update ) : <EOL> self . assertEqual ( model_update . update_id , update . update_id ) <EOL> self . assertMessage ( model_update . message , update . message ) <EOL> def assertInKeyboard ( self , button , keyboard ) : <EOL> found = False <EOL> for line in keyboard : <EOL> if button in line : <EOL> found = True <EOL> break <EOL> self . assertTrue ( found ) <EOL> def assertBotResponse ( self , mock_send , command ) : <EOL> args , kwargs = mock_send . call_args <EOL> self . assertEqual ( <NUM_LIT:1> , mock_send . call_count ) <EOL> self . assertEqual ( kwargs [ '<STR_LIT>' ] , self . update . message . chat . id ) <EOL> self . assertEqual ( kwargs [ '<STR_LIT>' ] , command [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> if not command [ '<STR_LIT>' ] [ '<STR_LIT>' ] : <EOL> self . assertTrue ( isinstance ( kwargs [ '<STR_LIT>' ] , ReplyKeyboardHide ) ) <EOL> else : <EOL> self . assertInKeyboard ( command [ '<STR_LIT>' ] [ '<STR_LIT>' ] , kwargs [ '<STR_LIT>' ] . keyboard ) <EOL> self . assertIn ( command [ '<STR_LIT>' ] [ '<STR_LIT:text>' ] , kwargs [ '<STR_LIT:text>' ] . decode ( '<STR_LIT:utf-8>' ) ) <EOL> def _test_message_ok ( self , action , update = None , number = <NUM_LIT:1> ) : <EOL> if not update : <EOL> update = self . update <EOL> with mock . patch ( "<STR_LIT>" , callable = mock . MagicMock ( ) ) as mock_send : <EOL> if '<STR_LIT>' in action : <EOL> update . message . text = action [ '<STR_LIT>' ] <EOL> response = self . client . post ( self . webhook_url , update . to_json ( ) , ** self . kwargs ) <EOL> self . assertEqual ( response . status_code , status . HTTP_200_OK ) <EOL> self . assertBotResponse ( mock_send , action ) <EOL> self . assertEqual ( number , Update . objects . count ( ) ) <EOL> self . assertUpdate ( Update . objects . get ( update_id = update . update_id ) , update ) <EOL> def _test_message_no_handler ( self , action , update = None , number = <NUM_LIT:1> ) : <EOL> if not update : <EOL> update = self . update <EOL> with mock . patch ( "<STR_LIT>" , callable = mock . MagicMock ( ) ) as mock_send : <EOL> if '<STR_LIT>' in action : <EOL> update . message . text = action [ '<STR_LIT>' ] <EOL> response = self . client . post ( self . webhook_url , update . to_json ( ) , ** self . kwargs ) <EOL> self . assertEqual ( response . status_code , status . HTTP_200_OK ) <EOL> self . assertEqual ( <NUM_LIT:0> , mock_send . call_count ) <EOL> self . assertEqual ( number , Update . objects . count ( ) ) <EOL> self . assertUpdate ( Update . objects . get ( update_id = update . update_id ) , update ) </s>
