-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_clob.py
More file actions
584 lines (471 loc) · 16.1 KB
/
Copy pathtest_clob.py
File metadata and controls
584 lines (471 loc) · 16.1 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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
import logging
import sys
import types
import pandas as pd
import pytest
from cwmscli.commands import commands_cwms
from cwmscli.commands.clob import (
_clob_endpoint_id,
_default_download_dest,
delete_cmd,
download_cmd,
list_cmd,
update_cmd,
)
def test_blob_and_clob_upload_keep_no_overwrite_flag():
blob_overwrite = next(
param
for param in commands_cwms.blob_upload.params
if getattr(param, "name", None) == "overwrite"
)
clob_overwrite = next(
param
for param in commands_cwms.clob_upload.params
if getattr(param, "name", None) == "overwrite"
)
assert "--overwrite" in blob_overwrite.opts
assert "--no-overwrite" in blob_overwrite.secondary_opts
assert "--overwrite" in clob_overwrite.opts
assert "--no-overwrite" in clob_overwrite.secondary_opts
def test_clob_endpoint_id_uses_ignored_path_for_special_chars():
assert _clob_endpoint_id("plain_id") == ("PLAIN_ID", None)
assert _clob_endpoint_id("path/id") == ("ignored", "PATH/ID")
def test_default_download_dest_strips_leading_path_separators():
assert _default_download_dest("/REPORTS/REL-CLOB") == "REPORTS/REL-CLOB"
assert _default_download_dest("\\REPORTS\\REL-CLOB") == "REPORTS\\REL-CLOB"
def test_download_cmd_uses_default_dest_and_writes_text(
tmp_path, monkeypatch: pytest.MonkeyPatch
):
calls = []
class FakeClobResponse:
json = {"value": "retrieved clob text"}
class FakeCwms:
@staticmethod
def init_session(api_root, api_key):
calls.append(("init_session", api_root, api_key))
return None
@staticmethod
def get_clob(office_id, clob_id):
calls.append(("get_clob", office_id, clob_id))
return FakeClobResponse()
monkeypatch.setitem(sys.modules, "cwms", FakeCwms)
monkeypatch.setattr("cwmscli.commands.clob.cwms", FakeCwms)
class FakeHTTPError(Exception):
pass
monkeypatch.setitem(
sys.modules, "requests", types.SimpleNamespace(HTTPError=FakeHTTPError)
)
monkeypatch.setattr(
"cwmscli.commands.clob.requests",
types.SimpleNamespace(HTTPError=FakeHTTPError),
)
monkeypatch.chdir(tmp_path)
download_cmd(
clob_id="test_clob",
dest=None,
office="SWT",
api_root="https://example.test/",
api_key="apikey 123",
dry_run=False,
)
saved = tmp_path / "TEST_CLOB"
assert saved.exists()
assert saved.read_text(encoding="utf-8") == "retrieved clob text"
assert calls == [
("init_session", "https://example.test/", "apikey 123"),
("get_clob", "SWT", "TEST_CLOB"),
]
def test_download_cmd_default_dest_stays_relative_for_leading_slash_id(
tmp_path, monkeypatch: pytest.MonkeyPatch
):
class FakeCwms:
@staticmethod
def init_session(api_root, api_key):
return None
monkeypatch.setitem(sys.modules, "cwms", FakeCwms)
monkeypatch.setattr("cwmscli.commands.clob.cwms", FakeCwms)
monkeypatch.setattr(
"cwmscli.commands.clob._get_special_clob_text",
lambda office, clob_id: "retrieved clob text",
)
class FakeHTTPError(Exception):
pass
monkeypatch.setitem(
sys.modules, "requests", types.SimpleNamespace(HTTPError=FakeHTTPError)
)
monkeypatch.setattr(
"cwmscli.commands.clob.requests",
types.SimpleNamespace(HTTPError=FakeHTTPError),
)
monkeypatch.chdir(tmp_path)
download_cmd(
clob_id="/reports/rel-clob",
dest=None,
office="SWT",
api_root="https://example.test/",
api_key="apikey 123",
dry_run=False,
)
saved = tmp_path / "REPORTS" / "REL-CLOB"
assert saved.exists()
assert saved.read_text(encoding="utf-8") == "retrieved clob text"
def test_download_cmd_uses_query_override_for_special_char_ids(
tmp_path, monkeypatch: pytest.MonkeyPatch
):
calls = []
class FakeCwms:
@staticmethod
def init_session(api_root, api_key):
calls.append(("init_session", api_root, api_key))
return None
@staticmethod
def get_clob(office_id, clob_id, clob_id_query=None):
calls.append(("get_clob", office_id, clob_id, clob_id_query))
raise AssertionError("special-char path should use direct SESSION.get")
class FakeResponse:
text = "retrieved clob text"
def __enter__(self):
return self
def __exit__(self, exc_type, exc, tb):
return False
def raise_for_status(self):
return None
class FakeSession:
@staticmethod
def get(endpoint, params=None, headers=None):
calls.append(("session.get", endpoint, params, headers))
return FakeResponse()
monkeypatch.setitem(sys.modules, "cwms", FakeCwms)
monkeypatch.setattr("cwmscli.commands.clob.cwms", FakeCwms)
monkeypatch.setattr("cwmscli.commands.clob.cwms_api.SESSION", FakeSession())
class FakeHTTPError(Exception):
pass
monkeypatch.setitem(
sys.modules, "requests", types.SimpleNamespace(HTTPError=FakeHTTPError)
)
monkeypatch.setattr(
"cwmscli.commands.clob.requests",
types.SimpleNamespace(HTTPError=FakeHTTPError),
)
download_cmd(
clob_id="path/id",
dest=str(tmp_path / "downloaded.txt"),
office="SWT",
api_root="https://example.test/",
api_key="apikey 123",
dry_run=False,
)
assert calls == [
("init_session", "https://example.test/", "apikey 123"),
(
"session.get",
"clobs/ignored",
{"office": "SWT", "clob-id": "PATH/ID"},
{"Accept": "text/plain"},
),
]
def test_download_cmd_anonymous_skips_api_key(
tmp_path, monkeypatch: pytest.MonkeyPatch
):
calls = []
class FakeClobResponse:
json = {"value": "retrieved clob text"}
class FakeCwms:
@staticmethod
def init_session(api_root, api_key):
calls.append(("init_session", api_root, api_key))
return None
@staticmethod
def get_clob(office_id, clob_id):
return FakeClobResponse()
monkeypatch.setitem(sys.modules, "cwms", FakeCwms)
monkeypatch.setattr("cwmscli.commands.clob.cwms", FakeCwms)
class FakeHTTPError(Exception):
pass
monkeypatch.setitem(
sys.modules, "requests", types.SimpleNamespace(HTTPError=FakeHTTPError)
)
monkeypatch.setattr(
"cwmscli.commands.clob.requests",
types.SimpleNamespace(HTTPError=FakeHTTPError),
)
download_cmd(
clob_id="test_clob",
dest=str(tmp_path / "downloaded.txt"),
office="SWT",
api_root="https://example.test/",
api_key="apikey 123",
dry_run=False,
anonymous=True,
)
assert calls == [("init_session", "https://example.test/", None)]
def test_download_cmd_local_error_skips_scope_hint(
monkeypatch: pytest.MonkeyPatch, caplog
):
class FakeClobResponse:
json = {"value": "retrieved clob text"}
class FakeCwms:
@staticmethod
def init_session(api_root, api_key):
return None
@staticmethod
def get_clob(office_id, clob_id):
return FakeClobResponse()
monkeypatch.setitem(sys.modules, "cwms", FakeCwms)
monkeypatch.setattr("cwmscli.commands.clob.cwms", FakeCwms)
class FakeHTTPError(Exception):
pass
monkeypatch.setitem(
sys.modules, "requests", types.SimpleNamespace(HTTPError=FakeHTTPError)
)
monkeypatch.setattr(
"cwmscli.commands.clob.requests",
types.SimpleNamespace(HTTPError=FakeHTTPError),
)
monkeypatch.setattr(
"cwmscli.commands.clob.log_scoped_read_hint",
lambda **kwargs: (_ for _ in ()).throw(
AssertionError("scope hint should not run")
),
)
monkeypatch.setattr(
"cwmscli.commands.clob._write_clob_content",
lambda *args, **kwargs: (_ for _ in ()).throw(PermissionError("denied")),
)
with caplog.at_level(logging.ERROR), pytest.raises(SystemExit) as exc:
download_cmd(
clob_id="test_clob",
dest="downloaded.txt",
office="SWT",
api_root="https://example.test/",
api_key="apikey 123",
dry_run=False,
)
assert exc.value.code == 1
assert "pass --dest explicitly" in caplog.text
assert "/cli/blob.html" not in caplog.text
def test_download_cmd_http_error_logs_scope_hint(
tmp_path, monkeypatch: pytest.MonkeyPatch, caplog
):
class FakeResponse:
text = "Forbidden"
class FakeHTTPError(Exception):
response = FakeResponse()
class FakeCwms:
@staticmethod
def init_session(api_root, api_key):
return None
@staticmethod
def get_clob(office_id, clob_id):
raise FakeHTTPError()
monkeypatch.setitem(sys.modules, "cwms", FakeCwms)
monkeypatch.setattr("cwmscli.commands.clob.cwms", FakeCwms)
monkeypatch.setattr(
"cwmscli.commands.clob.requests",
types.SimpleNamespace(HTTPError=FakeHTTPError),
)
with caplog.at_level(logging.WARNING), pytest.raises(SystemExit) as exc:
download_cmd(
clob_id="test_clob",
dest=str(tmp_path / "downloaded.txt"),
office="SWT",
api_root="https://example.test/",
api_key="apikey 123",
dry_run=False,
)
assert exc.value.code == 1
assert "Access scope hint: an API key was sent" in caplog.text
assert "clob content" in caplog.text
def test_list_cmd_initializes_session_with_api_key(monkeypatch: pytest.MonkeyPatch):
calls = []
class FakeCwms:
@staticmethod
def init_session(api_root, api_key):
calls.append(("init_session", api_root, api_key))
return None
@staticmethod
def get_clobs(office_id, clob_id_like, page_size=None):
calls.append(("get_clobs", office_id, clob_id_like, page_size))
return pd.DataFrame([{"id": "TEST_CLOB", "description": "x"}])
monkeypatch.setitem(sys.modules, "cwms", FakeCwms)
monkeypatch.setattr("cwmscli.commands.clob.cwms", FakeCwms)
list_cmd(
clob_id_like="TEST_.*",
columns=[],
sort_by=[],
desc=False,
limit=None,
page_size=None,
to_csv=None,
office="SWT",
api_root="https://example.test/",
api_key="apikey 123",
)
assert calls == [
("init_session", "https://example.test/", "apikey 123"),
("get_clobs", "SWT", "TEST_.*", None),
]
def test_list_cmd_uses_limit_as_fetch_page_size(monkeypatch: pytest.MonkeyPatch):
calls = []
class FakeCwms:
@staticmethod
def init_session(api_root, api_key):
return None
@staticmethod
def get_clobs(office_id, clob_id_like, page_size=None):
calls.append((office_id, clob_id_like, page_size))
return pd.DataFrame(
[
{"id": "A", "description": "x"},
{"id": "B", "description": "y"},
]
)
monkeypatch.setitem(sys.modules, "cwms", FakeCwms)
monkeypatch.setattr("cwmscli.commands.clob.cwms", FakeCwms)
list_cmd(
clob_id_like="TEST_.*",
columns=[],
sort_by=[],
desc=False,
limit=25,
page_size=None,
to_csv=None,
office="SWT",
api_root="https://example.test/",
api_key="apikey 123",
)
assert calls == [("SWT", "TEST_.*", 25)]
def test_list_cmd_page_size_overrides_limit_for_fetch(monkeypatch: pytest.MonkeyPatch):
calls = []
class FakeCwms:
@staticmethod
def init_session(api_root, api_key):
return None
@staticmethod
def get_clobs(office_id, clob_id_like, page_size=None):
calls.append((office_id, clob_id_like, page_size))
return pd.DataFrame([{"id": "A", "description": "x"}])
monkeypatch.setitem(sys.modules, "cwms", FakeCwms)
monkeypatch.setattr("cwmscli.commands.clob.cwms", FakeCwms)
list_cmd(
clob_id_like="TEST_.*",
columns=[],
sort_by=[],
desc=False,
limit=25,
page_size=200,
to_csv=None,
office="SWT",
api_root="https://example.test/",
api_key="apikey 123",
)
assert calls == [("SWT", "TEST_.*", 200)]
def test_list_cmd_anonymous_skips_api_key(monkeypatch: pytest.MonkeyPatch):
calls = []
class FakeCwms:
@staticmethod
def init_session(api_root, api_key):
calls.append(("init_session", api_root, api_key))
return None
@staticmethod
def get_clobs(office_id, clob_id_like, page_size=None):
calls.append(("get_clobs", office_id, clob_id_like, page_size))
return pd.DataFrame([{"id": "TEST_CLOB", "description": "x"}])
monkeypatch.setitem(sys.modules, "cwms", FakeCwms)
monkeypatch.setattr("cwmscli.commands.clob.cwms", FakeCwms)
list_cmd(
clob_id_like="TEST_.*",
columns=[],
sort_by=[],
desc=False,
limit=None,
page_size=None,
to_csv=None,
office="SWT",
api_root="https://example.test/",
api_key="apikey 123",
anonymous=True,
)
assert calls == [
("init_session", "https://example.test/", None),
("get_clobs", "SWT", "TEST_.*", None),
]
def test_delete_cmd_uses_query_override_for_special_char_ids(
monkeypatch: pytest.MonkeyPatch,
):
calls = []
class FakeCwms:
@staticmethod
def init_session(api_root, api_key):
calls.append(("init_session", api_root, api_key))
return None
@staticmethod
def delete_clob(office_id, clob_id):
calls.append(("delete_clob", office_id, clob_id))
class FakeApi:
@staticmethod
def delete(endpoint, params=None):
calls.append(("api.delete", endpoint, params))
monkeypatch.setitem(sys.modules, "cwms", FakeCwms)
monkeypatch.setattr("cwmscli.commands.clob.cwms", FakeCwms)
monkeypatch.setattr("cwmscli.commands.clob.cwms_api", FakeApi)
delete_cmd(
clob_id="path/id",
office="SWT",
api_root="https://example.test/",
api_key="apikey 123",
dry_run=False,
)
assert calls == [
("init_session", "https://example.test/", "apikey 123"),
(
"api.delete",
"clobs/ignored",
{"office": "SWT", "clob-id": "PATH/ID"},
),
]
def test_update_cmd_uses_query_override_for_special_char_ids(
tmp_path, monkeypatch: pytest.MonkeyPatch
):
calls = []
file_path = tmp_path / "updated.txt"
file_path.write_text("updated clob text", encoding="utf-8")
class FakeCwms:
@staticmethod
def init_session(api_root, api_key):
calls.append(("init_session", api_root, api_key))
return None
@staticmethod
def update_clob(data, clob_id, ignore_nulls=True):
calls.append(("update_clob", data, clob_id, ignore_nulls))
class FakeApi:
@staticmethod
def patch(endpoint, data=None, params=None):
calls.append(("api.patch", endpoint, data, params))
monkeypatch.setitem(sys.modules, "cwms", FakeCwms)
monkeypatch.setattr("cwmscli.commands.clob.cwms", FakeCwms)
monkeypatch.setattr("cwmscli.commands.clob.cwms_api", FakeApi)
update_cmd(
input_file=str(file_path),
clob_id="path/id",
description="updated description",
ignore_nulls=False,
dry_run=False,
office="SWT",
api_root="https://example.test/",
api_key="apikey 123",
)
assert calls == [
("init_session", "https://example.test/", "apikey 123"),
(
"api.patch",
"clobs/ignored",
{
"office-id": "SWT",
"id": "PATH/ID",
"description": "updated description",
"value": "updated clob text",
},
{"clob-id": "PATH/ID", "ignore-nulls": False},
),
]