mirror of
https://github.com/bec-project/bec_widgets.git
synced 2025-07-14 11:41:49 +02:00
test: test_validator_errors.py fixed
This commit is contained in:
@ -142,9 +142,9 @@ class PlotConfig(BaseModel):
|
||||
sources (list): A list of sources to be plotted on this axis.
|
||||
"""
|
||||
|
||||
plot_name: Optional[str]
|
||||
x_label: Optional[str]
|
||||
y_label: Optional[str]
|
||||
plot_name: Optional[str] = None
|
||||
x_label: Optional[str] = None
|
||||
y_label: Optional[str] = None
|
||||
sources: list = Field(default_factory=list)
|
||||
|
||||
@field_validator("sources")
|
||||
@ -156,23 +156,11 @@ class PlotConfig(BaseModel):
|
||||
Source(**source)
|
||||
source_type = source.get("type", None)
|
||||
|
||||
# Check if source type provided
|
||||
if source_type is None:
|
||||
raise PydanticCustomError(
|
||||
"no_source_type", "Source type must be provided", {"wrong_value": source}
|
||||
)
|
||||
|
||||
# Check if source type is supported
|
||||
if source_type == "scan_segment":
|
||||
validated_sources.append(SourceSegmentValidator(**source))
|
||||
elif source_type == "history":
|
||||
validated_sources.append(SourceHistoryValidator(**source))
|
||||
else:
|
||||
raise PydanticCustomError(
|
||||
"unsupported_source_type",
|
||||
"Unsupported source type: '{wrong_value}'",
|
||||
{"wrong_value": source_type},
|
||||
)
|
||||
return validated_sources
|
||||
|
||||
|
||||
@ -192,9 +180,9 @@ class PlotSettings(BaseModel):
|
||||
background_color: Literal["black", "white"] = "black"
|
||||
axis_width: Optional[int] = 2
|
||||
axis_color: Optional[str] = None
|
||||
num_columns: int
|
||||
colormap: str
|
||||
scan_types: bool
|
||||
num_columns: Optional[int] = 1
|
||||
colormap: Optional[str] = "magma"
|
||||
scan_types: Optional[bool] = False
|
||||
|
||||
|
||||
class DeviceMonitorConfig(BaseModel):
|
||||
|
@ -163,6 +163,22 @@ CONFIG_WRONG = {
|
||||
}
|
||||
],
|
||||
},
|
||||
{
|
||||
"plot_name": "Gauss plots vs samx",
|
||||
"x_label": "Motor X",
|
||||
"y_label": "Gauss",
|
||||
"sources": [
|
||||
{
|
||||
"signals": {
|
||||
"x": [{"name": "samx", "entry": "samx"}],
|
||||
"y": [
|
||||
{"name": "samx"},
|
||||
{"name": "samy", "entry": "samx"},
|
||||
],
|
||||
},
|
||||
}
|
||||
],
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
|
@ -4,7 +4,7 @@ from pydantic import ValidationError
|
||||
from bec_widgets.validation.monitor_config_validator import (
|
||||
MonitorConfigValidator,
|
||||
Signal,
|
||||
PlotAxis,
|
||||
AxisSignal,
|
||||
PlotConfig,
|
||||
)
|
||||
|
||||
@ -63,18 +63,43 @@ def test_plot_config_x_axis_signal_validation(setup_devices):
|
||||
# Setup a valid signal
|
||||
valid_signal = Signal(name="samx")
|
||||
|
||||
# Case with more than one signal for x-axis
|
||||
plot_axis_multiple_signals = PlotAxis(
|
||||
signals=[valid_signal, valid_signal], label="X Axis Label"
|
||||
)
|
||||
with pytest.raises(ValidationError) as excinfo:
|
||||
PlotConfig(
|
||||
plot_name="Test Plot",
|
||||
x=plot_axis_multiple_signals,
|
||||
y=PlotAxis(signals=[valid_signal], label="Y Axis Label"),
|
||||
)
|
||||
AxisSignal(x=[valid_signal, valid_signal], y=[valid_signal, valid_signal])
|
||||
|
||||
errors = excinfo.value.errors()
|
||||
assert len(errors) == 1
|
||||
assert errors[0]["type"] == "x_axis_multiple_signals"
|
||||
assert "There must be exactly one signal for x axis" in errors[0]["msg"]
|
||||
|
||||
|
||||
def test_plot_config_unsupported_source_type(setup_devices):
|
||||
with pytest.raises(ValidationError) as excinfo:
|
||||
PlotConfig(sources=[{"type": "unsupported_type", "signals": {}}])
|
||||
|
||||
errors = excinfo.value.errors()
|
||||
print(errors)
|
||||
assert len(errors) == 1
|
||||
assert errors[0]["type"] == "literal_error"
|
||||
|
||||
|
||||
def test_plot_config_no_source_type_provided(setup_devices):
|
||||
with pytest.raises(ValidationError) as excinfo:
|
||||
PlotConfig(sources=[{"signals": {}}])
|
||||
|
||||
errors = excinfo.value.errors()
|
||||
assert len(errors) == 1
|
||||
assert errors[0]["type"] == "missing"
|
||||
|
||||
|
||||
def test_plot_config_history_source_type(setup_devices):
|
||||
history_source = {
|
||||
"type": "history",
|
||||
"scanID": "valid_scan_id",
|
||||
"signals": {"x": [{"name": "samx"}], "y": [{"name": "samx"}]},
|
||||
}
|
||||
|
||||
plot_config = PlotConfig(sources=[history_source])
|
||||
|
||||
assert len(plot_config.sources) == 1
|
||||
assert plot_config.sources[0].type == "history"
|
||||
assert plot_config.sources[0].scanID == "valid_scan_id"
|
||||
|
Reference in New Issue
Block a user