def test_get_currency_exchange_rate_for_date(): date = datetime.utcnow() currency_exchange_rate_data = CurrencyExchangeRate( from_currency=Currency.RUB, to_currency=Currency.USD, rate=1.50, date=datetime.utcnow()) create_currency_exchange_rate(currency_exchange_rate_data) data = get_currency_exchange_rate_for_date(Currency.RUB, Currency.USD, date) assert data.id is not None
def _insert_currency_record(date: datetime, rates: Dict): for symbol in OTHER_CURRENCIES: exist_record = get_currency_exchange_rate_for_date( from_currency=MAIN_CURRENCY, to_currency=Currency(symbol), date=date) if exist_record: continue rate = 1 / rates[symbol] item = CurrencyExchangeRate(from_currency=MAIN_CURRENCY.value, to_currency=symbol, rate=rate, date=date) create_currency_exchange_rate(item)
def create_currency_exchange_rate_for_tests( rate: float = 1.50) -> CurrencyExchangeRate: return create_currency_exchange_rate( CurrencyExchangeRate(from_currency=Currency.RUB, to_currency=Currency.USD, rate=rate, date=datetime.utcnow()))
def test_get_currency_exchange_rate_for_nearest_date(): dates = [ datetime(2020, 1, 10), datetime(2020, 1, 11), datetime(2020, 1, 14), ] rates = [] for i, date in enumerate(dates): currency_exchange_rate_data = CurrencyExchangeRate( date=date, from_currency=Currency.RUB, to_currency=Currency.USD, rate=i) rates.append( create_currency_exchange_rate(currency_exchange_rate_data)) date = datetime(2020, 1, 13) data = get_currency_exchange_rate_for_nearest_date(Currency.RUB, Currency.USD, date) assert data.date.strftime("%Y-%m-%d") == "2020-01-11" date = datetime(2020, 1, 14) data = get_currency_exchange_rate_for_nearest_date(Currency.RUB, Currency.USD, date) assert data.date.strftime("%Y-%m-%d") == "2020-01-14"
def test_delete_all_currency_exchange_rate(): currency_exchange_rate_data = CurrencyExchangeRate( from_currency=Currency.RUB, to_currency=Currency.USD, rate=1.50, date=datetime.utcnow()) create_currency_exchange_rate(currency_exchange_rate_data) currency_exchange_rates, _ = get_currency_exchange_rates_list_for_pair( Currency.RUB, Currency.USD) assert len(currency_exchange_rates) == 1 delete_all_currency_exchange_rates() currency_exchange_rates, _ = get_currency_exchange_rates_list_for_pair( Currency.RUB, Currency.USD) assert len(currency_exchange_rates) == 0
def test_get_list_of_currency_exchange_rates_api(): user, token = create_user_and_token_for_tests() number_of_currency_exchange_rates = 40 for _ in range(number_of_currency_exchange_rates): currency_exchange_rate_data = CurrencyExchangeRate( from_currency=Currency.RUB, to_currency=Currency.USD, rate=1.50, date=datetime.utcnow()) create_currency_exchange_rate(currency_exchange_rate_data) response = client.get( "/finance/currency-exchange-rates/get-list", params={ "from_currency": Currency.RUB.value, "to_currency": Currency.USD.value, "page": 1 }, headers={"Authorization": f"Bearer {token}"}, ) assert response.status_code == status.HTTP_200_OK response_data = response.json() assert response_data["per_page"] > 0 assert response_data["page"] == 1 assert response_data["count"] == number_of_currency_exchange_rates assert len(response_data["items"]) == response_data["per_page"] response = client.get( "/finance/currency-exchange-rates/get-list", params={ "from_currency": Currency.RUB.value, "to_currency": Currency.USD.value, "page": 2 }, headers={"Authorization": f"Bearer {token}"}, ) assert response.status_code == status.HTTP_200_OK response_data = response.json() assert response_data["page"] == 2 assert len( response_data["items"] ) == number_of_currency_exchange_rates - response_data["per_page"]
def test_create_currency_exchange_rate(): currency_exchange_rate_data = CurrencyExchangeRate( from_currency=Currency.RUB, to_currency=Currency.USD, rate=1.50, date=datetime.utcnow()) currency_exchange_rate = create_currency_exchange_rate( currency_exchange_rate_data) assert currency_exchange_rate.id is not None assert currency_exchange_rate.updated is not None
def test_get_currency_exchange_rates_list(): per_page = 25 number_of_currency_exchange_rates = 40 for _ in range(number_of_currency_exchange_rates): currency_exchange_rate_data = CurrencyExchangeRate( from_currency=Currency.RUB, to_currency=Currency.USD, rate=1.50, date=datetime.utcnow()) create_currency_exchange_rate(currency_exchange_rate_data) currency_exchange_rates, _ = get_currency_exchange_rates_list_for_pair( Currency.RUB, Currency.USD, per_page=per_page, page=1) assert len(currency_exchange_rates) == per_page currency_exchange_rates, _ = get_currency_exchange_rates_list_for_pair( Currency.RUB, Currency.USD, per_page=per_page, page=2) assert len(currency_exchange_rates ) == number_of_currency_exchange_rates - per_page # query not exists pair currency_exchange_rates, _ = get_currency_exchange_rates_list_for_pair( Currency.RUB, Currency.EUR) assert len(currency_exchange_rates) == 0
def _create_currency_exchange_rates() -> List[CurrencyExchangeRate]: results = [] date = datetime.utcnow() - timedelta(days=1) currency_rates = { Currency.USD.value: 70, Currency.EUR.value: 100, } for symbol in OTHER_CURRENCIES: item = CurrencyExchangeRate(from_currency=MAIN_CURRENCY.value, to_currency=symbol, rate=currency_rates[symbol], date=date) results.append(create_currency_exchange_rate(item)) return results