feat(static): adds new static origins feature
This commit is contained in:
parent
6a29d68985
commit
15a85b1efe
20 changed files with 843 additions and 7 deletions
28
tests/utils/test_color.py
Normal file
28
tests/utils/test_color.py
Normal file
|
@ -0,0 +1,28 @@
|
|||
import pytest
|
||||
|
||||
from app.brm.utils import normalize_color
|
||||
|
||||
|
||||
def test_normalize_color():
|
||||
# Test valid CSS color names
|
||||
assert normalize_color('red') == '#ff0000'
|
||||
assert normalize_color('white') == '#ffffff'
|
||||
assert normalize_color('black') == '#000000'
|
||||
# Test valid 6-digit hex color codes
|
||||
assert normalize_color('#ffffff') == '#ffffff'
|
||||
assert normalize_color('#000000') == '#000000'
|
||||
assert normalize_color('#00ff00') == '#00ff00'
|
||||
# Test valid 3-digit hex color codes
|
||||
assert normalize_color('#fff') == '#fff'
|
||||
assert normalize_color('#000') == '#000'
|
||||
assert normalize_color('#0f0') == '#0f0'
|
||||
# Test case insensitivity
|
||||
assert normalize_color('#FFFFFF') == '#ffffff'
|
||||
assert normalize_color('#ABCDEF') == '#abcdef'
|
||||
# Test invalid color values
|
||||
with pytest.raises(ValueError):
|
||||
normalize_color('invalid')
|
||||
with pytest.raises(ValueError):
|
||||
normalize_color('#1234567')
|
||||
with pytest.raises(ValueError):
|
||||
normalize_color('#xyz')
|
59
tests/utils/test_images.py
Normal file
59
tests/utils/test_images.py
Normal file
|
@ -0,0 +1,59 @@
|
|||
import pytest
|
||||
from PIL import Image, ImageDraw
|
||||
from app.brm.utils import thumbnail_uploaded_image, create_data_uri
|
||||
from werkzeug.datastructures import FileStorage
|
||||
from io import BytesIO
|
||||
import base64
|
||||
|
||||
|
||||
def create_test_image(image_format: str, extension: str):
|
||||
"""
|
||||
Creates a test image with the data stored in a BytesIO buffer.
|
||||
|
||||
:return: A test image.
|
||||
"""
|
||||
img_byte = BytesIO()
|
||||
img = Image.new('RGB', (500, 500), color=(73, 109, 137))
|
||||
d = ImageDraw.Draw(img)
|
||||
d.text((10, 10), "Hello World", fill=(255, 255, 0))
|
||||
img.save(img_byte, format="jpeg")
|
||||
img_byte.seek(0)
|
||||
file = FileStorage(stream=img_byte, filename='test.jpg')
|
||||
return file
|
||||
|
||||
|
||||
@pytest.mark.parametrize("image_format, extension", [
|
||||
("jpeg", "jpg"),
|
||||
("png", "png"),
|
||||
("webp", "webp"),
|
||||
])
|
||||
def test_thumbnail_uploaded_image(image_format: str, extension: str):
|
||||
file = create_test_image(image_format, extension)
|
||||
output = thumbnail_uploaded_image(file)
|
||||
image = Image.open(BytesIO(output))
|
||||
|
||||
# Check if the processed image size is as expected
|
||||
assert image.size[0] <= 256
|
||||
assert image.size[1] <= 256
|
||||
|
||||
|
||||
@pytest.mark.parametrize("image_format, extension", [
|
||||
("jpeg", "jpg"),
|
||||
("png", "png"),
|
||||
("webp", "webp"),
|
||||
])
|
||||
def test_create_data_uri(image_format: str, extension: str):
|
||||
file = create_test_image(image_format, extension)
|
||||
img_bytes = thumbnail_uploaded_image(file)
|
||||
file_extension = 'jpg'
|
||||
data_uri = create_data_uri(img_bytes, file_extension)
|
||||
|
||||
# Check if the data URI starts as expected
|
||||
assert data_uri.startswith('data:image/jpeg;base64,')
|
||||
|
||||
# Check if the base64 image can be decoded
|
||||
try:
|
||||
base64_data = data_uri.split(',')[1]
|
||||
base64.b64decode(base64_data)
|
||||
except Exception as e:
|
||||
pytest.fail(f"Decoding base64 data URI failed with error {e}")
|
Loading…
Add table
Add a link
Reference in a new issue