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