From 8b77b64f1cf878b100d1d2001c8a73938ea3e1ed Mon Sep 17 00:00:00 2001 From: Danilo Bargen Date: Mon, 3 Nov 2014 18:00:18 +0100 Subject: [PATCH] Refactored URLValidator tests by moving URLs to text files. --- tests/validators/invalid_urls.txt | 12 ++++++++ tests/validators/tests.py | 48 ++++++++++++------------------- tests/validators/valid_urls.txt | 16 +++++++++++ 3 files changed, 46 insertions(+), 30 deletions(-) create mode 100644 tests/validators/invalid_urls.txt create mode 100644 tests/validators/valid_urls.txt diff --git a/tests/validators/invalid_urls.txt b/tests/validators/invalid_urls.txt new file mode 100644 index 0000000000..37c34776d0 --- /dev/null +++ b/tests/validators/invalid_urls.txt @@ -0,0 +1,12 @@ +foo +http:// +http://example +http://example. +http://.com +http://invalid-.com +http://-invalid.com +http://invalid.com- +http://inv-.alid-.com +http://inv-.-alid.com +file://localhost/path +git://example.com/ diff --git a/tests/validators/tests.py b/tests/validators/tests.py index cb552619b4..3e434910d8 100644 --- a/tests/validators/tests.py +++ b/tests/validators/tests.py @@ -2,6 +2,8 @@ from __future__ import unicode_literals from datetime import datetime, timedelta +import io +import os import re import types from unittest import TestCase @@ -16,12 +18,13 @@ from django.core.validators import ( ) from django.test import SimpleTestCase from django.test.utils import str_prefix +from django.utils._os import upath NOW = datetime.now() EXTENDED_SCHEMES = ['http', 'https', 'ftp', 'ftps', 'git', 'file'] -TEST_DATA = ( +TEST_DATA = [ # (validator, value, expected), (validate_integer, '42', None), (validate_integer, '-42', None), @@ -153,37 +156,9 @@ TEST_DATA = ( (MinLengthValidator(10), '', ValidationError), - (URLValidator(), 'http://www.djangoproject.com/', None), - (URLValidator(), 'HTTP://WWW.DJANGOPROJECT.COM/', None), - (URLValidator(), 'http://localhost/', None), - (URLValidator(), 'http://example.com/', None), - (URLValidator(), 'http://www.example.com/', None), - (URLValidator(), 'http://www.example.com:8000/test', None), - (URLValidator(), 'http://valid-with-hyphens.com/', None), - (URLValidator(), 'http://subdomain.example.com/', None), - (URLValidator(), 'http://200.8.9.10/', None), - (URLValidator(), 'http://200.8.9.10:8000/test', None), - (URLValidator(), 'http://valid-----hyphens.com/', None), - (URLValidator(), 'http://example.com?something=value', None), - (URLValidator(), 'http://example.com/index.php?something=value&another=value2', None), - (URLValidator(), 'https://example.com/', None), - (URLValidator(), 'ftp://example.com/', None), - (URLValidator(), 'ftps://example.com/', None), (URLValidator(EXTENDED_SCHEMES), 'file://localhost/path', None), (URLValidator(EXTENDED_SCHEMES), 'git://example.com/', None), - (URLValidator(), 'foo', ValidationError), - (URLValidator(), 'http://', ValidationError), - (URLValidator(), 'http://example', ValidationError), - (URLValidator(), 'http://example.', ValidationError), - (URLValidator(), 'http://.com', ValidationError), - (URLValidator(), 'http://invalid-.com', ValidationError), - (URLValidator(), 'http://-invalid.com', ValidationError), - (URLValidator(), 'http://invalid.com-', ValidationError), - (URLValidator(), 'http://inv-.alid-.com', ValidationError), - (URLValidator(), 'http://inv-.-alid.com', ValidationError), - (URLValidator(), 'file://localhost/path', ValidationError), - (URLValidator(), 'git://example.com/', ValidationError), (URLValidator(EXTENDED_SCHEMES), 'git://-invalid.com', ValidationError), (BaseValidator(True), True, None), @@ -208,7 +183,20 @@ TEST_DATA = ( (RegexValidator('x', flags=re.IGNORECASE), 'y', ValidationError), (RegexValidator('a'), 'A', ValidationError), (RegexValidator('a', flags=re.IGNORECASE), 'A', None), -) +] + + +def create_path(filename): + return os.path.abspath(os.path.join(os.path.dirname(upath(__file__)), filename)) + +# Add valid and invalid URL tests. +# This only tests the validator without extended schemes. +with io.open(create_path('valid_urls.txt'), encoding='utf8') as f: + for url in f: + TEST_DATA.append((URLValidator(), url.strip(), None)) +with io.open(create_path('invalid_urls.txt'), encoding='utf8') as f: + for url in f: + TEST_DATA.append((URLValidator(), url.strip(), ValidationError)) def create_simple_test_method(validator, expected, value, num): diff --git a/tests/validators/valid_urls.txt b/tests/validators/valid_urls.txt new file mode 100644 index 0000000000..9f1519c84f --- /dev/null +++ b/tests/validators/valid_urls.txt @@ -0,0 +1,16 @@ +http://www.djangoproject.com/ +HTTP://WWW.DJANGOPROJECT.COM/ +http://localhost/ +http://example.com/ +http://www.example.com/ +http://www.example.com:8000/test +http://valid-with-hyphens.com/ +http://subdomain.example.com/ +http://200.8.9.10/ +http://200.8.9.10:8000/test +http://su--b.valid-----hyphens.com/ +http://example.com?something=value +http://example.com/index.php?something=value&another=value2 +https://example.com/ +ftp://example.com/ +ftps://example.com/