Skip to content

Commit 8232649

Browse files
committed
Updated client to work with new API spec. Fixes #56
1 parent b6e1448 commit 8232649

File tree

4 files changed

+345
-163
lines changed

4 files changed

+345
-163
lines changed

meorg_client/cli.py

+22-10
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,9 @@ def file_upload(file_path, id, n: int = 1):
132132

133133
for response in responses:
134134

135-
# For singular case
136-
if n == 1:
137-
response = response[0]
138-
139135
files = response.get("data").get("files")
140136
for f in files:
141-
click.echo(f.get("file"))
137+
click.echo(f.get("id"))
142138

143139

144140
@click.command("list")
@@ -170,9 +166,9 @@ def file_attach(file_id: str, output_id: str):
170166
click.echo("SUCCESS")
171167

172168

173-
@click.command("detach_all")
169+
@click.command("delete_all")
174170
@click.argument("output_id")
175-
def file_detach_all(output_id: str):
171+
def file_delete_all(output_id: str):
176172
"""Detach all files from a model output.
177173
178174
Parameters
@@ -181,7 +177,23 @@ def file_detach_all(output_id: str):
181177
Model output ID.
182178
"""
183179
client = _get_client()
184-
_ = _call(client.detach_all_files_from_model_output, id=output_id)
180+
_ = _call(client.delete_all_files_from_model_output, id=output_id)
181+
click.echo("SUCCESS")
182+
183+
184+
@click.command("delete")
185+
@click.argument("output_id")
186+
@click.argument("file_id")
187+
def file_delete(output_id: str, file_id: str):
188+
"""Detach a file from a model output.
189+
190+
Parameters
191+
----------
192+
output_id : str
193+
Model output ID.
194+
"""
195+
client = _get_client()
196+
_ = _call(client.delete_file_from_model_output, id=output_id, file_id=file_id)
185197
click.echo("SUCCESS")
186198

187199

@@ -282,8 +294,8 @@ def cli_analysis():
282294
# Add file commands
283295
cli_file.add_command(file_list)
284296
cli_file.add_command(file_upload)
285-
# cli_file.add_command(file_upload_parallel)
286-
cli_file.add_command(file_attach)
297+
cli_file.add_command(file_delete)
298+
cli_file.add_command(file_delete_all)
287299

288300
# Add endpoint commands
289301
cli_endpoints.add_command(list_endpoints)

meorg_client/client.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ def upload_files(
291291
if n == 1:
292292
for fp in tqdm(files, total=len(files)):
293293
response = self._upload_file(fp, id=id)
294-
responses.append(response)
294+
responses += response
295295
else:
296296
responses += self._upload_files_parallel(
297297
files, n=n, id=id, progress=progress

meorg_client/tests/test_cli.py

+160-118
Original file line numberDiff line numberDiff line change
@@ -1,118 +1,160 @@
1-
# from click.testing import CliRunner
2-
# import meorg_client.cli as cli
3-
# import os
4-
# import meorg_client.utilities as mu
5-
# from conftest import store
6-
# import pytest
7-
# import time
8-
9-
10-
# @pytest.fixture
11-
# def runner() -> CliRunner:
12-
# """Get a runner object.
13-
14-
# Returns
15-
# -------
16-
# click.testing.CliRunner
17-
# Runner object.
18-
# """
19-
# return CliRunner()
20-
21-
22-
# @pytest.fixture
23-
# def test_filepath() -> str:
24-
# """Get a test filepath from the installation.
25-
26-
# Returns
27-
# -------
28-
# str
29-
# Path to the test filepath.
30-
# """
31-
# return os.path.join(mu.get_installed_data_root(), "test/test.txt")
32-
33-
34-
# def test_list_endpoints(runner: CliRunner):
35-
# """Test list-endpoints via CLI."""
36-
# result = runner.invoke(cli.list_endpoints)
37-
# assert result.exit_code == 0
38-
39-
40-
# def test_file_upload(runner: CliRunner, test_filepath: str):
41-
# """Test file-upload via CLI."""
42-
43-
# # Upload a tiny test file
44-
# result = runner.invoke(cli.file_upload, [test_filepath, store.get("model_output_id")])
45-
# assert result.exit_code == 0
46-
47-
# # Add the job_id to the store for the next test
48-
# store.set("file_id", result.stdout.split()[-1].strip())
49-
50-
# # Let it wait for a short while, allow the server to transfer to object store.
51-
# time.sleep(5)
52-
53-
54-
# def test_file_multiple(runner: CliRunner, test_filepath: str):
55-
# """Test file-upload via CLI."""
56-
57-
# # Upload a tiny test file
58-
# result = runner.invoke(cli.file_upload, [test_filepath, test_filepath, store.get("model_output_id")])
59-
# assert result.exit_code == 0
60-
61-
# # Add the job_id to the store for the next test
62-
# store.set("file_ids", result.output.strip())
63-
64-
# # Let it wait for a short while, allow the server to transfer to object store.
65-
# time.sleep(5)
66-
67-
68-
# def test_file_list(runner):
69-
# """Test file-list via CLI."""
70-
# result = runner.invoke(cli.file_list, [store.get("model_output_id")])
71-
# assert result.exit_code == 0
72-
73-
74-
# # def test_file_attach(runner):
75-
# # """Test file-attach via CLI."""
76-
77-
# # result = runner.invoke(
78-
# # cli.file_attach, [store.get("file_id"), store.get("model_output_id")]
79-
# # )
80-
81-
# # assert result.exit_code == 0
82-
83-
84-
# # def test_file_upload_with_attach(runner, test_filepath):
85-
# # """Test file upload with attachment via CLI."""
86-
# # model_output_id = store.get("model_output_id")
87-
# # result = runner.invoke(
88-
# # cli.file_upload, [test_filepath, test_filepath, "--attach_to", model_output_id]
89-
# # )
90-
# # assert result.exit_code == 0
91-
92-
93-
# def test_file_upload_parallel(runner: CliRunner, test_filepath: str):
94-
# """Test file-upload via CLI."""
95-
96-
# # Upload a tiny test file
97-
# result = runner.invoke(cli.file_upload, [test_filepath, test_filepath, store.get("model_output_id"), "-n", "2"])
98-
# assert result.exit_code == 0
99-
100-
101-
# # def test_file_upload_parallel_with_attach(runner, test_filepath):
102-
# # """Test file upload with attachment via CLI."""
103-
# # model_output_id = store.get("model_output_id")
104-
# # result = runner.invoke(
105-
# # cli.file_upload,
106-
# # [test_filepath, test_filepath, "-n", "2", "--attach_to", model_output_id],
107-
# # )
108-
# # assert result.exit_code == 0
109-
110-
111-
# # def test_detach_all(runner):
112-
# # """Test detaching all files from a model output."""
113-
# # model_output_id = store.get("model_output_id")
114-
# # result = runner.invoke(
115-
# # cli.file_detach_all,
116-
# # [model_output_id],
117-
# # )
118-
# # assert result.exit_code == 0
1+
"""Test the CLI actions."""
2+
3+
from click.testing import CliRunner
4+
import meorg_client.cli as cli
5+
import os
6+
import meorg_client.utilities as mu
7+
from conftest import store
8+
import pytest
9+
10+
11+
@pytest.fixture
12+
def runner() -> CliRunner:
13+
"""Get a runner object.
14+
15+
Returns
16+
-------
17+
click.testing.CliRunner
18+
Runner object.
19+
"""
20+
return CliRunner()
21+
22+
23+
@pytest.fixture
24+
def test_filepath() -> str:
25+
"""Get a test filepath from the installation.
26+
27+
Returns
28+
-------
29+
str
30+
Path to the test filepath.
31+
"""
32+
return os.path.join(mu.get_installed_data_root(), "test/test.txt")
33+
34+
35+
@pytest.fixture
36+
def model_output_id() -> str:
37+
"""Get the model output ID out of the environment.
38+
39+
Returns
40+
-------
41+
str
42+
Model output ID.
43+
"""
44+
return os.getenv("MEORG_MODEL_OUTPUT_ID")
45+
46+
47+
def test_list_endpoints(runner: CliRunner):
48+
"""Test list-endpoints via CLI.
49+
50+
Parameters
51+
----------
52+
runner : CliRunner
53+
Runner object
54+
"""
55+
result = runner.invoke(cli.list_endpoints)
56+
assert result.exit_code == 0
57+
58+
59+
def test_file_upload(runner: CliRunner, test_filepath: str, model_output_id: str):
60+
"""Test file-upload via CLI.
61+
62+
Parameters
63+
----------
64+
runner : CliRunner
65+
Runner.
66+
test_filepath : str
67+
Test filepath.
68+
model_output_id : str
69+
Model output ID.
70+
"""
71+
# Upload a tiny test file
72+
result = runner.invoke(cli.file_upload, [test_filepath, model_output_id])
73+
assert result.exit_code == 0
74+
75+
# Add the job_id to the store for the next test
76+
store.set("file_id", result.stdout.split()[-1].strip())
77+
78+
79+
def test_file_multiple(runner: CliRunner, test_filepath: str, model_output_id: str):
80+
"""Test file-upload via CLI.
81+
82+
Parameters
83+
----------
84+
runner : CliRunner
85+
Runner.
86+
test_filepath : str
87+
Test filepath.
88+
"""
89+
# Upload multiple files
90+
result = runner.invoke(
91+
cli.file_upload, [test_filepath, test_filepath, model_output_id]
92+
)
93+
assert result.exit_code == 0
94+
95+
# Add the job_id to the store for the next test
96+
store.set("file_ids", result.stdout.strip())
97+
98+
99+
def test_file_upload_parallel(
100+
runner: CliRunner, test_filepath: str, model_output_id: str
101+
):
102+
"""Test file-upload via CLI.
103+
104+
Parameters
105+
----------
106+
runner : _type_
107+
Runner.
108+
model_output_id : str
109+
Model output ID.
110+
"""
111+
# Upload multiple files in parallel.
112+
result = runner.invoke(
113+
cli.file_upload, [test_filepath, test_filepath, model_output_id, "-n", "2"]
114+
)
115+
assert result.exit_code == 0
116+
117+
118+
def test_file_list(runner: CliRunner):
119+
"""Test file-list via CLI.
120+
121+
Parameters
122+
----------
123+
runner : CliRunner
124+
Runner.
125+
"""
126+
result = runner.invoke(cli.file_list, [store.get("model_output_id")])
127+
assert result.exit_code == 0
128+
129+
130+
def test_delete_file_from_output(runner: CliRunner, model_output_id: str):
131+
"""Test deleting a file from a model output.
132+
133+
Parameters
134+
----------
135+
runner : CliRunner
136+
Runner.
137+
model_output_id : str
138+
Model output ID.
139+
"""
140+
# Get the last file added
141+
file_id = store.get("file_ids").splitlines()[-1]
142+
143+
# Delete it
144+
result = runner.invoke(cli.file_delete, [store.get("model_output_id"), file_id])
145+
assert result.exit_code == 0
146+
147+
148+
def test_delete_all_files_from_output(runner: CliRunner, model_output_id: str):
149+
"""Test deleting all files from a model output.
150+
151+
Parameters
152+
----------
153+
runner : CliRunner
154+
Runner.
155+
model_output_id : str
156+
Model output ID.
157+
"""
158+
159+
result = runner.invoke(cli.file_delete_all, [model_output_id])
160+
assert result.exit_code == 0

0 commit comments

Comments
 (0)