Skip to content
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

14 rework schematpy for more unit testing #15

Merged
merged 3 commits into from
Nov 19, 2024
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
3 changes: 2 additions & 1 deletion sedr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
__license__ = "GPL-3.0"
__version__ = "v0.7.5"

import sys
import pytest
import util

Expand All @@ -14,7 +15,7 @@ def main():


# Handle --version and --help
util.args = util.parse_args(__version__)
util.args = util.parse_args(sys.argv[1:], __version__)
util.logger = util.set_up_logging(
args=util.args, logfile=util.args.log_file, version=__version__
)
Expand Down
6 changes: 3 additions & 3 deletions sedr/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class TestInit(unittest.TestCase):
def test_version(self):
result = subprocess.run(
[sys.executable, "./__init__.py", "--version"],
[sys.executable, "sedr/__init__.py", "--version"],
capture_output=True,
text=True,
check=True,
Expand All @@ -17,7 +17,7 @@ def test_version(self):

def test_help(self):
result = subprocess.run(
[sys.executable, "./__init__.py", "--help"],
[sys.executable, "sedr/__init__.py", "--help"],
capture_output=True,
text=True,
check=True,
Expand All @@ -28,7 +28,7 @@ def test_run(self):
result = subprocess.run(
[
sys.executable,
"./__init__.py",
"sedr/__init__.py",
"--url",
"https://edrisobaric.k8s.met.no",
],
Expand Down
39 changes: 39 additions & 0 deletions sedr/test_schemat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""Unit tests for schemat.py."""

import unittest
import util


class TestInit(unittest.TestCase):
def test_set_up_schemathesis(self):
"""Test set_up_schemathesis."""
__version__ = "testversion"

util.args = util.parse_args([], __version__)
util.args.openapi_version == "3.1"

util.logger = util.set_up_logging(
args=util.args, logfile=util.args.log_file, version=__version__
)
import schemat

schemat.schema = schemat.set_up_schemathesis(util.args)
self.assertTrue(schemat.schema)

# def test_edr_landingpage(self):
# """Test test_edr_landingpage."""
# __version__ = "testversion"

# util.args = util.parse_args([], __version__)
# util.args.openapi_version == "3.1"

# util.logger = util.set_up_logging(
# args=util.args, logfile=util.args.log_file, version=__version__
# )
# import schemat

# schemat.schema = schemat.set_up_schemathesis(util.args)

# TODO: Need to set up a case to run this
# t = schemat.test_edr_landingpage()
# self.assertTrue(t)
9 changes: 8 additions & 1 deletion sedr/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@
class TestUtil(unittest.TestCase):
def test_parse_landing_json(self):
"""Test parsing a generic landing page in json."""
with open("../testdata/landingpage.json", "r", encoding="utf-8") as f:
with open("testdata/landingpage.json", "r", encoding="utf-8") as f:
landingpage_json = json.loads(f.read())
landing, _ = util.parse_landing_json(landingpage_json)
self.assertTrue(landing)

# def test_parse_bad_landing(self):
# """Test parsing a bad landing page (in json)."""
# with open("testdata/landingpage_bad_service-desc.json", "r", encoding="utf-8") as f:
# landingpage_json = json.loads(f.read())
# landing, _ = util.parse_landing_json(landingpage_json)
# self.assertFalse(landing)
4 changes: 2 additions & 2 deletions sedr/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
args = logger = None


def parse_args(version: str = "") -> argparse.Namespace:
def parse_args(args, version: str = "") -> argparse.Namespace:
"""Parse arguments."""
parser = argparse.ArgumentParser(description="schemathesis-edr")
parser.add_argument("-v", "--version", action="version", version=version)
Expand Down Expand Up @@ -53,7 +53,7 @@ def parse_args(version: str = "") -> argparse.Namespace:
help="Use the rodeo profile even though the API doesn't specify it. Default False.",
)

args = parser.parse_args()
args = parser.parse_args(args)
# Parse out base_path for conveience
args.base_path = urlsplit(args.url).path or "/"

Expand Down
79 changes: 79 additions & 0 deletions testdata/landingpage_bad_service-desc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
{
"title": "Example API Landing Page",
"description": "This is an example of an API landing page in JSON format",
"attribution": "<a href='www.ign.es' rel=' '>IGN</a> <a href='www.govdata.de/dl-de/by-2-0'>(c)</a>",
"links": [
{
"rel": "service-desc",
"type": "application/json",
"title": "API definition for this endpoint as JSON",
"href": "http://www.example.com/oapi-c/api?f=application/json"
},
{
"rel": "service-desc",
"type": "text/html",
"title": "API definition for this endpoint as HTML",
"href": "https://edrisobaric.k8s.met.no/docs"
},
{
"rel": "http://www.opengis.net/def/rel/ogc/1.0/data-meta",
"type": "application/xml",
"title": "ISO 19115 Metadata as XML",
"href": "http://www.example.com/oapi-c/data-meta?f=application/xml"
},
{
"rel": "http://www.opengis.net/def/rel/ogc/1.0/conformance",
"type": "application/json",
"title": "Conformance Declaration as JSON",
"href": "http://www.example.com/oapi-c/conformance?f=application/json"
},
{
"rel": "http://www.opengis.net/def/rel/ogc/1.0/conformance",
"type": "application/xml",
"title": "Conformance Declaration as XML",
"href": "http://www.example.com/oapi-c/conformance?f=application/xml"
},
{
"rel": "http://www.opengis.net/def/rel/ogc/1.0/conformance",
"type": "text/html",
"title": "Conformance Declaration as HTML",
"href": "http://www.example.com/oapi-c/conformance?f=text/html"
},
{
"rel": "http://www.opengis.net/def/rel/ogc/1.0/data",
"type": "application/json",
"title": "Collections Metadata as JSON",
"href": "http://www.example.com/oapi-c/collections?f=application/json"
},
{
"rel": "http://www.opengis.net/def/rel/ogc/1.0/data",
"type": "application/xml",
"title": "Collections Metadata as XML",
"href": "http://www.example.com/oapi-c/collections?f=application/xml"
},
{
"rel": "http://www.opengis.net/def/rel/ogc/1.0/data",
"type": "text/html",
"title": "Collections Metadata as HTML",
"href": "http://www.example.com/oapi-c/collections?f=text/html"
},
{
"rel": "self",
"type": "application/json",
"title": "This Document",
"href": "http://www.example.com/oapi-c?f=application/json"
},
{
"rel": "alternate",
"type": "application/xml",
"title": "This Document as XML",
"href": "http://www.example.com/oapi-c?f=application/xml"
},
{
"rel": "alternate",
"type": "text/html",
"title": "This Document as HTML",
"href": "http://www.example.com/oapi-c?f=text/html"
}
]
}
6 changes: 3 additions & 3 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ commands = python3 ./sedr/__init__.py

[testenv:unittest]
description = Unit tests
change_dir = sedr
; change_dir = sedr
deps =
-r{toxinidir}/requirements.txt
-r{toxinidir}/requirements-dev.txt
commands = python -m unittest -v test_util test_init
commands = python -m unittest discover -v -s ./sedr -p "test_*.py"

[testenv:prospector]
description = Run static analysis using prospector, but dont fail on errors
Expand Down Expand Up @@ -59,6 +59,6 @@ commands = bandit -r --ini .bandit {toxinidir}/sedr/
[testenv:markdown]
ignore_outcome = true
description = Lint README.md
commands = pymarkdown scan README.md
commands = pymarkdown -d line-length scan README.md
deps =
-r requirements-dev.txt