Skip to content

Commit 81ac1a9

Browse files
NRL-1798 Update result with filename instead of path
1 parent 8b9eac6 commit 81ac1a9

File tree

2 files changed

+95
-54
lines changed

2 files changed

+95
-54
lines changed

scripts/delete_pointers_by_id.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class PointerDeletionContext:
8383
failed_deletes: list[str]
8484
start_time: datetime
8585
end_time: datetime
86-
output_file_path: str
86+
output_filename: str
8787

8888

8989
def _build_and_write_result(ctx: PointerDeletionContext) -> Dict[str, Any]:
@@ -109,13 +109,17 @@ def _build_and_write_result(ctx: PointerDeletionContext) -> Dict[str, Any]:
109109
},
110110
"failed_deletes": {"count": len(ctx.failed_deletes), "ids": ctx.failed_deletes},
111111
"deletes-took-secs": timedelta.total_seconds(ctx.end_time - ctx.start_time),
112-
"output_file_path": ctx.output_file_path,
112+
"output_filename": ctx.output_filename,
113113
}
114+
115+
script_dir = os.path.dirname(os.path.abspath(__file__)) or "."
116+
output_file_path = os.path.join(script_dir, ctx.output_filename)
117+
114118
try:
115-
_write_result_file(result, ctx.output_file_path)
119+
_write_result_file(result, output_file_path)
116120
except Exception as exc:
117121
result["_output_error"] = (
118-
f"Failed to write result file {ctx.output_file_path}: {exc}"
122+
f"Failed to write result file {ctx.output_filename}: {exc}"
119123
)
120124
_print_summary(result)
121125
return result
@@ -151,13 +155,13 @@ def count_from(field):
151155
print(f" pointer_not_found: {count_from('pointer_not_found')}")
152156
print(f" deleted_pointers: {count_from('deleted_pointers')}")
153157
print(f" failed_deletes: {count_from('failed_deletes')}")
154-
if "_output_error" in result:
155-
print(f" output_error: {result['_output_error']}")
156158
if "deletes-took-secs" in result:
157159
print(f" deletes-took-secs: {result.get('deletes-took-secs')}")
158-
if "output_file_path" in result:
159-
output_filename = os.path.basename(result.get("output_file_path"))
160-
print(f" See output file for full results: {output_filename}")
160+
161+
if "_output_error" in result:
162+
print(f" output_error: {result['_output_error']}")
163+
elif "output_filename" in result:
164+
print(f" See output file for full results: {result.get('output_filename')}")
161165
print("*******************************************************")
162166

163167

@@ -295,10 +299,7 @@ def _delete_pointers_by_id(
295299

296300
start_time = datetime.now(tz=timezone.utc)
297301
timestamp = start_time.strftime("%Y%m%dT%H%M%SZ")
298-
script_dir = os.path.dirname(os.path.abspath(__file__)) or "."
299-
output_file_path = os.path.join(
300-
script_dir, f"delete_results_{ods_code}_{timestamp}.json"
301-
)
302+
output_filename = f"delete_results_{ods_code}_{timestamp}.json"
302303

303304
if not pointers_to_delete:
304305
end_time = datetime.now(tz=timezone.utc)
@@ -313,7 +314,7 @@ def _delete_pointers_by_id(
313314
failed_deletes=[],
314315
start_time=start_time,
315316
end_time=end_time,
316-
output_file_path=output_file_path,
317+
output_filename=output_filename,
317318
)
318319
)
319320

@@ -342,7 +343,7 @@ def _delete_pointers_by_id(
342343
failed_deletes=[],
343344
start_time=start_time,
344345
end_time=end_time,
345-
output_file_path=output_file_path,
346+
output_filename=output_filename,
346347
)
347348
)
348349

@@ -369,7 +370,7 @@ def _delete_pointers_by_id(
369370
failed_deletes=[],
370371
start_time=start_time,
371372
end_time=end_time,
372-
output_file_path=output_file_path,
373+
output_filename=output_filename,
373374
)
374375
)
375376

@@ -390,7 +391,7 @@ def _delete_pointers_by_id(
390391
failed_deletes=failed_deletes,
391392
start_time=start_time,
392393
end_time=end_time,
393-
output_file_path=output_file_path,
394+
output_filename=output_filename,
394395
)
395396
)
396397
print(" Done")

scripts/tests/test_delete_pointers_by_id.py

Lines changed: 77 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -41,33 +41,6 @@ def test_load_empty_file(self, mock_open):
4141
assert result == []
4242

4343

44-
class TestBuildAndWriteResult:
45-
@patch("delete_pointers_by_id._write_result_file")
46-
@patch("delete_pointers_by_id._print_summary")
47-
def test_build_and_write_result(self, mock_print, mock_write):
48-
start_time = datetime.now(tz=timezone.utc)
49-
end_time = datetime.now(tz=timezone.utc)
50-
ctx = PointerDeletionContext(
51-
pointers_to_delete=["G3H9E-1", "G3H9E-2"],
52-
ods_code="G3H9E",
53-
matched_pointers=["G3H9E-1", "G3H9E-2"],
54-
mismatched_pointers=[],
55-
not_found_pointers=[],
56-
pointers_deleted=["G3H9E-1", "G3H9E-2"],
57-
failed_deletes=[],
58-
start_time=start_time,
59-
end_time=end_time,
60-
output_file_path="/tmp/test.json",
61-
)
62-
result = _build_and_write_result(ctx)
63-
64-
assert result["pointers_to_delete"] == 2
65-
assert result["deleted_pointers"]["count"] == 2
66-
assert result["ods_code"] == "G3H9E"
67-
mock_write.assert_called_once()
68-
mock_print.assert_called_once()
69-
70-
7144
class TestParseJsonPointers:
7245
def test_invalid_json(self):
7346
content = '[{"id": "G3H9E-id1"'
@@ -131,6 +104,60 @@ def test_empty_plain_text(self):
131104
assert result == []
132105

133106

107+
class TestBuildAndWriteResult:
108+
@patch("delete_pointers_by_id._write_result_file")
109+
@patch("delete_pointers_by_id._print_summary")
110+
def test_build_and_write_result_success(self, mock_print, mock_write):
111+
start_time = datetime.now(tz=timezone.utc)
112+
end_time = datetime.now(tz=timezone.utc)
113+
ctx = PointerDeletionContext(
114+
pointers_to_delete=["G3H9E-1", "G3H9E-2"],
115+
ods_code="G3H9E",
116+
matched_pointers=["G3H9E-1", "G3H9E-2"],
117+
mismatched_pointers=[],
118+
not_found_pointers=[],
119+
pointers_deleted=["G3H9E-1", "G3H9E-2"],
120+
failed_deletes=[],
121+
start_time=start_time,
122+
end_time=end_time,
123+
output_filename="delete_results_G3H9E_20231125T120000Z.json",
124+
)
125+
result = _build_and_write_result(ctx)
126+
127+
assert result["pointers_to_delete"] == 2
128+
assert result["deleted_pointers"]["count"] == 2
129+
assert result["ods_code"] == "G3H9E"
130+
assert result["output_filename"] == "delete_results_G3H9E_20231125T120000Z.json"
131+
assert "_output_error" not in result
132+
mock_write.assert_called_once()
133+
mock_print.assert_called_once()
134+
135+
@patch("delete_pointers_by_id._write_result_file")
136+
@patch("delete_pointers_by_id._print_summary")
137+
def test_build_and_write_result_with_error(self, mock_print, mock_write):
138+
mock_write.side_effect = Exception("Write failed")
139+
start_time = datetime.now(tz=timezone.utc)
140+
end_time = datetime.now(tz=timezone.utc)
141+
ctx = PointerDeletionContext(
142+
pointers_to_delete=["G3H9E-1"],
143+
ods_code="G3H9E",
144+
matched_pointers=["G3H9E-1"],
145+
mismatched_pointers=[],
146+
not_found_pointers=[],
147+
pointers_deleted=["G3H9E-1"],
148+
failed_deletes=[],
149+
start_time=start_time,
150+
end_time=end_time,
151+
output_filename="delete_results_G3H9E_20231125T120000Z.json",
152+
)
153+
result = _build_and_write_result(ctx)
154+
155+
assert "_output_error" in result
156+
assert "Write failed" in result["_output_error"]
157+
mock_write.assert_called_once()
158+
mock_print.assert_called_once()
159+
160+
134161
class TestCheckPointersMatchOdsCode:
135162
def test_all_match(self):
136163
ods = "G3H9E"
@@ -173,16 +200,6 @@ def test_none_exist(self, mock_dynamodb):
173200
existing, not_found = _batch_get_existing_pointers(table, ids)
174201
assert existing == [] and not_found == ids
175202

176-
@patch("delete_pointers_by_id.dynamodb")
177-
def test_some_exist(self, mock_dynamodb):
178-
table = "t"
179-
ids = ["G3H9E-1", "G3H9E-3"]
180-
mock_dynamodb.batch_get_item.return_value = {
181-
"Responses": {table: [{"pk": {"S": "D#G3H9E-1"}}]}
182-
}
183-
existing, not_found = _batch_get_existing_pointers(table, ids)
184-
assert existing == ["G3H9E-1"] and not_found == ["G3H9E-3"]
185-
186203

187204
class TestBatchDeletePointers:
188205
@patch("delete_pointers_by_id.dynamodb")
@@ -224,6 +241,23 @@ def test_both_params_provided(self):
224241
"t", "G3H9E", pointers_to_delete=["a"], pointers_file="./f"
225242
)
226243

244+
@patch("delete_pointers_by_id._build_and_write_result")
245+
@patch("delete_pointers_by_id._check_pointers_match_ods_code")
246+
@patch("delete_pointers_by_id._batch_get_existing_pointers")
247+
@patch("delete_pointers_by_id._batch_delete_pointers")
248+
def test_empty_pointers_list(self, mock_delete, mock_get, mock_check, mock_build):
249+
mock_build.return_value = {
250+
"pointers_to_delete": 0,
251+
"ods_code_matched": {"count": 0},
252+
"output_filename": "delete_results_G3H9E_20231125T120000Z.json",
253+
}
254+
result = _delete_pointers_by_id("t", "G3H9E", pointers_to_delete=[])
255+
assert result["pointers_to_delete"] == 0
256+
mock_build.assert_called_once()
257+
mock_check.assert_not_called()
258+
mock_get.assert_not_called()
259+
mock_delete.assert_not_called()
260+
227261
@patch("delete_pointers_by_id._build_and_write_result")
228262
@patch("delete_pointers_by_id._check_pointers_match_ods_code")
229263
@patch("delete_pointers_by_id._batch_get_existing_pointers")
@@ -233,13 +267,16 @@ def test_no_matched_ods_codes(self, mock_delete, mock_get, mock_check, mock_buil
233267
mock_build.return_value = {
234268
"ods_code_matched": {"count": 0},
235269
"ods_code_mismatched": {"count": 2},
270+
"output_filename": "delete_results_G3H9E_20231125T120000Z.json",
236271
}
237272
result = _delete_pointers_by_id(
238273
"t", "G3H9E", pointers_to_delete=["RAT-1", "RAT-2"]
239274
)
240275
assert result["ods_code_matched"]["count"] == 0
241276
assert result["ods_code_mismatched"]["count"] == 2
242277
mock_build.assert_called_once()
278+
mock_get.assert_not_called()
279+
mock_delete.assert_not_called()
243280

244281
@patch("delete_pointers_by_id._build_and_write_result")
245282
@patch("delete_pointers_by_id._check_pointers_match_ods_code")
@@ -254,6 +291,7 @@ def test_successful_flow(self, mock_delete, mock_get, mock_check, mock_build):
254291
"pointers_to_delete": 2,
255292
"deleted_pointers": {"count": 2},
256293
"pointer_not_found": {"count": 0},
294+
"output_filename": "delete_results_G3H9E_20231125T120000Z.json",
257295
}
258296
result = _delete_pointers_by_id("t", "G3H9E", pointers_to_delete=ids)
259297
assert result["pointers_to_delete"] == 2
@@ -274,6 +312,7 @@ def test_partial_with_failures(self, mock_delete, mock_get, mock_check, mock_bui
274312
"ods_code_mismatched": {"count": 1},
275313
"deleted_pointers": {"count": 2},
276314
"failed_deletes": {"count": 1, "ids": ["G3H9E-3"]},
315+
"output_filename": "delete_results_G3H9E_20231125T120000Z.json",
277316
}
278317
result = _delete_pointers_by_id(
279318
"t", "G3H9E", pointers_to_delete=matched + ["RAT-1"]
@@ -298,6 +337,7 @@ def test_some_pointers_not_found(
298337
mock_build.return_value = {
299338
"pointer_not_found": {"count": 1, "ids": ["G3H9E-3"]},
300339
"deleted_pointers": {"count": 2},
340+
"output_filename": "delete_results_G3H9E_20231125T120000Z.json",
301341
}
302342
result = _delete_pointers_by_id("t", "G3H9E", pointers_to_delete=matched)
303343
assert result["pointer_not_found"]["count"] == 1

0 commit comments

Comments
 (0)