Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nextbus: Listify directions #103337

Merged
merged 1 commit into from
Nov 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion homeassistant/components/nextbus/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
)

from .const import CONF_AGENCY, CONF_ROUTE, CONF_STOP, DOMAIN
from .util import listify

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -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"]

Expand Down
39 changes: 28 additions & 11 deletions tests/components/nextbus/conftest.py
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -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,
}
}

Expand Down