Skip to content
This repository was archived by the owner on Aug 11, 2020. It is now read-only.

Commit 52999c0

Browse files
committed
Add filtering to 'jobs list' command
1 parent 3fce0c3 commit 52999c0

File tree

3 files changed

+81
-4
lines changed

3 files changed

+81
-4
lines changed

paperspace/cli/jobs.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ def jobs_group():
1515
"--jobId",
1616
"job_id",
1717
required=True,
18+
help="Delete job with given ID",
1819
)
1920
@common.api_key_option
2021
def delete_job(job_id, api_key=None):
@@ -28,6 +29,7 @@ def delete_job(job_id, api_key=None):
2829
"--jobId",
2930
"job_id",
3031
required=True,
32+
help="Stop job with given ID",
3133
)
3234
@common.api_key_option
3335
def stop_job(job_id, api_key=None):
@@ -37,8 +39,24 @@ def stop_job(job_id, api_key=None):
3739

3840

3941
@jobs_group.command("list", help="List jobs with optional filtering")
42+
@click.option(
43+
"--project",
44+
"project",
45+
help="Use to filter jobs by project name",
46+
)
47+
@click.option(
48+
"--projectId",
49+
"projectId",
50+
help="Use to filter jobs by project ID",
51+
)
52+
@click.option(
53+
"--experimentId",
54+
"experimentId",
55+
help="Use to filter jobs by experiment ID",
56+
)
4057
@common.api_key_option
41-
def list_jobs(api_key):
58+
def list_jobs(api_key, **filters):
59+
common.del_if_value_is_none(filters)
4260
jobs_api = client.API(config.CONFIG_HOST, api_key=api_key)
4361
command = jobs_commands.ListJobsCommand(api=jobs_api)
44-
command.execute()
62+
command.execute(filters)

paperspace/commands/jobs.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ def execute(self, job_id):
4343

4444

4545
class ListJobsCommand(JobsCommandBase):
46-
def execute(self):
47-
response = self.api.get("/jobs/getJobs/", json=None)
46+
def execute(self, filters=None):
47+
json_ = filters or None
48+
response = self.api.get("/jobs/getJobs/", json=json_)
4849

4950
try:
5051
data = response.json()

tests/functional/test_jobs.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,35 @@ class TestListJobs(object):
3939
RESPONSE_JSON_WHEN_NO_JOBS_WERE_FOUND = []
4040
EXPECTED_STDOUT_WHEN_NO_JOBS_WERE_FOUND = "No jobs found\n"
4141

42+
BASIC_COMMAND_WITH_FILTERING = [
43+
"jobs", "list",
44+
"--project", "some_project_name",
45+
"--experimentId", "some_experiment_id",
46+
]
47+
EXPECTED_REQUEST_JSON_WITH_FILTERING = {
48+
"project": "some_project_name",
49+
"experimentId": "some_experiment_id",
50+
}
51+
52+
BASIC_COMMAND_WITH_MUTUALLY_EXCLUSIVE_FILTERS = [
53+
"jobs", "list",
54+
"--project", "some_project_name",
55+
"--projectId", "some_project_id",
56+
]
57+
EXPECTED_REQUEST_JSON_WITH_MUTUALLY_EXCLUSIVE_FILTERS = {
58+
"project": "some_project_name",
59+
"projectId": "some_project_id",
60+
}
61+
RESPONSE_JSON_WITH_MUTUALLY_EXCLUSIVE_FILTERS = {
62+
"error": {
63+
"name": "Error",
64+
"status": 422,
65+
"message": "Incompatible parameters: project and projectId cannot both be specified",
66+
},
67+
}
68+
EXPECTED_STDOUT_WHEN_MUTUALLY_EXCLUSIVE_FILTERS = "Incompatible parameters: project and projectId " \
69+
"cannot both be specified\n"
70+
4271
@mock.patch("paperspace.cli.cli.client.requests.get")
4372
def test_should_send_valid_post_request_and_print_table_when_jobs_list_was_used(self, get_patched):
4473
get_patched.return_value = MockResponse(json_data=self.EXPECTED_RESPONSE_JSON, status_code=200)
@@ -109,3 +138,32 @@ def test_should_print_error_message_when_error_status_code_received_but_no_conte
109138
params=None)
110139
assert result.output == "Error while parsing response data: No JSON\n"
111140
assert result.exit_code == 0
141+
142+
@mock.patch("paperspace.cli.cli.client.requests.get")
143+
def test_should_send_valid_post_request_when_jobs_list_was_used_with_filter_options(self, get_patched):
144+
get_patched.return_value = MockResponse(json_data=self.EXPECTED_RESPONSE_JSON, status_code=200)
145+
146+
cli_runner = CliRunner()
147+
result = cli_runner.invoke(cli.cli, self.BASIC_COMMAND_WITH_FILTERING)
148+
149+
get_patched.assert_called_with(self.URL,
150+
headers=self.EXPECTED_HEADERS,
151+
json=self.EXPECTED_REQUEST_JSON_WITH_FILTERING,
152+
params=None)
153+
assert result.output == self.EXPECTED_STDOUT
154+
assert result.exit_code == 0
155+
156+
@mock.patch("paperspace.cli.cli.client.requests.get")
157+
def test_should_print_proper_message_when_jobs_list_was_used_with_mutually_exclusive_filters(self, get_patched):
158+
get_patched.return_value = MockResponse(json_data=self.RESPONSE_JSON_WITH_MUTUALLY_EXCLUSIVE_FILTERS,
159+
status_code=422)
160+
161+
cli_runner = CliRunner()
162+
result = cli_runner.invoke(cli.cli, self.BASIC_COMMAND_WITH_MUTUALLY_EXCLUSIVE_FILTERS)
163+
164+
get_patched.assert_called_with(self.URL,
165+
headers=self.EXPECTED_HEADERS,
166+
json=self.EXPECTED_REQUEST_JSON_WITH_MUTUALLY_EXCLUSIVE_FILTERS,
167+
params=None)
168+
assert result.output == self.EXPECTED_STDOUT_WHEN_MUTUALLY_EXCLUSIVE_FILTERS
169+
assert result.exit_code == 0

0 commit comments

Comments
 (0)