Example #1
0
 def test_list_production(self):
     self.assertEqual(
         [1, 2, 3],
         c.list(c.String)([1, 2, 3], ctx=context.create(production_mode=True))
     )
     self.assertIsNone(
         c.list(c.String)(None, ctx=context.create(production_mode=True))
     )
Example #2
0
    def test_list_type_combinator_then(self):
        with self.assertRaises(TypeError):
            t.list()

        with util.throws_with_message(
            "Error on List(Struct{x: Number, y: Number})[0]: expected Struct{x: Number, y: Number} but was int",
            "Error on List(Struct{y: Number, x: Number})[0]: expected Struct{y: Number, x: Number} but was int",
        ):
            t.list(self.Point)([1])
Example #3
0
    def test_tuple(self):
        c.list(c.String)('hello')

        with(self.assertRaises(exceptions.PyCombValidationError)):
            c.list(c.String)((1, 2, 3))

        l = c.list(c.String)(['a', 'b'])
        self.assertEqual(tuple, type(l))
        self.assertEqual(2, len(l))
        self.assertEqual('a', l[0])
        self.assertEqual('b', l[1])
Example #4
0
    def setUp(self):
        self.Point = t.struct({"x": t.Number, "y": t.Number})

        self.MyElement = t.struct({}, "MyElement")
        self.MyList = t.list(self.MyElement, "MyList")
        self.ListOfNumbers = t.list(t.Number, "ListOfNumbers")
        self.Path = t.list(self.Point, "Path")

        self.PathOfPoint = t.list(self.Point)
        self.p1 = self.Point({"x": 0, "y": 0})
        self.p2 = self.Point({"x": 1, "y": 1})
Example #5
0
    def test_tuple_custom_error(self):
        observer = Mock()
        ctx = context.create(validation_error_observer=observer)
        c.list(c.String)('hello')

        c.list(c.String)((1, 2, 3), ctx=ctx)
        observer.on_error.assert_has_calls(
            [
                mock.call(_ANY_CONTEXT, 'String', int),
                mock.call(_ANY_CONTEXT, 'String', int),
                mock.call(_ANY_CONTEXT, 'String', int)
            ]
        )
Example #6
0
    def test_list(self):
        c.list(c.String)('hello')
        with(self.assertRaises(exceptions.PyCombValidationError)):
            c.list(c.String)([1, 2, 3])
        self.assertFalse(c.list(c.String).is_type([1, 2, 3]))

        with self.assertRaises(exceptions.PyCombValidationError) as e:
            c.list(c.String)(['1', 2, '3'])
        e = e.exception
        expected = 'Error on List(String)[1]: expected String but was int'
        self.assertEqual(expected, e.args[0])
        l = c.list(c.String)(['a', 'b'])
        self.assertEqual(tuple, type(l))
        self.assertEqual(2, len(l))
        self.assertEqual('a', l[0])
        self.assertEqual('b', l[1])
        self.assertTrue(c.list(c.String).is_type(['a', 'b']))
Example #7
0
    def test(self):
        # noinspection PyUnusedLocal
        @cmb.function(cmb.String, cmb.Int, c=cmb.Float, d=cmb.list(cmb.Int))
        def f(a, b, c=None, d=None):
            pass

        f('John', 1, c=1.0, d=[3, 4])

        with(self.assertRaises(exceptions.PyCombValidationError)):
            f(1, 1, c=1.0, d=[3, 4])

        with(self.assertRaises(exceptions.PyCombValidationError)):
            f('John', 1, c=1.0, d=['3', 4])
Example #8
0
 def test_list_custom_error(self):
     observer = Mock()
     ctx = context.create(validation_error_observer=observer)
     c.list(c.String)('hello', ctx=ctx)
     self.assertEqual(0, observer.on_error.call_count)
     c.list(c.String)([1, 2, 3], ctx=ctx)
     observer.on_error.assert_has_calls(
         [
             mock.call(_ANY_CONTEXT, 'String', int),
             mock.call(_ANY_CONTEXT, 'String', int),
             mock.call(_ANY_CONTEXT, 'String', int)
         ])
     self.assertFalse(c.list(c.String).is_type([1, 2, 3]))
     observer.reset_mock()
     c.list(c.String)(['1', 2, '3'], ctx=ctx)
     observer.on_error.assert_called_once_with(_ANY_CONTEXT, 'String', int)
     observer.reset_mock()
     self.assertIsNone(
         c.list(c.String)(None, ctx=ctx)
     )
     observer.on_error.assert_called_once_with(_ANY_CONTEXT, 'List', type(None))
Example #9
0
    def test_struct_of_list(self):
        my_struct = c.struct(
            {
                'l': c.list(c.String)
            }
        )

        my_struct({'l': ['1', '2', '3']})
        with self.assertRaises(exceptions.PyCombValidationError) as e:
            my_struct({'l': ['1', '2', 3]})
        self.assertEqual(
            'Error on Struct{l: List(String)}[l][2]: expected String but was int',
            e.exception.args[0])

        with self.assertRaises(exceptions.PyCombValidationError) as e:
            my_struct({'w': ['1', '2', '3']})
        self.assertEqual(
            'Error on Struct{l: List(String)}[l]: expected List but was NoneType',
            e.exception.args[0])
Example #10
0
    def test_struct_list(self):
        r = c.list(c.struct({'name': c.String}))([{'name': 'John'}])
        self.assertEqual(1, len(r))

        self.assertEqual(StructType, type(r[0]))