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()
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()
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()
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)
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)
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)
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