-
Notifications
You must be signed in to change notification settings - Fork 6
add more tests for issue #23 #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tddschn
wants to merge
7
commits into
tensorchord:main
Choose a base branch
from
tddschn:add-tests-issue-23-2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ae9462d
add tests/test_serde.py
tddschn cdff4ba
add tests/test_cmd.py
tddschn 39085eb
fix tests/test_cmd.py
tddschn 27f9868
add tests/test_env.py
tddschn b798167
Merge branch 'add-tests-issue-23' into add-tests-issue-23-2
tddschn f80f969
use tempfile in test_cmd
tddschn 8b8f841
format
tddschn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,86 @@ | ||
| import sys | ||
| import tempfile | ||
| from io import StringIO | ||
| from unittest.mock import MagicMock | ||
|
|
||
| from modelz.client import ModelzClient, ModelzResponse | ||
| from modelz.cmd import console, inference | ||
| from modelz.serde import RawSerde | ||
|
|
||
|
|
||
| def test_inference(monkeypatch): | ||
| # Mocking the behavior of `ModelzClient.inference` | ||
| params_dict = {"key": "value"} | ||
| params_as_saved_in_file = b"Mocked Response" + b" saved in file" | ||
| # output_file = "output.bin" | ||
| # use a temp file instead | ||
| output_file = tempfile.NamedTemporaryFile().name | ||
| output_written_message = f"result has been written in {output_file}\n" | ||
|
|
||
| try: | ||
|
|
||
| def mock_inference(self, params, serde): | ||
| mock_response = ModelzResponse( | ||
| resp=MagicMock(status_code=200, content=params), | ||
| serde=RawSerde(), | ||
| ) | ||
| return mock_response | ||
|
|
||
| # Patching the `client.inference` method with the mock | ||
| monkeypatch.setattr(ModelzClient, "inference", mock_inference) | ||
| mock_init = MagicMock() | ||
| mock_init.return_value = None | ||
| monkeypatch.setattr(ModelzClient, "__init__", mock_init) | ||
|
|
||
| # Creating a StringIO object to capture the console output | ||
| console_output = StringIO() | ||
| console.file = console_output | ||
|
|
||
| # Running the inference function with the mocked client and write to file | ||
| inference( | ||
| deployment="deployment_id", | ||
| params=params_dict, | ||
| read_stdin=False, | ||
| ) | ||
|
|
||
| # Asserting the console output | ||
| expected_output = "{'key': 'value'}\n" | ||
| assert console_output.getvalue() == expected_output | ||
|
|
||
| # sys.stdin = StringIO(stdin_input) | ||
| # Mocking the behavior of `sys.stdin.buffer.read()` | ||
| mock_stdin_read = MagicMock(return_value=params_as_saved_in_file) | ||
|
|
||
| # Patching the `sys.stdin.buffer.read()` with the mock | ||
| monkeypatch.setattr(sys.stdin.buffer, "read", mock_stdin_read) | ||
|
|
||
| console_output = StringIO() | ||
| console.file = console_output | ||
|
|
||
| inference( | ||
| deployment="deployment_id", | ||
| read_stdin=True, | ||
| write_file=output_file, | ||
| ) | ||
|
|
||
| # Asserting the file content | ||
| with open(output_file, "rb") as file: | ||
| file_content = file.read() | ||
| assert file_content == params_as_saved_in_file | ||
|
|
||
| # Asserting the console output | ||
| assert console_output.getvalue() == output_written_message | ||
|
|
||
| except Exception as e: | ||
| raise e | ||
|
|
||
| finally: | ||
| # Resetting the sys.stdin | ||
| # sys.stdin = sys.__stdin__ | ||
| monkeypatch.undo() | ||
|
|
||
| # remove output_file if exists | ||
| import os | ||
|
|
||
| if os.path.exists(output_file): | ||
| os.remove(output_file) | ||
This file contains hidden or 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,26 @@ | ||
| import os | ||
|
|
||
| import pytest | ||
|
|
||
| from modelz.env import EnvConfig | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def config(): | ||
| os.environ["MODELZ_API_KEY"] = "abc123" | ||
| os.environ["MODELZ_HOST"] = "https://custom.host/" | ||
| yield EnvConfig() | ||
| os.environ.pop("MODELZ_API_KEY") | ||
| os.environ.pop("MODELZ_HOST") | ||
|
|
||
|
|
||
| def test_update_from_env(config): | ||
| assert config.api_key == "abc123" | ||
| assert config.host == "https://custom.host/" | ||
|
|
||
|
|
||
| def test_default(): | ||
| with pytest.raises(AttributeError): | ||
| config = EnvConfig() | ||
| assert config.api_key is None | ||
| assert config.host == "https://{}.modelz.io/" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tempfile will be removed when you close it. The usage is incorrect. You should hold the fp not the name.