Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added check for metadata validation #807

Merged
merged 11 commits into from
Aug 5, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

SupportedEnumType = Union[Type[Enum], _EnumTypeWrapper]

_VALID_CHARS = set(" ().,;:!?-_'+")


class ParameterMetadata(NamedTuple):
"""Class that represents the metadata of parameters."""
Expand Down Expand Up @@ -86,6 +88,18 @@ def initialize(
)


def validate_display_name(display_name: str) -> None:
LazeringDeath marked this conversation as resolved.
Show resolved Hide resolved
"""Validate and raise exception if 'display_name' has invalid characters.

Raises:
ValueError: If display_name has invalid characters.
"""
if display_name == "" or not display_name[0].isalpha():
raise ValueError("First letter in " + display_name + " must be a letter")
elif not all(char in _VALID_CHARS or char.isalnum() for char in display_name):
raise ValueError("Invalid characters in " + display_name)
LazeringDeath marked this conversation as resolved.
Show resolved Hide resolved


def validate_default_value_type(parameter_metadata: ParameterMetadata) -> None:
"""Validate and raise exception if the default value does not match the type info.

Expand Down
LazeringDeath marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,7 @@ def configuration(
"DataType.PinArray1D is deprecated. Use DataType.IOResourceArray1D instead.",
DeprecationWarning,
)
parameter_metadata.validate_display_name(display_name)
data_type_info = _datatypeinfo.get_type_info(type)
annotations = self._make_annotations_dict(
data_type_info.type_specialization, instrument_type=instrument_type, enum_type=enum_type
Expand Down Expand Up @@ -472,6 +473,7 @@ def output(
"DataType.PinArray1D is deprecated. Use DataType.IOResourceArray1D instead.",
DeprecationWarning,
)
parameter_metadata.validate_display_name(display_name)
data_type_info = _datatypeinfo.get_type_info(type)
annotations = self._make_annotations_dict(
data_type_info.type_specialization, enum_type=enum_type
Expand Down
2 changes: 1 addition & 1 deletion packages/service/tests/unit/test_decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def _get_grpc_serialized_data(values):
def _get_test_parameter_by_id(default_values):
parameter_by_id = {
1: ParameterMetadata.initialize(
display_name="float_data!",
display_name="float_data",
bkeryan marked this conversation as resolved.
Show resolved Hide resolved
type=type_pb2.Field.TYPE_FLOAT,
repeated=False,
default_value=default_values[0],
Expand Down
32 changes: 31 additions & 1 deletion packages/service/tests/unit/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
TYPE_SPECIALIZATION_KEY,
)
from ni_measurement_plugin_sdk_service._internal.parameter import metadata
from ni_measurement_plugin_sdk_service.measurement.info import DataType, TypeSpecialization
from ni_measurement_plugin_sdk_service.measurement.info import (
DataType,
TypeSpecialization,
)


class Color(Enum):
Expand Down Expand Up @@ -148,3 +151,30 @@ def test___default_value_same_as_type___validate___raises_no_exception(
)

metadata.validate_default_value_type(parameter_metadata) # implicitly assert does not throw


@pytest.mark.parametrize(
"display_name",
[
" test_string",
"_____()!",
"test@string",
"",
],
)
def test___display_name_invalid_characters___validate___raises_value_exception(display_name):
with pytest.raises(ValueError):
metadata.validate_display_name(display_name)


@pytest.mark.parametrize(
"display_name",
[
"teststring()",
"tEsT StRIng?",
"test_string!",
"Test String: -10",
],
)
def test___display_name_valid_characters___validate___raises_no_exception(display_name):
metadata.validate_display_name(display_name)
9 changes: 7 additions & 2 deletions packages/service/tests/unit/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@

from ni_measurement_plugin_sdk_service import _datatypeinfo
from ni_measurement_plugin_sdk_service._annotations import TYPE_SPECIALIZATION_KEY
from ni_measurement_plugin_sdk_service._internal.stubs.ni.protobuf.types import xydata_pb2
from ni_measurement_plugin_sdk_service.measurement.info import DataType, TypeSpecialization
from ni_measurement_plugin_sdk_service._internal.stubs.ni.protobuf.types import (
xydata_pb2,
)
from ni_measurement_plugin_sdk_service.measurement.info import (
DataType,
TypeSpecialization,
)
from ni_measurement_plugin_sdk_service.measurement.service import MeasurementService


Expand Down
Loading