Skip to content

Commit

Permalink
✅ Updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafalz13 committed Oct 4, 2024
1 parent c3589a1 commit cab70b2
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions tests/unit/test_eurostat.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
from viadot.sources import Eurostat


@pytest.fixture
def eurostat_instance():
return Eurostat(dataset_code="TEIBS020", params={"unit": "EUR"})


@patch("viadot.utils.handle_api_response")
def test_to_df(mock_handle_api_response):
mock_response_data = {
Expand Down Expand Up @@ -46,32 +51,37 @@ def test_to_df(mock_handle_api_response):
assert df["updated"][0] == mock_response_data["updated"]


@patch("viadot.utils.handle_api_response")
def test_validate_params_invalid_key(mock_handle_api_response): # noqa: ARG001
eurostat = Eurostat(dataset_code="TEIBS020", params={"invalid_key": "value"})
def test_validate_params_invalid_key(mocker, eurostat_instance):
mocker.patch.object(
eurostat_instance,
"get_parameters_codes",
return_value={"unit": ["EUR"], "geo": ["EU"]},
)

params = {"unit": "EUR", "country": "US"}
with pytest.raises(ValueError, match="Wrong parameters or codes were provided!"):
eurostat.validate_params(
dataset_code="TEIBS020",
url="https://ec.europa.eu/eurostat/api/dissemination/statistics/1.0/data/ILC_DI04?format=JSON&lang=EN",
params={"invalid_key": "value"},
)
eurostat_instance.validate_params("TEIBS020", "", params)


@patch("viadot.utils.handle_api_response")
def test_validate_params_invalid_value(mock_handle_api_response):
def test_validate_params_invalid_value(mock_handle_api_response, eurostat_instance):
mock_handle_api_response.return_value.json.return_value = {
"id": ["geo"],
"dimension": {
"geo": {"category": {"index": {"EU": 0}, "label": {"EU": "European Union"}}}
},
}

eurostat = Eurostat(dataset_code="TEIBS020", params={"geo": "US"})

with pytest.raises(ValueError, match="Wrong parameters or codes were provided"):
eurostat.validate_params(
eurostat_instance.validate_params(
dataset_code="TEIBS020",
url="https://ec.europa.eu/eurostat/api/dissemination/statistics/1.0/data/ILC_DI04?format=JSON&lang=EN",
params={"geo": "US"},
)


def test_to_df_invalid_params_type(mocker, eurostat_instance):
eurostat_instance.params = "invalid_type"

with pytest.raises(TypeError, match="Params should be a dictionary."):
eurostat_instance.to_df()

0 comments on commit cab70b2

Please sign in to comment.