Skip to content

Commit 1931d8c

Browse files
Feature/case bulk commands (#273)
* added cases file-events bulk commands * fix style * add unit tests * Added changelog * change docs
1 parent 153a2a2 commit 1931d8c

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
The intended audience of this file is for py42 consumers -- as such, changes that don't affect
99
how a consumer would use the library (e.g. adding unit tests, updating documentation, etc) are not captured here.
1010

11+
## Unreleased
12+
13+
### Added
14+
15+
- `code42 cases file-events bulk` with sub-commands:
16+
- `generate-template`: that creates the file template. And parameters:
17+
- `cmd`: with options `add` and `remove`.
18+
- `path`
19+
- `add`: that takes a csv file with case number and event ID.
20+
- `remove`: that takes a csv file with case number and event ID.
21+
1122
## 1.4.2 - 2021-04-22
1223

1324
### Added

src/code42cli/cmds/cases.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88
from py42.exceptions import Py42NotFoundError
99
from py42.exceptions import Py42UpdateClosedCaseError
1010

11+
from code42cli.bulk import generate_template_cmd_factory
12+
from code42cli.bulk import run_bulk_process
1113
from code42cli.click_ext.groups import OrderedGroup
1214
from code42cli.errors import Code42CLIError
15+
from code42cli.file_readers import read_csv_arg
1316
from code42cli.options import format_option
1417
from code42cli.options import sdk_options
1518
from code42cli.options import set_begin_default_dict
@@ -261,3 +264,60 @@ def remove(state, case_number, event_id):
261264
state.sdk.cases.file_events.delete(case_number, event_id)
262265
except Py42NotFoundError:
263266
raise Code42CLIError("Invalid case-number or event-id.")
267+
268+
269+
@file_events.group(cls=OrderedGroup)
270+
@sdk_options(hidden=True)
271+
def bulk(state):
272+
"""Tools for executing bulk case file-event actions."""
273+
pass
274+
275+
276+
FILE_EVENTS_HEADERS = [
277+
"number",
278+
"eventId",
279+
]
280+
281+
case_file_events_generate_template = generate_template_cmd_factory(
282+
group_name="file_events",
283+
commands_dict={"add": FILE_EVENTS_HEADERS, "remove": FILE_EVENTS_HEADERS},
284+
)
285+
bulk.add_command(case_file_events_generate_template)
286+
287+
288+
@bulk.command(
289+
name="add",
290+
help="Bulk associate file events to cases using a CSV file with "
291+
"format: {}.".format(",".join(FILE_EVENTS_HEADERS)),
292+
)
293+
@read_csv_arg(headers=FILE_EVENTS_HEADERS)
294+
@sdk_options()
295+
def bulk_add(state, csv_rows):
296+
sdk = state.sdk
297+
298+
def handle_row(case_number, event_id):
299+
sdk.cases.file_events.add(case_number, event_id)
300+
301+
run_bulk_process(
302+
handle_row, csv_rows, progress_label="Associating file events to cases:",
303+
)
304+
305+
306+
@bulk.command(
307+
name="remove",
308+
help="Bulk remove the file event association from cases using a CSV file with "
309+
"format: {}.".format(",".join(FILE_EVENTS_HEADERS)),
310+
)
311+
@read_csv_arg(headers=FILE_EVENTS_HEADERS)
312+
@sdk_options()
313+
def bulk_remove(state, csv_rows):
314+
sdk = state.sdk
315+
316+
def handle_row(case_number, event_id):
317+
sdk.cases.file_events.delete(case_number, event_id)
318+
319+
run_bulk_process(
320+
handle_row,
321+
csv_rows,
322+
progress_label="Removing the file event association from cases:",
323+
)

tests/cmds/test_cases.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,3 +464,39 @@ def test_fileevents_when_event_id_is_already_associated_with_case_py42_raises_ex
464464
)
465465
cli_state.sdk.cases.file_events.add.assert_called_once_with(1, "1")
466466
assert "Event is already associated to the case." in result.output
467+
468+
469+
def test_add_bulk_file_events_to_cases_uses_expected_arguments(
470+
runner, mocker, cli_state_with_user
471+
):
472+
bulk_processor = mocker.patch("code42cli.cmds.cases.run_bulk_process")
473+
with runner.isolated_filesystem():
474+
with open("test_add.csv", "w") as csv:
475+
csv.writelines(["number,eventId\n", "1,abc\n", "2,pqr\n"])
476+
runner.invoke(
477+
cli,
478+
["cases", "file-events", "bulk", "add", "test_add.csv"],
479+
obj=cli_state_with_user,
480+
)
481+
assert bulk_processor.call_args[0][1] == [
482+
{"number": "1", "eventId": "abc"},
483+
{"number": "2", "eventId": "pqr"},
484+
]
485+
486+
487+
def test_remove_bulk_file_events_from_cases_uses_expected_arguments(
488+
runner, mocker, cli_state_with_user
489+
):
490+
bulk_processor = mocker.patch("code42cli.cmds.cases.run_bulk_process")
491+
with runner.isolated_filesystem():
492+
with open("test_remove.csv", "w") as csv:
493+
csv.writelines(["number,eventId\n", "1,abc\n", "2,pqr\n"])
494+
runner.invoke(
495+
cli,
496+
["cases", "file-events", "bulk", "remove", "test_remove.csv"],
497+
obj=cli_state_with_user,
498+
)
499+
assert bulk_processor.call_args[0][1] == [
500+
{"number": "1", "eventId": "abc"},
501+
{"number": "2", "eventId": "pqr"},
502+
]

0 commit comments

Comments
 (0)