-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✅
click
CLI interface is prepared and tested
- Loading branch information
Dmytro Parfeniuk
committed
Jul 29, 2024
1 parent
2c8d517
commit 4163232
Showing
7 changed files
with
155 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from typing import Any, Dict | ||
from unittest.mock import MagicMock | ||
|
||
import pytest | ||
from click.testing import CliRunner | ||
|
||
|
||
@pytest.fixture | ||
def cli_runner(): | ||
return CliRunner() | ||
|
||
|
||
@pytest.fixture | ||
def patch_main(mocker) -> MagicMock: | ||
return mocker.patch("guidellm.main.main.callback") | ||
|
||
|
||
@pytest.fixture | ||
def default_main_kwargs() -> Dict[str, Any]: | ||
""" | ||
All the defaults come from the `guidellm.main` function. | ||
""" | ||
|
||
return { | ||
"target": "localhost:8000/completions", | ||
"host": None, | ||
"port": None, | ||
"path": None, | ||
"backend": "openai_server", | ||
"model": None, | ||
"task": None, | ||
"data": None, | ||
"data_type": "transformers", | ||
"tokenizer": None, | ||
"rate_type": "synchronous", | ||
"rate": (1.0,), | ||
"num_seconds": 120, | ||
"num_requests": None, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from typing import List | ||
from unittest.mock import MagicMock | ||
|
||
import pytest | ||
from click.testing import CliRunner | ||
|
||
from guidellm.main import main | ||
|
||
|
||
def test_main_defaults_from_cli( | ||
patch_main: MagicMock, cli_runner: CliRunner, default_main_kwargs | ||
): | ||
cli_runner.invoke(main) | ||
|
||
assert patch_main.call_count == 1 | ||
assert patch_main.call_args.kwargs == default_main_kwargs | ||
|
||
|
||
def test_main_cli_overrided( | ||
patch_main: MagicMock, cli_runner: CliRunner, default_main_kwargs | ||
): | ||
cli_runner.invoke( | ||
main, | ||
["--target", "localhost:9000", "--backend", "test", "--rate-type", "sweep"], | ||
) | ||
default_main_kwargs.update( | ||
{"target": "localhost:9000", "backend": "test", "rate_type": "sweep"} | ||
) | ||
|
||
assert patch_main.call_count == 1 | ||
assert patch_main.call_args.kwargs == default_main_kwargs | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"args,expected_stdout", | ||
[ | ||
( | ||
["--backend", "invalid", "--rate-type", "sweep"], | ||
( | ||
b"Usage: main [OPTIONS]\nTry 'main --help' for help.\n\n" | ||
b"Error: Invalid value for '--backend': " | ||
b"'invalid' is not one of 'test', 'openai_server'.\n" | ||
), | ||
), | ||
( | ||
["--num-requests", "str instead of int"], | ||
( | ||
b"Usage: main [OPTIONS]\nTry 'main --help' for help.\n\n" | ||
b"Error: Invalid value for '--num-requests': " | ||
b"'str instead of int' is not a valid integer.\n" | ||
), | ||
), | ||
], | ||
) | ||
def test_main_cli_validation_error( | ||
patch_main: MagicMock, | ||
cli_runner: CliRunner, | ||
args: List[str], | ||
expected_stdout: bytes, | ||
): | ||
result = cli_runner.invoke(main, args) | ||
|
||
assert patch_main.call_count == 0 | ||
assert result.stdout_bytes == expected_stdout |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import pytest | ||
|
||
from guidellm.main import main | ||
|
||
|
||
def test_task_without_data(mocker, default_main_kwargs): | ||
patch = mocker.patch("guidellm.backend.Backend.create") | ||
default_main_kwargs.update({"task": "can't be used without data"}) | ||
with pytest.raises(NotImplementedError): | ||
getattr(main, "callback")(**default_main_kwargs) | ||
|
||
assert patch.call_count == 1 | ||
|
||
|
||
def test_invalid_data_type(mocker, default_main_kwargs): | ||
patch = mocker.patch("guidellm.backend.Backend.create") | ||
default_main_kwargs.update({"data_type": "invalid"}) | ||
|
||
with pytest.raises(ValueError): | ||
getattr(main, "callback")(**default_main_kwargs) | ||
|
||
assert patch.call_count == 1 | ||
|
||
|
||
def test_invalid_rate_type(mocker, default_main_kwargs): | ||
patch = mocker.patch("guidellm.backend.Backend.create") | ||
file_request_generator_initialization_patch = mocker.patch( | ||
"guidellm.request.file.FileRequestGenerator.__init__", | ||
return_value=None, | ||
) | ||
default_main_kwargs.update({"rate_type": "invalid", "data_type": "file"}) | ||
|
||
with pytest.raises(ValueError): | ||
getattr(main, "callback")(**default_main_kwargs) | ||
|
||
assert patch.call_count == 1 | ||
assert file_request_generator_initialization_patch.call_count == 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters