Skip to content

Commit

Permalink
Updated client to work with new API spec. Fixes #56
Browse files Browse the repository at this point in the history
  • Loading branch information
bschroeter committed Sep 19, 2024
1 parent b6e1448 commit 8232649
Show file tree
Hide file tree
Showing 4 changed files with 345 additions and 163 deletions.
32 changes: 22 additions & 10 deletions meorg_client/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,9 @@ def file_upload(file_path, id, n: int = 1):

for response in responses:

# For singular case
if n == 1:
response = response[0]

files = response.get("data").get("files")
for f in files:
click.echo(f.get("file"))
click.echo(f.get("id"))


@click.command("list")
Expand Down Expand Up @@ -170,9 +166,9 @@ def file_attach(file_id: str, output_id: str):
click.echo("SUCCESS")


@click.command("detach_all")
@click.command("delete_all")
@click.argument("output_id")
def file_detach_all(output_id: str):
def file_delete_all(output_id: str):
"""Detach all files from a model output.
Parameters
Expand All @@ -181,7 +177,23 @@ def file_detach_all(output_id: str):
Model output ID.
"""
client = _get_client()
_ = _call(client.detach_all_files_from_model_output, id=output_id)
_ = _call(client.delete_all_files_from_model_output, id=output_id)
click.echo("SUCCESS")


@click.command("delete")
@click.argument("output_id")
@click.argument("file_id")
def file_delete(output_id: str, file_id: str):
"""Detach a file from a model output.
Parameters
----------
output_id : str
Model output ID.
"""
client = _get_client()
_ = _call(client.delete_file_from_model_output, id=output_id, file_id=file_id)
click.echo("SUCCESS")


Expand Down Expand Up @@ -282,8 +294,8 @@ def cli_analysis():
# Add file commands
cli_file.add_command(file_list)
cli_file.add_command(file_upload)
# cli_file.add_command(file_upload_parallel)
cli_file.add_command(file_attach)
cli_file.add_command(file_delete)
cli_file.add_command(file_delete_all)

# Add endpoint commands
cli_endpoints.add_command(list_endpoints)
Expand Down
2 changes: 1 addition & 1 deletion meorg_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ def upload_files(
if n == 1:
for fp in tqdm(files, total=len(files)):
response = self._upload_file(fp, id=id)
responses.append(response)
responses += response
else:
responses += self._upload_files_parallel(
files, n=n, id=id, progress=progress
Expand Down
278 changes: 160 additions & 118 deletions meorg_client/tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,118 +1,160 @@
# from click.testing import CliRunner
# import meorg_client.cli as cli
# import os
# import meorg_client.utilities as mu
# from conftest import store
# import pytest
# import time


# @pytest.fixture
# def runner() -> CliRunner:
# """Get a runner object.

# Returns
# -------
# click.testing.CliRunner
# Runner object.
# """
# return CliRunner()


# @pytest.fixture
# def test_filepath() -> str:
# """Get a test filepath from the installation.

# Returns
# -------
# str
# Path to the test filepath.
# """
# return os.path.join(mu.get_installed_data_root(), "test/test.txt")


# def test_list_endpoints(runner: CliRunner):
# """Test list-endpoints via CLI."""
# result = runner.invoke(cli.list_endpoints)
# assert result.exit_code == 0


# def test_file_upload(runner: CliRunner, test_filepath: str):
# """Test file-upload via CLI."""

# # Upload a tiny test file
# result = runner.invoke(cli.file_upload, [test_filepath, store.get("model_output_id")])
# assert result.exit_code == 0

# # Add the job_id to the store for the next test
# store.set("file_id", result.stdout.split()[-1].strip())

# # Let it wait for a short while, allow the server to transfer to object store.
# time.sleep(5)


# def test_file_multiple(runner: CliRunner, test_filepath: str):
# """Test file-upload via CLI."""

# # Upload a tiny test file
# result = runner.invoke(cli.file_upload, [test_filepath, test_filepath, store.get("model_output_id")])
# assert result.exit_code == 0

# # Add the job_id to the store for the next test
# store.set("file_ids", result.output.strip())

# # Let it wait for a short while, allow the server to transfer to object store.
# time.sleep(5)


# def test_file_list(runner):
# """Test file-list via CLI."""
# result = runner.invoke(cli.file_list, [store.get("model_output_id")])
# assert result.exit_code == 0


# # def test_file_attach(runner):
# # """Test file-attach via CLI."""

# # result = runner.invoke(
# # cli.file_attach, [store.get("file_id"), store.get("model_output_id")]
# # )

# # assert result.exit_code == 0


# # def test_file_upload_with_attach(runner, test_filepath):
# # """Test file upload with attachment via CLI."""
# # model_output_id = store.get("model_output_id")
# # result = runner.invoke(
# # cli.file_upload, [test_filepath, test_filepath, "--attach_to", model_output_id]
# # )
# # assert result.exit_code == 0


# def test_file_upload_parallel(runner: CliRunner, test_filepath: str):
# """Test file-upload via CLI."""

# # Upload a tiny test file
# result = runner.invoke(cli.file_upload, [test_filepath, test_filepath, store.get("model_output_id"), "-n", "2"])
# assert result.exit_code == 0


# # def test_file_upload_parallel_with_attach(runner, test_filepath):
# # """Test file upload with attachment via CLI."""
# # model_output_id = store.get("model_output_id")
# # result = runner.invoke(
# # cli.file_upload,
# # [test_filepath, test_filepath, "-n", "2", "--attach_to", model_output_id],
# # )
# # assert result.exit_code == 0


# # def test_detach_all(runner):
# # """Test detaching all files from a model output."""
# # model_output_id = store.get("model_output_id")
# # result = runner.invoke(
# # cli.file_detach_all,
# # [model_output_id],
# # )
# # assert result.exit_code == 0
"""Test the CLI actions."""

from click.testing import CliRunner
import meorg_client.cli as cli
import os
import meorg_client.utilities as mu
from conftest import store
import pytest


@pytest.fixture
def runner() -> CliRunner:
"""Get a runner object.
Returns
-------
click.testing.CliRunner
Runner object.
"""
return CliRunner()


@pytest.fixture
def test_filepath() -> str:
"""Get a test filepath from the installation.
Returns
-------
str
Path to the test filepath.
"""
return os.path.join(mu.get_installed_data_root(), "test/test.txt")


@pytest.fixture
def model_output_id() -> str:
"""Get the model output ID out of the environment.
Returns
-------
str
Model output ID.
"""
return os.getenv("MEORG_MODEL_OUTPUT_ID")


def test_list_endpoints(runner: CliRunner):
"""Test list-endpoints via CLI.
Parameters
----------
runner : CliRunner
Runner object
"""
result = runner.invoke(cli.list_endpoints)
assert result.exit_code == 0


def test_file_upload(runner: CliRunner, test_filepath: str, model_output_id: str):
"""Test file-upload via CLI.
Parameters
----------
runner : CliRunner
Runner.
test_filepath : str
Test filepath.
model_output_id : str
Model output ID.
"""
# Upload a tiny test file
result = runner.invoke(cli.file_upload, [test_filepath, model_output_id])
assert result.exit_code == 0

# Add the job_id to the store for the next test
store.set("file_id", result.stdout.split()[-1].strip())


def test_file_multiple(runner: CliRunner, test_filepath: str, model_output_id: str):
"""Test file-upload via CLI.
Parameters
----------
runner : CliRunner
Runner.
test_filepath : str
Test filepath.
"""
# Upload multiple files
result = runner.invoke(
cli.file_upload, [test_filepath, test_filepath, model_output_id]
)
assert result.exit_code == 0

# Add the job_id to the store for the next test
store.set("file_ids", result.stdout.strip())


def test_file_upload_parallel(
runner: CliRunner, test_filepath: str, model_output_id: str
):
"""Test file-upload via CLI.
Parameters
----------
runner : _type_
Runner.
model_output_id : str
Model output ID.
"""
# Upload multiple files in parallel.
result = runner.invoke(
cli.file_upload, [test_filepath, test_filepath, model_output_id, "-n", "2"]
)
assert result.exit_code == 0


def test_file_list(runner: CliRunner):
"""Test file-list via CLI.
Parameters
----------
runner : CliRunner
Runner.
"""
result = runner.invoke(cli.file_list, [store.get("model_output_id")])
assert result.exit_code == 0


def test_delete_file_from_output(runner: CliRunner, model_output_id: str):
"""Test deleting a file from a model output.
Parameters
----------
runner : CliRunner
Runner.
model_output_id : str
Model output ID.
"""
# Get the last file added
file_id = store.get("file_ids").splitlines()[-1]

# Delete it
result = runner.invoke(cli.file_delete, [store.get("model_output_id"), file_id])
assert result.exit_code == 0


def test_delete_all_files_from_output(runner: CliRunner, model_output_id: str):
"""Test deleting all files from a model output.
Parameters
----------
runner : CliRunner
Runner.
model_output_id : str
Model output ID.
"""

result = runner.invoke(cli.file_delete_all, [model_output_id])
assert result.exit_code == 0
Loading

0 comments on commit 8232649

Please sign in to comment.