예제 #1
0
 def test_no_data(self):
     '''
     rate file is empty
     :return:
     '''
     converse = ConvertRate(from_currency=POLISH_ZLOTY, to_currency=CZECH_KRONA, amount=1234.89)
     with self.assertRaises(IOError):
         converse.convert()
예제 #2
0
 def test_empty_from(self):
     '''
     ther is no info about from_currency in the rates
     :return:
     '''
     converse = ConvertRate(from_currency=US_DOLLAR, to_currency=CZECH_KRONA, amount=1000)
     del self.rates[US_DOLLAR]
     with self.assertRaises(AttributeError):
         converse.find_rate(self.rates)
예제 #3
0
 def test_empty_to(self):
     '''
     in the rates does not info about rate for to_currency
     :return:
     '''
     converse = ConvertRate(from_currency=US_DOLLAR, to_currency=CZECH_KRONA, amount=1000)
     del self.rates[US_DOLLAR][CZECH_KRONA]
     finded_rate = converse.find_rate(self.rates)
     self.assertIsNone(finded_rate)
예제 #4
0
class FormatAmountTest(ConvertRateTestBase):
    def setUp(self):
        super(FormatAmountTest, self).setUp()
        self.converse = ConvertRate(from_currency=CZECH_KRONA, to_currency=US_DOLLAR, amount=1000)


    def test_double_digit(self):
        formated_amount = self.converse.format_amount(23.45)
        self.assertEqual('23.45', formated_amount)

    def test_int_double_digit(self):
        formated_amount = self.converse.format_amount(23)
        self.assertEqual('23.00', formated_amount)

    def test_four_digit(self):
        formated_amount = self.converse.format_amount(2323.7634)
        self.assertEqual('2 323.76', formated_amount)

    def test_seven_digit(self):
        formated_amount = self.converse.format_amount(2345872.4587)
        self.assertEqual('2 345 872.46', formated_amount)

    def test_none(self):
        with self.assertRaises(ValueError):
            self.converse.format_amount(None)

    def test_string(self):
        with self.assertRaises(ValueError):
            self.converse.format_amount('abra')

    def test_null(self):

        formated_amount = self.converse.format_amount(0)
        self.assertEqual('0.00', formated_amount)
예제 #5
0
class GetRateFromFileTest(ConvertRateTestBase):

    def setUp(self):
        super(GetRateFromFileTest, self).setUp()
        self.converse = ConvertRate(from_currency=CZECH_KRONA, to_currency=US_DOLLAR, amount=1000)

    def test_success(self):
        with open(settings.RATES_FNAME, 'w') as f:
            json.dump(self.rates, f, indent=2)

        last_rates = self.converse.get_last_rate_from_file()
        self.assertEquals(self.rates, last_rates)

    def test_file_not_exist(self):
        with self.assertRaises(IOError):
            self.converse.get_last_rate_from_file()
예제 #6
0
 def test_empty(self):
     converse = ConvertRate(from_currency=POLISH_ZLOTY, to_currency=EURO, amount=1000)
     with self.assertRaises(AttributeError):
         converse.find_rate({})
예제 #7
0
 def test_pln_eur(self):
     converse = ConvertRate(from_currency=POLISH_ZLOTY, to_currency=EURO, amount=1000)
     finded_rate = converse.find_rate(self.rates)
     self.assertEqual(0.2324662343, finded_rate)
예제 #8
0
 def test_usd_czk(self):
     converse = ConvertRate(from_currency=US_DOLLAR, to_currency=CZECH_KRONA, amount=1000)
     finded_rate = converse.find_rate(self.rates)
     self.assertEqual(22.5518744551, finded_rate)
예제 #9
0
 def setUp(self):
     super(GetRateFromFileTest, self).setUp()
     self.converse = ConvertRate(from_currency=CZECH_KRONA, to_currency=US_DOLLAR, amount=1000)
예제 #10
0
 def test_wrong_amount(self):
     self.__create_rate_file()
     converse = ConvertRate(from_currency=POLISH_ZLOTY, to_currency=CZECH_KRONA, amount='kjhkjg')
     with self.assertRaises(TypeError):
         converse.convert()
예제 #11
0
 def test_wrong_json(self):
     with open(settings.RATES_FNAME, 'w') as f:
         pass
     converse = ConvertRate(from_currency=POLISH_ZLOTY, to_currency=CZECH_KRONA, amount=1234.89)
     with self.assertRaises(ValueError):
         converse.convert()
예제 #12
0
 def test_success_pln_czk(self):
     self.__create_rate_file()
     converse = ConvertRate(from_currency=POLISH_ZLOTY, to_currency=CZECH_KRONA, amount=1234.89)
     converted_amount = converse.convert()
     self.assertEqual('7 425.65', converted_amount)
예제 #13
0
 def test_success_eur_usd(self):
     self.__create_rate_file()
     converse = ConvertRate(from_currency=EURO, to_currency=US_DOLLAR, amount=1000)
     converted_amount = converse.convert()
     self.assertEqual('1 147.00', converted_amount)
예제 #14
0
 def test_success_czk_usd(self):
     self.__create_rate_file()
     converse = ConvertRate(from_currency=CZECH_KRONA, to_currency=US_DOLLAR, amount=1000)
     converted_amount = converse.convert()
     self.assertEqual('44.34', converted_amount)
예제 #15
0
def currency_converse(request):
    '''
    POST: /api/currency/converse
    :param request:
    :return:
    '''
    if request.method == 'POST':
        errors = []
        from_currency = request.POST.get('from_currency', '').strip()

        if not from_currency:
            errors.append('Param "from_currency" can\'t be empty')
        if from_currency not in CURRENCIES:
            errors.append(
                'Param "from_currency" must be one of "{}" values'.format(
                    ','.join(CURRENCIES)))

        to_currency = request.POST.get('to_currency', '').strip()
        if not to_currency:
            errors.append('Param "to_currency" can\'t be empty')
        if to_currency not in CURRENCIES:
            errors.append(
                'Param "to_currency" must be one of "{}" values'.format(
                    ','.join(CURRENCIES)))

        try:
            amount = float(request.POST.get('amount'))
        except ValueError:
            errors.append('Bad type of "amount" param')

        if errors:
            return JsonResponse({
                'status': 400,
                'error': {
                    'code': 1,
                    'message': errors
                }
            })

        converse = ConvertRate(from_currency=from_currency,
                               to_currency=to_currency,
                               amount=amount)

        try:
            converted_amount = converse.convert()
        except Exception as e:
            return JsonResponse({
                'status': 500,
                'error': {
                    'code': 2,
                    'message': 'Internal error'
                }
            })
        return JsonResponse({
            'converted_amount': converted_amount,
            'currency': to_currency
        })
    else:
        return HttpResponseNotAllowed([
            'POST',
        ])


# end