Skip to content

Commit

Permalink
Update metobs unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mgcth committed Jan 22, 2024
1 parent 3a28e25 commit 91f80bb
Showing 1 changed file with 60 additions and 20 deletions.
80 changes: 60 additions & 20 deletions tests/unit/test_unit_metobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,35 +353,72 @@ def test_unit_baselevel_get_and_parse_request(
assert content == mock_json_loads.return_value

@pytest.mark.parametrize(
"data, key, parameters, data_type, expected_result",
"data, key, parameters, data_type, expected_url, expected_summary",
[
(
[{"key": "p1", "link": [{"href": "URL", "type": "application/json"}]}],
[
{
"key": "p1",
"summary": "sum",
"link": [{"href": "URL", "type": "application/json"}],
}
],
"key",
"p1",
"application/json",
"URL",
"sum",
),
(
[
{
"title": "p2",
"summary": "sum",
"link": [{"href": "URL", "type": "application/json"}],
}
],
"title",
"p2",
"application/json",
"URL",
"sum",
),
(
[{"key": "p1", "link": []}],
"key",
"p1",
None,
IndexError,
None,
),
(
[{"link": []}],
"key",
"p1",
None,
KeyError,
None,
),
(
[{"link": []}],
"key",
None,
None,
KeyError,
None,
),
(
[{"link": []}],
None,
None,
None,
KeyError,
None,
),
([{"key": "p1", "link": []}], "key", "p1", None, IndexError),
([{"link": []}], "key", "p1", None, KeyError),
([{"link": []}], "key", None, None, KeyError),
([{"link": []}], None, None, None, KeyError),
],
)
def test_unit_baselevel_get_url(
self, data, key, parameters, data_type, expected_result
self, data, key, parameters, data_type, expected_url, expected_summary
):
"""Unit test for BaseLevel _get_url method.
Expand All @@ -390,19 +427,21 @@ def test_unit_baselevel_get_url(
key: key
parameters: parameters
data_type: format of api data
expected_result: expected result
expected_url: expected result
expected_summary: expected summary
"""
level = BaseLevel()

if type(expected_result) != str: # noqa: E721
with pytest.raises(expected_result):
if type(expected_url) != str: # noqa: E721
with pytest.raises(expected_url):
level._get_url(data, key, parameters, data_type)
return None

url = level._get_url(data, key, parameters, data_type)
url, summary = level._get_url(data, key, parameters, data_type)

assert level.data_type == data_type
assert url == expected_result
assert url == expected_url
assert summary == expected_summary


class TestUnitVersions:
Expand Down Expand Up @@ -456,7 +495,7 @@ class TestUnitParameter:
@patch("smhi.metobs.tuple")
@patch("smhi.metobs.sorted")
@patch("smhi.metobs.BaseLevel._get_and_parse_request")
@patch("smhi.metobs.BaseLevel._get_url")
@patch("smhi.metobs.BaseLevel._get_url", return_value=(1, 2))
@patch("smhi.metobs.Versions")
def test_unit_parameters_init(
self,
Expand Down Expand Up @@ -522,7 +561,7 @@ class TestUnitStation:
@patch("smhi.metobs.tuple")
@patch("smhi.metobs.sorted")
@patch("smhi.metobs.BaseLevel._get_and_parse_request")
@patch("smhi.metobs.BaseLevel._get_url")
@patch("smhi.metobs.BaseLevel._get_url", return_value=(1, 2))
def test_unit_stations_init(
self,
mock_get_url,
Expand Down Expand Up @@ -602,7 +641,7 @@ class TestUnitPeriod:
)
@patch("smhi.metobs.sorted")
@patch("smhi.metobs.BaseLevel._get_and_parse_request")
@patch("smhi.metobs.BaseLevel._get_url")
@patch("smhi.metobs.BaseLevel._get_url", return_value=(1, 2))
def test_unit_periods_init(
self,
mock_get_url,
Expand Down Expand Up @@ -688,7 +727,7 @@ class TestUnitData:
],
)
@patch("smhi.metobs.BaseLevel._get_and_parse_request")
@patch("smhi.metobs.BaseLevel._get_url")
@patch("smhi.metobs.BaseLevel._get_url", return_value=(1, 2))
@patch("smhi.metobs.Data._get_data")
def test_unit_data_init(
self,
Expand Down Expand Up @@ -764,7 +803,7 @@ def test_unit_data_init(
)
@patch("smhi.metobs.requests.get")
@patch("smhi.metobs.BaseLevel._get_and_parse_request")
@patch("smhi.metobs.BaseLevel._get_url")
@patch("smhi.metobs.BaseLevel._get_url", return_value=(1, 2))
@patch("smhi.metobs.Data._parse_header")
@patch("smhi.metobs.Data._parse_data")
def test_unit_data_get_data(
Expand Down Expand Up @@ -812,7 +851,7 @@ def test_unit_data_get_data(
],
)
@patch("smhi.metobs.BaseLevel._get_and_parse_request")
@patch("smhi.metobs.BaseLevel._get_url")
@patch("smhi.metobs.BaseLevel._get_url", return_value=(1, 2))
@patch("smhi.metobs.Data._get_data")
def test_unit_data_separate_header_data(
self,
Expand Down Expand Up @@ -844,7 +883,7 @@ def test_unit_data_separate_header_data(
[(METOBS_DATA, METOBS_UNIT_1)],
)
@patch("smhi.metobs.BaseLevel._get_and_parse_request")
@patch("smhi.metobs.BaseLevel._get_url")
@patch("smhi.metobs.BaseLevel._get_url", return_value=(1, 2))
@patch("smhi.metobs.Data._get_data")
def test_unit_data_parse_header(
self,
Expand Down Expand Up @@ -878,7 +917,7 @@ def test_unit_data_parse_header(
],
)
@patch("smhi.metobs.BaseLevel._get_and_parse_request")
@patch("smhi.metobs.BaseLevel._get_url")
@patch("smhi.metobs.BaseLevel._get_url", return_value=(1, 2))
@patch("smhi.metobs.Data._get_data")
def test_unit_data_parse_data(
self,
Expand All @@ -898,6 +937,7 @@ def test_unit_data_parse_data(
result: expected result
"""
data_object = Data(MagicMock(), "corrected-archive", "json")
data_object.parameter_summary = "momentanvärde, 1 gång/tim"

if result is None:
with pytest.raises(TypeError):
Expand Down

0 comments on commit 91f80bb

Please sign in to comment.