class AttribsWithName(ValidatingAttribs): name = attr.ib(type=str, validator=[ instance_of(str), has_length(f'>={MIN_NAME_LENGTH}'), has_length(f'<={MAX_NAME_LENGTH}'), matches_re(RE_NAME), matches_re(RE_NOT_NAME, func=re.match), ])
def test_catches_invalid_func(self): """ Invalid match functions are caught. """ with pytest.raises(ValueError) as ei: matches_re("a", 0, lambda: None) assert ("'func' must be one of None, fullmatch, match, search." == ei.value.args[0])
def test_precompiled_pattern_no_flags(self): """ A pre-compiled pattern cannot be combined with a 'flags' argument. """ pattern = re.compile("") with pytest.raises(TypeError, match="can only be used with a string pattern"): matches_re(pattern, flags=re.IGNORECASE)
def test_repr(self): """ __repr__ is meaningful. """ assert repr(matches_re("a")).startswith( "<matches_re validator for pattern" )
class Marktteilnehmer(Geschaeftspartner): """ Objekt zur Aufnahme der Information zu einem Marktteilnehmer """ # required attributes bo_typ: BoTyp = attr.ib(default=BoTyp.MARKTTEILNEHMER) marktrolle: Marktrolle rollencodenummer: str = attr.ib(validator=matches_re(r"^\d{13}$")) rollencodetyp: Rollencodetyp # optional attributes makoadresse: str = attr.ib(default=None)
class ValidatedAttribute: x = attr.ib() y = attr.ib(validator=[instance_of(str), matches_re("foo.*qux")])
class ContainerDeleteAttribs(ValidatingAttribs): id = attr.ib(type=str, validator=[has_length(10), matches_re(RE_UUID)])
class UrlItem: """A URL in a toctree.""" # regex should match sphinx.util.url_re url: str = attr.ib(validator=[instance_of(str), matches_re(r".+://.*")]) title: Optional[str] = attr.ib(None, validator=optional(instance_of(str)))
def test_accepts_all_valid_func(self, func): """ Every valid match function is accepted. """ matches_re("a", func=func)
class SearchTester: val = attr.ib(validator=matches_re("a", 0, re.search))
class RePatternTester: val = attr.ib(validator=matches_re(pattern))
class MatchTester: val = attr.ib(validator=matches_re("a", re.IGNORECASE, re.match))
class ReTester: str_match = attr.ib(validator=matches_re("a|ab"))
class ReTester(object): str_match = attr.ib(validator=matches_re("a"))
class JsonCrudOperation(FromRequest['JsonCrudOperation']): operation: str = attr.ib(validator=in_(['create','update'])) docId: str = attr.ib(validator=matches_re(r'.*\.txt')) text: str = attr.ib(validator=instance_of(str))
def id_attrib_factory(): return attr.ib(type=str, validator=[has_length(10), matches_re(RE_UUID)])