Skip to content

Commit

Permalink
Fix string pattern constraint.
Browse files Browse the repository at this point in the history
  • Loading branch information
nealkruis committed May 24, 2024
1 parent e01ba42 commit 8584bfa
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 52 deletions.
11 changes: 7 additions & 4 deletions lattice/schema.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from __future__ import (
annotations,
) # Needed for type hinting classes that are not yet fully defined
from typing import List
import pathlib
import re

from .file_io import load, get_file_basename

core_schema_path = pathlib.Path(pathlib.Path(__file__).parent, "core.schema.yaml")
Expand Down Expand Up @@ -160,7 +162,7 @@ class ArrayLengthLimitsConstraint(Constraint):
pattern = RegularExpressionPattern(r"\[(\d*)\.\.(\d*)\]")


_constraint_list = [
_constraint_list: List[Constraint] = [
RangeConstraint,
MultipleConstraint,
SetConstraint,
Expand All @@ -171,7 +173,7 @@ class ArrayLengthLimitsConstraint(Constraint):
]


def _constraint_factory(input: str, parent_data_element: DataElement):
def _constraint_factory(input: str, parent_data_element: DataElement) -> Constraint:
number_of_matches = 0
match_type = None
for constraint in _constraint_list:
Expand Down Expand Up @@ -263,7 +265,7 @@ def init_method(self, text, parent_data_element):


class DataGroup:
def __init__(self, name, data_group_dictionary, parent_schema: Schema):
def __init__(self, name: str, data_group_dictionary, parent_schema: Schema):
self.name = name
self.dictionary = data_group_dictionary
self.parent_schema = parent_schema
Expand Down Expand Up @@ -368,7 +370,8 @@ def __init__(self, schema=None):
f"({sets})|"
f"({self.data_element_value_constraint})|"
f"({reference_scope})|"
f"({self.selector_constraint})"
f"({self.selector_constraint})|"
f"({StringPatternConstraint.pattern})"
)

# Conditional Requirements
Expand Down
84 changes: 36 additions & 48 deletions test/test_schema_patterns.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
import lattice.schema

# Needed for python versions < 3.9. 3.8 reaches end-of-life 2024-10.
from typing import List


def execute_pattern_test(pattern: lattice.schema.RegularExpressionPattern, valid_examples: List[str], invalid_examples: List[str], anchored: bool = True):
def execute_pattern_test(
pattern: lattice.schema.RegularExpressionPattern,
valid_examples: List[str],
invalid_examples: List[str],
anchored: bool = True,
) -> None:

pattern_text = pattern.anchored() if anchored else pattern.pattern

for test in valid_examples:
if not pattern.match(test, anchored):
raise Exception(f"\"{test}\" does not match: {pattern_text}")
raise Exception(f'"{test}" does not match: {pattern_text}')

for test in invalid_examples:
if pattern.match(test, anchored):
raise Exception(f"\"{test}\" matches: {pattern_text}")
raise Exception(f'"{test}" matches: {pattern_text}')


def test_integer_pattern():
Expand All @@ -29,12 +35,8 @@ def test_integer_pattern():
"999999",
"-999999",
],
invalid_examples=[
"0.5",
"a",
"1.0",
"-9999.0"
])
invalid_examples=["0.5", "a", "1.0", "-9999.0"],
)


def test_data_type_pattern():
Expand All @@ -47,9 +49,8 @@ def test_data_type_pattern():
"{DataGroup}",
"[String][1..]",
],
invalid_examples=[
"Wrong"
])
invalid_examples=["Wrong"],
)


def test_enumerator_pattern():
Expand All @@ -62,57 +63,44 @@ def test_enumerator_pattern():
"NEW_ENUMERATOR",
"A",
],
invalid_examples=[
"Wrong",
"wrong",
"_A",
"A_",
"000_A"
],
anchored=True)
invalid_examples=["Wrong", "wrong", "_A", "A_", "000_A"],
anchored=True,
)


def test_value_pattern():
execute_pattern_test(
pattern=lattice.schema._value_pattern,
valid_examples=[
"3.14",
"\"\"",
"\"String\"",
"True",
"False",
"ENUMERATOR",
"ENUMERATOR_2",
"NEW_ENUMERATOR"
],
invalid_examples=[
"Wrong",
"true",
"false"
],
anchored=True)
valid_examples=["3.14", '""', '"String"', "True", "False", "ENUMERATOR", "ENUMERATOR_2", "NEW_ENUMERATOR"],
invalid_examples=["Wrong", "true", "false"],
anchored=True,
)


def test_data_element_value_constraint_pattern():

execute_pattern_test(
pattern=lattice.schema.DataElementValueConstraint.pattern,
valid_examples=[
"schema=RS0001"
],
invalid_examples=[
"Wrong",
"data_element=wronG"
],
anchored=True)
valid_examples=["schema=RS0001"],
invalid_examples=["Wrong", "data_element=wronG"],
anchored=True,
)


def test_selector_constraint_pattern():

execute_pattern_test(
pattern=lattice.schema.SelectorConstraint.pattern,
valid_examples=[
"operation_speed_control_type(CONTINUOUS, DISCRETE)"
],
valid_examples=["operation_speed_control_type(CONTINUOUS, DISCRETE)"],
invalid_examples=[],
anchored=True)
anchored=True,
)


def test_string_pattern_constraint_pattern():
execute_pattern_test(
pattern=lattice.schema.StringPatternConstraint.pattern,
valid_examples=['"[A-Z]{2}"'],
invalid_examples=["[A-Z]{2}"],
anchored=True,
)

0 comments on commit 8584bfa

Please sign in to comment.