From 59b584d9432439722cc32ec73e78a3b5c1f6f2fe Mon Sep 17 00:00:00 2001 From: Jules Dejaeghere Date: Sun, 16 Jun 2024 17:24:03 +0200 Subject: [PATCH] Refactor and add tests --- src/open_irceline/api.py | 23 ++++----------------- src/open_irceline/utils.py | 18 ++++++++++++++++ tests/test_api.py | 2 ++ tests/{test_sized_dict.py => test_utils.py} | 21 ++++++++++++++++--- 4 files changed, 42 insertions(+), 22 deletions(-) rename tests/{test_sized_dict.py => test_utils.py} (55%) diff --git a/src/open_irceline/api.py b/src/open_irceline/api.py index 26a1863..3f62e7b 100644 --- a/src/open_irceline/api.py +++ b/src/open_irceline/api.py @@ -11,9 +11,9 @@ import aiohttp import async_timeout from aiohttp import ClientResponse -from . import project_transform, rio_wfs_base_url, user_agent, forecast_base_url +from . import rio_wfs_base_url, user_agent, forecast_base_url from .data import RioFeature, FeatureValue, ForecastFeature -from .utils import SizedDict +from .utils import SizedDict, epsg_transform, round_coordinates class IrcelineApiError(Exception): @@ -80,16 +80,6 @@ class IrcelineBaseClient: class IrcelineRioClient(IrcelineBaseClient): """API client for RIO interpolated IRCEL - CELINE open data""" - @staticmethod - def _epsg_transform(position: Tuple[float, float]) -> tuple: - """ - Convert 'EPSG:4326' coordinates to 'EPSG:31370' coordinates - :param position: (x, y) coordinates - :return: tuple of int in the EPSG:31370 system - """ - result = project_transform.transform(position[0], position[1]) - return round(result[0]), round(result[1]) - async def get_rio_value(self, timestamp: datetime | date, features: List[RioFeature], @@ -116,7 +106,7 @@ class IrcelineRioClient(IrcelineBaseClient): else: raise IrcelineApiError(f"Wrong parameter type for timestamp: {type(timestamp)}") - coord = self._epsg_transform(position) + coord = epsg_transform(position) querystring = {"service": "WFS", "version": "1.3.0", "request": "GetFeature", @@ -207,13 +197,8 @@ class IrcelineRioClient(IrcelineBaseClient): class IrcelineForecastClient(IrcelineBaseClient): """API client for forecast IRCEL - CELINE open data""" - @staticmethod - def _round_coordinates(x: float, y: float, step=.05): - n = 1 / step - return round(x * n) / n, round(y * n) / n - async def get_forecast(self, day: date, features: List[ForecastFeature], position: Tuple[float, float]) -> dict: - x, y = self._round_coordinates(position[0], position[1]) + x, y = round_coordinates(position[0], position[1]) result = dict() diff --git a/src/open_irceline/utils.py b/src/open_irceline/utils.py index 4ed0db1..e81cb63 100644 --- a/src/open_irceline/utils.py +++ b/src/open_irceline/utils.py @@ -1,4 +1,7 @@ from collections import OrderedDict +from typing import Tuple + +from src.open_irceline import project_transform class SizedDict(OrderedDict): @@ -24,3 +27,18 @@ class SizedDict(OrderedDict): def update(self, __m, **kwargs): raise NotImplementedError() + + +def epsg_transform(position: Tuple[float, float]) -> tuple: + """ + Convert 'EPSG:4326' coordinates to 'EPSG:31370' coordinates + :param position: (x, y) coordinates + :return: tuple of int in the EPSG:31370 system + """ + result = project_transform.transform(position[0], position[1]) + return round(result[0]), round(result[1]) + + +def round_coordinates(x: float, y: float, step=.05): + n = 1 / step + return round(x * n) / n, round(y * n) / n diff --git a/tests/test_api.py b/tests/test_api.py index 7b84e94..864fe64 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -61,3 +61,5 @@ def test_parse_capabilities(): 'rio:bc_dmean'} assert result == expected + +# TODO add new tests for IrcelineForecastClient diff --git a/tests/test_sized_dict.py b/tests/test_utils.py similarity index 55% rename from tests/test_sized_dict.py rename to tests/test_utils.py index 94797ac..b8fac2f 100644 --- a/tests/test_sized_dict.py +++ b/tests/test_utils.py @@ -1,5 +1,5 @@ -from src.open_irceline.utils import SizedDict - +from src.open_irceline.utils import SizedDict, round_coordinates, epsg_transform +import pytest def test_size_dict(): s_dict = SizedDict(5) @@ -19,7 +19,7 @@ def test_size_dict(): s_dict['b'] = 42 s_dict['g'] = 7 - assert s_dict['f'] == 6 + assert s_dict.get('f') == 6 assert s_dict['g'] == 7 assert s_dict['b'] == 42 assert 'c' not in s_dict @@ -28,3 +28,18 @@ def test_size_dict(): del s_dict['b'] assert len(s_dict) == 4 assert 'b' not in s_dict + + with pytest.raises(NotImplementedError): + s_dict.update({'a': 1}) + + +def test_round_coord(): + x, y = round_coordinates(50.4657, 4.8647) + assert x == 50.45 + assert y == 4.85 + + +def test_epsg_transform(): + x, y = epsg_transform((50.4657, 4.8647)) + assert x == 185211 + assert y == 128437