예제 #1
0
 def test_load_rates(self):
     self.retriever = RateRetriever(MOCKED_RATES_FILE)
     self.retriever.conversion_rates = {
         "disclaimer":
         "Usage subject to terms: https://openexchangerates.org/terms",
         "license": "https://openexchangerates.org/license",
         "timestamp": "TEST",
         "base": "USD",
         "rates": {
             "CZK": 22.9508,
             "EUR": 0.89194,
             "PLN": 3.8174
         }
     }
     self.retriever._update_rates()
     with open(MOCKED_RATES_FILE, 'r') as rates:
         file_written = json.load(rates)
     self.assertDictEqual(file_written, self.retriever.conversion_rates)
예제 #2
0
def convert():
    if request.method == 'GET':
        from_currency = request.headers.get('from_currency')
        to_currency = request.headers.get('to_currency')
        value = request.headers.get('value')
        retriever = RateRetriever(RATES_FILE)
        converter = CurrencyConverter(from_currency, to_currency, value,
                                      retriever)
        converted_value = converter.convert()
        return json.dumps(converter.build_response(converted_value), indent=4)
예제 #3
0
def convert_multi():
    if request.method == 'POST':
        with open(SWAGGER, 'r') as swagger:
            try:
                schema = yaml.safe_load(swagger)
            except yaml.YAMLError as exc:
                print(exc)
        try:
            validate(request.get_json(), schema)
        except ValidationError as err:
            response = dict()
            response['ERROR'] = str(err)
            return json.dumps(response, indent=4)
        retriever = RateRetriever(RATES_FILE)
        converter = MultiCurrencyConverter(retriever)
        return json.dumps(converter.multi_convert(request.get_json()),
                          indent=4)
예제 #4
0
 def setUp(self):
     self.retriever = RateRetriever(MOCKED_RATES_FILE)
예제 #5
0
 def test_load_rates_no_file(self):
     self.retriever = RateRetriever(None)
     self.assertDictEqual(self.retriever.conversion_rates, {})
예제 #6
0
class TestRateRetriever(unittest.TestCase):
    def setUp(self):
        self.retriever = RateRetriever(MOCKED_RATES_FILE)

    def test_get_rates(self):
        expected_response = {"CZK": 22.9508, "EUR": 0.89194, "PLN": 3.8174}
        response = self.retriever.get_rates(None)
        self.assertEqual(response, expected_response)

    def test_get_rates_with_currency(self):
        expected_response = dict()
        expected_response['EUR'] = 0.89194
        response = self.retriever.get_rates('EUR')
        self.assertDictEqual(response, expected_response)

    def test_currency_not_supported(self):
        expected_response = dict()
        expected_response['ERROR'] = "Currency not supported"
        response = self.retriever.get_rates('KIWI')
        self.assertDictEqual(response, expected_response)

    def test_load_rates_no_file(self):
        self.retriever = RateRetriever(None)
        self.assertDictEqual(self.retriever.conversion_rates, {})

    def test_load_rates(self):
        self.retriever = RateRetriever(MOCKED_RATES_FILE)
        self.retriever.conversion_rates = {
            "disclaimer":
            "Usage subject to terms: https://openexchangerates.org/terms",
            "license": "https://openexchangerates.org/license",
            "timestamp": "TEST",
            "base": "USD",
            "rates": {
                "CZK": 22.9508,
                "EUR": 0.89194,
                "PLN": 3.8174
            }
        }
        self.retriever._update_rates()
        with open(MOCKED_RATES_FILE, 'r') as rates:
            file_written = json.load(rates)
        self.assertDictEqual(file_written, self.retriever.conversion_rates)

    @patch('requests.get', side_effect=mocked_requests_get)
    def test_call_api(self, mock_get):
        expected_get_response = {
            "disclaimer": "NO TERMS",
            "license": "MOCKEDLICENSE",
            "timestamp": 34239429042,
            "base": "USD",
            "rates": {
                "CZK": 2000,
                "EUR": 432,
                "PLN": 4344
            }
        }
        params = {
            'app_id': APP_ID,
            'symbols': CURRENCIES,
            'prettyprint': False,
            'show_alternative': False
        }
        url = 'https://openexchangerates.org/api/latest.json'
        self.retriever.conversion_rates = {}

        self.retriever._call_api(url, params)

        self.assertEqual(expected_get_response,
                         self.retriever.conversion_rates)

    @patch('requests.get', side_effect=mocked_requests_get)
    def test_timestamp(self, mock_get):
        self.retriever.rates_file = MOCKED_TIMESTAMP
        with open(self.retriever.rates_file, 'w') as rates:
            timestamp_5h_old = {
                "timestamp": (datetime.now() - timedelta(hours=5)).timestamp()
            }
            json.dump(timestamp_5h_old, rates, indent=4)

        self.retriever.load_rates()

        self.assertEqual(timestamp_5h_old, self.retriever.conversion_rates)

        with open(self.retriever.rates_file, 'w') as rates:
            timestamp_27h_old = {
                "timestamp":
                (datetime.now() - timedelta(hours=27)).timestamp()
            }
            json.dump(timestamp_27h_old, rates, indent=4)

        self.retriever.load_rates()

        self.assertEqual(34239429042,
                         self.retriever.conversion_rates["timestamp"])

        self.retriever.rates_file = MOCKED_RATES_FILE

    @patch('requests.get', side_effect=mocked_requests_get)
    def test_api_down(self, mock_get):
        expected_get_response = "Unable to call exchange rates API"
        params = {
            'app_id': APP_ID,
            'symbols': CURRENCIES,
            'prettyprint': False,
            'show_alternative': False
        }
        url = 'wrong url'
        self.retriever.conversion_rates = {}

        self.retriever._call_api(url, params)

        self.assertEqual(expected_get_response,
                         self.retriever.response['ERROR'])
예제 #7
0
def rate():
    if request.method == 'GET':
        retriever = RateRetriever(RATES_FILE)
        return json.dumps(retriever.get_rates(request.headers.get('currency')),
                          indent=4)
 def setUp(self):
     retriever = RateRetriever(MOCKED_RATES_FILE)
     self.converter = MultiCurrencyConverter(retriever)