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_is_in(self):
validate = Validator().validate({
'test': 1
}, is_in(['test'], [1, 2, 3]))
self.assertEqual(len(validate), 0)
validate = Validator().validate({
'test': 1
}, is_in(['test'], [4, 2, 3]))
self.assertEqual(validate, {'test': ['test must contain an element in [4, 2, 3]']})
def test_dot_required(self):
validate = Validator().validate({
'user': {
'email': 'user@example.com'
}
}, required(['user.id']))
self.assertEqual(validate, {'user.id': ['user.id is required']})
validate = Validator().validate({
'user': {
'id': 1
}
}, required(['user.id']))
self.assertEqual(len(validate), 0)
def test_loaded_validator_class(self):
self.assertIsInstance(self.app.make(Validator), Validator)
def test_enclosure_can_encapsulate_rules(self):
validate = Validator().validate({
'username': 'user123',
'email': 'user@example.com',
'terms': 'on'
}, MockRuleEnclosure)
self.assertEqual(len(validate), 0)
def test_exists(self):
validate = Validator().validate({
'terms': 'on',
'user': 'here',
}, exists(['user']))
self.assertEqual(len(validate), 0)
validate = Validator().validate({
'terms': 'test'
}, exists(['user']))
self.assertEqual(validate, {'user': ['user must exist']})
def test_numeric(self):
validate = Validator().validate({
'test': 1
}, numeric(['test']))
self.assertEqual(len(validate), 0)
validate = Validator().validate({
'test': 'hey'
}, numeric(['test']))
self.assertEqual(validate, {'test': ['test must be a numeric']})
def test_several_tests(self):
validate = Validator().validate({
'test': 'hey'
}, required(['notin']), numeric(['notin']))
self.assertEqual(validate, {'notin': ['notin is required', 'notin must be a numeric']})
def test_dot_in_range(self):
validate = Validator().validate({
'user': {
'id': 1,
'email': 'user@example.com',
'age': 25
}
}, in_range(['user.age'], min=25, max=72))
self.assertEqual(len(validate), 0)
validate = Validator().validate({
'user': {
'id': 1,
'email': 'user@example.com',
'age': 25
}
}, in_range(['user.age'], min=27, max=72))
def test_exception(self):
with self.assertRaises(AttributeError) as e:
validate = Validator().validate({
'terms': 'on',
}, required(['user'], raises={
'user': AttributeError
}))
try:
validate = Validator().validate({
'terms': 'on',
}, required(['user'], raises={
'user': AttributeError
}))
except AttributeError as e:
self.assertEqual(str(e), 'user is required')
try:
validate = Validator().validate({
def test_before_today(self):
validate = Validator().validate({
'date': '1975-05-21T22:00:00',
}, before_today(['date']))
self.assertEqual(len(validate), 0)
validate = Validator().validate({
'date': pendulum.yesterday().to_datetime_string(),
}, before_today(['date']))
self.assertEqual(len(validate), 0)
validate = Validator().validate({
'date': '2020-05-21T22:00:00',
}, before_today(['date']))
self.assertEqual(validate, {'date': ['date must be a date before today']})