Skip to content

Commit

Permalink
Refactor unit and integration tests, remove doc args and returns from…
Browse files Browse the repository at this point in the history
… all tests
  • Loading branch information
mgcth committed Mar 31, 2024
1 parent bbd5f38 commit a49cb40
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 289 deletions.
17 changes: 2 additions & 15 deletions tests/integration/test_integration_mesan.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,7 @@ def test_integration_mesan_geo_polygon(self):
],
)
def test_integration_mesan_get_geo_multipoint(self, downsample, result):
"""Integration test for get_geo_multipoint method.
Args:
downsample: downsample parameter
result: expected result
"""
"""Integration test for get_geo_multipoint method."""
client = Mesan()
assert client.get_geo_multipoint(downsample) == result["coordinates"]

Expand Down Expand Up @@ -140,15 +135,7 @@ def test_integration_mesan_get_point(self, lat, lon):
def test_integration_mesan_get_multipoint(
self, validtime, parameter, level_type, level, downsample
):
"""Integration test for get_multipoint method.
Args:
validtime: valid time
parameter: parameter
level_type: level type
level: level
downsample: downsample
"""
"""Integration test for get_multipoint method."""
client = Mesan()
data = client.get_multipoint(
validtime, parameter, level_type, level, downsample
Expand Down
9 changes: 0 additions & 9 deletions tests/integration/test_integration_strang.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,6 @@ def test_integration_strang_point(
"""Strang Point class integration tests.
These tests require internet connectivity.
Args:
lat: longitude
lon: latitude
parameter: parameter
time_from: from
time_to: to
time_interval: interval
expected_result: expected result
"""
client = Strang()
point_model = client.get_point(
Expand Down
49 changes: 1 addition & 48 deletions tests/unit/test_unit_mesan.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from smhi.constants import MESAN_LEVELS_UNIT, MESAN_PARAMETER_DESCRIPTIONS
from smhi.mesan import Mesan
from smhi.models.mesan_model import MesanGeometry, MesanParameterItem, MesanParameters
from utils import get_response

BASE_URL = (
"https://opendata-download-metanalys.smhi.se/" + "api/category/mesan2g/version/1/"
Expand Down Expand Up @@ -39,13 +40,6 @@
)


class MockResponse:
def __init__(self, status, header, content):
self.status_code = status
self.headers = header
self.content = content


class MockMesanPointData(BaseModel):
model_config = ConfigDict(arbitrary_types_allowed=True)

Expand All @@ -61,47 +55,6 @@ class MockMesanMultiPointData(BaseModel):
valid_time: str


def get_response(file, encode=False):
"""Read in response.
Args:
file: file to load
Returns:
mocked response
"""
with open(file) as f:
mocked_response = f.read()

headers, content = mocked_response.split("\n\n")
status = 200
headers = {x.split(":")[0]: x.split(":")[1] for x in headers.split("\n")[1:]}

if encode is True:
content = content.encode("utf-8")

mocked_get = MockResponse(status, headers, content)

return mocked_get


def get_data(file, load_type=None):
"""Read in expected data structure.
Args:
file: file to load
model
Returns:
expected pydantic model
"""
with open(file) as f:
if load_type == "data":
return f.read().encode("utf-8")

return json.load(f)


@pytest.fixture
def setup_point():
"""Read in Point response."""
Expand Down
152 changes: 14 additions & 138 deletions tests/unit/test_unit_metobs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""SMHI Metobs v1 unit tests."""

import json
from typing import Optional
from unittest.mock import MagicMock, patch

Expand All @@ -21,6 +20,7 @@
from smhi.models.metobs_periods import MetobsPeriodModel
from smhi.models.metobs_stations import MetobsStationModel
from smhi.models.metobs_versions import MetobsVersionModel
from utils import MockResponse, get_data, get_response


class MockModelInner(BaseModel):
Expand All @@ -35,13 +35,6 @@ class MockModel(BaseModel):
link: list[MockModelInner]


class MockResponse:
def __init__(self, status, header, content):
self.status_code = status
self.headers = header
self.content = content


class MockVersions:
def __init__(self, data):
self.data = data
Expand All @@ -66,53 +59,9 @@ def __init__(self, period, data):
self.data = data


def get_response(file, encode=False):
"""Read in response.
Args:
file: file to load
Returns:
mocked response
"""
with open(file) as f:
mocked_response = f.read()

headers, content = mocked_response.split("\n\n")

if encode is True:
content = content.encode("utf-8")

mocked_get = MockResponse(200, headers, content)

return mocked_get


def get_data(file, load_type=None):
"""Read in expected data structure.
Args:
file: file to load
model
Returns:
expected pydantic model
"""
with open(file) as f:
if load_type == "data":
return f.read().encode("utf-8")

return json.load(f)


@pytest.fixture
def setup_versions():
"""Read in Versions response.
Returns:
mocked response
expected answer as pydantic structure
"""
"""Read in Versions response."""
mocked_response = get_response("tests/fixtures/metobs/versions.txt")
mocked_model = MetobsVersionModel.model_validate_json(mocked_response.content)

Expand All @@ -121,13 +70,7 @@ def setup_versions():

@pytest.fixture
def setup_parameters(setup_versions):
"""Read in Parameters response.
Returns:
mocked response
expected answer as pydantic structure
parameters data
"""
"""Read in Parameters response."""
_, mocked_model_versions = setup_versions

mocked_response = get_response("tests/fixtures/metobs/parameters.txt")
Expand All @@ -147,13 +90,7 @@ def setup_parameters(setup_versions):

@pytest.fixture
def setup_stations(setup_parameters):
"""Read in Stations response.
Returns:
mocked response
expected answer as pydantic structure
stations data
"""
"""Read in Stations response."""
_, mocked_model_parameters, _, _ = setup_parameters

mocked_response = get_response("tests/fixtures/metobs/stations.txt")
Expand All @@ -172,13 +109,7 @@ def setup_stations(setup_parameters):

@pytest.fixture
def setup_periods(setup_stations):
"""Read in Periods response.
Returns:
mocked response
expected answer as pydantic structure
periods data
"""
"""Read in Periods response."""
_, mocked_model_stations, _, _ = setup_stations

mocked_response = get_response("tests/fixtures/metobs/periods.txt")
Expand All @@ -195,13 +126,7 @@ def setup_periods(setup_stations):

@pytest.fixture
def setup_periods_set(setup_stations):
"""Read in Periods for station_set.
Returns:
mocked response
expected answer as pydantic structure
periods data
"""
"""Read in Periods for station_set."""
_, mocked_model_stations, _, _ = setup_stations

mocked_response = get_response("tests/fixtures/metobs/periods_set.txt")
Expand All @@ -218,13 +143,7 @@ def setup_periods_set(setup_stations):

@pytest.fixture
def setup_data(setup_periods):
"""Read in Data response.
Returns:
mocked response
expected answer as pydantic structure
data data
"""
"""Read in Data response."""
_, mocked_model_periods, _, _ = setup_periods

mocked_response = get_response("tests/fixtures/metobs/data.txt", encode=True)
Expand Down Expand Up @@ -287,11 +206,7 @@ def test_unit_basemetobs_init(self):

@patch("smhi.utils.requests.get", return_value=MockResponse(200, None, None))
def test_unit_basemetobs_get_and_parse_request(self, mock_requests_get):
"""Unit test for BaseMetobs _get_and_parse_request method.
Args:
mock_requests_get: mock requests get method
"""
"""Unit test for BaseMetobs _get_and_parse_request method."""
base = BaseMetobs()
url = "URL"

Expand Down Expand Up @@ -321,16 +236,7 @@ def test_unit_basemetobs_get_and_parse_request(self, mock_requests_get):
def test_unit_basemetobs_get_url(
self, data, key, parameters, data_type, expected_url, expected_summary
):
"""Unit test for BaseMetobs _get_url method.
Args:
data: list of data
key: key
parameters: parameters
data_type: format of api data
expected_url: expected result
expected_summary: expected summary
"""
"""Unit test for BaseMetobs _get_url method."""
base = BaseMetobs()

if type(expected_url) != str: # noqa: E721
Expand All @@ -351,13 +257,7 @@ class TestUnitVersions:
@pytest.mark.parametrize("data_type", [("json"), ("yaml"), ("xml"), (None)])
@patch("smhi.utils.requests.get")
def test_unit_versions_init(self, mock_requests_get, data_type, setup_versions):
"""Unit test for Parameters init method.
Args:
mock_get_and_parse_request: mock _get_and_parse_request get method
mock_tuple: mock of tuple call
data_type: format of api data
"""
"""Unit test for Parameters init method."""
mock_response, expected_answer = setup_versions
mock_requests_get.return_value = mock_response

Expand Down Expand Up @@ -404,13 +304,7 @@ def test_unit_parameters_init(
data_type,
setup_parameters,
):
"""Unit test for Parameters init method.
Args:
mock_get_and_parse_request: mock _get_and_parse_request get method
mock_tuple: mock of tuple call
data_type: format of api data
"""
"""Unit test for Parameters init method."""
mock_response, expected_answer, mock_versions, expected_data = setup_parameters
mock_requests_get.return_value = mock_response

Expand Down Expand Up @@ -463,13 +357,7 @@ def test_unit_stations_init(
data_type,
setup_stations,
):
"""Unit test for Stations init method.
Args:
mock_get_and_parse_request: mock _get_and_parse_request get method
mock_tuple: mock of tuple call
data_type: format of api data
"""
"""Unit test for Stations init method."""
mock_response, expected_answer, mock_parameters, expected_data = setup_stations
mock_requests_get.return_value = mock_response

Expand Down Expand Up @@ -530,13 +418,7 @@ def test_unit_periods_init(
setup_periods,
setup_periods_set,
):
"""Unit test for Periods init method.
Args:
mock_get_and_parse_request: mock _get_and_parse_request get method
mock_tuple: mock of tuple call
data_type: format of api data
"""
"""Unit test for Periods init method."""
if stationset is None:
mock_response, expected_answer, mock_stations, expected_data = setup_periods
mock_requests_get.return_value = mock_response
Expand Down Expand Up @@ -606,13 +488,7 @@ def test_unit_data_init(
data_type,
setup_data,
):
"""Unit test for Data init method.
Args:
mock_get_and_parse_request: mock _get_and_parse_request get method
mock_tuple: mock of tuple call
data_type: format of api data
"""
"""Unit test for Data init method."""
(
mock_response,
expected_answer,
Expand Down
Loading

0 comments on commit a49cb40

Please sign in to comment.