From f86614e29676d30401e025c63a0928b50341b1f2 Mon Sep 17 00:00:00 2001 From: Mladen Gibanica <11275336+mgcth@users.noreply.github.com> Date: Sun, 24 Mar 2024 19:12:12 +0100 Subject: [PATCH] Lint and format --- src/smhi/mesan.py | 9 +-- src/smhi/metfcts.py | 5 +- src/smhi/smhi.py | 7 ++- src/smhi/strang.py | 15 ++--- tests/integration/test_integration_mesan.py | 3 +- tests/integration/test_integration_strang.py | 3 +- tests/unit/test_unit_mesan.py | 6 +- tests/unit/test_unit_metfcts.py | 1 - tests/unit/test_unit_smhi.py | 65 ++++++++++---------- tests/unit/test_unit_strang.py | 16 ++--- 10 files changed, 67 insertions(+), 63 deletions(-) diff --git a/src/smhi/mesan.py b/src/smhi/mesan.py index 1bb32155..d755bfd5 100644 --- a/src/smhi/mesan.py +++ b/src/smhi/mesan.py @@ -1,13 +1,14 @@ """SMHI Mesan API module.""" import json +from functools import wraps +from typing import Any, Callable, Optional, Union + import arrow -import requests import pandas as pd -from functools import wraps -from typing import Any, Callable, Union, Optional +import requests from requests.structures import CaseInsensitiveDict -from smhi.constants import MESAN_URL, MESAN_LEVELS_UNIT +from smhi.constants import MESAN_LEVELS_UNIT, MESAN_URL def get_data(key: Optional[str] = None) -> Callable: diff --git a/src/smhi/metfcts.py b/src/smhi/metfcts.py index baa587d8..dbd7ec6e 100644 --- a/src/smhi/metfcts.py +++ b/src/smhi/metfcts.py @@ -1,9 +1,10 @@ """SMHI Metfcts API module.""" from typing import Optional -from smhi.mesan import Mesan -from smhi.constants import METFCTS_URL + from requests.structures import CaseInsensitiveDict +from smhi.constants import METFCTS_URL +from smhi.mesan import Mesan class Metfcts(Mesan): diff --git a/src/smhi/smhi.py b/src/smhi/smhi.py index 606eff2b..7db8656d 100644 --- a/src/smhi/smhi.py +++ b/src/smhi/smhi.py @@ -1,12 +1,13 @@ """Read SMHI data.""" import logging +from typing import Any, List, Optional, Tuple + import pandas as pd -from geopy.geocoders import Nominatim from geopy import distance -from typing import Optional, Any, List, Tuple -from smhi.metobs import Metobs +from geopy.geocoders import Nominatim from smhi.constants import TYPE_MAP +from smhi.metobs import Metobs class SMHI: diff --git a/src/smhi/strang.py b/src/smhi/strang.py index 3b134b34..8f35410f 100644 --- a/src/smhi/strang.py +++ b/src/smhi/strang.py @@ -4,20 +4,21 @@ """ import json -import arrow -import requests import logging -import pandas as pd -from functools import partial from collections import defaultdict -from typing import Optional, Any +from functools import partial +from typing import Any, Optional + +import arrow +import pandas as pd +import requests from requests.structures import CaseInsensitiveDict from smhi.constants import ( STRANG, STRANG_EMPTY, - STRANG_POINT_URL, - STRANG_PARAMETERS, STRANG_MULTIPOINT_URL, + STRANG_PARAMETERS, + STRANG_POINT_URL, STRANG_TIME_INTERVALS, ) diff --git a/tests/integration/test_integration_mesan.py b/tests/integration/test_integration_mesan.py index c4e68f31..f7856dff 100644 --- a/tests/integration/test_integration_mesan.py +++ b/tests/integration/test_integration_mesan.py @@ -1,10 +1,11 @@ """Mesan integration tests.""" +import datetime as dt import json + import pytest import requests from smhi.mesan import Mesan -import datetime as dt BASE_URL = "https://opendata-download-metanalys.smhi.se" APPROVED_TIME = BASE_URL + "/api/category/mesan2g/version/1/approvedtime.json" diff --git a/tests/integration/test_integration_strang.py b/tests/integration/test_integration_strang.py index e3546cad..1e3f10e3 100644 --- a/tests/integration/test_integration_strang.py +++ b/tests/integration/test_integration_strang.py @@ -1,10 +1,9 @@ """Strang integration tests.""" -import pytest import pandas as pd +import pytest from smhi.strang import Strang - RESULT_HOURLY_2020_01_01_2020_01_02 = pd.read_csv( "tests/fixtures/STRANG_RESULT_HOURLY_2020_01_01_2020_01_02.csv", parse_dates=[0], diff --git a/tests/unit/test_unit_mesan.py b/tests/unit/test_unit_mesan.py index 110ba3fe..685d575b 100644 --- a/tests/unit/test_unit_mesan.py +++ b/tests/unit/test_unit_mesan.py @@ -1,12 +1,12 @@ """SMHI Mesan v1 unit tests.""" import json +from unittest.mock import patch + import arrow -import pytest import pandas as pd +import pytest from smhi.mesan import Mesan -from unittest.mock import patch - BASE_URL = ( "https://opendata-download-metanalys.smhi.se/" + "api/category/mesan2g/version/1/" diff --git a/tests/unit/test_unit_metfcts.py b/tests/unit/test_unit_metfcts.py index f9e7f70f..18e8a01e 100644 --- a/tests/unit/test_unit_metfcts.py +++ b/tests/unit/test_unit_metfcts.py @@ -2,7 +2,6 @@ from smhi.metfcts import Metfcts - BASE_URL = ( "https://opendata-download-metfcst.smhi.se/" + "api/category/pmp3g/version/2/" ) diff --git a/tests/unit/test_unit_smhi.py b/tests/unit/test_unit_smhi.py index 9481b5c7..84d0eded 100644 --- a/tests/unit/test_unit_smhi.py +++ b/tests/unit/test_unit_smhi.py @@ -1,9 +1,10 @@ """SMHI unit tests.""" from unittest.mock import patch -from smhi.smhi import SMHI -import pytest + import pandas as pd +import pytest +from smhi.smhi import SMHI class TestUnitSMHI: @@ -23,44 +24,44 @@ def test_unit_smhi_init(self, mock_requests_metobs): mock_requests_metobs.return_value.get_parameters.assert_called_once() @patch("smhi.smhi.Metobs") - def test_unit_smhi_parameters(self, mock_Metobs): + def test_unit_smhi_parameters(self, mock_metobs): """Unit test for SMHI parameters method. Args: - mock_Metobs: mock Metobs object + mock_metobs: mock Metobs object """ client = SMHI() - assert client.parameters == mock_Metobs.return_value.parameters.data + assert client.parameters == mock_metobs.return_value.parameters.data @pytest.mark.parametrize( - "parameter,Metobs_parameters", + "parameter,metobs_parameters", [(None, None), (1, [(2, 0), (3, 0)]), (2, [(2, 0)])], ) @patch("smhi.smhi.Metobs") @patch("smhi.smhi.logging.info") def test_unit_smhi_get_stations( - self, mock_logging_info, mock_Metobs, parameter, Metobs_parameters + self, mock_logging_info, mock_metobs, parameter, metobs_parameters ): """Unit test for SMHI get_stations method. Args: mock_logging_info: mock logging info object - mock_Metobs: mock Metobs object + mock_metobs: mock Metobs object parameter: parameter (int) - Metobs_parameters: Metobs return parameters + metobs_parameters: Metobs return parameters """ - mock_Metobs.return_value.parameters = Metobs_parameters + mock_metobs.return_value.parameters = metobs_parameters client = SMHI() - if Metobs_parameters is None: + if metobs_parameters is None: client.get_stations(parameter) mock_logging_info.assert_called_once() return stations = client.get_stations(parameter) - assert stations == mock_Metobs.return_value.stations.data + assert stations == mock_metobs.return_value.stations.data @pytest.mark.parametrize( - "title,Metobs_stations", + "title, metobs_stations", [ (None, None), ( @@ -72,25 +73,25 @@ def test_unit_smhi_get_stations( @patch("smhi.smhi.Metobs") @patch("smhi.smhi.logging.info") def test_unit_smhi_get_stations_from_title( - self, mock_logging_info, mock_Metobs, title, Metobs_stations + self, mock_logging_info, mock_metobs, title, metobs_stations ): """Unit test for SMHI get_stations_from_title method. Args: title: title of station """ - mock_Metobs.return_value.stations = Metobs_stations + mock_metobs.return_value.stations = metobs_stations client = SMHI() - if Metobs_stations is None: + if metobs_stations is None: client.get_stations_from_title(title) mock_logging_info.assert_called_once() return data = client.get_stations_from_title(title) - assert (data == mock_Metobs.return_value.stations.data).all() + assert (data == mock_metobs.return_value.stations.data).all() @pytest.mark.parametrize( - "parameter,latitude,longitude,dist,Metobs_stations", + "parameter,latitude,longitude,dist,metobs_stations", [ (None, None, None, None, None), ( @@ -122,27 +123,27 @@ def test_unit_smhi_get_stations_from_title( def test_find_stations_from_gps( self, mock_logging_info, - mock_Metobs, + mock_metobs, mock_distance, parameter, latitude, longitude, dist, - Metobs_stations, + metobs_stations, ): """Unit test for SMHI find_stations_from_gps method. Args: mock_logging_info: mock logging info object - mock_Metobs: mock Metobs object + mock_metobs: mock Metobs object mock_distance: mock distance object parameter: parameter (int) latitude: latitude (int) longitude: longitude (int) dist: Distance radius in which to look for stations - Metobs_stations: Metobs stations + metobs_stations: Metobs stations """ - mock_Metobs.return_value.stations = Metobs_stations + mock_metobs.return_value.stations = metobs_stations client = SMHI() if parameter is None: @@ -155,7 +156,7 @@ def test_find_stations_from_gps( assert len(client.nearby_stations[0]) > 0 @pytest.mark.parametrize( - "parameter,city,dist,Metobs_stations", + "parameter,city,dist,metobs_stations", [ (None, None, None, None), ( @@ -185,26 +186,26 @@ def test_find_stations_from_gps( @patch("smhi.smhi.Metobs") def test_find_stations_by_city( self, - mock_Metobs, - mock_Nominatim, + mock_metobs, + mock_nominatim, mock_distance, parameter, city, dist, - Metobs_stations, + metobs_stations, ): """Unit test for SMHI find_stations_by_city method. Args: - mock_Metobs: mock Metobs object - mock_Nominatim: mock Nominatim object + mock_metobs: mock Metobs object + mock_nominatim: mock Nominatim object mock_distance: mock distance object parameter: parameter (int) city: city name dist: Distance radius in which to look for stations - Metobs_stations: Metobs stations + metobs_stations: Metobs stations """ - mock_Metobs.return_value.stations = Metobs_stations + mock_metobs.return_value.stations = metobs_stations client = SMHI() client.find_stations_by_city(parameter, city, dist) - mock_Nominatim.assert_called_once() + mock_nominatim.assert_called_once() diff --git a/tests/unit/test_unit_strang.py b/tests/unit/test_unit_strang.py index 6be6fde9..e4310951 100644 --- a/tests/unit/test_unit_strang.py +++ b/tests/unit/test_unit_strang.py @@ -1,21 +1,21 @@ """SMHI Strang unit tests.""" -import arrow -import pytest import datetime -import pandas as pd -from dateutil.tz import tzutc from functools import partial from unittest.mock import patch -from smhi.strang import Strang + +import arrow +import pandas as pd +import pytest +from dateutil.tz import tzutc from smhi.constants import ( STRANG, STRANG_EMPTY, - STRANG_POINT_URL, - STRANG_PARAMETERS, STRANG_MULTIPOINT_URL, + STRANG_PARAMETERS, + STRANG_POINT_URL, ) - +from smhi.strang import Strang INPUT_DAILY_2020_01_01_2020_01_02 = [ {"date_time": datetime.datetime(2020, 1, 1, 0, 0, tzinfo=tzutc()), "value": 608.0},