Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_regex_validation_flags():
# Invalid flags ignored
validator = validators.Regex("foo", bla=True, ble=False, ignorecase=True)
assert validator.flags == [validators.Regex.FLAGS["ignorecase"]]
# Flag kwargs must be True-y
validator = validators.Regex("foo", ignorecase=False, multiline=True)
assert validator.flags == [validators.Regex.FLAGS["multiline"]]
# ECMA pattern flags recognized
validator = validators.Regex("/foo/im")
assert sorted(validator.flags) == sorted([
validators.Regex.FLAGS["multiline"],
validators.Regex.FLAGS["ignorecase"],
])
# ECMA pattern overrides flags kwargs
validator = validators.Regex("/foo/", ignorecase=True, multiline=True)
assert validator.flags == []
def test_regex_validation_flags():
# Invalid flags ignored
validator = validators.Regex("foo", bla=True, ble=False, ignorecase=True)
assert validator.flags == [validators.Regex.FLAGS["ignorecase"]]
# Flag kwargs must be True-y
validator = validators.Regex("foo", ignorecase=False, multiline=True)
assert validator.flags == [validators.Regex.FLAGS["multiline"]]
# ECMA pattern flags recognized
validator = validators.Regex("/foo/im")
assert sorted(validator.flags) == sorted([
validators.Regex.FLAGS["multiline"],
validators.Regex.FLAGS["ignorecase"],
])
# ECMA pattern overrides flags kwargs
validator = validators.Regex("/foo/", ignorecase=True, multiline=True)
assert validator.flags == []
def test_regex_validator_when_ecma_regex_given(self):
class Person(models.Base):
name = fields.StringField(
validators=validators.Regex('/^some pattern$/'))
schema = Person.to_json_schema()
pattern = get_fixture('schema_pattern.json')
self.assertTrue(compare_schemas(pattern, schema))
validator = validators.Regex("foo", bla=True, ble=False, ignorecase=True)
assert validator.flags == [validators.Regex.FLAGS["ignorecase"]]
# Flag kwargs must be True-y
validator = validators.Regex("foo", ignorecase=False, multiline=True)
assert validator.flags == [validators.Regex.FLAGS["multiline"]]
# ECMA pattern flags recognized
validator = validators.Regex("/foo/im")
assert sorted(validator.flags) == sorted([
validators.Regex.FLAGS["multiline"],
validators.Regex.FLAGS["ignorecase"],
])
# ECMA pattern overrides flags kwargs
validator = validators.Regex("/foo/", ignorecase=True, multiline=True)
assert validator.flags == []
def test_regex_validator(self):
class Person(models.Base):
name = fields.StringField(
validators=validators.Regex('^some pattern$'))
schema = Person.to_json_schema()
pattern = get_fixture('schema_pattern.json')
self.assertTrue(compare_schemas(pattern, schema))
def test_regex_validator_with_flag(self):
class Person(models.Base):
name = fields.StringField(
validators=validators.Regex(
'^some pattern$', ignorecase=True))
schema = Person.to_json_schema()
pattern = get_fixture('schema_pattern_flag.json')
self.assertTrue(compare_schemas(pattern, schema))
def test_regex_validator():
class Person(models.Base):
name = fields.StringField(
validators=validators.Regex('^[a-z]+$', ignorecase=True))
person = Person()
with pytest.raises(errors.ValidationError):
person.name = '123'
person.name = 'Jimmy'
def test_regex_validator_when_ecma_regex_given():
class Person(models.Base):
name = fields.StringField(
validators=validators.Regex('/^[a-z]+$/i', ignorecase=False))
person = Person()
with pytest.raises(errors.ValidationError):
person.name = '123'
person.name = 'Jimmy'
def test_regex_validation_flags():
# Invalid flags ignored
validator = validators.Regex("foo", bla=True, ble=False, ignorecase=True)
assert validator.flags == [validators.Regex.FLAGS["ignorecase"]]
# Flag kwargs must be True-y
validator = validators.Regex("foo", ignorecase=False, multiline=True)
assert validator.flags == [validators.Regex.FLAGS["multiline"]]
# ECMA pattern flags recognized
validator = validators.Regex("/foo/im")
assert sorted(validator.flags) == sorted([
validators.Regex.FLAGS["multiline"],
validators.Regex.FLAGS["ignorecase"],
])
# ECMA pattern overrides flags kwargs
validator = validators.Regex("/foo/", ignorecase=True, multiline=True)
assert validator.flags == []