def test_parse_value_errors(self):
     """These cases should raise an error when parsing."""
     tests = {'array': ['{}', '['],
              'boolean': ['0', '1'],
              'float': ['bad'],
              'integer': ['bad', '0.1'],
              'object': ['[]', '{']}
     for allowed_type, bad_values in list(tests.items()):
         for bad_value in bad_values:
             with pytest.raises(ParseError):
                 parse_value(bad_value, [allowed_type])
Exemple #2
0
    def test_parse_value(self):
        """These cases should return a value when parsing."""
        tests = [
            ('', ['null'], None),
            ('true', ['boolean'], True),
            ('True', ['boolean'], True),
            ('TRUE', ['boolean'], True),
            ('false', ['boolean'], False),
            ('False', ['boolean'], False),
            ('FALSE', ['boolean'], False),
            ('0', ['integer'], 0),
            ('1', ['integer'], 1),
            ('-1', ['integer'], -1),
            ('0', ['number'], 0.0),
            ('0.5', ['number'], 0.5),
            ('[]', ['array'], []),
            ('[0]', ['array'], [0]),
            ('["0", 0]', ['array'], ['0', 0]),
            ('{}', ['object'], {}),
            ('{"a": 1}', ['object'], {
                'a': 1
            }),
            ('foo', ['string'], 'foo'),
            ('"foo"', ['string'], '"foo"'),
            (u'foø', ['string'], u'foø'.encode('utf-8')),

            # ambiguous test cases
            ('', ['string', 'null'], None),
            ('', ['boolean', 'string'], ''),
            ('', ['boolean', 'string', 'null'], None),
            ('true', ['string', 'boolean'], True),
            ('false', ['string', 'boolean'], False),
            ('0', ['string', 'float', 'integer'], 0),
            ('1', ['string', 'float', 'integer'], 1),
            ('[]', ['string', 'object', 'array'], []),
            ('  []', ['string', 'object', 'array'], []),
            ('{}', ['string', 'array', 'object'], {}),
            ('    {}', ['string', 'array', 'object'], {}),
        ]
        for value, allowed_types, expected_value in tests:
            expected = (allowed_types[-1], expected_value)
            self.assertEqual(parse_value(value, allowed_types), expected)
    def __new__(cls, value):
        # Attempt to parse the value if it came from a query string
        try:
            _, value = parse_value(value, [cls.json_type])
        except ValueError:
            pass
        request_schema = None
        if cls.definition_key is not None:
            params = [cls.definition_key]
            request_schema = cls.schema._create_request_schema(params, params)
            data = {cls.definition_key: value}
        else:
            data = value

        super().__new__(cls)
        # Validate the data against the schema and raise an error if it
        # does not validate.
        validator = cls.schema.get_validator(request_schema)
        try:
            cls.schema.validate(data, validator)
        except SchemaValidationError as e:
            raise TypeSystemError(e.args[0], cls=cls)

        return value
 def test_parse_value_allowed_types_is_str(self):
     assert ('integer', 12) == parse_value('12', allowed_types='integer')
 def test_parse_value_error_not_string(self):
     """Should raise an error if the value isn't a string."""
     with pytest.raises(
             ValueError, match=r"value for 'foo' must be a string"):
         parse_value(1337, [], 'foo')
Exemple #6
0
 def test_parse_value_error_not_string(self):
     """Should raise an error if the value isn't a string."""
     with self.assertRaisesRegexp(ValueError,
                                  r"value for 'foo' must be a string"):
         parse_value(1337, [], 'foo')