-
Notifications
You must be signed in to change notification settings - Fork 0
fix: use config.IMAGE_TIMEOUT for image URL download #151
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,94 @@ | ||
| """Tests that call_image_api uses config.IMAGE_TIMEOUT for the image download request.""" | ||
|
|
||
| import base64 | ||
| import json | ||
| from unittest.mock import MagicMock, patch | ||
|
|
||
|
|
||
| def _make_api_response(payload: dict) -> MagicMock: | ||
| """Build a mock requests.Response for the image generation POST call.""" | ||
| resp = MagicMock() | ||
| resp.status_code = 200 | ||
| resp.json.return_value = payload | ||
| resp.raise_for_status.return_value = None | ||
| return resp | ||
|
|
||
|
|
||
| def _make_download_response(content: bytes = b"fake_image_data") -> MagicMock: | ||
| """Build a mock streaming requests.Response for the image download GET call.""" | ||
| resp = MagicMock() | ||
| resp.status_code = 200 | ||
| resp.raise_for_status.return_value = None | ||
| resp.iter_content.return_value = iter([content]) | ||
| return resp | ||
|
|
||
|
|
||
| class TestImageDownloadUsesConfigTimeout: | ||
| """Verify that requests.get for image URL download uses config.IMAGE_TIMEOUT.""" | ||
|
|
||
| def test_download_uses_config_image_timeout(self, tmp_path, monkeypatch): | ||
| import novelforge.config as config | ||
| import novelforge.llm.image as image_mod | ||
|
|
||
| # Configure necessary settings | ||
| monkeypatch.setattr(config, "IMAGE_API_KEY", "test-key") | ||
| monkeypatch.setattr(config, "IMAGE_API_URL", "https://api.example.com/images") | ||
| monkeypatch.setattr(config, "IMAGE_MODEL", "dall-e-3") | ||
| monkeypatch.setattr(config, "IMAGE_SIZE", "1024x1024") | ||
| monkeypatch.setattr(config, "IMAGE_TIMEOUT", 120) | ||
| monkeypatch.setattr(config, "EXPORT_DIR", str(tmp_path)) | ||
|
|
||
| api_resp = _make_api_response({ | ||
| "data": [{"url": "https://cdn.example.com/image.png"}] | ||
| }) | ||
| download_resp = _make_download_response(b"\x89PNG\r\n") | ||
|
|
||
| get_calls = [] | ||
|
|
||
| def fake_get(url, **kwargs): | ||
| get_calls.append({"url": url, "kwargs": kwargs}) | ||
| return download_resp | ||
|
|
||
| with patch.object(image_mod.requests, "post", return_value=api_resp), \ | ||
| patch.object(image_mod.requests, "get", side_effect=fake_get): | ||
| result = image_mod.call_image_api("a brave hero", filename_prefix="test") | ||
|
|
||
| assert result is not None, "call_image_api should return a filename on success" | ||
| assert len(get_calls) == 1, "requests.get should be called once for the download" | ||
|
|
||
| actual_timeout = get_calls[0]["kwargs"].get("timeout") | ||
| assert actual_timeout == 120, ( | ||
| f"Expected timeout=config.IMAGE_TIMEOUT (120), got {actual_timeout!r}" | ||
| ) | ||
|
|
||
| def test_download_timeout_reflects_updated_config(self, tmp_path, monkeypatch): | ||
| """Changing IMAGE_TIMEOUT should change the download timeout accordingly.""" | ||
| import novelforge.config as config | ||
| import novelforge.llm.image as image_mod | ||
|
|
||
| monkeypatch.setattr(config, "IMAGE_API_KEY", "test-key") | ||
| monkeypatch.setattr(config, "IMAGE_API_URL", "https://api.example.com/images") | ||
| monkeypatch.setattr(config, "IMAGE_MODEL", "dall-e-3") | ||
| monkeypatch.setattr(config, "IMAGE_SIZE", "1024x1024") | ||
| monkeypatch.setattr(config, "IMAGE_TIMEOUT", 300) | ||
| monkeypatch.setattr(config, "EXPORT_DIR", str(tmp_path)) | ||
|
|
||
| api_resp = _make_api_response({ | ||
| "data": [{"url": "https://cdn.example.com/image.png"}] | ||
| }) | ||
| download_resp = _make_download_response(b"\x89PNG\r\n") | ||
|
|
||
| get_calls = [] | ||
|
|
||
| def fake_get(url, **kwargs): | ||
| get_calls.append({"url": url, "kwargs": kwargs}) | ||
| return download_resp | ||
|
|
||
| with patch.object(image_mod.requests, "post", return_value=api_resp), \ | ||
| patch.object(image_mod.requests, "get", side_effect=fake_get): | ||
| image_mod.call_image_api("sunset scene", filename_prefix="cover") | ||
|
|
||
| actual_timeout = get_calls[0]["kwargs"].get("timeout") | ||
| assert actual_timeout == 300, ( | ||
| f"Expected timeout=config.IMAGE_TIMEOUT (300), got {actual_timeout!r}" | ||
| ) | ||
|
Comment on lines
+89
to
+94
|
||
Oops, something went wrong.
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.
base64andjsonare imported but not used in this test module. Removing unused imports will keep the tests clean and avoid failing future linting/static checks if introduced.