Skip to content

Commit

Permalink
HDXDSYS-554 Update secondary rates url and disable WFP tests and data…
Browse files Browse the repository at this point in the history
…bridges install for now (#35)

* databridges library needs to be released by WFP to pypi as direct references aren't allowed
Remove dependency for now

* Without databridges dependency we can't run the tests

* Update secondary current url
  • Loading branch information
mcarans authored Mar 3, 2024
1 parent dd16290 commit 6310c90
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 76 deletions.
8 changes: 4 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ Homepage = "https://github.com/OCHA-DAP/hdx-python-country"
[project.optional-dependencies]
test = ["pytest", "pytest-cov"]
dev = ["pre-commit"]
wfp = ["data-bridges-client@git+https://github.com/WFP-VAM/DataBridgesAPI@dev#egg=data-bridges-client"]
#wfp = ["data-bridges-client@git+https://github.com/WFP-VAM/DataBridgesAPI@dev#egg=data-bridges-client"]


#########
# Hatch #
#########

[tool.hatch.metadata]
allow-direct-references = true
#[tool.hatch.metadata]
#allow-direct-references = true

# Build

Expand All @@ -80,7 +80,7 @@ version_scheme = "python-simplified-semver"
# Tests

[tool.hatch.envs.test]
features = ["test", "wfp"]
features = ["test"]#, "wfp"]

[tool.hatch.envs.test.scripts]
test = """
Expand Down
41 changes: 6 additions & 35 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,13 @@
#
annotated-types==0.6.0
# via pydantic
anyio==4.3.0
# via httpx
attrs==23.2.0
# via
# frictionless
# jsonlines
# jsonschema
certifi==2024.2.2
# via
# httpcore
# httpx
# requests
# via requests
cfgv==3.4.0
# via pre-commit
chardet==5.2.0
Expand All @@ -29,11 +24,7 @@ click==8.1.7
colorama==0.4.6
# via typer
coverage[toml]==7.4.3
# via
# coverage
# pytest-cov
data-bridges-client @ git+https://github.com/WFP-VAM/DataBridgesAPI@dev
# via hdx-python-country (pyproject.toml)
# via pytest-cov
distlib==0.3.8
# via virtualenv
et-xmlfile==1.1.0
Expand All @@ -42,23 +33,14 @@ filelock==3.13.1
# via virtualenv
frictionless==5.16.1
# via hdx-python-utilities
h11==0.14.0
# via httpcore
hdx-python-utilities==3.6.5
# via hdx-python-country (pyproject.toml)
httpcore==1.0.4
# via httpx
httpx==0.27.0
# via data-bridges-client
humanize==4.9.0
# via frictionless
identify==2.5.35
# via pre-commit
idna==3.6
# via
# anyio
# httpx
# requests
# via requests
ijson==3.2.3
# via hdx-python-utilities
iniconfig==2.0.0
Expand Down Expand Up @@ -106,9 +88,7 @@ ply==3.11
pre-commit==3.6.2
# via hdx-python-country (pyproject.toml)
pydantic==2.6.3
# via
# data-bridges-client
# frictionless
# via frictionless
pydantic-core==2.16.3
# via pydantic
pygments==2.17.2
Expand All @@ -125,7 +105,6 @@ pytest-cov==4.1.0
# via hdx-python-country (pyproject.toml)
python-dateutil==2.8.2
# via
# data-bridges-client
# frictionless
# hdx-python-utilities
# libhxl
Expand Down Expand Up @@ -163,10 +142,6 @@ six==1.16.0
# via
# isodate
# python-dateutil
sniffio==1.3.1
# via
# anyio
# httpx
stringcase==1.2.0
# via frictionless
structlog==24.1.0
Expand All @@ -178,12 +153,9 @@ tabulate==0.9.0
text-unidecode==1.3
# via python-slugify
typer[all]==0.9.0
# via
# frictionless
# typer
# via frictionless
typing-extensions==4.10.0
# via
# data-bridges-client
# frictionless
# pydantic
# pydantic-core
Expand All @@ -192,9 +164,8 @@ unidecode==1.3.8
# via
# libhxl
# pyphonetics
urllib3==2.0.7
urllib3==2.2.1
# via
# data-bridges-client
# libhxl
# requests
validators==0.22.0
Expand Down
2 changes: 1 addition & 1 deletion src/hdx/location/currency.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Currency:
"""

_primary_rates_url = "https://query2.finance.yahoo.com/v8/finance/chart/{currency}=X?period1={date}&period2={date}&interval=1d&events=div%2Csplit&formatted=false&lang=en-US&region=US&corsDomain=finance.yahoo.com"
_secondary_rates_url = "https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@1/latest/currencies/usd.min.json"
_secondary_rates_url = "https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@latest/v1/currencies/usd.min.json"
_secondary_historic_url = (
"https://codeforiati.org/imf-exchangerates/imf_exchangerates.csv"
)
Expand Down
7 changes: 2 additions & 5 deletions src/hdx/location/wfp_exchangerates.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
from datetime import timezone
from typing import Dict, List

from data_bridges_client.exceptions import (
ApiException,
)

from . import get_int_timestamp
from hdx.utilities.typehint import ListTuple

try:
from data_bridges_client import ApiClient, Configuration, CurrencyApi
from data_bridges_client.exceptions import ApiException
from data_bridges_client.token import WfpApiToken
except ImportError:
pass
WfpApiToken = None


class WFPExchangeRates:
Expand Down
65 changes: 34 additions & 31 deletions tests/hdx/location/test_wfp_exchangerates.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,39 @@

from hdx.location import get_int_timestamp
from hdx.location.currency import Currency
from hdx.location.wfp_exchangerates import WFPExchangeRates
from hdx.location.wfp_exchangerates import WfpApiToken, WFPExchangeRates
from hdx.utilities.dateparse import parse_date


class TestWFPExchangeRates:
@pytest.fixture(scope="class")
def wfp_fx(self):
key = environ.get("WFP_KEY")
secret = environ.get("WFP_SECRET")
return WFPExchangeRates(key, secret)

@pytest.fixture(scope="class")
def currency(self):
return "afn"

@pytest.fixture(scope="class")
def date(self):
return parse_date("2020-02-20")

def test_get_currencies(self, wfp_fx):
currencies = wfp_fx.get_currencies()
assert len(currencies) == 126

def test_get_historic_rates(self, wfp_fx, currency, date):
assert Currency.get_historic_rate(currency, date) == 76.80000305175781
timestamp = get_int_timestamp(date)
historic_rates = wfp_fx.get_currency_historic_rates(currency)
assert historic_rates[timestamp] == 77.01

def test_get_all_historic_rates(self, wfp_fx, currency, date):
all_historic_rates = wfp_fx.get_historic_rates([currency])
Currency.setup(historic_rates_cache=all_historic_rates)
assert Currency.get_historic_rate(currency, date) == 77.01
if WfpApiToken is not None:

class TestWFPExchangeRates:
@pytest.fixture(scope="class")
def wfp_fx(self):
key = environ.get("WFP_KEY")
secret = environ.get("WFP_SECRET")
return WFPExchangeRates(key, secret)

@pytest.fixture(scope="class")
def currency(self):
return "afn"

@pytest.fixture(scope="class")
def date(self):
return parse_date("2020-02-20")

def test_get_currencies(self, wfp_fx):
currencies = wfp_fx.get_currencies()
assert len(currencies) == 126

def test_get_historic_rates(self, wfp_fx, currency, date):
assert (
Currency.get_historic_rate(currency, date) == 76.80000305175781
)
timestamp = get_int_timestamp(date)
historic_rates = wfp_fx.get_currency_historic_rates(currency)
assert historic_rates[timestamp] == 77.01

def test_get_all_historic_rates(self, wfp_fx, currency, date):
all_historic_rates = wfp_fx.get_historic_rates([currency])
Currency.setup(historic_rates_cache=all_historic_rates)
assert Currency.get_historic_rate(currency, date) == 77.01

0 comments on commit 6310c90

Please sign in to comment.