def test_constraints_maxlength_valid_value_equals(self):
        '''maxLength with valid value equal to each other'''
        value = {'a': 1, 'b': 2, 'c': 3}
        field = self._make_default_field(type='object',
                                         constraints={'maxLength': 3})
        _type = types.ObjectType(field)

        self.assertEqual(_type.cast(value), value)
    def test_constraints_minlength_valid_value(self):
        '''minLength with valid value'''
        value = {'a': 1, 'b': 2, 'c': 3}
        field = self._make_default_field(type='object',
                                         constraints={'minLength': 2})
        _type = types.ObjectType(field)

        self.assertEqual(_type.cast(value), value)
    def test_constraints_pattern_valid_value(self):
        '''value is valid for pattern'''
        value = '{"a":1, "b":2, "c":3}'
        field = self._make_default_field(
            type='object',
            constraints={"pattern": '\{("[a-z]":[0-9],?\s?)*\}'})

        _type = types.ObjectType(field)

        self.assertEqual(_type.cast(value), json.loads(value))
    def test_constraints_maxlength_invalid_value(self):
        '''maxLength with invalid value'''
        value = {'a': 1, 'b': 2, 'c': 3}
        field = self._make_default_field(type='object',
                                         constraints={'maxLength': 2})
        _type = types.ObjectType(field)

        with pytest.raises(exceptions.ConstraintError) as e:
            _type.cast(value)
        self.assertEqual(e.value.msg,
                         "The field 'Name' must have a maximum length of 2")
    def test_constraints_pattern_invalid_value(self):
        '''value is invalid for pattern'''
        value = '{"a":"fred", "b":2, "c":3}'
        field = self._make_default_field(
            type='object',
            constraints={"pattern": '\{("[a-z]":[0-9],?\s?)*\}'})

        _type = types.ObjectType(field)

        with pytest.raises(exceptions.ConstraintError) as e:
            _type.cast(value)
        self.assertEqual(
            e.value.msg, "The value for field 'Name' "
            "must match the pattern")
    def test_constraints_enum_valid_value(self):
        '''value is in enum array'''
        value = {'a': 'first', 'b': 'second', 'c': 'third'}
        field = self._make_default_field(type='object',
                                         constraints={
                                             'enum': [{
                                                 'a': 'first',
                                                 'b': 'second',
                                                 'c': 'third'
                                             }]
                                         })

        _type = types.ObjectType(field)

        self.assertEqual(_type.cast(value), value)
    def test_constraints_enum_valid_value_different_order(self):
        '''value is in enum array. Same members in each array, but different
        order.'''
        value = {'a': 'first', 'b': 'second', 'c': 'third'}
        field = self._make_default_field(type='object',
                                         constraints={
                                             'enum': [{
                                                 'a': 'first',
                                                 'c': 'third',
                                                 'b': 'second'
                                             }],
                                         })

        _type = types.ObjectType(field)

        self.assertEqual(_type.cast(value), value)
    def test_constraints_enum_invalid_value(self):
        '''value is not in enum array'''
        value = {'a': 'first', 'b': 'second', 'c': 'third'}
        field = self._make_default_field(
            type='object',
            constraints={'enum': [{
                'a': 'fred',
                'b': 'alice',
                'c': 'bob'
            }]})

        _type = types.ObjectType(field)

        with pytest.raises(exceptions.ConstraintError) as e:
            _type.cast(value)
        self.assertEqual(
            e.value.msg, "The value for field 'Name' "
            "must be in the enum array")
    def test_invalid(self):
        value = ['boo', 'ya']
        _type = types.ObjectType(self.field)

        self.assertRaises(exceptions.InvalidObjectType, _type.cast, value)
    def test_json_string(self):
        value = '{"key": "value"}'
        _type = types.ObjectType(self.field)

        self.assertDictEqual(_type.cast(value), {'key': 'value'})
    def test_dict(self):
        value = {'key': 'value'}
        _type = types.ObjectType(self.field)

        self.assertDictEqual(_type.cast(value), value)