Commits

Andy Mikhailenko  committed 9b778f5

Fix FuncValidator + datetime.now() in PyPy

  • Participants
  • Parent commits 7ff6efa

Comments (0)

Files changed (4)

File monk/compat.py

+# -*- coding: utf-8 -*-
+#
+#    Monk is a lightweight schema/query framework for document databases.
+#    Copyright © 2011—2013  Andrey Mikhaylenko
+#
+#    This file is part of Monk.
+#
+#    Monk is free software: you can redistribute it and/or modify
+#    it under the terms of the GNU Lesser General Public License as published
+#    by the Free Software Foundation, either version 3 of the License, or
+#    (at your option) any later version.
+#
+#    Monk is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU Lesser General Public License for more details.
+#
+#    You should have received a copy of the GNU Lesser General Public License
+#    along with Monk.  If not, see <http://gnu.org/licenses/>.
+"""
+Compatibility
+=============
+
+This module is intended to hide away implementation details of various Python
+versions.
+"""
+import types
+
+func_types = (
+    types.FunctionType,
+    types.MethodType,
+    # CPython: datetime.datetime.[utc]now()
+    types.BuiltinFunctionType,
+)
+

File monk/manipulation.py

     * :class:`AnyMerger`
 
 """
-import types
+from monk import compat
 
 
 __all__ = [
 
     """
     def check(self):
-        func_types = types.FunctionType, types.BuiltinFunctionType
-        return isinstance(self.spec, func_types)
+        return isinstance(self.spec, compat.func_types)
 
     def process(self):
         if self.value is None:

File monk/validation.py

 #      that we don't pass validators/skip_missing/skip_unknown recursively to
 #      each validator.
 
-import types
-
+from monk import compat
 from manipulation import merged
 
 
 
     """
     def check(self):
-        func_types = types.FunctionType, types.BuiltinFunctionType
-        return isinstance(self.spec, func_types)
+        return isinstance(self.spec, compat.func_types)
 
     def validate(self):
         spec = self.spec()

File unittests/test_validation.py

         validate_structure({'a': bson.DBRef},
                            {'a': bson.DBRef('a', 'b')})
 
+    def test_callable(self):
+        def func():
+            return 1
+
+        class Obj:
+            @staticmethod
+            def smeth():
+                return 1
+
+            @classmethod
+            def cmeth(cls):
+                return 1
+
+            def ometh(self):
+                return 1
+
+        validate_structure({'a': func}, {'a': 2})
+        validate_structure({'a': Obj.smeth}, {'a': 2})
+        validate_structure({'a': Obj.cmeth}, {'a': 2})
+        validate_structure({'a': Obj().ometh}, {'a': 2})
+
         with pytest.raises(TypeError):
+            validate_structure({'a': func}, {'a': 'foo'})
+        with pytest.raises(TypeError):
+            validate_structure({'a': Obj.smeth}, {'a': 'foo'})
+        with pytest.raises(TypeError):
+            validate_structure({'a': Obj.cmeth}, {'a': 'foo'})
+        with pytest.raises(TypeError):
+            validate_structure({'a': Obj().ometh}, {'a': 'foo'})
 
     def test_valid_document(self):
         "a complex document"