Skip to content

Commit

Permalink
Added tests to improve coverage of exceptional cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
IgorTatarnikov committed Oct 23, 2023
1 parent 77c213d commit 61879c6
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
7 changes: 3 additions & 4 deletions bg_atlasapi/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ def get_download_size(url: str) -> int:
------
requests.exceptions.HTTPError: If there's an issue with HTTP request.
ValueError: If the file size cannot be extracted from the response.
IndexError: If the url is not formatted as expected
"""
try:
Expand All @@ -233,7 +234,7 @@ def get_download_size(url: str) -> int:

response_string = response.content.decode("utf-8")
search_result = re.search(
"([0-9]+.[0-9] [MGK]B)|([0-9]+ [MGK]B)", response_string
r"([0-9]+\.[0-9] [MGK]B)|([0-9]+ [MGK]B)", response_string
)

assert search_result is not None
Expand All @@ -251,14 +252,12 @@ def get_download_size(url: str) -> int:
size *= 1e6
elif prefix == "K":
size *= 1e3
else:
raise ValueError("File size information not found in the response")

return int(size)

except requests.exceptions.HTTPError as e:
raise e
except ValueError:
except AssertionError:
raise ValueError("File size information not found in the response.")
except IndexError:
raise IndexError("Improperly formatted URL")
Expand Down
41 changes: 40 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
from pathlib import Path
from unittest import mock

import pytest
import requests
from requests import HTTPError

from bg_atlasapi import utils

test_url = "https://gin.g-node.org/BrainGlobe/atlases/raw/master/example_mouse_100um_v1.2.tar.gz"


def test_http_check():
assert utils.check_internet_connection()
Expand All @@ -22,7 +29,7 @@ def test_get_download_size_bad_url():


def test_get_download_size_no_size_url():
with pytest.raises(AssertionError):
with pytest.raises(ValueError):
utils.get_download_size(
"https://gin.g-node.org/BrainGlobe/atlases/src/master/last_versions.conf"
)
Expand Down Expand Up @@ -55,3 +62,35 @@ def test_get_download_size(url, real_size):
real_size = real_size * 1e6

assert size == real_size


def test_get_download_size_kb():
with mock.patch("requests.get", autospec=True) as mock_request:
mock_response = mock.Mock(spec=requests.Response)
mock_response.status_code = 200
mock_response.content = b"asd 24.7 KB 123sd"
mock_request.return_value = mock_response

size = utils.get_download_size(test_url)

assert size == 24700


def test_get_download_size_HTTPError():
with mock.patch("requests.get", autospec=True) as mock_request:
mock_request.side_effect = HTTPError()

with pytest.raises(HTTPError):
utils.get_download_size(test_url)


def test_retrieve_over_http_get_download_size_exception():
with mock.patch("requests.get", autospec=True) as mock_request:
mock_response = mock.Mock(spec=requests.Response)
mock_response.status_code = 200
mock_response.content = b"1234"
mock_response.headers = {}
mock_request.return_value = mock_response

with pytest.raises(FileNotFoundError):
utils.retrieve_over_http(test_url, Path("/tmp/path"))

0 comments on commit 61879c6

Please sign in to comment.