示例#1
0
def test_invalid_top_level_item():
    with pytest.raises(ValidationError) as exc:
        parse_json('123', VALIDATOR)

    error_messages = exc.value.get_error_messages()
    expecting = [ErrorMessage('Must be an object.', Marker(0))]
    assert error_messages == expecting
示例#2
0
def test_object_unterminated_after_key():
    with pytest.raises(ParseError) as exc:
        parse_json('{"abc": ', VALIDATOR)

    error_messages = exc.value.get_error_messages()
    expecting = [ErrorMessage("Expecting value.", Marker(8))]
    assert error_messages == expecting
示例#3
0
def test_invalid_token():
    with pytest.raises(ParseError) as exc:
        parse_json('-', VALIDATOR)

    error_messages = exc.value.get_error_messages()
    expecting = [ErrorMessage("Expecting value.", Marker(0))]
    assert error_messages == expecting
示例#4
0
def test_object_unterminated_after_value():
    with pytest.raises(ParseError) as exc:
        parse_json('{"abc": "def"', VALIDATOR)

    error_messages = exc.value.get_error_messages()
    expecting = [ErrorMessage("Expecting ',' delimiter.", Marker(13))]
    assert error_messages == expecting
示例#5
0
def test_object_missing_comma_delimiter():
    with pytest.raises(ParseError) as exc:
        parse_json('{"abc": "def" 1', VALIDATOR)

    error_messages = exc.value.get_error_messages()
    expecting = [ErrorMessage("Expecting ',' delimiter.", Marker(14))]
    assert error_messages == expecting
示例#6
0
def test_empty_string():
    with pytest.raises(ParseError) as exc:
        parse_json(b'', VALIDATOR)

    error_messages = exc.value.get_error_messages()
    expecting = [ErrorMessage('No content.', Marker(0))]
    assert error_messages == expecting
示例#7
0
def test_invalid_property():
    with pytest.raises(ValidationError) as exc:
        parse_json('{"a": "abc"}', VALIDATOR)

    error_messages = exc.value.get_error_messages()
    expecting = [ErrorMessage('Must be a number.', Marker(6))]
    assert error_messages == expecting
示例#8
0
def test_object_invalid_property_name():
    with pytest.raises(ParseError) as exc:
        parse_json('{"abc": "def", 1', VALIDATOR)

    error_messages = exc.value.get_error_messages()
    expecting = [
        ErrorMessage("Expecting property name enclosed in double quotes.",
                     Marker(15))
    ]
    assert error_messages == expecting
示例#9
0
def test_object_missing_property_name():
    with pytest.raises(ParseError) as exc:
        parse_json('{', VALIDATOR)

    error_messages = exc.value.get_error_messages()
    expecting = [
        ErrorMessage('Expecting property name enclosed in double quotes.',
                     Marker(1))
    ]
    assert error_messages == expecting
示例#10
0
    def decode(self, content, **options):
        base_format = infer_json_or_yaml(content)
        if base_format == 'json':
            data = parse_json(content, validator=OPEN_API)
        else:
            data = parse_yaml(content, validator=OPEN_API)

        title = lookup(data, ['info', 'title'])
        description = lookup(data, ['info', 'description'])
        version = lookup(data, ['info', 'version'])
        base_url = lookup(data, ['servers', 0, 'url'])
        schema_definitions = self.get_schema_definitions(data)
        content = self.get_content(data, base_url, schema_definitions)

        return Document(title=title,
                        description=description,
                        version=version,
                        url=base_url,
                        content=content)
示例#11
0
    def decode(self, content, **options):
        base_format = infer_json_or_yaml(content)
        if base_format == 'json':
            data = parse_json(content, validator=SWAGGER)
        else:
            data = parse_yaml(content, validator=SWAGGER)

        title = lookup(data, ['info', 'title'])
        description = lookup(data, ['info', 'description'])
        version = lookup(data, ['info', 'version'])
        host = lookup(data, ['host'])
        path = lookup(data, ['basePath'], '/')
        scheme = lookup(data, ['schemes', 0], 'https')
        base_url = None
        if host:
            base_url = '%s://%s%s' % (scheme, host, path)
        schema_definitions = self.get_schema_definitions(data)
        content = self.get_content(data, base_url, schema_definitions)
        return Document(title=title,
                        description=description,
                        version=version,
                        url=base_url,
                        content=content)
示例#12
0
def test_valid_json():
    parse_json('{"abc": "def"}')