Skip to content

Commit a5b596c

Browse files
authored
fix: explain empty location loads (#233)
## Summary - return a friendly message when a load location source query returns no locations - point users toward refining `--like` / `--location-kind-like` and checking filters in CDA Swagger or the regex guide - avoid initializing the target session or reporting a misleading success path when there is nothing to copy Resolves #186 ## Validation - `poetry run pytest tests/commands/test_load_location_ids.py -q` - `poetry run pytest -q` - `poetry check`
1 parent 0917ce9 commit a5b596c

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

cwmscli/load/location/location_ids.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ def load_locations(
5757
if verbose:
5858
logger.info("Got %s locations from source", len(locations))
5959

60+
if not locations:
61+
click.echo(
62+
"No locations were returned from the source. Refine --like or "
63+
"--location-kind-like and try the filter in CDA Swagger or the "
64+
f"CDA regular expression guide: {CDA_REGEXP_GUIDE_URL}"
65+
)
66+
return
67+
6068
if dry_run:
6169
for loc in locations:
6270
logger.info(

tests/commands/test_load_location_ids.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,52 @@ def store_location(data, fail_if_exists=False):
126126
assert stored == [("LOC_A", False), ("LOC_B", False)]
127127

128128

129+
def test_load_locations_reports_friendly_message_for_empty_source(monkeypatch, capsys):
130+
monkeypatch.setattr(
131+
"cwmscli.utils.get_saved_login_token", lambda *args, **kwargs: None
132+
)
133+
calls = []
134+
135+
class FakeCatalogResponse:
136+
df = pd.DataFrame([])
137+
138+
class FakeCwms:
139+
@staticmethod
140+
def init_session(api_root, api_key=None):
141+
calls.append(("init_session", api_root, api_key))
142+
143+
@staticmethod
144+
def get_locations_catalog(**kwargs):
145+
calls.append(("get_locations_catalog", kwargs))
146+
return FakeCatalogResponse()
147+
148+
@staticmethod
149+
def store_location(data, fail_if_exists=False):
150+
calls.append(("store_location", data["name"]))
151+
152+
monkeypatch.setattr(location_ids_module, "cwms", FakeCwms)
153+
154+
location_ids_module.load_locations(
155+
source_cda="https://source.example/cwms-data",
156+
source_office="SWT",
157+
target_cda="http://localhost:8082/cwms-data",
158+
target_api_key="apikey 123",
159+
verbose=0,
160+
dry_run=False,
161+
like="DOES_NOT_MATCH*",
162+
location_kind_like=["PROJECT"],
163+
)
164+
165+
output = capsys.readouterr().out
166+
assert "No locations were returned from the source" in output
167+
assert "Refine --like or --location-kind-like" in output
168+
assert "CDA Swagger" in output
169+
assert not [call for call in calls if call[0] == "store_location"]
170+
assert [call for call in calls if call[0] == "init_session"] == [
171+
("init_session", "https://source.example/cwms-data", None)
172+
]
173+
174+
129175
def test_target_csv_writes_locations_and_skips_store(tmp_path, monkeypatch):
130176
monkeypatch.setattr(
131177
"cwmscli.utils.get_saved_login_token", lambda *args, **kwargs: None

0 commit comments

Comments
 (0)