Exemplo n.º 1
0
    def test_can_parse_currency_exchange_line_with_shortcuts(self):
        amount, from_, to = parse_exchange_line('$10 to CAD')
        self.assertEqual(10, amount)
        self.assertEqual('USD', from_)
        self.assertEqual('CAD', to)

        amount, from_, to = parse_exchange_line('10 CAD to $')
        self.assertEqual(10, amount)
        self.assertEqual('CAD', from_)
        self.assertEqual('USD', to)
Exemplo n.º 2
0
    def test_raise_error_if_cant_parse_currency_exchange_line(self):
        with self.assertRaises(ValueError):
            parse_exchange_line('')

        with self.assertRaises(ValueError):
            parse_exchange_line('USD to')

        with self.assertRaises(ValueError):
            parse_exchange_line('to USD')
Exemplo n.º 3
0
def exchange_currency(message):
    format_recommendation = 'Please use format like this: \n/exchange 10 USD to THB.'

    chat_id = message['chat']['id']
    exchange_message_text = message['text'].lstrip('/exchange').strip()
    if not exchange_message_text:
        return send_message(chat_id, format_recommendation)
    try:
        amount, from_, to = parse_exchange_line(exchange_message_text)
    except ValueError as ex:
        return send_message(chat_id, f'{str(ex)}\n{format_recommendation}')
    try:
        exchanged_amount = amount * get_exchange_rate(from_, to)
    except ValueError as ex:
        return send_message(chat_id, f'{str(ex)}')
    return send_message(
        chat_id, f'{amount:.2f}  {from_} ~ {exchanged_amount:.2f} {to}')
Exemplo n.º 4
0
 def test_can_parse_currency_exchange_line_without_amount(self):
     amount, from_, to = parse_exchange_line('USD to CAD')
     self.assertEqual(1, amount)
     self.assertEqual('USD', from_)
     self.assertEqual('CAD', to)
Exemplo n.º 5
0
 def test_can_parse_currency_exchange_line_register_of_symbols_not_have_meaning(self):
     amount, from_, to = parse_exchange_line('10 Usd to cAD')
     self.assertEqual(10, amount)
     self.assertEqual('USD', from_)
     self.assertEqual('CAD', to)
Exemplo n.º 6
0
 def test_can_parse_currency_exchange_line_without_whitespaces(self):
     amount, from_, to = parse_exchange_line('12.7531USDtoCAD')
     self.assertEqual('12.7531', str(amount))
     self.assertEqual('USD', from_)
     self.assertEqual('CAD', to)
Exemplo n.º 7
0
 def test_can_parse_currency_exchange_line_with_decimal_value(self):
     amount, from_, to = parse_exchange_line('12.7531 USD to CAD')
     self.assertEqual('12.7531', str(amount))
     self.assertEqual('USD', from_)
     self.assertEqual('CAD', to)
Exemplo n.º 8
0
 def test_can_parse_currency_exchange_line_with_full_specified_names(self):
     amount, from_, to = parse_exchange_line('10 USD to CAD')
     self.assertEqual(10, amount)
     self.assertEqual('USD', from_)
     self.assertEqual('CAD', to)