|
5 | 5 | from typing import Any
|
6 | 6 |
|
7 | 7 | import pytest
|
| 8 | +from niquests import AsyncSession, Session |
8 | 9 | from openmeteo_sdk.Variable import Variable
|
9 | 10 |
|
10 | 11 | from openmeteo_requests import AsyncClient, Client, OpenMeteoRequestsError
|
11 | 12 |
|
12 | 13 |
|
| 14 | +def _process_fetchall_basic_responses(responses: list) -> None: |
| 15 | + assert len(responses) == 3 |
| 16 | + response = responses[0] |
| 17 | + assert response.Latitude() == pytest.approx(52.5) |
| 18 | + assert response.Longitude() == pytest.approx(13.4) |
| 19 | + response = responses[1] |
| 20 | + assert response.Latitude() == pytest.approx(48.1) |
| 21 | + assert response.Longitude() == pytest.approx(9.3) |
| 22 | + response = responses[0] |
| 23 | + |
| 24 | + hourly = response.Hourly() |
| 25 | + hourly_variables = list(map(lambda i: hourly.Variables(i), range(hourly.VariablesLength()))) |
| 26 | + |
| 27 | + temperature_2m = next( |
| 28 | + filter( |
| 29 | + lambda x: x.Variable() == Variable.temperature and x.Altitude() == 2, |
| 30 | + hourly_variables, |
| 31 | + ) |
| 32 | + ) |
| 33 | + precipitation = next( |
| 34 | + filter( |
| 35 | + lambda x: x.Variable() == Variable.precipitation, |
| 36 | + hourly_variables, |
| 37 | + ) |
| 38 | + ) |
| 39 | + |
| 40 | + assert temperature_2m.ValuesLength() == 48 |
| 41 | + assert precipitation.ValuesLength() == 48 |
| 42 | + |
| 43 | + |
13 | 44 | @pytest.fixture
|
14 | 45 | def client() -> Client:
|
15 | 46 | return Client()
|
@@ -44,90 +75,58 @@ def params() -> _ParamsType:
|
44 | 75 |
|
45 | 76 |
|
46 | 77 | class TestClient:
|
47 |
| - def test_fetch_all( |
48 |
| - self, client: Client, url: str, params: _ParamsType |
49 |
| - ) -> None: |
| 78 | + def test_fetch_all(self, client: Client, url: str, params: _ParamsType) -> None: |
50 | 79 | responses = client.weather_api(url=url, params=params)
|
51 |
| - |
52 |
| - assert len(responses) == 3 |
53 |
| - response = responses[0] |
54 |
| - assert response.Latitude() == pytest.approx(52.5) |
55 |
| - assert response.Longitude() == pytest.approx(13.4) |
56 |
| - response = responses[1] |
57 |
| - assert response.Latitude() == pytest.approx(48.1) |
58 |
| - assert response.Longitude() == pytest.approx(9.3) |
59 |
| - response = responses[0] |
60 |
| - |
61 |
| - hourly = response.Hourly() |
62 |
| - hourly_variables = list( |
63 |
| - map(lambda i: hourly.Variables(i), range(hourly.VariablesLength())) |
64 |
| - ) |
65 |
| - |
66 |
| - temperature_2m = next( |
67 |
| - filter( |
68 |
| - lambda x: x.Variable() == Variable.temperature |
69 |
| - and x.Altitude() == 2, |
70 |
| - hourly_variables, |
71 |
| - ) |
72 |
| - ) |
73 |
| - precipitation = next( |
74 |
| - filter( |
75 |
| - lambda x: x.Variable() == Variable.precipitation, |
76 |
| - hourly_variables, |
77 |
| - ) |
78 |
| - ) |
79 |
| - |
80 |
| - assert temperature_2m.ValuesLength() == 48 |
81 |
| - assert precipitation.ValuesLength() == 48 |
| 80 | + _process_fetchall_basic_responses(responses) |
82 | 81 |
|
83 | 82 | def test_empty_url_error(self, client: Client, params: _ParamsType) -> None:
|
84 | 83 | with pytest.raises(OpenMeteoRequestsError):
|
85 | 84 | client.weather_api(url="", params=params)
|
86 | 85 |
|
| 86 | + def test_sequential_requests_with_common_session(self, url: str, params: _ParamsType) -> None: |
| 87 | + session = Session() |
| 88 | + client = Client(session) |
| 89 | + try: |
| 90 | + # OK |
| 91 | + _process_fetchall_basic_responses(client.weather_api(url=url, params=params)) |
| 92 | + # must not fail |
| 93 | + _process_fetchall_basic_responses(client.weather_api(url=url, params=params)) |
| 94 | + finally: |
| 95 | + session.close() |
| 96 | + # expected result -> the session is already closed -> failure |
| 97 | + # actual result -> OK. Niquests seems to allow reuse of closed sessions |
| 98 | + client.weather_api(url=url, params=params) |
| 99 | + # with pytest.raises(OpenMeteoRequestsError): |
| 100 | + _process_fetchall_basic_responses(client.weather_api(url=url, params=params)) |
| 101 | + |
87 | 102 |
|
88 | 103 | @pytest.mark.asyncio
|
89 | 104 | class TestAsyncClient:
|
90 | 105 | async def test_async_fetch_all(
|
91 | 106 | self, async_client: AsyncClient, url: str, params: _ParamsType
|
92 | 107 | ) -> None:
|
93 | 108 | responses = await async_client.weather_api(url=url, params=params)
|
| 109 | + _process_fetchall_basic_responses(responses) |
94 | 110 |
|
95 |
| - assert len(responses) == 3 |
96 |
| - response = responses[0] |
97 |
| - assert response.Latitude() == pytest.approx(52.5) |
98 |
| - assert response.Longitude() == pytest.approx(13.4) |
99 |
| - response = responses[1] |
100 |
| - assert response.Latitude() == pytest.approx(48.1) |
101 |
| - assert response.Longitude() == pytest.approx(9.3) |
102 |
| - response = responses[0] |
103 |
| - |
104 |
| - hourly = response.Hourly() |
105 |
| - hourly_variables = list( |
106 |
| - map(lambda i: hourly.Variables(i), range(hourly.VariablesLength())) |
107 |
| - ) |
108 |
| - |
109 |
| - temperature_2m = next( |
110 |
| - filter( |
111 |
| - lambda x: x.Variable() == Variable.temperature |
112 |
| - and x.Altitude() == 2, |
113 |
| - hourly_variables, |
114 |
| - ) |
115 |
| - ) |
116 |
| - precipitation = next( |
117 |
| - filter( |
118 |
| - lambda x: x.Variable() == Variable.precipitation, |
119 |
| - hourly_variables, |
120 |
| - ) |
121 |
| - ) |
122 |
| - |
123 |
| - assert temperature_2m.ValuesLength() == 48 |
124 |
| - assert precipitation.ValuesLength() == 48 |
| 111 | + async def test_empty_url_error(self, async_client: AsyncClient, params: _ParamsType) -> None: |
| 112 | + with pytest.raises(OpenMeteoRequestsError): |
| 113 | + await async_client.weather_api(url="", params=params) |
125 | 114 |
|
126 |
| - async def test_empty_url_error( |
127 |
| - self, async_client: AsyncClient, params: _ParamsType |
| 115 | + async def test_sequential_requests_with_common_session( |
| 116 | + self, url: str, params: _ParamsType |
128 | 117 | ) -> None:
|
| 118 | + session = AsyncSession() |
| 119 | + client = AsyncClient(session) |
| 120 | + try: |
| 121 | + # OK |
| 122 | + _process_fetchall_basic_responses(await client.weather_api(url=url, params=params)) |
| 123 | + # must not fail |
| 124 | + _process_fetchall_basic_responses(await client.weather_api(url=url, params=params)) |
| 125 | + finally: |
| 126 | + await session.close() |
| 127 | + # The session is already closed -> failure |
129 | 128 | with pytest.raises(OpenMeteoRequestsError):
|
130 |
| - await async_client.weather_api(url="", params=params) |
| 129 | + _process_fetchall_basic_responses(await client.weather_api(url=url, params=params)) |
131 | 130 |
|
132 | 131 |
|
133 | 132 | def test_int_client():
|
|
0 commit comments