Skip to content

Commit

Permalink
Lint and format
Browse files Browse the repository at this point in the history
  • Loading branch information
mgcth committed Mar 24, 2024
1 parent b23776c commit f86614e
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 63 deletions.
9 changes: 5 additions & 4 deletions src/smhi/mesan.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
5 changes: 3 additions & 2 deletions src/smhi/metfcts.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
7 changes: 4 additions & 3 deletions src/smhi/smhi.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
15 changes: 8 additions & 7 deletions src/smhi/strang.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

Expand Down
3 changes: 2 additions & 1 deletion tests/integration/test_integration_mesan.py
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
3 changes: 1 addition & 2 deletions tests/integration/test_integration_strang.py
Original file line number Diff line number Diff line change
@@ -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],
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/test_unit_mesan.py
Original file line number Diff line number Diff line change
@@ -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/"
Expand Down
1 change: 0 additions & 1 deletion tests/unit/test_unit_metfcts.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from smhi.metfcts import Metfcts


BASE_URL = (
"https://opendata-download-metfcst.smhi.se/" + "api/category/pmp3g/version/2/"
)
Expand Down
65 changes: 33 additions & 32 deletions tests/unit/test_unit_smhi.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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),
(
Expand All @@ -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),
(
Expand Down Expand Up @@ -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:
Expand All @@ -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),
(
Expand Down Expand Up @@ -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()
16 changes: 8 additions & 8 deletions tests/unit/test_unit_strang.py
Original file line number Diff line number Diff line change
@@ -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},
Expand Down

0 comments on commit f86614e

Please sign in to comment.