Example #1
0
    def test_intersection_production(self):
        name_type = c.struct({'name': c.String})
        age_type = c.struct({'age': c.Int})
        my_type = c.intersection(name_type, age_type)

        base_dict = {'name': 'mirko', 'age': '36'}
        self.assertEqual(base_dict, my_type(base_dict, ctx=context.create(production_mode=True)))
Example #2
0
 def test_intersection_custom_error(self):
     name_type = c.struct({'name': c.String})
     age_type = c.struct({'age': c.Int})
     my_type = c.intersection(name_type, age_type)
     observer = Mock()
     ctx = context.create(validation_error_observer=observer)
     my_type({'name': 'mirko', 'age': '36'}, ctx=ctx)
     observer.on_error.assert_called_once_with(_ANY_CONTEXT, 'Struct{name: String} or Struct{age: Int}', dict)
Example #3
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 #4
0
 def test_struct_custom_error(self):
     observer = Mock()
     ctx = context.create(validation_error_observer=observer)
     d = {'name': c.String, 'value': c.Int}
     c.struct(d)('hello', ctx=ctx)
     call = observer.on_error.call_args_list
     self.assertEqual(1, len(call))
     call = call[0]
     self.assertTrue(
         call == mock.call(_ANY_CONTEXT, 'Struct{value: Int, name: String}', str) or \
         call == mock.call(_ANY_CONTEXT, 'Struct{name: String, value: Int}', str),
         msg=str(call)
     )
Example #5
0
    def test_named_intersection(self):
        name_type = c.struct({'name': c.String})
        age_type = c.struct({'age': c.Int})
        my_type = c.intersection(name_type, age_type, name='MyType')

        d = my_type({'name': 'mirko', 'age': 36})
        self.assertEqual('mirko', d.name)

        with self.assertRaises(exceptions.PyCombValidationError) as e:
            my_type({'name': 'mirko', 'age': '36'})
        e = e.exception
        self.assertEqual(
            'Error on MyType: '
            'expected Struct{name: String} or Struct{age: Int} but was dict',
            e.args[0])
Example #6
0
    def test_intersection_dispatcher(self):
        name_type = c.struct({'name': c.String})
        age_type = c.struct({'age': c.Int})
        my_type = c.intersection(name_type, age_type, dispatcher=lambda x: age_type)

        d = my_type({'name': 'mirko', 'age': 36})
        self.assertEqual(36, d.age)

        with self.assertRaises(exceptions.PyCombValidationError) as e:
            my_type({'name': 'mirko', 'age': '36'})
        e = e.exception
        self.assertEqual(
            'Error on Intersection(Struct{name: String}, Struct{age: Int}): '
            'expected Struct{name: String} or Struct{age: Int} but was dict',
            e.args[0])
Example #7
0
    def test_struct_of_struct(self):
        r = c.struct({'name': c.String, 'data': c.struct({'age': c.Int})})({'name': 'Mirko', 'data': {'age': 36}})

        self.assertEqual('Mirko', r.name)
        self.assertEqual(36, r.data.age)

        with self.assertRaises(exceptions.PyCombValidationError) as exception:
            c.struct({'name': c.String, 'data': c.struct({'age': c.Int})})({'name': 'Mirko', 'data': {'age': '36'}})
        exception = exception.exception
        self.assertTrue(
            exception.args[0] in (
                'Error on Struct{data: Struct{age: Int}, name: String}[data][age]: expected Int but was str',
                'Error on Struct{name: String, data: Struct{age: Int}}[data][age]: expected Int but was str',
            ),
            msg=exception.args[0])
Example #8
0
 def test_struct_production(self):
     my_struct = c.struct({
         'a': c.Int, 'b': c.String
     })
     base_dict = {'a': 'hello', 'b': 'world'}
     s = my_struct(base_dict, ctx=context.create(production_mode=True))
     self.assertEqual(base_dict, s)
Example #9
0
    def test_struct(self):
        d = {'name': c.String, 'value': c.Int}
        with(self.assertRaises(exceptions.PyCombValidationError)):
            c.struct(d)('hello')

        with self.assertRaises(exceptions.PyCombValidationError) as e:
            c.struct(d)({'name': 0, 'value': 1})

        e = e.exception
        self.assertTrue(
            e.args[0] in
            ('Error on Struct{name: String, value: Int}[name]: expected String but was int',
             'Error on Struct{value: Int, name: String}[name]: expected String but was int'),
            msg=e.args[0])

        result = c.struct(d)({'name': 'myName', 'value': 1})
        self.assertEqual('myName', result.name)

        with(self.assertRaises(TypeError)):
            result.name = 'anotherName'
Example #10
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 #11
0
    def test_struct_maybe(self):
        r = c.maybe(c.struct({'name': c.String}))({'name': 'John'})

        self.assertEqual(StructType, type(r))
Example #12
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]))
Example #13
0
 def test_struct_maybe_field(self):
     User = c.struct({'name': c.String, 'age': c.Int, 'city': c.maybe(c.String)})
     User({'name': 'John Burns', 'age': 30})
Example #14
0
            return fun(*a, params=p, **kw)

        return wrapper

    return decorator


UUIDValidator = validators.subtype(validators.String, is_uuid)

SplitProtocolValidator = validators.subtype(validators.String,
                                            lambda x: x in ['fxc1'])

SplitSessionValidator = validators.struct({
    "secret":
    validators.struct({
        "value": validators.String,
        "protocol": validators.maybe(SplitProtocolValidator),
    })
})

SplitSessionCreateValidator = validators.struct(
    {
        "client_alias":
        validators.String,
        "session_alias":
        validators.String,
        "session_policies":
        validators.struct({
            "shares": validators.Int,
            "quorum": validators.Int
        })