From e2c971cb465f822218e45f0beb116d8ea0418f93 Mon Sep 17 00:00:00 2001 From: Teo Stocco Date: Sun, 31 Jul 2022 18:49:46 +0200 Subject: [PATCH] chore: stabilize tests on main --- poetry.lock | 57 ++++++++++++++++++++++++++++----------------- pyproject.toml | 1 + tests/conftest.py | 14 +++++++++++ tests/test_parse.py | 43 +++++++++++++++++----------------- 4 files changed, 73 insertions(+), 42 deletions(-) create mode 100644 tests/conftest.py diff --git a/poetry.lock b/poetry.lock index 83beae8..20354b1 100644 --- a/poetry.lock +++ b/poetry.lock @@ -26,9 +26,9 @@ optional = false python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*" [package.extras] -dev = ["coverage[toml] (>=5.0.2)", "hypothesis", "pympler", "pytest (>=4.3.0)", "six", "mypy", "pytest-mypy-plugins", "zope-interface", "furo", "sphinx", "sphinx-notfound-page", "pre-commit", "cloudpickle"] -docs = ["furo", "sphinx", "zope-interface", "sphinx-notfound-page"] -tests = ["coverage[toml] (>=5.0.2)", "hypothesis", "pympler", "pytest (>=4.3.0)", "six", "mypy", "pytest-mypy-plugins", "zope-interface", "cloudpickle"] +dev = ["coverage[toml] (>=5.0.2)", "hypothesis", "pympler", "pytest (>=4.3.0)", "six", "mypy", "pytest-mypy-plugins", "zope.interface", "furo", "sphinx", "sphinx-notfound-page", "pre-commit", "cloudpickle"] +docs = ["furo", "sphinx", "zope.interface", "sphinx-notfound-page"] +tests = ["coverage[toml] (>=5.0.2)", "hypothesis", "pympler", "pytest (>=4.3.0)", "six", "mypy", "pytest-mypy-plugins", "zope.interface", "cloudpickle"] tests_no_zope = ["coverage[toml] (>=5.0.2)", "hypothesis", "pympler", "pytest (>=4.3.0)", "six", "mypy", "pytest-mypy-plugins", "cloudpickle"] [[package]] @@ -153,9 +153,6 @@ category = "dev" optional = false python-versions = ">=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5.*,!=3.6.*" -[package.dependencies] -setuptools = "*" - [[package]] name = "packaging" version = "21.3" @@ -269,6 +266,22 @@ tomli = ">=1.0.0" [package.extras] testing = ["argcomplete", "hypothesis (>=3.56)", "mock", "nose", "pygments (>=2.7.2)", "requests", "xmlschema"] +[[package]] +name = "pytest-httpserver" +version = "1.0.5" +description = "pytest-httpserver is a httpserver for pytest" +category = "dev" +optional = false +python-versions = ">=3.6,<4.0" + +[package.dependencies] +Werkzeug = ">=2.0.0" + +[package.extras] +dev = ["types-requests", "mypy", "reno", "sphinx-rtd-theme", "sphinx", "requests", "ipdb", "coverage", "pytest-cov", "pytest"] +test = ["types-requests", "mypy", "requests", "coverage", "pytest-cov", "pytest"] +doc = ["sphinx-rtd-theme", "sphinx"] + [[package]] name = "python-dateutil" version = "2.8.2" @@ -302,19 +315,6 @@ prompt_toolkit = ">=2.0,<4.0" [package.extras] docs = ["Sphinx (>=3.3,<4.0)", "sphinx-rtd-theme (>=0.5.0,<0.6.0)", "sphinx-autobuild (>=2020.9.1,<2021.0.0)", "sphinx-copybutton (>=0.3.1,<0.4.0)", "sphinx-autodoc-typehints (>=1.11.1,<2.0.0)"] -[[package]] -name = "setuptools" -version = "63.2.0" -description = "Easily download, build, install, upgrade, and uninstall Python packages" -category = "dev" -optional = false -python-versions = ">=3.7" - -[package.extras] -docs = ["sphinx", "jaraco.packaging (>=9)", "rst.linker (>=1.9)", "jaraco.tidelift (>=1.4)", "pygments-github-lexers (==0.0.5)", "sphinx-favicon", "sphinx-inline-tabs", "sphinx-reredirects", "sphinxcontrib-towncrier", "furo"] -testing = ["pytest (>=6)", "pytest-checkdocs (>=2.4)", "pytest-flake8", "pytest-enabler (>=1.3)", "pytest-perf", "mock", "flake8-2020", "virtualenv (>=13.0.0)", "wheel", "pip (>=19.1)", "jaraco.envs (>=2.2)", "pytest-xdist", "jaraco.path (>=3.2.0)", "build", "filelock (>=3.4.0)", "pip-run (>=8.8)", "ini2toml[lite] (>=0.9)", "tomli-w (>=1.0.0)", "pytest-black (>=0.3.7)", "pytest-cov", "pytest-mypy (>=0.9.1)"] -testing-integration = ["pytest", "pytest-xdist", "pytest-enabler", "virtualenv (>=13.0.0)", "tomli", "wheel", "jaraco.path (>=3.2.0)", "jaraco.envs (>=2.2)", "build", "filelock (>=3.4.0)"] - [[package]] name = "six" version = "1.16.0" @@ -388,10 +388,24 @@ category = "dev" optional = false python-versions = "*" +[[package]] +name = "werkzeug" +version = "2.2.1" +description = "The comprehensive WSGI web application library." +category = "dev" +optional = false +python-versions = ">=3.7" + +[package.dependencies] +MarkupSafe = ">=2.1.1" + +[package.extras] +watchdog = ["watchdog"] + [metadata] lock-version = "1.1" python-versions = "^3.8" -content-hash = "ce6b55b506d56e361180b78a8e293836d10d13b1df8a8560d89066aed5315899" +content-hash = "684c110642dfa8909b10473e10666f91015f097103c334d31d293a7188922257" [metadata.files] argcomplete = [] @@ -418,10 +432,10 @@ py = [] pyhocon = [] pyparsing = [] pytest = [] +pytest-httpserver = [] python-dateutil = [] pyyaml = [] questionary = [] -setuptools = [] six = [] termcolor = [] toml = [] @@ -430,3 +444,4 @@ tomlkit = [] typing-extensions = [] virtualenv = [] wcwidth = [] +werkzeug = [] diff --git a/pyproject.toml b/pyproject.toml index 84b67eb..adaf806 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -28,6 +28,7 @@ PyYAML = "^6.0" pytest = "^7.1.2" pre-commit = "^2.20.0" commitizen = "^2.29.2" +pytest-httpserver = "^1.0.5" [tool.commitizen] name = "cz_conventional_commits" diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..561fa5c --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,14 @@ +import mimetypes + +from werkzeug.wrappers import Request +from werkzeug.wrappers import Response + + +def file_handler(path: str): + with open("confs/simple.json", "r") as f: + data = f.read() + + def handler(request: Request): + return Response(data, mimetype=mimetypes.guess_type(path)[0]) + + return handler diff --git a/tests/test_parse.py b/tests/test_parse.py index a8f9039..d5f803e 100644 --- a/tests/test_parse.py +++ b/tests/test_parse.py @@ -25,6 +25,8 @@ from dataconf.main import url from dateutil.relativedelta import relativedelta import pytest +from pytest_httpserver import HTTPServer +from tests.conftest import file_handler PARENT_DIR = os.path.normpath( os.path.dirname(os.path.realpath(__file__)) + os.sep + os.pardir @@ -602,27 +604,21 @@ class A: assert file("confs/simple.yaml", A) == A(hello="bonjour", foo=["bar"]) - def test_yaml_url(self) -> None: - @dataclass - class Repo: - repo: str - rev: str - + def test_yaml_url(self, httpserver: HTTPServer) -> None: @dataclass class A: - repos: List[Repo] - - assert ( - len( - url( - "https://raw.githubusercontent.com/zifeo/dataconf/main/.pre-commit-config.yaml", - A, - ignore_unexpected=True, - ).repos - ) - > 0 + hello: Text + foo: List[str] + + httpserver.expect_request("/simple.yaml").respond_with_handler( + file_handler("confs/simple.yaml") ) + assert url( + httpserver.url_for("/simple.yaml"), + A, + ) == A(hello="bonjour", foo=["bar"]) + def test_json_file(self) -> None: @dataclass class A: @@ -631,11 +627,16 @@ class A: assert file("confs/simple.json", A) == A(hello="bonjour", foo=["bar"]) - def test_json_url(self) -> None: + def test_json_url(self, httpserver: HTTPServer) -> None: @dataclass class A: hello: Text + foo: List[str] - assert url( - "https://raw.githubusercontent.com/zifeo/dataconf/main/confs/simple.json", A - ) == A(hello="bonjour") + httpserver.expect_request("/simple.json").respond_with_handler( + file_handler("confs/simple.json") + ) + + assert url(httpserver.url_for("/simple.json"), A) == A( + hello="bonjour", foo=["bar"] + )