Ejemplo n.º 1
0
 def test_object_production(self):
     class TestClass(object):
         def __init__(self, f1, f2):
             self.f1 = f1
             self.f2 = f2
     t = TestClass('hello', 10)
     type1 = generic_object({'f1': Int, 'f2': Int}, TestClass)
     self.assertEqual(t, type1(t, ctx=context.create(production_mode=True)))
Ejemplo n.º 2
0
    def test_object_custom_error(self):
        observer = Mock()
        ctx = context.create(validation_error_observer=observer)

        class TestClass(object):
            def __init__(self, f1, f2):
                self.f1 = f1
                self.f2 = f2

        t = TestClass('hello', 10)

        type1 = generic_object({'f1': Int, 'f2': Int}, TestClass)
        type1(t, ctx=ctx)
        observer.on_error.assert_called_once_with(_ANY_CONTEXT, 'Int', str)
Ejemplo n.º 3
0
    def test_object(self):
        class TestClass(object):
            def __init__(self, f1, f2):
                self.f1 = f1
                self.f2 = f2

        t = TestClass('hello', 10)

        type1 = generic_object({'f1': Int, 'f2': Int}, TestClass)

        with self.assertRaises(exceptions.PyCombValidationError) as e:
            type1(t)
        e = e.exception
        self.assertEqual(
            'Error on TestClass.f1: expected Int but was str',
            e.args[0])
        self.assertFalse(type1.is_type(t))
        t = TestClass(20, 3.5)

        with self.assertRaises(exceptions.PyCombValidationError) as e:
            type1(t)
        e = e.exception
        self.assertEqual(
            'Error on TestClass.f2: expected Int but was float',
            e.args[0])
        self.assertFalse(type1.is_type(t))
        with self.assertRaises(exceptions.PyCombValidationError) as e:
            type1('hello')
        e = e.exception
        self.assertEqual(
            'Error on TestClass: expected TestClass but was str',
            e.args[0])
        self.assertFalse(type1.is_type(t))
        t = TestClass(20, 3)

        type1(t)
        self.assertTrue(type1.is_type(t))