From c9a959043b509218f963527708dce29b97a1080c Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 1 Oct 2015 21:27:05 +0200 Subject: [PATCH] Smarter tests for incdec_number Now we generate possible combinations automatically instead of listing several example values by hand. --- tests/unit/utils/test_urlutils.py | 79 +++++++++++++++---------------- 1 file changed, 38 insertions(+), 41 deletions(-) diff --git a/tests/unit/utils/test_urlutils.py b/tests/unit/utils/test_urlutils.py index 69f467f36..df06dfd85 100644 --- a/tests/unit/utils/test_urlutils.py +++ b/tests/unit/utils/test_urlutils.py @@ -531,49 +531,46 @@ class TestIncDecNumber: """Tests for urlutils.incdec_number().""" - @pytest.mark.parametrize('url, incdec, segments, output', [ - ("http://example.com/index1.html", "increment", - {'path'}, "http://example.com/index2.html"), - ("http://foo.bar/folder_1/image_2", "increment", - {'path'}, "http://foo.bar/folder_1/image_3"), - ("http://bbc.c0.uk:80/story_1", "increment", - {'path'}, "http://bbc.c0.uk:80/story_2"), - ("http://mydomain.tld/1_%C3%A4", "increment", - {'path'}, "http://mydomain.tld/2_%C3%A4"), - ("http://example.com/site/5#5", "increment", - {'path'}, "http://example.com/site/6#5"), - ("http://example.com/123foo", "increment", - {'path'}, "http://example.com/124foo"), - ("http://example.com/12foo34", "increment", - {'path'}, "http://example.com/12foo35"), - ("http://example.com/foo7bar", "increment", - {'path'}, "http://example.com/foo8bar"), - ("http://example.com/site/1#1", "increment", - {'path', 'anchor'}, "http://example.com/site/1#2"), - ("http://example.com/site/1?page=2#3", "increment", - {'path', 'query'}, "http://example.com/site/1?page=3#3"), - - ("http://example.com/index10.html", "decrement", - {'path'}, "http://example.com/index9.html"), - ("http://foo.bar/folder_1/image_3", "decrement", - {'path'}, "http://foo.bar/folder_1/image_2"), - ("http://bbc.c0.uk:80/story_1", "decrement", - {'path'}, "http://bbc.c0.uk:80/story_0"), - ("http://mydomain.tld/2_%C3%A4", "decrement", - {'path'}, "http://mydomain.tld/1_%C3%A4"), - ("http://example.com/site/5#5", "decrement", - {'path'}, "http://example.com/site/4#5"), - ("http://example.com/123foo", "decrement", - {'path'}, "http://example.com/122foo"), - ("http://example.com/12foo34", "decrement", - {'path'}, "http://example.com/12foo33"), - ("http://example.com/foo7bar", "decrement", - {'path'}, "http://example.com/foo6bar"), + @pytest.mark.parametrize('incdec', ['increment', 'decrement']) + @pytest.mark.parametrize('value', [ + '{}foo', 'foo{}', 'foo{}bar', '42foo{}' ]) - def test_incdec_number(self, url, incdec, segments, output): + @pytest.mark.parametrize('url', [ + 'http://example.com/v1/path/{}/test', + 'http://example.com/v1/query_test?value={}', + 'http://example.com/v1/anchor_test#{}', + 'http://host_{}_test.com', + 'http://m4ny.c0m/number5/3very?where=yes#{}' + ]) + def test_incdec_number(self, incdec, value, url): """Test incdec_number with valid URLs.""" - new_url = urlutils.incdec_number(QUrl(url), incdec, segments=segments) - assert new_url == QUrl(output) + # The integer used should not affect test output, as long as it's + # bigger than 1 + # 4 was chosen by dice roll, guaranteed to be random + base_value = value.format(4) + if incdec == 'increment': + expected_value = value.format(5) + else: + expected_value = value.format(3) + + base_url = QUrl(url.format(base_value)) + expected_url = QUrl(url.format(expected_value)) + new_url = urlutils.incdec_number( + base_url, incdec, segments={'host', 'path', 'query', 'anchor'}) + assert new_url == expected_url + + @pytest.mark.parametrize('url, segments, expected', [ + ('http://ex4mple.com/test_4?page=3#anchor2', {'host'}, + 'http://ex5mple.com/test_4?page=3#anchor2'), + ('http://ex4mple.com/test_4?page=3#anchor2', {'host', 'path'}, + 'http://ex4mple.com/test_5?page=3#anchor2'), + ('http://ex4mple.com/test_4?page=3#anchor5', {'host', 'path', 'query'}, + 'http://ex4mple.com/test_4?page=4#anchor5'), + ]) + def test_incdec_segment_ignored(self, url, segments, expected): + new_url = urlutils.incdec_number(QUrl(url), 'increment', + segments=segments) + assert new_url == QUrl(expected) @pytest.mark.parametrize('url', [ "http://example.com/long/path/but/no/number",