Skip to content

Commit 8b77b64

Browse files
dbrgntimgraham
authored andcommitted
Refactored URLValidator tests by moving URLs to text files.
1 parent ebc8e79 commit 8b77b64

File tree

3 files changed

+46
-30
lines changed

3 files changed

+46
-30
lines changed

tests/validators/invalid_urls.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
foo
2+
http://
3+
http://example
4+
http://example.
5+
http://.com
6+
http://invalid-.com
7+
http://-invalid.com
8+
http://invalid.com-
9+
http://inv-.alid-.com
10+
http://inv-.-alid.com
11+
file://localhost/path
12+
git://example.com/

tests/validators/tests.py

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
from __future__ import unicode_literals
33

44
from datetime import datetime, timedelta
5+
import io
6+
import os
57
import re
68
import types
79
from unittest import TestCase
@@ -16,12 +18,13 @@
1618
)
1719
from django.test import SimpleTestCase
1820
from django.test.utils import str_prefix
21+
from django.utils._os import upath
1922

2023

2124
NOW = datetime.now()
2225
EXTENDED_SCHEMES = ['http', 'https', 'ftp', 'ftps', 'git', 'file']
2326

24-
TEST_DATA = (
27+
TEST_DATA = [
2528
# (validator, value, expected),
2629
(validate_integer, '42', None),
2730
(validate_integer, '-42', None),
@@ -153,37 +156,9 @@
153156

154157
(MinLengthValidator(10), '', ValidationError),
155158

156-
(URLValidator(), 'http://www.djangoproject.com/', None),
157-
(URLValidator(), 'HTTP://WWW.DJANGOPROJECT.COM/', None),
158-
(URLValidator(), 'http://localhost/', None),
159-
(URLValidator(), 'http://example.com/', None),
160-
(URLValidator(), 'http://www.example.com/', None),
161-
(URLValidator(), 'http://www.example.com:8000/test', None),
162-
(URLValidator(), 'http://valid-with-hyphens.com/', None),
163-
(URLValidator(), 'http://subdomain.example.com/', None),
164-
(URLValidator(), 'http://200.8.9.10/', None),
165-
(URLValidator(), 'http://200.8.9.10:8000/test', None),
166-
(URLValidator(), 'http://valid-----hyphens.com/', None),
167-
(URLValidator(), 'http://example.com?something=value', None),
168-
(URLValidator(), 'http://example.com/index.php?something=value&another=value2', None),
169-
(URLValidator(), 'https://example.com/', None),
170-
(URLValidator(), 'ftp://example.com/', None),
171-
(URLValidator(), 'ftps://example.com/', None),
172159
(URLValidator(EXTENDED_SCHEMES), 'file://localhost/path', None),
173160
(URLValidator(EXTENDED_SCHEMES), 'git://example.com/', None),
174161

175-
(URLValidator(), 'foo', ValidationError),
176-
(URLValidator(), 'http://', ValidationError),
177-
(URLValidator(), 'http://example', ValidationError),
178-
(URLValidator(), 'http://example.', ValidationError),
179-
(URLValidator(), 'http://.com', ValidationError),
180-
(URLValidator(), 'http://invalid-.com', ValidationError),
181-
(URLValidator(), 'http://-invalid.com', ValidationError),
182-
(URLValidator(), 'http://invalid.com-', ValidationError),
183-
(URLValidator(), 'http://inv-.alid-.com', ValidationError),
184-
(URLValidator(), 'http://inv-.-alid.com', ValidationError),
185-
(URLValidator(), 'file://localhost/path', ValidationError),
186-
(URLValidator(), 'git://example.com/', ValidationError),
187162
(URLValidator(EXTENDED_SCHEMES), 'git://-invalid.com', ValidationError),
188163

189164
(BaseValidator(True), True, None),
@@ -208,7 +183,20 @@
208183
(RegexValidator('x', flags=re.IGNORECASE), 'y', ValidationError),
209184
(RegexValidator('a'), 'A', ValidationError),
210185
(RegexValidator('a', flags=re.IGNORECASE), 'A', None),
211-
)
186+
]
187+
188+
189+
def create_path(filename):
190+
return os.path.abspath(os.path.join(os.path.dirname(upath(__file__)), filename))
191+
192+
# Add valid and invalid URL tests.
193+
# This only tests the validator without extended schemes.
194+
with io.open(create_path('valid_urls.txt'), encoding='utf8') as f:
195+
for url in f:
196+
TEST_DATA.append((URLValidator(), url.strip(), None))
197+
with io.open(create_path('invalid_urls.txt'), encoding='utf8') as f:
198+
for url in f:
199+
TEST_DATA.append((URLValidator(), url.strip(), ValidationError))
212200

213201

214202
def create_simple_test_method(validator, expected, value, num):

tests/validators/valid_urls.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
http://www.djangoproject.com/
2+
HTTP://WWW.DJANGOPROJECT.COM/
3+
http://localhost/
4+
http://example.com/
5+
http://www.example.com/
6+
http://www.example.com:8000/test
7+
http://valid-with-hyphens.com/
8+
http://subdomain.example.com/
9+
http://200.8.9.10/
10+
http://200.8.9.10:8000/test
11+
http://su--b.valid-----hyphens.com/
12+
http://example.com?something=value
13+
http://example.com/index.php?something=value&another=value2
14+
https://example.com/
15+
ftp://example.com/
16+
ftps://example.com/

0 commit comments

Comments
 (0)