From bff235f08d6e82ffeb4636f316be4411d251da9c Mon Sep 17 00:00:00 2001 From: Massimiliano Pippi Date: Sun, 10 Nov 2024 18:15:48 +0100 Subject: [PATCH] add unit tests --- src/banks/filters/image.py | 7 +-- tests/test_image.py | 92 ++++++++++++++++++++++++++++++++++++++ tests/test_types.py | 41 +++++++++++++++++ 3 files changed, 135 insertions(+), 5 deletions(-) create mode 100644 tests/test_image.py create mode 100644 tests/test_types.py diff --git a/src/banks/filters/image.py b/src/banks/filters/image.py index 81ea7cd..6482bdb 100644 --- a/src/banks/filters/image.py +++ b/src/banks/filters/image.py @@ -8,11 +8,8 @@ def _is_url(string: str) -> bool: - try: - result = urlparse(string) - return all([result.scheme, result.netloc]) - except ValueError: - return False + result = urlparse(string) + return all([result.scheme, result.netloc]) def image(value: str) -> str: diff --git a/tests/test_image.py b/tests/test_image.py new file mode 100644 index 0000000..29a8151 --- /dev/null +++ b/tests/test_image.py @@ -0,0 +1,92 @@ +import json +from pathlib import Path + +import pytest + +from banks.filters.image import _is_url, image + + +def test_is_url(): + """Test the internal URL validation function""" + assert _is_url("https://example.com/image.jpg") is True + assert _is_url("http://example.com/image.jpg") is True + assert _is_url("ftp://example.com/image.jpg") is True + assert _is_url("not_a_url.jpg") is False + assert _is_url("/path/to/image.jpg") is False + assert _is_url("relative/path/image.jpg") is False + assert _is_url("") is False + assert _is_url("https:\\example.com/image.jpg") is False + + +def test_image_with_url(): + """Test image filter with a URL input""" + url = "https://example.com/image.jpg" + result = image(url) + + # Verify the content block wrapper + assert result.startswith("") + assert result.endswith("") + + # Parse the JSON content + json_content = result[15:-16] # Remove wrapper tags + content_block = json.loads(json_content) + + assert content_block["type"] == "image_url" + assert content_block["image_url"]["url"] == url + + +def test_image_with_file_path(tmp_path): + """Test image filter with a file path input""" + # Create a temporary test image file + test_image = tmp_path / "test_image.jpg" + test_content = b"fake image content" + test_image.write_bytes(test_content) + + result = image(str(test_image)) + + # Verify the content block wrapper + assert result.startswith("") + assert result.endswith("") + + # Parse the JSON content + json_content = result[15:-16] # Remove wrapper tags + content_block = json.loads(json_content) + + assert content_block["type"] == "image_url" + assert content_block["image_url"]["url"].startswith("data:image/jpeg;base64,") + + +def test_image_with_nonexistent_file(): + """Test image filter with a nonexistent file path""" + with pytest.raises(FileNotFoundError): + image("nonexistent/image.jpg") + + +def test_image_content_block_structure(): + """Test the structure of the generated content block""" + url = "https://example.com/image.jpg" + result = image(url) + + json_content = result[15:-16] # Remove wrapper tags + content_block = json.loads(json_content) + + # Verify the content block has all expected fields + assert set(content_block.keys()) >= {"type", "image_url"} + assert content_block["type"] == "image_url" + assert isinstance(content_block["image_url"], dict) + assert "url" in content_block["image_url"] + + +@pytest.mark.parametrize( + "invalid_input", + [ + "", # empty string + None, # None value + 123, # non-string number + True, # boolean + ], +) +def test_image_with_invalid_input(invalid_input): + """Test image filter with various invalid inputs""" + with pytest.raises((IsADirectoryError, ValueError, AttributeError, TypeError)): + image(invalid_input) diff --git a/tests/test_types.py b/tests/test_types.py new file mode 100644 index 0000000..71a9a4e --- /dev/null +++ b/tests/test_types.py @@ -0,0 +1,41 @@ +import base64 +from pathlib import Path + +import pytest + +from banks.types import ImageUrl + + +def test_image_url_from_base64(): + """Test creating ImageUrl from base64 encoded data""" + test_data = "Hello, World!" + base64_data = base64.b64encode(test_data.encode()).decode("utf-8") + media_type = "image/jpeg" + + image_url = ImageUrl.from_base64(media_type, base64_data) + expected_url = f"data:{media_type};base64,{base64_data}" + assert image_url.url == expected_url + + +def test_image_url_from_path(tmp_path): + """Test creating ImageUrl from a file path""" + # Create a temporary test image file + test_image = tmp_path / "test_image.jpg" + test_content = b"fake image content" + test_image.write_bytes(test_content) + + image_url = ImageUrl.from_path(test_image) + + # Verify the URL starts with the expected data URI prefix + assert image_url.url.startswith("data:image/jpeg;base64,") + + # Decode the base64 part and verify the content matches + base64_part = image_url.url.split(",")[1] + decoded_content = base64.b64decode(base64_part) + assert decoded_content == test_content + + +def test_image_url_from_path_nonexistent(): + """Test creating ImageUrl from a nonexistent file path""" + with pytest.raises(FileNotFoundError): + ImageUrl.from_path(Path("nonexistent.jpg"))