1. Ludia
  2. Untitled project
  3. onctuous

Commits

Jean-Tiare Le Bigot  committed 1233267

clean + test Schema.validate_scalar

  • Participants
  • Parent commits a4fa0d1
  • Branches default

Comments (0)

Files changed (2)

File tests/unit/test_schema.py

View file
  • Ignore whitespace
 
         m_val_scalar.side_effect = InvalidList([Invalid(MSG1)])
         self.assertRaisesRegexp(InvalidList, MSG1, Schema(None).validate, [], int, 123)
+
+    def test_validate_scalar(self):
+        from voluptuous import Schema, Invalid
+
+        # test value specification
+        self.assertEqual(123, Schema.validate_scalar([], 123, 123))
+        self.assertEqual("123", Schema.validate_scalar([], "123", "123"))
+
+        # test type specification
+        self.assertEqual(123, Schema.validate_scalar([], int, 123))
+        self.assertEqual("123", Schema.validate_scalar([], str, "123"))
+
+        # test callable specification
+        spec = mock.Mock()
+        spec.return_value = 123
+        self.assertEqual(123, Schema.validate_scalar([], spec, "123"))
+
+        # errors
+        self.assertRaises(Invalid, Schema.validate_scalar, [], "toto", "123")
+        self.assertRaises(Invalid, Schema.validate_scalar, [], int, "123")
+
+        spec.side_effect = ValueError()
+        self.assertRaises(Invalid, Schema.validate_scalar, [], spec, "123")
+        spec.side_effect = Invalid(MSG1)
+        self.assertRaisesRegexp(Invalid, MSG1, Schema.validate_scalar, [], spec, "123")

File voluptuous/voluptuous.py

View file
  • Ignore whitespace
 
     @staticmethod
     def validate_scalar(path, schema, data):
-        """A scalar value.
+        """Validate a scalar value. The schema can be:
+         - a value
+         - a type
+         - a callable (function or objects)
+        """
+        # value specification ?
+        if data == schema:
+            pass
 
-        The schema can either be a value or a type.
-
-        >>> Schema.validate_scalar([], int, 1)
-        1
-        >>> Schema.validate_scalar([], float, '1')
-        Traceback (most recent call last):
-        ...
-        Invalid: expected float
-
-        Callables have
-        >>> Schema.validate_scalar([], lambda v: float(v), '1')
-        1.0
-
-        As a convenience, ValueError's are trapped:
-
-        >>> Schema.validate_scalar([], lambda v: float(v), 'a')
-        Traceback (most recent call last):
-        ...
-        Invalid: not a valid value
-        """
-        if type(schema) is type:
+        # type specification ?
+        elif type(schema) is type:
             if not isinstance(data, schema):
                 raise Invalid('expected %s' % schema.__name__, path)
+
+        # object / callable specification ?
         elif callable(schema):
             try:
-                return schema(data)
+                data = schema(data)
             except ValueError, e:
                 raise Invalid('not a valid value', path)
             except Invalid, e:
                 raise Invalid(e.msg, path + e.path)
+
+        # when everything failed, re-read the manual
         else:
-            if data != schema:
-                raise Invalid('not a valid value', path)
+            raise Invalid('not a valid value', path)
+
         return data