-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_load_location_ids.py
More file actions
314 lines (257 loc) · 9.26 KB
/
Copy pathtest_load_location_ids.py
File metadata and controls
314 lines (257 loc) · 9.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import pandas as pd
import pytest
from click.testing import CliRunner
import cwmscli.load.location.location_ids as location_ids_module
from cwmscli.load.location.location import location as location_group
def test_load_locations_prefers_saved_token_for_target(monkeypatch):
calls = []
monkeypatch.setattr(
"cwmscli.utils.get_saved_login_token", lambda *args, **kwargs: "saved-token"
)
class FakeCatalogResponse:
df = pd.DataFrame([{"name": "LOC_A"}])
class FakeLocationResponse:
json = [{"name": "LOC_A", "active": True}]
class FakeCwms:
@staticmethod
def init_session(api_root, api_key=None, token=None):
calls.append(("init_session", api_root, api_key, token))
@staticmethod
def get_locations_catalog(**kwargs):
return FakeCatalogResponse()
@staticmethod
def get_locations(**kwargs):
return FakeLocationResponse()
@staticmethod
def store_location(data, fail_if_exists=False):
return {"stored": data["name"]}
monkeypatch.setattr(location_ids_module, "cwms", FakeCwms)
location_ids_module.load_locations(
source_cda="https://source.example/cwms-data",
source_office="SWT",
target_cda="http://localhost:8082/cwms-data",
target_api_key="apikey 123",
verbose=0,
dry_run=False,
like="LOC*",
location_kind_like=["PROJECT"],
)
assert calls[0] == (
"init_session",
"https://source.example/cwms-data",
None,
"saved-token",
)
assert calls[1] == (
"init_session",
"http://localhost:8082/cwms-data",
None,
"saved-token",
)
def test_load_locations_uses_catalog_matches_to_fetch_full_records(monkeypatch):
monkeypatch.setattr(
"cwmscli.utils.get_saved_login_token", lambda *args, **kwargs: None
)
calls = []
stored = []
class FakeCatalogResponse:
df = pd.DataFrame([{"name": "LOC_A"}, {"name": "LOC_B"}])
class FakeLocationResponse:
def __init__(self, location_id):
self.json = [{"name": location_id, "active": True}]
class FakeCwms:
@staticmethod
def init_session(api_root, api_key=None):
calls.append(("init_session", api_root, api_key))
@staticmethod
def get_locations_catalog(**kwargs):
calls.append(("get_locations_catalog", kwargs))
return FakeCatalogResponse()
@staticmethod
def get_locations(**kwargs):
calls.append(("get_locations", kwargs))
return FakeLocationResponse(kwargs["location_ids"].strip("^$"))
@staticmethod
def store_location(data, fail_if_exists=False):
stored.append((data["name"], fail_if_exists))
return {"stored": data["name"]}
monkeypatch.setattr(location_ids_module, "cwms", FakeCwms)
location_ids_module.load_locations(
source_cda="https://source.example/cwms-data",
source_office="SWT",
target_cda="http://localhost:8082/cwms-data",
target_api_key="apikey 123",
verbose=0,
dry_run=False,
like="LOC*",
location_kind_like=["PROJECT"],
)
catalog_calls = [call for call in calls if call[0] == "get_locations_catalog"]
assert catalog_calls == [
(
"get_locations_catalog",
{"office_id": "SWT", "like": "LOC*", "location_kind_like": "PROJECT"},
)
]
get_location_calls = [call for call in calls if call[0] == "get_locations"]
assert get_location_calls == [
("get_locations", {"office_id": "SWT", "location_ids": "^LOC_A$"}),
("get_locations", {"office_id": "SWT", "location_ids": "^LOC_B$"}),
]
assert stored == [("LOC_A", False), ("LOC_B", False)]
def test_load_locations_reports_friendly_message_for_empty_source(monkeypatch, capsys):
monkeypatch.setattr(
"cwmscli.utils.get_saved_login_token", lambda *args, **kwargs: None
)
calls = []
class FakeCatalogResponse:
df = pd.DataFrame([])
class FakeCwms:
@staticmethod
def init_session(api_root, api_key=None):
calls.append(("init_session", api_root, api_key))
@staticmethod
def get_locations_catalog(**kwargs):
calls.append(("get_locations_catalog", kwargs))
return FakeCatalogResponse()
@staticmethod
def store_location(data, fail_if_exists=False):
calls.append(("store_location", data["name"]))
monkeypatch.setattr(location_ids_module, "cwms", FakeCwms)
location_ids_module.load_locations(
source_cda="https://source.example/cwms-data",
source_office="SWT",
target_cda="http://localhost:8082/cwms-data",
target_api_key="apikey 123",
verbose=0,
dry_run=False,
like="DOES_NOT_MATCH*",
location_kind_like=["PROJECT"],
)
output = capsys.readouterr().out
assert "No locations were returned from the source" in output
assert "Refine --like or --location-kind-like" in output
assert "CDA Swagger" in output
assert not [call for call in calls if call[0] == "store_location"]
assert [call for call in calls if call[0] == "init_session"] == [
("init_session", "https://source.example/cwms-data", None)
]
def test_target_csv_writes_locations_and_skips_store(tmp_path, monkeypatch):
monkeypatch.setattr(
"cwmscli.utils.get_saved_login_token", lambda *args, **kwargs: None
)
stored = []
class FakeLocationResponse:
json = [
{"name": "LOC_A", "office-id": "SWT", "active": True, "latitude": 36.1},
{"name": "LOC_B", "office-id": "SWT", "active": True, "latitude": 36.2},
]
class FakeCwms:
@staticmethod
def init_session(api_root, api_key=None):
pass
@staticmethod
def get_locations(**kwargs):
return FakeLocationResponse()
@staticmethod
def store_location(data, fail_if_exists=False):
stored.append(data["name"])
monkeypatch.setattr(location_ids_module, "cwms", FakeCwms)
out = tmp_path / "out.csv"
location_ids_module.load_locations(
source_cda="https://source.example/cwms-data",
source_office="SWT",
target_cda=None,
target_api_key=None,
verbose=0,
dry_run=False,
like=None,
location_kind_like=["ALL"],
target_csv=str(out),
)
assert stored == []
df = pd.read_csv(out)
assert df["name"].tolist() == ["LOC_A", "LOC_B"]
assert "office-id" in df.columns
# no anonymous index column leaks in
assert "Unnamed: 0" not in df.columns
def test_source_csv_reads_and_calls_store_per_row(tmp_path, monkeypatch):
monkeypatch.setattr(
"cwmscli.utils.get_saved_login_token", lambda *args, **kwargs: "saved-token"
)
stored = []
src = tmp_path / "in.csv"
pd.DataFrame(
[
{"name": "FROM_CSV_A", "office-id": "SWT", "active": True, "latitude": 1.0},
{"name": "FROM_CSV_B", "office-id": "SWT", "active": True, "latitude": 2.0},
]
).to_csv(src, index=False)
class FakeCwms:
@staticmethod
def init_session(api_root, api_key=None, token=None):
pass
@staticmethod
def store_location(data, fail_if_exists=False):
stored.append((data["name"], data["active"], data.get("latitude")))
monkeypatch.setattr(location_ids_module, "cwms", FakeCwms)
location_ids_module.load_locations(
source_cda=None,
source_office=None,
target_cda="http://localhost:8082/cwms-data",
target_api_key=None,
verbose=0,
dry_run=False,
like=None,
location_kind_like=["ALL"],
source_csv=str(src),
)
assert stored == [
("FROM_CSV_A", True, 1.0),
("FROM_CSV_B", True, 2.0),
]
def test_cli_rejects_source_csv_and_target_csv_together(tmp_path, monkeypatch):
monkeypatch.setattr(
"cwmscli.utils.get_saved_login_token", lambda *args, **kwargs: None
)
src = tmp_path / "in.csv"
src.write_text("name,office-id,active\nLOC_A,SWT,True\n")
out = tmp_path / "out.csv"
runner = CliRunner()
result = runner.invoke(
location_group,
[
"ids-all",
"--source-csv",
str(src),
"--target-csv",
str(out),
],
)
assert result.exit_code != 0
assert (
"no CDA is involved" in result.output or "mutually exclusive" in result.output
)
def test_cli_rejects_source_csv_with_explicit_source_cda(tmp_path, monkeypatch):
monkeypatch.setattr(
"cwmscli.utils.get_saved_login_token", lambda *args, **kwargs: None
)
src = tmp_path / "in.csv"
src.write_text("name,office-id,active\nLOC_A,SWT,True\n")
runner = CliRunner()
result = runner.invoke(
location_group,
[
"ids-all",
"--source-csv",
str(src),
"--source-cda",
"https://override.example/cwms-data",
"--target-cda",
"http://localhost:8082/cwms-data",
"--target-api-key",
"k",
],
)
assert result.exit_code != 0
assert "mutually exclusive" in result.output