Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
22 changes: 10 additions & 12 deletions dataretrieval/nldi.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,14 +253,11 @@ def get_features(
raise ValueError(
"navigation_mode is required if comid or data_source is provided"
)
# validate the feature source and comid
_validate_feature_source_comid(feature_source, feature_id, comid)
# validate the data source
if data_source:
if data_source is not None:
_validate_data_source(data_source)
# validate feature source
_validate_data_source(feature_source)
# validate the navigation mode
if feature_source is not None:
_validate_data_source(feature_source)
if navigation_mode:
_validate_navigation_mode(navigation_mode)

Expand Down Expand Up @@ -475,12 +472,13 @@ def _validate_data_source(data_source: str):
url, {}, "Error getting available data sources"
)
_AVAILABLE_DATA_SOURCES = [ds["source"] for ds in available_data_sources]
if data_source not in _AVAILABLE_DATA_SOURCES:
err_msg = (
f"Invalid data source '{data_source}'."
f" Available data sources are: {_AVAILABLE_DATA_SOURCES}"
)
raise ValueError(err_msg)

if data_source not in _AVAILABLE_DATA_SOURCES:
err_msg = (
f"Invalid data source '{data_source}'."
f" Available data sources are: {_AVAILABLE_DATA_SOURCES}"
)
raise ValueError(err_msg)


def _validate_navigation_mode(navigation_mode: str):
Expand Down
22 changes: 22 additions & 0 deletions tests/nldi_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import pytest
from geopandas import GeoDataFrame

import dataretrieval.nldi as nldi
from dataretrieval.nldi import (
NLDI_API_BASE_URL,
get_basin,
Expand All @@ -9,6 +11,12 @@
)


@pytest.fixture(autouse=True)
def _reset_data_source_cache(monkeypatch):
"""Reset the module-level cache between tests."""
monkeypatch.setattr(nldi, "_AVAILABLE_DATA_SOURCES", None)


def mock_request_data_sources(requests_mock):
request_url = f"{NLDI_API_BASE_URL}/"
available_data_sources = [
Expand Down Expand Up @@ -280,3 +288,17 @@ def test_search_for_features_by_lat_long(requests_mock):
assert search_results["features"][0]["type"] == "Feature"
assert search_results["features"][0]["geometry"]["type"] == "LineString"
assert len(search_results["features"][0]["geometry"]["coordinates"]) == 27


def test_validate_data_source_rejects_invalid_after_cache_populated(requests_mock):
"""Once the cache is warm, invalid data sources must still raise ValueError.

Regression: previously the validation check was nested inside the
cache-population branch, so all calls after the first silently passed.
"""
mock_request_data_sources(requests_mock)

nldi._validate_data_source("WQP")

with pytest.raises(ValueError, match="Invalid data source 'not_a_real_source'"):
nldi._validate_data_source("not_a_real_source")