Skip to content

test suites refactor #165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 7 additions & 18 deletions openapi_spec_validator/schemas.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,14 @@
"""OpenAIP spec validator schemas module."""
import os
import urllib.parse
import urllib.request
from os import path

import importlib_resources

from jsonschema_spec.handlers.compat import SafeLoader
from jsonschema_spec.handlers.file import FileHandler
from jsonschema_spec.readers import FilePathReader


def get_openapi_schema(version):
path = 'resources/schemas/v{0}/schema.json'.format(version)
ref = importlib_resources.files('openapi_spec_validator') / path
with importlib_resources.as_file(ref) as path_resource:
path_full = os.path.join(os.path.dirname(__file__), path_resource)
schema = read_yaml_file(path_full)
schema_url = urllib.parse.urljoin('file:', urllib.request.pathname2url(path_full))
return schema, schema_url


def read_yaml_file(path, loader=SafeLoader):
"""Open a file, read it and return its contents."""
with open(path) as fh:
return FileHandler(loader=loader)(fh)
schema_path = 'resources/schemas/v{0}/schema.json'.format(version)
ref = importlib_resources.files('openapi_spec_validator') / schema_path
with importlib_resources.as_file(ref) as resource_path:
schema_path_full = path.join(path.dirname(__file__), resource_path)
return FilePathReader(schema_path_full).read()
24 changes: 12 additions & 12 deletions tests/integration/conftest.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
from os import path
from pathlib import PurePath
from urllib.parse import urlunparse

from jsonschema_spec.handlers.file import FilePathHandler
from jsonschema_spec.handlers.urllib import UrllibHandler
import pytest
from urllib import request
from urllib.parse import urlunparse
from yaml import safe_load

from openapi_spec_validator import (openapi_v2_spec_validator,
openapi_v30_spec_validator,
openapi_v31_spec_validator)
from openapi_spec_validator.schemas import read_yaml_file
from openapi_spec_validator import openapi_v2_spec_validator
from openapi_spec_validator import openapi_v30_spec_validator
from openapi_spec_validator import openapi_v31_spec_validator


def spec_url(spec_file, schema='file'):
def spec_file_url(spec_file, schema='file'):
directory = path.abspath(path.dirname(__file__))
full_path = path.join(directory, spec_file)
return urlunparse((schema, None, full_path, None, None, None))
Expand All @@ -20,12 +20,12 @@ def spec_url(spec_file, schema='file'):
def spec_from_file(spec_file):
directory = path.abspath(path.dirname(__file__))
path_full = path.join(directory, spec_file)
return read_yaml_file(path_full)
uri = PurePath(path_full).as_uri()
return FilePathHandler()(uri)


def spec_from_url(spec_url):
content = request.urlopen(spec_url)
return safe_load(content)
return UrllibHandler("http", "https")(spec_url)


class Factory(dict):
Expand All @@ -36,7 +36,7 @@ class Factory(dict):
@pytest.fixture
def factory():
return Factory(
spec_url=spec_url,
spec_file_url=spec_file_url,
spec_from_file=spec_from_file,
spec_from_url=spec_from_url,
)
Expand Down
1 change: 1 addition & 0 deletions tests/integration/data/v2.0/empty.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
swagger: "2.0"
Loading