Nextbus: Listify directions (#103337)

When a single value is returned, the list wrapper is not present in the
json payload. This patch ensures that the result is always a list.
pull/103356/head
Ian 2023-11-04 00:56:27 -07:00 committed by GitHub
parent ae1117bc74
commit 51c3a5d11d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 12 deletions

View File

@ -16,6 +16,7 @@ from homeassistant.helpers.selector import (
)
from .const import CONF_AGENCY, CONF_ROUTE, CONF_STOP, DOMAIN
from .util import listify
_LOGGER = logging.getLogger(__name__)
@ -51,7 +52,7 @@ def _get_stop_tags(
title_counts = Counter(tags.values())
stop_directions: dict[str, str] = {}
for direction in route_config["route"]["direction"]:
for direction in listify(route_config["route"]["direction"]):
for stop in direction["stop"]:
stop_directions[stop["tag"]] = direction["name"]

View File

@ -1,11 +1,37 @@
"""Test helpers for NextBus tests."""
from typing import Any
from unittest.mock import MagicMock
import pytest
@pytest.fixture(
params=[
{"name": "Outbound", "stop": [{"tag": "5650"}]},
[
{
"name": "Outbound",
"stop": [{"tag": "5650"}],
},
{
"name": "Inbound",
"stop": [{"tag": "5651"}],
},
],
]
)
def route_config_direction(request: pytest.FixtureRequest) -> Any:
"""Generate alternative directions values.
When only on edirection is returned, it is not returned as a list, but instead an object.
"""
return request.param
@pytest.fixture
def mock_nextbus_lists(mock_nextbus: MagicMock) -> MagicMock:
def mock_nextbus_lists(
mock_nextbus: MagicMock, route_config_direction: Any
) -> MagicMock:
"""Mock all list functions in nextbus to test validate logic."""
instance = mock_nextbus.return_value
instance.get_agency_list.return_value = {
@ -22,16 +48,7 @@ def mock_nextbus_lists(mock_nextbus: MagicMock) -> MagicMock:
# Error case test. Duplicate title with no unique direction
{"tag": "5652", "title": "Market St & 7th St"},
],
"direction": [
{
"name": "Outbound",
"stop": [{"tag": "5650"}],
},
{
"name": "Inbound",
"stop": [{"tag": "5651"}],
},
],
"direction": route_config_direction,
}
}