fix: H1 — IndexError в format_weather_data_for_console при пустом current_condition
- Защита от пустого списка [] и пустого dict [{}] в current_condition
- Возврат None вместо краша, команда !pg показывает fallback-сообщение
- Обновлён тест test_pg_empty_current_condition (graceful fallback вместо IndexError)
230 тестов проходят.
This commit is contained in:
parent
900128fbd4
commit
5d9c33a12c
@ -73,14 +73,15 @@ class TestPgCommand:
|
|||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_pg_empty_current_condition(self):
|
async def test_pg_empty_current_condition(self):
|
||||||
"""current_condition пустой список — код выбрасывает IndexError (баг в коде)."""
|
"""current_condition пустой список — graceful fallback."""
|
||||||
cog = self._make_cog()
|
cog = self._make_cog()
|
||||||
ctx = self._make_ctx()
|
ctx = self._make_ctx()
|
||||||
weather = {"current_condition": []}
|
weather = {"current_condition": []}
|
||||||
|
|
||||||
with patch("commands.pg.fetch_weather", new=AsyncMock(return_value=weather)):
|
with patch("commands.pg.fetch_weather", new=AsyncMock(return_value=weather)):
|
||||||
with pytest.raises(IndexError):
|
|
||||||
await cog.pg.callback(cog, ctx)
|
await cog.pg.callback(cog, ctx)
|
||||||
|
ctx.send.assert_called_once()
|
||||||
|
assert "Не удалось получить данные о погоде" in ctx.send.call_args[0][0]
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_pg_current_condition_none(self):
|
async def test_pg_current_condition_none(self):
|
||||||
|
|||||||
@ -155,7 +155,10 @@ def format_weather_data_for_console(data):
|
|||||||
"""
|
"""
|
||||||
if data is None:
|
if data is None:
|
||||||
return None
|
return None
|
||||||
current = data.get("current_condition", [{}])[0]
|
current_condition_list = data.get("current_condition", [])
|
||||||
|
if not current_condition_list:
|
||||||
|
return None
|
||||||
|
current = current_condition_list[0]
|
||||||
if not current:
|
if not current:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user