def test_no_config_file_found(): config_file = "/tmp/a-nonexistent-RAML-config-file.ini" with pytest.raises(IOError) as e: setup_config(config_file) msg = ("No such file or directory: '{0}'".format(config_file),) assert e.value.args == msg
def test_no_config_file_found(): config_file = "/tmp/a-nonexistent-RAML-config-file.ini" with pytest.raises(IOError) as e: setup_config(config_file) msg = ("No such file or directory: '{0}'".format(config_file), ) assert e.value.args == msg
def validate(raml, config_file=None): """ Module helper function to validate a RAML File. First loads \ the RAML file \ with :py:class:`.loader.RAMLLoader` then validates with \ :py:func:`.validate.validate_raml`. :param str raml: Either string path to the RAML file, a file object, \or a string representation of RAML. :param str config_file: String path to desired config file, if any. :return: No return value if successful :raises LoadRAMLError: If error occurred trying to load the RAML file (see :py:class:`.loader.RAMLLoader`) :raises InvalidRootNodeError: API metadata is invalid according to RAML \ `specification <http://raml.org/spec.html>`_. :raises InvalidResourceNodeError: API resource endpoint is invalid \ according to RAML `specification <http://raml.org/spec.html>`_. :raises InvalidParameterError: Named parameter is invalid \ according to RAML `specification <http://raml.org/spec.html>`_. :raises InvalidRAMLError: RAML file is invalid according to RAML \ `specification <http://raml.org/spec.html>`_. """ loader = load(raml) config = setup_config(config_file) config["validate"] = True parse_raml(loader, config)
def test_resource_type_empty_mapping_headers(): raml_file = os.path.join(EXAMPLES + "empty-mapping.raml") loaded_raml_file = load_file(raml_file) config = setup_config(EXAMPLES + "test-config.ini") config['validate'] = False api = pw.parse_raml(loaded_raml_file, config) base_res_type = api.resource_types[0] assert len(base_res_type.headers) == 3 assert base_res_type.headers[-1].description is None
def test_resource_response_no_desc(): raml_file = os.path.join(EXAMPLES + "empty-mapping.raml") loaded_raml_file = load_file(raml_file) config = setup_config(EXAMPLES + "test-config.ini") config['validate'] = False api = pw.parse_raml(loaded_raml_file, config) res = api.resources[-1] response = res.responses[-1] assert response.code == 204 assert response.description is None
def test_resource_type_empty_mapping(): raml_file = os.path.join(EXAMPLES + "empty-mapping-resource-type.raml") loaded_raml_file = load_file(raml_file) config = setup_config(EXAMPLES + "test-config.ini") config['validate'] = False api = pw.parse_raml(loaded_raml_file, config) assert len(api.resource_types) == 1 res = api.resource_types[0] assert res.name == "emptyType" assert res.raw == {}
def test_config_file(config, basic_config): bc = basic_config # lazy bc["resp_codes"] = _clean(bc["resp_codes"] + [420, 421, 422]) bc["auth_schemes"] = _clean(bc["auth_schemes"] + ["oauth_3_0", "oauth_4_0"]) bc["media_types"] = _clean(bc["media_types"] + ["application/vnd.github.v3", "foo/bar"]) bc["protocols"] = _clean(bc["protocols"] + ["FTP"]) bc["raml_versions"] = _clean(bc["raml_versions"] + ["0.8"]) bc["production"] = 'True' bc["validate"] = 'True' parsed_config = setup_config(config) assert _dict_equal(bc, parsed_config)
def test_config_file(config, basic_config): bc = basic_config # lazy bc["resp_codes"] = _clean(bc["resp_codes"] + [420, 421, 422]) bc["auth_schemes"] = _clean( bc["auth_schemes"] + ["oauth_3_0", "oauth_4_0"] ) bc["media_types"] = _clean( bc["media_types"] + ["application/vnd.github.v3", "foo/bar"] ) bc["protocols"] = _clean(bc["protocols"] + ["FTP"]) bc["raml_versions"] = _clean(bc["raml_versions"] + ["0.8"]) bc["production"] = 'True' bc["validate"] = 'True' parsed_config = setup_config(config) assert _dict_equal(bc, parsed_config)
def decode_raml(bytestring, base_url=None): loader = RAMLLoader(base_url) data = loader.load(bytestring) config = setup_config() config['validate'] = False raml = parse_raml(data, config) content = {} for resource in raml.resources: fields = [] encoding = '' for param in resource.uri_params or []: field = coreapi.Field(param.name, param.required, location='path') fields.append(field) for param in resource.query_params or []: field = coreapi.Field(param.name, param.required, location='query') fields.append(field) if resource.body: body = resource.body[0] encoding = body.mime_type for form_param in body.form_params or []: field = coreapi.Field(param.name, param.required, location='form') fields.append(field) if body.schema: schema_fields = expand_schema(body.schema) fields.extend(schema_fields) link = coreapi.Link(url=resource.absolute_uri, action=resource.method.lower(), encoding=encoding, fields=fields) content[resource.display_name] = link return coreapi.Document(title=raml.title, url=base_url, content=content)
def validate(raml, config_file=None): """ Module helper function to validate a RAML File. First loads \ the RAML file \ with :py:class:`.loader.RAMLLoader` then validates with \ :py:func:`.validate.validate_raml`. :param str raml: Either string path to the RAML file, a file object, \or a string representation of RAML. :param str config_file: String path to desired config file, if any. :return: No return value if successful :raises LoadRAMLError: If error occurred trying to load the RAML file (see :py:class:`.loader.RAMLLoader`) :raises InvalidRamlFileError: If error occurred trying to validate the RAML file (see :py:mod:`.validate`) """ loader = load(raml) config = setup_config(config_file) config["validate"] = True parse_raml(loader, config)
def parse(raml, config_file=None): """ Module helper function to parse a RAML File. First loads the RAML file with :py:class:`.loader.RAMLLoader` then parses with :py:func:`.parser.parse_raml` to return a :py:class:`.raml.RAMLRoot` object. :param raml: Either string path to the RAML file, a file object, or \ a string representation of RAML. :param str config_file: String path to desired config file, if any. :return: parsed API :rtype: RAMLRoot :raises LoadRAMLError: If error occurred trying to load the RAML file (see :py:class:`.loader.RAMLLoader`) :raises RAMLParserError: If error occurred during parsing of RAML file (see :py:class:`.raml.RAMLRoot`) :raises InvalidRamlFileError: RAML file is invalid according to RAML \ `specification <http://raml.org/spec.html>`_. """ loader = load(raml) config = setup_config(config_file) return parse_raml(loader, config)
def parse(raml, config_file=None): """ Module helper function to parse a RAML File. First loads the RAML file with :py:class:`.loader.RAMLLoader` then parses with :py:func:`.parser.parse_raml` to return a :py:class:`.raml.RAMLRoot` object. :param raml: Either string path to the RAML file, a file object, or \ a string representation of RAML. :param str config_file: String path to desired config file, if any. :return: parsed API :rtype: RAMLRoot :raises LoadRAMLError: If error occurred trying to load the RAML file (see :py:class:`.loader.RAMLLoader`) :raises InvalidRootNodeError: API metadata is invalid according to RAML \ `specification <http://raml.org/spec.html>`_. :raises InvalidResourceNodeError: API resource endpoint is invalid \ according to RAML `specification <http://raml.org/spec.html>`_. :raises InvalidParameterError: Named parameter is invalid \ according to RAML `specification <http://raml.org/spec.html>`_. """ loader = load(raml) config = setup_config(config_file) return parse_raml(loader, config)
def inherited_resources(): raml_file = os.path.join(EXAMPLES, "resource-type-inherited.raml") loaded_raml = load_file(raml_file) config = setup_config(EXAMPLES + "test-config.ini") config["validate"] = False return pw.parse_raml(loaded_raml, config)
def undef_uri_params_resources(): raml_file = os.path.join(EXAMPLES, "undefined-uri-params.raml") loaded_raml = load_file(raml_file) config = setup_config(EXAMPLES + "test-config.ini") config['validate'] = False return pw.parse_raml(loaded_raml, config)
def uri_param_resources(): raml_file = os.path.join(EXAMPLES, "preserve-uri-order.raml") loaded_raml = load_file(raml_file) config = setup_config(EXAMPLES + "test-config.ini") config['validate'] = False return pw.parse_raml(loaded_raml, config)
def root(): raml_file = os.path.join(EXAMPLES + "complete-valid-example.raml") loaded_raml_file = load_file(raml_file) config = setup_config(EXAMPLES + "test-config.ini") return pw.create_root(loaded_raml_file, config)
def api(raml): config_file = os.path.join(EXAMPLES, "twitter-config.ini") config = setup_config(config_file) return pw.parse_raml(raml, config)
def api(): raml_str = os.path.join(EXAMPLES, "simple-tree.raml") loaded_raml = load_file(raml_str) config_file = os.path.join(EXAMPLES + "test-config.ini") config = setup_config(config_file) return parser.parse_raml(loaded_raml, config)
def root(raml): config_file = os.path.join(EXAMPLES, "twitter-config.ini") config = setup_config(config_file) return pw.create_root(raml, config)
def md_includes(): raml_file = os.path.join(EXAMPLES + "md_includes.raml") loaded_raml_file = load_file(raml_file) config = setup_config(EXAMPLES + "test-config.ini") return pw.parse_raml(loaded_raml_file, config)
def resources(): raml_file = os.path.join(EXAMPLES + "complete-valid-example.raml") loaded_raml_file = load_file(raml_file) config = setup_config(EXAMPLES + "test-config.ini") api = pw.parse_raml(loaded_raml_file, config) return api.resources
def test_parse_raml(loaded_raml): config = setup_config(EXAMPLES + "test-config.ini") root = pw.parse_raml(loaded_raml, config) assert isinstance(root, RootNode)
def root(): raml_file = os.path.join(EXAMPLES, "github.raml") loaded_raml_file = load_file(raml_file) config_file = os.path.join(EXAMPLES, "github-config.ini") config = setup_config(config_file) return pw.create_root(loaded_raml_file, config)
def resource_protocol(): raml_file = os.path.join(EXAMPLES, "protocols.raml") loaded_raml = load_file(raml_file) config = setup_config(EXAMPLES + "test-config.ini") config['validate'] = False return pw.parse_raml(loaded_raml, config)
def test_no_config_file_given(basic_config): parsed_config = setup_config(config_file=None) assert parsed_config == basic_config
def test_parse_raml(raml): config_file = os.path.join(EXAMPLES, "twitter-config.ini") config = setup_config(config_file) root = pw.parse_raml(raml, config) assert isinstance(root, RootNode)
def load_config(filename): config_file = os.path.join(VALIDATE + filename) return setup_config(config_file)
def external_resource_with_multiple_methods(): raml_file = os.path.join( EXAMPLES + "external_resource_with_multiple_methods.raml") loaded_raml_file = load_file(raml_file) config = setup_config(EXAMPLES + "test-config.ini") return pw.parse_raml(loaded_raml_file, config)