-
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests: add tests for _get_image_from_url
- Loading branch information
1 parent
0330216
commit a141d5a
Showing
2 changed files
with
45 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from unittest.mock import patch | ||
|
||
import pytest | ||
import requests | ||
|
||
from robotoff.utils.image import ImageLoadingException, _get_image_from_url | ||
|
||
|
||
@patch("robotoff.utils.image.requests.get") | ||
def test__get_image_from_url(mock_get): | ||
# Mock the response from requests.get | ||
mock_response = mock_get.return_value | ||
mock_response.content = b"fake image content" | ||
mock_response.status_code = 200 | ||
|
||
# Call the function with an image URL | ||
url = "https://example.com/image.jpg" | ||
result = _get_image_from_url(url) | ||
|
||
# Check that requests.get was called with the correct URL | ||
mock_get.assert_called_once_with(url, auth=None) | ||
|
||
# Check that the content of the response is the same as the mock content | ||
assert result.content == b"fake image content" | ||
|
||
# Check that the status code of the response is the same as the mock | ||
# status code | ||
assert result.status_code == 200 | ||
|
||
# Test when r.ok returns False | ||
mock_response.status_code = 404 | ||
mock_response.content = b"" | ||
mock_response.ok = False | ||
with pytest.raises(ImageLoadingException): | ||
_get_image_from_url(url) | ||
|
||
# Test when there is an error during HTTP request | ||
mock_get.side_effect = requests.exceptions.SSLError | ||
with pytest.raises(ImageLoadingException): | ||
_get_image_from_url(url) | ||
|
||
# Check that the function returns None when error_raise is False | ||
image = _get_image_from_url(url, error_raise=False) | ||
assert image is None |