From 1a30a7774b7d530394fdd761f2a59403b778fe10 Mon Sep 17 00:00:00 2001 From: Paul Tremberth Date: Fri, 22 Jan 2016 18:22:19 +0100 Subject: [PATCH] Use pytest.mark.parametrize decorator --- tests/test_command_shell.py | 96 +++++++++++++++---------------------- 1 file changed, 39 insertions(+), 57 deletions(-) diff --git a/tests/test_command_shell.py b/tests/test_command_shell.py index a61d520fa..9032e4124 100644 --- a/tests/test_command_shell.py +++ b/tests/test_command_shell.py @@ -1,5 +1,7 @@ from os.path import join +import pytest + from twisted.trial import unittest from twisted.internet import defer @@ -10,63 +12,6 @@ from scrapy.utils.testproc import ProcessTest from tests import tests_datadir -class ShellURLTest(unittest.TestCase): - pass - -def create_guess_scheme_t(args): - def do_expected(self): - url = guess_scheme(args[0]) - assert url.startswith(args[1]), \ - 'Wrong scheme guessed: for `%s` got `%s`, expected `%s...`' % ( - args[0], url, args[1]) - return do_expected - -def create_skipped_scheme_t(args): - def do_expected(self): - raise unittest.SkipTest(args[2]) - url = guess_scheme(args[0]) - assert url.startswith(args[1]) - return do_expected - -for k, args in enumerate ([ - ('/index', 'file://'), - ('/index.html', 'file://'), - ('./index.html', 'file://'), - ('../index.html', 'file://'), - ('../../index.html', 'file://'), - ('./data/index.html', 'file://'), - ('.hidden/data/index.html', 'file://'), - ('/home/user/www/index.html', 'file://'), - ('//home/user/www/index.html', 'file://'), - ('file:///home/user/www/index.html', 'file://'), - - ('index.html', 'http://'), - ('example.com', 'http://'), - ('www.example.com', 'http://'), - ('www.example.com/index.html', 'http://'), - ('http://example.com', 'http://'), - ('http://example.com/index.html', 'http://'), - ('localhost', 'http://'), - ('localhost/index.html', 'http://'), - - # some corner cases (default to http://) - ('/', 'http://'), - ('.../test', 'http://'), - - ], start=1): - t_method = create_guess_scheme_t(args) - t_method.__name__ = 'test_uri_%03d' % k - setattr (ShellURLTest, t_method.__name__, t_method) - -# TODO: the following tests do not pass with current implementation -for k, args in enumerate ([ - ('C:\absolute\path\to\a\file.html', 'file://', - 'Windows filepath are not supported for scrapy shell'), - ], start=1): - t_method = create_skipped_scheme_t(args) - t_method.__name__ = 'test_uri_skipped_%03d' % k - setattr (ShellURLTest, t_method.__name__, t_method) - class ShellTest(ProcessTest, SiteTest, unittest.TestCase): @@ -136,3 +81,40 @@ class ShellTest(ProcessTest, SiteTest, unittest.TestCase): check_code=False) self.assertEqual(errcode, 1, out or err) self.assertIn(b'DNS lookup failed', err) + + +@pytest.mark.parametrize("url, scheme", [ + ('/index', 'file://'), + ('/index.html', 'file://'), + ('./index.html', 'file://'), + ('../index.html', 'file://'), + ('../../index.html', 'file://'), + ('./data/index.html', 'file://'), + ('.hidden/data/index.html', 'file://'), + ('/home/user/www/index.html', 'file://'), + ('//home/user/www/index.html', 'file://'), + ('file:///home/user/www/index.html', 'file://'), + + ('index.html', 'http://'), + ('example.com', 'http://'), + ('www.example.com', 'http://'), + ('www.example.com/index.html', 'http://'), + ('http://example.com', 'http://'), + ('http://example.com/index.html', 'http://'), + ('localhost', 'http://'), + ('localhost/index.html', 'http://'), + + # some corner cases (default to http://) + ('/', 'http://'), + ('.../test', 'http://'), + + pytest.mark.xfail( + (r'C:\absolute\path\to\a\file.html', 'file://'), + reason = 'Windows filepath are not supported for scrapy shell' + ), +]) +def test_guess_scheme(url, scheme): + guessed_url = guess_scheme(url) + assert guessed_url.startswith(scheme), \ + 'Wrong scheme guessed: for `%s` got `%s`, expected `%s...`' % ( + url, guessed_url, scheme)