Skip to content

Commit

Permalink
Mock for dowload_file
Browse files Browse the repository at this point in the history
  • Loading branch information
movchan74 committed Nov 3, 2023
1 parent 09783d5 commit 4619d9e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
20 changes: 16 additions & 4 deletions aana/tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,19 @@ def load_numpy_from_image_bytes(content: bytes) -> np.ndarray:
return image.get_numpy()


def test_image():
@pytest.fixture
def mock_download_file(mocker):
"""
Mock download_file.
"""
mock = mocker.patch("aana.models.core.image.download_file", autospec=True)
path = resources.path("aana.tests.files.images", "Starry_Night.jpeg")
content = path.read_bytes()
mock.return_value = content
return mock


def test_image(mock_download_file):
"""
Test that the image can be created from path, url, content, or numpy.
"""
Expand All @@ -36,7 +48,7 @@ def test_image():
image.cleanup()

try:
url = "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ea/Van_Gogh_-_Starry_Night_-_Google_Art_Project.jpg/909px-Van_Gogh_-_Starry_Night_-_Google_Art_Project.jpg"
url = "http://example.com/Starry_Night.jpeg"
image = Image(url=url, save_on_disc=False)
assert image.path is None
assert image.content is None
Expand Down Expand Up @@ -97,7 +109,7 @@ def test_image_path_not_exist():
Image(path=path)


def test_save_image():
def test_save_image(mock_download_file):
"""
Test that save_on_disc works.
"""
Expand All @@ -114,7 +126,7 @@ def test_save_image():
image.cleanup()

try:
url = "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ea/Van_Gogh_-_Starry_Night_-_Google_Art_Project.jpg/1024px-Van_Gogh_-_Starry_Night_-_Google_Art_Project.jpg"
url = "http://example.com/Starry_Night.jpeg"
image = Image(url=url, save_on_disc=True)
assert image.content is None
assert image.numpy is None
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ scipy = "^1.11.3"
rapidfuzz = "^3.4.0"
transformers = "^4.34.1"
opencv-python = "^4.8.1.78"
pytest-mock = "^3.12.0"

[tool.poetry.group.dev.dependencies]
ipykernel = "^6.25.2"
Expand Down

0 comments on commit 4619d9e

Please sign in to comment.