Skip to content

Commit 6cf1280

Browse files
author
Juliya Smith
authored
org and split up tests (#216)
1 parent 53c5ffe commit 6cf1280

File tree

2 files changed

+130
-86
lines changed

2 files changed

+130
-86
lines changed

src/code42cli/cmds/cases.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818

1919
case_number_arg = click.argument("case-number", type=int)
20+
case_number_option = click.option(
21+
"--case-number", type=int, help="The number assigned to the case.", required=True
22+
)
2023
name_option = click.option("--name", help="Name of the case.",)
2124
assignee_option = click.option("--assignee", help="User UID of the assignee.")
2225
description_option = click.option("--description", help="Description of the case.")
@@ -225,7 +228,7 @@ def file_events_list(state, case_number, format):
225228

226229

227230
@file_events.command()
228-
@case_number_arg
231+
@case_number_option
229232
@file_event_id_option
230233
@sdk_options()
231234
def add(state, case_number, event_id):
@@ -237,7 +240,7 @@ def add(state, case_number, event_id):
237240

238241

239242
@file_events.command()
240-
@case_number_arg
243+
@case_number_option
241244
@file_event_id_option
242245
@sdk_options()
243246
def remove(state, case_number, event_id):

tests/cmds/test_cases.py

Lines changed: 125 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,12 @@
3030
"totalCount": 31
3131
}"""
3232
CASE_DETAILS = '{"number": 3, "name": "test@test.test"}'
33-
CASES_COMMAND = "cases"
34-
CASES_FILE_EVENTS_COMMAND = "cases file-events"
3533
MISSING_ARGUMENT_ERROR = "Missing argument '{}'."
3634
MISSING_NAME = MISSING_ARGUMENT_ERROR.format("NAME")
37-
MISSING_CASE_NUMBER = MISSING_ARGUMENT_ERROR.format("CASE_NUMBER")
35+
MISSING_CASE_NUMBER_ARG = MISSING_ARGUMENT_ERROR.format("CASE_NUMBER")
3836
MISSING_OPTION_ERROR = "Missing option '--{}'."
3937
MISSING_EVENT_ID = MISSING_OPTION_ERROR.format("event-id")
38+
MISSING_CASE_NUMBER_OPTION = MISSING_OPTION_ERROR.format("case-number")
4039

4140

4241
@pytest.fixture
@@ -85,6 +84,13 @@ def test_create_with_optional_fields_calls_create_with_expected_params(
8584
)
8685

8786

87+
def test_create_when_missing_name_prints_error(runner, cli_state):
88+
command = ["cases", "create", "--description", "d"]
89+
result = runner.invoke(cli, command, obj=cli_state)
90+
assert result.exit_code == 2
91+
assert MISSING_NAME in result.output
92+
93+
8894
def test_update_with_optional_fields_calls_update_with_expected_params(
8995
runner, cli_state
9096
):
@@ -135,13 +141,33 @@ def test_update_calls_update_with_expected_params(runner, cli_state):
135141
)
136142

137143

144+
def test_update_when_missing_case_number_prints_error(runner, cli_state):
145+
command = ["cases", "update", "--description", "d"]
146+
result = runner.invoke(cli, command, obj=cli_state)
147+
assert result.exit_code == 2
148+
assert MISSING_CASE_NUMBER_ARG in result.output
149+
150+
138151
def test_list_calls_get_all_with_expected_params(runner, cli_state):
139152
runner.invoke(
140153
cli, ["cases", "list"], obj=cli_state,
141154
)
142155
assert cli_state.sdk.cases.get_all.call_count == 1
143156

144157

158+
def test_list_prints_expected_data(runner, cli_state, py42_response):
159+
py42_response.data = json.loads(ALL_CASES)
160+
161+
def gen():
162+
yield py42_response.data
163+
164+
cli_state.sdk.cases.get_all.return_value = gen()
165+
result = runner.invoke(cli, ["cases", "list"], obj=cli_state,)
166+
assert "test@test.test" in result.output
167+
assert "2021-01-24T11:00:04.217878Z" in result.output
168+
assert "942897" in result.output
169+
170+
145171
def test_show_calls_get_case_with_expected_params(runner, cli_state):
146172
runner.invoke(
147173
cli, ["cases", "show", "1"], obj=cli_state,
@@ -158,6 +184,43 @@ def test_show_with_include_file_events_calls_file_events_get_all_with_expected_p
158184
cli_state.sdk.cases.file_events.get_all.assert_called_once_with(1)
159185

160186

187+
def test_show_when_py42_raises_exception_prints_error_message(runner, cli_state, error):
188+
cli_state.sdk.cases.file_events.get_all.side_effect = Py42NotFoundError(error)
189+
result = runner.invoke(
190+
cli, ["cases", "show", "1", "--include-file-events"], obj=cli_state,
191+
)
192+
cli_state.sdk.cases.file_events.get_all.assert_called_once_with(1)
193+
assert "Invalid case-number 1." in result.output
194+
195+
196+
def test_show_prints_expected_data(runner, cli_state, py42_response):
197+
py42_response.data = json.loads(CASE_DETAILS)
198+
cli_state.sdk.cases.get.return_value = py42_response
199+
result = runner.invoke(cli, ["cases", "show", "1"], obj=cli_state,)
200+
assert "test@test.test" in result.output
201+
202+
203+
def test_show_prints_expected_data_with_include_file_events_option(
204+
runner, cli_state, py42_response
205+
):
206+
py42_response.text = ALL_EVENTS
207+
cli_state.sdk.cases.file_events.get_all.return_value = py42_response
208+
result = runner.invoke(
209+
cli, ["cases", "show", "1", "--include-file-events"], obj=cli_state,
210+
)
211+
assert (
212+
"0_1d71796f-af5b-4231-9d8e-df6434da4663_984418168383179707_986472527798692818_971"
213+
in result.output
214+
)
215+
216+
217+
def test_show_case_when_missing_case_number_prints_error(runner, cli_state):
218+
command = ["cases", "show"]
219+
result = runner.invoke(cli, command, obj=cli_state)
220+
assert result.exit_code == 2
221+
assert MISSING_CASE_NUMBER_ARG in result.output
222+
223+
161224
def test_export_calls_export_summary_with_expected_params(runner, cli_state, mocker):
162225
with mock.patch("builtins.open", mock_open()) as mf:
163226
runner.invoke(
@@ -167,95 +230,93 @@ def test_export_calls_export_summary_with_expected_params(runner, cli_state, moc
167230
mf.assert_called_once_with("./1_case_summary.pdf", "wb")
168231

169232

233+
def test_export_when_missing_case_number_prints_error(runner, cli_state):
234+
command = ["cases", "export"]
235+
result = runner.invoke(cli, command, obj=cli_state)
236+
assert result.exit_code == 2
237+
assert MISSING_CASE_NUMBER_ARG in result.output
238+
239+
170240
def test_file_events_add_calls_add_event_with_expected_params(runner, cli_state):
171241
runner.invoke(
172-
cli, ["cases", "file-events", "add", "1", "--event-id", "1"], obj=cli_state,
242+
cli,
243+
["cases", "file-events", "add", "--case-number", "1", "--event-id", "1"],
244+
obj=cli_state,
173245
)
174246
cli_state.sdk.cases.file_events.add.assert_called_once_with(1, "1")
175247

176248

177-
def test_file_events_remove_calls_delete_event_with_expected_params(runner, cli_state):
178-
runner.invoke(
179-
cli, ["cases", "file-events", "remove", "1", "--event-id", "1"], obj=cli_state,
249+
def test_file_events_add_when_py42_raises_exception_prints_error_message(
250+
runner, cli_state, error
251+
):
252+
cli_state.sdk.cases.file_events.add.side_effect = Py42BadRequestError(error)
253+
result = runner.invoke(
254+
cli,
255+
["cases", "file-events", "add", "--case-number", "1", "--event-id", "1"],
256+
obj=cli_state,
180257
)
181-
cli_state.sdk.cases.file_events.delete.assert_called_once_with(1, "1")
258+
cli_state.sdk.cases.file_events.add.assert_called_once_with(1, "1")
259+
assert "Invalid case-number or event-id." in result.output
182260

183261

184-
def test_file_events_list_calls_get_all_with_expected_params(runner, cli_state):
185-
runner.invoke(
186-
cli, ["cases", "file-events", "list", "1"], obj=cli_state,
187-
)
188-
cli_state.sdk.cases.file_events.get_all.assert_called_once_with(1)
262+
def test_file_events_add_when_missing_event_id_prints_error(runner, cli_state):
263+
command = ["cases", "file-events", "remove", "--case-number", "4"]
264+
result = runner.invoke(cli, command, obj=cli_state)
265+
assert result.exit_code == 2
266+
assert MISSING_EVENT_ID in result.output
189267

190268

191-
def test_show_when_py42_raises_exception_returns_error_message(
192-
runner, cli_state, error
193-
):
194-
cli_state.sdk.cases.file_events.get_all.side_effect = Py42NotFoundError(error)
195-
result = runner.invoke(
196-
cli, ["cases", "show", "1", "--include-file-events"], obj=cli_state,
197-
)
198-
cli_state.sdk.cases.file_events.get_all.assert_called_once_with(1)
199-
assert "Invalid case-number 1." in result.output
269+
def test_file_events_add_when_missing_case_number_prints_error(runner, cli_state):
270+
command = ["cases", "file-events", "add"]
271+
result = runner.invoke(cli, command, obj=cli_state)
272+
assert result.exit_code == 2
273+
assert MISSING_CASE_NUMBER_OPTION in result.output
200274

201275

202-
def test_file_events_add_when_py42_raises_exception_returns_error_message(
203-
runner, cli_state, error
204-
):
205-
cli_state.sdk.cases.file_events.add.side_effect = Py42BadRequestError(error)
206-
result = runner.invoke(
207-
cli, ["cases", "file-events", "add", "1", "--event-id", "1"], obj=cli_state,
276+
def test_file_events_remove_calls_delete_event_with_expected_params(runner, cli_state):
277+
runner.invoke(
278+
cli,
279+
["cases", "file-events", "remove", "--case-number", "1", "--event-id", "1"],
280+
obj=cli_state,
208281
)
209-
cli_state.sdk.cases.file_events.add.assert_called_once_with(1, "1")
210-
assert "Invalid case-number or event-id." in result.output
282+
cli_state.sdk.cases.file_events.delete.assert_called_once_with(1, "1")
211283

212284

213-
def test_file_events_remove_when_py42_raises_exception_returns_error_message(
285+
def test_file_events_remove_when_py42_raises_exception_prints_error_message(
214286
runner, cli_state, error
215287
):
216288
cli_state.sdk.cases.file_events.delete.side_effect = Py42NotFoundError(error)
217289
result = runner.invoke(
218-
cli, ["cases", "file-events", "remove", "1", "--event-id", "1"], obj=cli_state,
290+
cli,
291+
["cases", "file-events", "remove", "--case-number", "1", "--event-id", "1"],
292+
obj=cli_state,
219293
)
220294
cli_state.sdk.cases.file_events.delete.assert_called_once_with(1, "1")
221295
assert "Invalid case-number or event-id." in result.output
222296

223297

224-
def test_show_returns_expected_data(runner, cli_state, py42_response):
225-
py42_response.data = json.loads(CASE_DETAILS)
226-
cli_state.sdk.cases.get.return_value = py42_response
227-
result = runner.invoke(cli, ["cases", "show", "1"], obj=cli_state,)
228-
assert "test@test.test" in result.output
229-
298+
def test_file_events_remove_when_missing_event_id_prints_error(runner, cli_state):
299+
command = ["cases", "file-events", "remove", "--case-number", "4"]
300+
result = runner.invoke(cli, command, obj=cli_state)
301+
assert result.exit_code == 2
302+
assert MISSING_EVENT_ID in result.output
230303

231-
def test_list_returns_expected_data(runner, cli_state, py42_response):
232-
py42_response.data = json.loads(ALL_CASES)
233304

234-
def gen():
235-
yield py42_response.data
236-
237-
cli_state.sdk.cases.get_all.return_value = gen()
238-
result = runner.invoke(cli, ["cases", "list"], obj=cli_state,)
239-
assert "test@test.test" in result.output
240-
assert "2021-01-24T11:00:04.217878Z" in result.output
241-
assert "942897" in result.output
305+
def test_file_events_remove_when_missing_case_number_prints_error(runner, cli_state):
306+
command = ["cases", "file-events", "add"]
307+
result = runner.invoke(cli, command, obj=cli_state)
308+
assert result.exit_code == 2
309+
assert MISSING_CASE_NUMBER_OPTION in result.output
242310

243311

244-
def test_show_returns_expected_data_with_include_file_events_option(
245-
runner, cli_state, py42_response
246-
):
247-
py42_response.text = ALL_EVENTS
248-
cli_state.sdk.cases.file_events.get_all.return_value = py42_response
249-
result = runner.invoke(
250-
cli, ["cases", "show", "1", "--include-file-events"], obj=cli_state,
251-
)
252-
assert (
253-
"0_1d71796f-af5b-4231-9d8e-df6434da4663_984418168383179707_986472527798692818_971"
254-
in result.output
312+
def test_file_events_list_calls_get_all_with_expected_params(runner, cli_state):
313+
runner.invoke(
314+
cli, ["cases", "file-events", "list", "1"], obj=cli_state,
255315
)
316+
cli_state.sdk.cases.file_events.get_all.assert_called_once_with(1)
256317

257318

258-
def test_events_list_returns_expected_data(runner, cli_state):
319+
def test_file_events_list_prints_expected_data(runner, cli_state):
259320
cli_state.sdk.cases.file_events.get_all.return_value = json.loads(ALL_EVENTS)
260321
result = runner.invoke(cli, ["cases", "file-events", "list", "1"], obj=cli_state,)
261322
assert (
@@ -265,28 +326,8 @@ def test_events_list_returns_expected_data(runner, cli_state):
265326
assert "2020-12-23T12:41:38.592Z" in result.output
266327

267328

268-
@pytest.mark.parametrize(
269-
"command, error_msg",
270-
[
271-
("{} create --description d".format(CASES_COMMAND), MISSING_NAME),
272-
("{} update --description d".format(CASES_COMMAND), MISSING_CASE_NUMBER),
273-
("{} show".format(CASES_COMMAND), MISSING_CASE_NUMBER),
274-
("{} export".format(CASES_COMMAND), MISSING_CASE_NUMBER),
275-
("{} add".format(CASES_FILE_EVENTS_COMMAND), MISSING_CASE_NUMBER),
276-
("{} add --event-id 3".format(CASES_FILE_EVENTS_COMMAND), MISSING_CASE_NUMBER),
277-
("{} add 3".format(CASES_FILE_EVENTS_COMMAND), MISSING_EVENT_ID),
278-
("{} remove 3".format(CASES_FILE_EVENTS_COMMAND), MISSING_EVENT_ID),
279-
("{} remove".format(CASES_FILE_EVENTS_COMMAND), MISSING_CASE_NUMBER),
280-
(
281-
"{} remove --event-id 3".format(CASES_FILE_EVENTS_COMMAND),
282-
MISSING_CASE_NUMBER,
283-
),
284-
("{} list".format(CASES_FILE_EVENTS_COMMAND), MISSING_CASE_NUMBER),
285-
],
286-
)
287-
def test_cases_command_when_missing_required_parameters_errors(
288-
command, error_msg, runner, cli_state
289-
):
290-
result = runner.invoke(cli, command.split(" "), obj=cli_state)
329+
def test_file_events_list_when_missing_case_number_prints_error(runner, cli_state):
330+
command = ["cases", "file-events", "list"]
331+
result = runner.invoke(cli, command, obj=cli_state)
291332
assert result.exit_code == 2
292-
assert error_msg in "".join(result.output)
333+
assert MISSING_CASE_NUMBER_ARG in result.output

0 commit comments

Comments
 (0)