def test_all_errors(self): obj = {'foo': 10, 'bar': 10} fmt = '{foo: str, bar: str}' vres = validate(obj, fmt) self.assertEqual(len(vres.errors), 1) vres = validate(obj, fmt, all_errors=True) self.assertEqual(len(vres.errors), 2)
def test_validate(self): vres = validate({'foo': 'bar'}, '{foo: str}') self.assertIsInstance(vres, vmod.Result) self.assertTrue(bool(vres)) self.assertTrue(vres.match) self.assertIsNone(vres.error) self.assertEqual(vres.errors, []) vres = validate({'foo': 'bar'}, '{foo: int}') self.assertIsInstance(vres, vmod.Result) self.assertFalse(bool(vres)) self.assertFalse(vres.match) self.assertIsInstance(vres.error, six.string_types) self.assertEqual(len(vres.errors), 1)
def test_optional(self): self.assertTrue(match(None, '*int')) self.assertFalse(match(None, 'int')) vres = validate({}, '{foo: *str}') self.assertTrue(match({}, '{foo: *str}')) self.assertFalse(match({}, '{foo: str}')) self.assertTrue(match([], '[*str]')) self.assertFalse(match([], '[str]')) self.assertTrue(match(None, '*{foo: str}')) self.assertFalse(match(None, '{foo: str}')) vres = validate(None, '*[str]') self.assertTrue(match(None, '*[str]')) self.assertFalse(match(None, '[str]'))