mirror of
https://github.com/bec-project/bec_widgets.git
synced 2025-07-13 11:11:49 +02:00
test(color): validation tests added
This commit is contained in:
@ -250,7 +250,7 @@ class Colors:
|
||||
if not 0 <= value <= 255:
|
||||
raise PydanticCustomError(
|
||||
"unsupported color",
|
||||
f"The color values must be between 0 and 255. Provide color {color}.",
|
||||
f"The color values must be between 0 and 255 in RGBA format (R,G,B,A)",
|
||||
{"wrong_value": color},
|
||||
)
|
||||
return color
|
||||
|
60
tests/unit_tests/test_color_validation.py
Normal file
60
tests/unit_tests/test_color_validation.py
Normal file
@ -0,0 +1,60 @@
|
||||
import pytest
|
||||
from pydantic import ValidationError
|
||||
|
||||
from bec_widgets.utils import Colors
|
||||
from bec_widgets.widgets.figure.plots.waveform.waveform_curve import CurveConfig
|
||||
|
||||
|
||||
def test_color_validation_CSS():
|
||||
# Test valid color
|
||||
color = Colors.validate_color("teal")
|
||||
assert color == "teal"
|
||||
|
||||
# Test invalid color
|
||||
with pytest.raises(ValidationError) as excinfo:
|
||||
CurveConfig(color="invalid_color")
|
||||
|
||||
errors = excinfo.value.errors()
|
||||
assert len(errors) == 1
|
||||
assert errors[0]["type"] == ("unsupported color")
|
||||
assert "The color must be a valid HEX string or CSS Color." in str(excinfo.value)
|
||||
|
||||
|
||||
def test_color_validation_hex():
|
||||
# Test valid color
|
||||
color = Colors.validate_color("#ff0000")
|
||||
assert color == "#ff0000"
|
||||
|
||||
# Test invalid color
|
||||
with pytest.raises(ValidationError) as excinfo:
|
||||
CurveConfig(color="#ff00000")
|
||||
|
||||
errors = excinfo.value.errors()
|
||||
assert len(errors) == 1
|
||||
assert errors[0]["type"] == ("unsupported color")
|
||||
assert "The color must be a valid HEX string or CSS Color." in str(excinfo.value)
|
||||
|
||||
|
||||
def test_color_validation_RGBA():
|
||||
# Test valid color
|
||||
color = Colors.validate_color((255, 0, 0, 255))
|
||||
assert color == (255, 0, 0, 255)
|
||||
|
||||
# Test invalid color
|
||||
with pytest.raises(ValidationError) as excinfo:
|
||||
CurveConfig(color=(255, 0, 0))
|
||||
|
||||
errors = excinfo.value.errors()
|
||||
assert len(errors) == 1
|
||||
assert errors[0]["type"] == ("unsupported color")
|
||||
assert "The color must be a tuple of 4 elements (R, G, B, A)." in str(excinfo.value)
|
||||
|
||||
with pytest.raises(ValidationError) as excinfo:
|
||||
CurveConfig(color=(255, 0, 0, 355))
|
||||
|
||||
errors = excinfo.value.errors()
|
||||
assert len(errors) == 1
|
||||
assert errors[0]["type"] == ("unsupported color")
|
||||
assert "The color values must be between 0 and 255 in RGBA format (R,G,B,A)" in str(
|
||||
excinfo.value
|
||||
)
|
Reference in New Issue
Block a user