From d75d9611bfbcae92a2998d4238ae23ebab81ce36 Mon Sep 17 00:00:00 2001 From: jadirj <52298160+jadirj@users.noreply.github.com> Date: Thu, 17 Oct 2024 16:53:23 -0300 Subject: [PATCH] =?UTF-8?q?Exerc=C3=ADcio=2047?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../intranet/services/clima/openmeteo.py | 13 ++- backend/tests/services/conftest.py | 34 +++++++ backend/tests/services/openmeteo.json | 96 +++++++++++++++++++ backend/tests/services/test_clima_get.py | 50 ++++++++++ 4 files changed, 191 insertions(+), 2 deletions(-) create mode 100644 backend/tests/services/conftest.py create mode 100644 backend/tests/services/openmeteo.json create mode 100644 backend/tests/services/test_clima_get.py diff --git a/backend/src/trepr/intranet/services/clima/openmeteo.py b/backend/src/trepr/intranet/services/clima/openmeteo.py index a60a28b..82cfe9a 100644 --- a/backend/src/trepr/intranet/services/clima/openmeteo.py +++ b/backend/src/trepr/intranet/services/clima/openmeteo.py @@ -65,6 +65,15 @@ def _formatar_resposta(data: dict) -> dict: } +def _obtem_dados_open_meteo(params: dict) -> dict: + """Realiza chamada ao serviço Open Meteo. + + Esse método existe para facilitar o mock em testes. + """ + response = requests.get(BASE_URL, params=params) + return response.json() + + @cache(time_30m_key) def dados_clima(latitude: str, longitude: str, timezone: str) -> dict: """Chama o serviço Open Meteo e retorna os dados de clima.""" @@ -80,7 +89,7 @@ def dados_clima(latitude: str, longitude: str, timezone: str) -> dict: } # Realiza a requisição logger.info("Acesso ao OpenMeteo") - response = requests.get(BASE_URL, params=params) + raw_data = _obtem_dados_open_meteo(params) logger.info("Parseia dados recebidos") - data = _formatar_resposta(response.json()) + data = _formatar_resposta(raw_data) return data diff --git a/backend/tests/services/conftest.py b/backend/tests/services/conftest.py new file mode 100644 index 0000000..dd06a16 --- /dev/null +++ b/backend/tests/services/conftest.py @@ -0,0 +1,34 @@ +from plone.app.testing import SITE_OWNER_NAME +from plone.app.testing import SITE_OWNER_PASSWORD +from plone.restapi.testing import RelativeSession + +import pytest + + +@pytest.fixture() +def request_factory(portal): + def factory(): + url = portal.absolute_url() + api_session = RelativeSession(url) + api_session.headers.update({"Accept": "application/json"}) + return api_session + + return factory + + +@pytest.fixture() +def anon_request(request_factory): + return request_factory() + + +@pytest.fixture() +def manager_request(request_factory): + request = request_factory() + request.auth = (SITE_OWNER_NAME, SITE_OWNER_PASSWORD) + yield request + request.auth = () + + +@pytest.fixture +def portal(functional): + return functional["portal"] diff --git a/backend/tests/services/openmeteo.json b/backend/tests/services/openmeteo.json new file mode 100644 index 0000000..9b62699 --- /dev/null +++ b/backend/tests/services/openmeteo.json @@ -0,0 +1,96 @@ +{ + "latitude": -25.5, + "longitude": -49.25, + "generationtime_ms": 0.03898143768310547, + "utc_offset_seconds": -10800, + "timezone": "America/Sao_Paulo", + "timezone_abbreviation": "-03", + "elevation": 901.0, + "current_units": { + "time": "iso8601", + "interval": "seconds", + "weather_code": "wmo code", + "temperature_2m": "\u00b0C" + }, + "current": { + "time": "2024-10-17T11:00", + "interval": 900, + "weather_code": 2, + "temperature_2m": 24.0 + }, + "hourly_units": { + "time": "iso8601", + "temperature_2m": "\u00b0C" + }, + "hourly": { + "time": [ + "2024-10-17T00:00", + "2024-10-17T01:00", + "2024-10-17T02:00", + "2024-10-17T03:00", + "2024-10-17T04:00", + "2024-10-17T05:00", + "2024-10-17T06:00", + "2024-10-17T07:00", + "2024-10-17T08:00", + "2024-10-17T09:00", + "2024-10-17T10:00", + "2024-10-17T11:00", + "2024-10-17T12:00", + "2024-10-17T13:00", + "2024-10-17T14:00", + "2024-10-17T15:00", + "2024-10-17T16:00", + "2024-10-17T17:00", + "2024-10-17T18:00", + "2024-10-17T19:00", + "2024-10-17T20:00", + "2024-10-17T21:00", + "2024-10-17T22:00", + "2024-10-17T23:00" + ], + "temperature_2m": [ + 16.2, + 16.1, + 16.1, + 16.1, + 16.0, + 15.8, + 15.7, + 16.5, + 18.2, + 20.0, + 22.2, + 24.0, + 25.6, + 26.5, + 27.0, + 27.4, + 27.6, + 27.2, + 25.7, + 20.8, + 18.9, + 17.8, + 17.3, + 17.2 + ] + }, + "daily_units": { + "time": "iso8601", + "sunrise": "iso8601", + "sunset": "iso8601" + }, + "daily": { + "time": [ + "2024-10-17" + ], + "sunrise": [ + "2024-10-17T05:39" + ], + "sunset": [ + "2024-10-17T18:24" + ] + } + } + \ No newline at end of file diff --git a/backend/tests/services/test_clima_get.py b/backend/tests/services/test_clima_get.py new file mode 100644 index 0000000..5aad742 --- /dev/null +++ b/backend/tests/services/test_clima_get.py @@ -0,0 +1,50 @@ +from pathlib import Path + +import json +import pytest + + +LOCAL_PATH = Path(__file__).parent + + +@pytest.fixture(scope="session") +def openmeteo_response(): + raw_data = (LOCAL_PATH / "openmeteo.json").read_text() + return json.loads(raw_data) + + +@pytest.fixture +def patch_openmeteo(monkeypatch, openmeteo_response): + from trepr.intranet.services.clima import openmeteo + + def mockreturn(params): + return openmeteo_response + + monkeypatch.setattr(openmeteo, "_obtem_dados_open_meteo", mockreturn) + + +class TestServiceClimaGet: + portal: str = "" + + @pytest.fixture(autouse=True) + def _setup(self, portal, patch_openmeteo): + self.portal = portal + self.endpoint = "/@clima" + + def test_get_status_code(self, manager_request): + response = manager_request.get(self.endpoint) + assert response.status_code == 200 + + @pytest.mark.parametrize( + "key,expected", + [ + ["@id", True], + ["events", True], + ["temperature", True], + ["weather", True], + ], + ) + def test_get_keys(self, manager_request, key: str, expected: bool): + response = manager_request.get(self.endpoint) + data = response.json() + assert (key in data) is expected