def test_latest_rates_when_date_provided_and_multiple_available( self, database): base = 'EUR' date = '2020-02-01' currency_information_prev = CurrencyInformation( base_currency=base, changes={'GBP': 0.81}, change_date=date, retrieved_at=datetime.now()) db.session.add(currency_information_prev) db.session.commit() currency_information = CurrencyInformation(base_currency=base, changes={ 'GBP': 0.84, 'CAD': 1.45 }, change_date='2020-02-02', retrieved_at=datetime.now()) db.session.add(currency_information) db.session.commit() latest_rate = CurrencyInformation.latest_rates( base, date=parser.parse(date).date()) assert latest_rate.base_currency == currency_information_prev.base_currency assert latest_rate.changes == currency_information_prev.changes assert latest_rate.change_date == currency_information_prev.change_date assert latest_rate.retrieved_at == currency_information_prev.retrieved_at
def test_rate_with_valid_base_and_a_date_filter(self, testapp, database): client = testapp.test_client() another_date = '2020-02-02' date = '2020-02-01' base = 'EUR' currency_information_prev = CurrencyInformation( base_currency = base, changes = { 'GBP': 0.81 }, change_date = date, retrieved_at = datetime.now() ) currency_information = CurrencyInformation( base_currency = base, changes = { 'GBP': 0.71 }, change_date = another_date, retrieved_at = datetime.now() ) database.session.add_all([currency_information_prev, currency_information]) database.session.commit() response = client.get(f'/rates/{base}?date={date}') assert response.status_code == HTTPStatus.OK assert response.json == { 'GBP': 0.81 } response = client.get(f'/rates/{base}?date={another_date}') assert response.status_code == HTTPStatus.OK assert response.json == { 'GBP': 0.71 }
def test_latest_rates_when_no_date_provided_and_only_one_available( self, database): base = 'EUR' date = datetime.now().date() currency_information = CurrencyInformation(base_currency=base, changes={'GBP': 0.81}, change_date=date, retrieved_at=datetime.now()) db.session.add(currency_information) db.session.commit() latest_rate = CurrencyInformation.latest_rates(base) assert latest_rate.base_currency == currency_information.base_currency assert latest_rate.changes == currency_information.changes assert latest_rate.change_date == currency_information.change_date assert latest_rate.retrieved_at == currency_information.retrieved_at
def process(client=FixerClient): rates = client.latest_rates() currency_information = CurrencyInformation( base_currency=rates['base'], changes=rates['rates'], change_date=rates['date'], retrieved_at=datetime.fromtimestamp(rates['timestamp'])) try: db.session.add(currency_information) db.session.commit() except IntegrityError as e: db.session.rollback() print(f'Error ingesting rate:\n {e}')
def rate(base): if base != 'EUR': return 'Currency rate not supported', HTTPStatus.NOT_FOUND if request.args.get('date'): filter_date = parser.parse(request.args.get('date')).date() else: filter_date = datetime.now().date() rates = CurrencyInformation.latest_rates(base, date=filter_date) if rates: rates = rates.changes else: rates = {} return jsonify(rates), HTTPStatus.OK
def test_rate_with_valid_base_and_no_date_filter(self, testapp, database): client = testapp.test_client() date = datetime.now().date() base = 'EUR' currency_information = CurrencyInformation( base_currency = base, changes = { 'GBP': 0.81 }, change_date = date, retrieved_at = datetime.now() ) database.session.add(currency_information) database.session.commit() response = client.get(f'/rates/{base}') assert response.status_code == HTTPStatus.OK assert response.json == { 'GBP': 0.81 }