Move incdec_number tests to own class

and add tests for numbers in anchors
This commit is contained in:
Daniel 2015-08-08 13:47:17 +02:00
parent c3f6246274
commit 72c65a812f

View File

@ -525,58 +525,65 @@ def test_same_domain_invalid_url(url1, url2):
with pytest.raises(ValueError): with pytest.raises(ValueError):
urlutils.same_domain(QUrl(url1), QUrl(url2)) urlutils.same_domain(QUrl(url1), QUrl(url2))
@pytest.mark.parametrize('url, incdec, output', [ class TestIncDecNumber:
"""Tests for urlutils.incdec_number()."""
@pytest.mark.parametrize('url, incdec, output', [
("http://example.com/index1.html", "increment", "http://example.com/index2.html"), ("http://example.com/index1.html", "increment", "http://example.com/index2.html"),
("http://foo.bar/folder_1/image_2", "increment", "http://foo.bar/folder_1/image_3"), ("http://foo.bar/folder_1/image_2", "increment", "http://foo.bar/folder_1/image_3"),
("http://bbc.c0.uk:80/story_1", "increment", "http://bbc.c0.uk:80/story_2"), ("http://bbc.c0.uk:80/story_1", "increment", "http://bbc.c0.uk:80/story_2"),
("http://mydomain.tld/1_%C3%A4", "increment", "http://mydomain.tld/2_%C3%A4"), ("http://mydomain.tld/1_%C3%A4", "increment", "http://mydomain.tld/2_%C3%A4"),
("http://example.com/site/5#5", "increment", "http://example.com/site/6#5"),
("http://example.com/index10.html", "decrement", "http://example.com/index9.html"), ("http://example.com/index10.html", "decrement", "http://example.com/index9.html"),
("http://foo.bar/folder_1/image_3", "decrement", "http://foo.bar/folder_1/image_2"), ("http://foo.bar/folder_1/image_3", "decrement", "http://foo.bar/folder_1/image_2"),
("http://bbc.c0.uk:80/story_1", "decrement", "http://bbc.c0.uk:80/story_0"), ("http://bbc.c0.uk:80/story_1", "decrement", "http://bbc.c0.uk:80/story_0"),
("http://mydomain.tld/2_%C3%A4", "decrement", "http://mydomain.tld/1_%C3%A4"), ("http://mydomain.tld/2_%C3%A4", "decrement", "http://mydomain.tld/1_%C3%A4"),
]) ("http://example.com/site/5#5", "decrement", "http://example.com/site/4#5"),
def test_incdec_number(url, incdec, output): ])
def test_incdec_number(self, url, incdec, output):
"""Test incdec_number with valid URLs.""" """Test incdec_number with valid URLs."""
new_url = urlutils.incdec_number(QUrl(url), incdec) new_url = urlutils.incdec_number(QUrl(url), incdec)
assert new_url == QUrl(output) assert new_url == QUrl(output)
@pytest.mark.parametrize('url', [ @pytest.mark.parametrize('url', [
"http://example.com/long/path/but/no/number", "http://example.com/long/path/but/no/number",
"http://ex4mple.com/number/in/hostname", "http://ex4mple.com/number/in/hostname",
"http://example.com:42/number/in/port", "http://example.com:42/number/in/port",
"http://www2.example.com/number/in/subdomain", "http://www2.example.com/number/in/subdomain",
"http://example.com/%C3%B6/urlencoded/data", "http://example.com/%C3%B6/urlencoded/data",
"http://www2.ex4mple.com:42/all/of/the/%C3%A4bove", "http://example.com/number/in/anchor#5",
]) "http://www2.ex4mple.com:42/all/of/the/%C3%A4bove#5",
def test_incdec_number_no_number(url): ])
def test_no_number(self, url):
"""Test incdec_number with URLs that don't contain a number.""" """Test incdec_number with URLs that don't contain a number."""
with pytest.raises(urlutils.IncDecError): with pytest.raises(urlutils.IncDecError):
urlutils.incdec_number(QUrl(url), "increment") urlutils.incdec_number(QUrl(url), "increment")
def test_incdec_number_below_0(): def test_number_below_0(self):
"""Test incdec_number with a number that would be below zero """Test incdec_number with a number that would be below zero
after decrementing.""" after decrementing."""
with pytest.raises(urlutils.IncDecError): with pytest.raises(urlutils.IncDecError):
urlutils.incdec_number(QUrl('http://example.com/page_0.html'), urlutils.incdec_number(QUrl('http://example.com/page_0.html'),
'decrement') 'decrement')
def test_incdec_number_invalid_url(): def test_invalid_url(self):
"""Test if incdec_number rejects an invalid URL.""" """Test if incdec_number rejects an invalid URL."""
with pytest.raises(ValueError): with pytest.raises(ValueError):
urlutils.incdec_number(QUrl(""), "increment") urlutils.incdec_number(QUrl(""), "increment")
def test_incdec_number_wrong_mode(): def test_wrong_mode(self):
"""Test if incdec_number rejects a wrong parameter for the incdec """Test if incdec_number rejects a wrong parameter for the incdec
argument.""" argument."""
valid_url = QUrl("http://example.com/0") valid_url = QUrl("http://example.com/0")
with pytest.raises(ValueError): with pytest.raises(ValueError):
urlutils.incdec_number(valid_url, "foobar") urlutils.incdec_number(valid_url, "foobar")
@pytest.mark.parametrize("url, msg, expected_str", [ @pytest.mark.parametrize("url, msg, expected_str", [
("http://example.com", "Invalid", "Invalid: http://example.com"), ("http://example.com", "Invalid", "Invalid: http://example.com"),
]) ])
def test_incdec_error(url, msg, expected_str): def test_incdec_error(self, url, msg, expected_str):
"""Test IncDecError.""" """Test IncDecError."""
url = QUrl(url) url = QUrl(url)
with pytest.raises(urlutils.IncDecError) as excinfo: with pytest.raises(urlutils.IncDecError) as excinfo: