Skip to content

Commit

Permalink
Convert test/jsonld/test_util.py to pytest (#1961)
Browse files Browse the repository at this point in the history
Not the most straight forward conversion, but it simplifies it somewhat.

Main reason for doing this is to remove the use of `unittest` `subTest` as
we need `pytest-subtest` to process that with `pytest` and that dependency
is causing some complications in dependency solving for pip sometimes.

Merged with one review as this has no runtime impact.
  • Loading branch information
aucampia authored May 21, 2022
1 parent a24586a commit dd56451
Showing 1 changed file with 68 additions and 83 deletions.
151 changes: 68 additions & 83 deletions test/jsonld/test_util.py
Original file line number Diff line number Diff line change
@@ -1,89 +1,74 @@
import unittest
from typing import NamedTuple
import pytest

from rdflib.plugins.shared.jsonld.util import norm_url


class URLTests(unittest.TestCase):
@unittest.expectedFailure
def test_norm_url_xfail(self):
class TestSpec(NamedTuple):
base: str
url: str
result: str
@pytest.mark.parametrize(
["base", "url", "expected_result"],
[
pytest.param(
"git+ssh://example.com:1231/some/thing/",
"a",
"git+ssh://example.com:1231/some/thing/a",
marks=pytest.mark.xfail(
reason="""
URL normalizes to the wrong thing.
tests = [
TestSpec(
"git+ssh://example.com:1231/some/thing/",
"a",
"git+ssh://example.com:1231/some/thing/a",
AssertionError: assert 'git+ssh://example.com:1231/some/thing/a' == 'a'
""",
raises=AssertionError,
),
]

for test in tests:
(base, url, result) = test
with self.subTest(base=base, url=url):
self.assertEqual(norm_url(base, url), result)

def test_norm_url(self):
class TestSpec(NamedTuple):
base: str
url: str
result: str

tests = [
TestSpec("http://example.org/", "/one", "http://example.org/one"),
TestSpec("http://example.org/", "/one#", "http://example.org/one#"),
TestSpec("http://example.org/one", "two", "http://example.org/two"),
TestSpec("http://example.org/one/", "two", "http://example.org/one/two"),
TestSpec(
"http://example.org/",
"http://example.net/one",
"http://example.net/one",
),
TestSpec(
"",
"1 2 3",
"1 2 3",
),
TestSpec(
"http://example.org/",
"http://example.org//one",
"http://example.org//one",
),
TestSpec("", "http://example.org", "http://example.org"),
TestSpec("", "http://example.org/", "http://example.org/"),
TestSpec("", "mailto:[email protected]", "mailto:[email protected]"),
TestSpec(
"http://example.org/",
"mailto:[email protected]",
"mailto:[email protected]",
),
TestSpec("http://example.org/a/b/c", "../../z", "http://example.org/z"),
TestSpec("http://example.org/a/b/c", "../", "http://example.org/a/"),
TestSpec(
"",
"git+ssh://example.com:1231/some/thing",
"git+ssh://example.com:1231/some/thing",
),
TestSpec(
"git+ssh://example.com:1231/some/thing",
"",
"git+ssh://example.com:1231/some/thing",
),
TestSpec(
"http://example.com/RDFLib/rdflib",
"http://example.org",
"http://example.org",
),
TestSpec(
"http://example.com/RDFLib/rdflib",
"http://example.org/",
"http://example.org/",
),
]

for test in tests:
(base, url, result) = test
with self.subTest(base=base, url=url):
self.assertEqual(norm_url(base, url), result)
),
("http://example.org/", "/one", "http://example.org/one"),
("http://example.org/", "/one#", "http://example.org/one#"),
("http://example.org/one", "two", "http://example.org/two"),
("http://example.org/one/", "two", "http://example.org/one/two"),
(
"http://example.org/",
"http://example.net/one",
"http://example.net/one",
),
(
"",
"1 2 3",
"1 2 3",
),
(
"http://example.org/",
"http://example.org//one",
"http://example.org//one",
),
("", "http://example.org", "http://example.org"),
("", "http://example.org/", "http://example.org/"),
("", "mailto:[email protected]", "mailto:[email protected]"),
(
"http://example.org/",
"mailto:[email protected]",
"mailto:[email protected]",
),
("http://example.org/a/b/c", "../../z", "http://example.org/z"),
("http://example.org/a/b/c", "../", "http://example.org/a/"),
(
"",
"git+ssh://example.com:1231/some/thing",
"git+ssh://example.com:1231/some/thing",
),
(
"git+ssh://example.com:1231/some/thing",
"",
"git+ssh://example.com:1231/some/thing",
),
(
"http://example.com/RDFLib/rdflib",
"http://example.org",
"http://example.org",
),
(
"http://example.com/RDFLib/rdflib",
"http://example.org/",
"http://example.org/",
),
],
)
def test_norm_url_xfail(base: str, url: str, expected_result: str) -> None:
assert expected_result == norm_url(base, url)

0 comments on commit dd56451

Please sign in to comment.