Skip to content

Commit

Permalink
Generalize find_stations_from_gps test through mock object classes
Browse files Browse the repository at this point in the history
  • Loading branch information
docNord committed May 4, 2024
1 parent 226fa31 commit a9142f3
Showing 1 changed file with 28 additions and 24 deletions.
52 changes: 28 additions & 24 deletions tests/unit/test_unit_smhi.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,25 @@
from smhi.smhi import SMHI


class MockMetobsStationLink:
"""Support class to mock MetObs StationLink object."""

def __init__(self, id: int, name: str, lat: float, lon: float):
"""Initiate parameters."""
self.id = id
self.name = name
self.latitude = lat
self.longitude = lon


class MockParameterModel:
"""Support class to mock Metobs Parameter object."""

def __init__(self, station: MockMetobsStationLink):
"""Initiate station."""
self.station = station


class TestUnitSMHI:
"""Unit tests for SMHI class."""

Expand Down Expand Up @@ -120,43 +139,28 @@ def test_unit_get_data_by_city(
distanceresponse.km = 0

@pytest.mark.parametrize(
"latitude, longitude,dist",
[
(
59,
17,
0,
),
(
59.4,
17,
30,
),
],
"latitude, longitude, dist, expected_result",
[(59, 17, 0, 1), (59.5, 17.75, 30, 1)],
)
@patch("geopy.distance.distance", return_value=distanceresponse)
def test_find_stations_from_gps(self, mock_distance, latitude, longitude, dist):
def test_find_stations_from_gps(self, latitude, longitude, dist, expected_result):
"""Unit test for SMHI find_stations_from_gps method.
Args:
mock_distance,
latitude,
longitude,
dist
expected_result
"""
station1 = MagicMock()
station1.id = 1
station1.name = "Akalla"
station1.latitude = 59.5
station1.longitude = 17.8
stations = MagicMock()
stations.station = [station1]
station1 = MockMetobsStationLink(1, "Akalla", 59.5, 17.8)
station2 = MockMetobsStationLink(2, "name", 57, 16)
station3 = MockMetobsStationLink(3, "name", 58, 17)
stations = MockParameterModel([station1, station2, station3])

client = SMHI()
nearby_town = client._find_stations_from_gps(
stations, latitude, longitude, dist
)
assert nearby_town[0][0] == 1
assert nearby_town[0][0] == expected_result

@pytest.mark.parametrize(
"parameter, city, distance", [(8, "Bengtsfors", None), (8, "Bengtsfors", 50)]
Expand Down

0 comments on commit a9142f3

Please sign in to comment.