示例#1
0
def do_central_bank(message):
    """
    Функция поиска курса заданной валюты и даты в Центробанке
    :param message: сообщение пользователя
    :return: курс валюты на заданное число
    """
    chat_id = message.chat.id
    Logs.log_message(message)
    try:
        text = str(message.text).lower()
        result = re.findall(r'доллар|евр|фунт|иен|', text)
        result += re.findall(r'\d*-\d*-\d*', text)
        result = list(filter(lambda a: a != '', result))

        if len(result) == 2:
            date_format = "%d-%m-%Y"
            date = datetime.strptime(result[1], date_format)

            text = f'Вот данные из центробанка\n{date.strftime(date_format)}\n'
            bank = CentralBank(date)

            if result[0] == 'доллар':
                rate = bank.get_rates_usd()
                text += f'{rate.name} = {rate.rate} руб.'
                Logs.log_message(bot.send_message(chat_id=chat_id, text=text))

            if result[0] == 'евр':
                rate = bank.get_rates_eur()
                text += f'{rate.name} = {rate.rate} руб.'
                Logs.log_message(bot.send_message(chat_id=chat_id, text=text))

            if result[0] == 'фунт':
                rate = bank.get_rates_gbp()
                text += f'{rate.name} = {rate.rate} руб.'
                Logs.log_message(bot.send_message(chat_id=chat_id, text=text))

            if result[0] == 'иен':
                rate = bank.get_rates_jpy()
                text += f'{rate.name} = {rate.rate} руб.'
                Logs.log_message(bot.send_message(chat_id=chat_id, text=text))
        else:
            text = 'Прости, но ты ввел команду не правильно. Тебя никогда не примут в орден джедаев!'
            Logs.log_message(bot.send_message(chat_id=chat_id, text=text))
    except CbError:
        text = 'Данная дата выходит за пределы. "В пределы поспасть должен ты" © Йода'
        Logs.log_exception(CbError)
        Logs.log_message(bot.send_message(chat_id=chat_id, text=text))
    except ValueError:
        text = 'Ваша дата невалидна. "Головой думать должен ты" © Йода'
        Logs.log_exception(ValueError)
        Logs.log_message(bot.send_message(chat_id=chat_id, text=text))
示例#2
0
 def test_get_rates_jpy_error(self):
     bank_4 = CentralBank(datetime.date(1900, 3, 20))
     with self.assertRaises(CbError):
         bank_4.get_rates_jpy()
示例#3
0
 def test_get_rates_rate_jpy(self):
     bank_1 = CentralBank(datetime.date(2002, 3, 2))
     jpy = bank_1.get_rates_jpy()
     self.assertEqual(23.1527, jpy.rate)
示例#4
0
 def test_get_rates_currency_name_jpy(self):
     bank_1 = CentralBank(datetime.date(2002, 3, 2))
     jpy = bank_1.get_rates_jpy()
     self.assertEqual('JPY', jpy.name)
示例#5
0
 def test_get_rates_rate_gbp(self):
     bank_1 = CentralBank(datetime.date(2002, 3, 2))
     gbp = bank_1.get_rates_gbp()
     self.assertEqual(43.8254, gbp.rate)
示例#6
0
 def test_get_rates_currency_name_gbp(self):
     bank_1 = CentralBank(datetime.date(2002, 3, 2))
     gbp = bank_1.get_rates_gbp()
     self.assertEqual('GBP', gbp.name)
示例#7
0
 def test_get_rates_rate_eur(self):
     bank_1 = CentralBank(datetime.date(2002, 3, 2))
     eur = bank_1.get_rates_eur()
     self.assertEqual(26.8343, eur.rate)
示例#8
0
 def test_get_rates_currency_name_eur(self):
     bank_1 = CentralBank(datetime.date(2002, 3, 2))
     eur = bank_1.get_rates_eur()
     self.assertEqual('EUR', eur.name)
示例#9
0
 def test_get_rates_usd_error(self):
     bank_2 = CentralBank(datetime.date(1900, 3, 20))
     with self.assertRaises(CbError):
         bank_2.get_rates_usd()
示例#10
0
 def test_get_rates_rate_usd(self):
     bank_1 = CentralBank(datetime.date(2002, 3, 2))
     usd = bank_1.get_rates_usd()
     self.assertEqual(30.9436, usd.rate)
示例#11
0
 def test_get_rates_currency_name_usd(self):
     bank_1 = CentralBank(datetime.date(2002, 3, 2))
     usd = bank_1.get_rates_usd()
     self.assertEqual('USD', usd.name)