Expanded the test code which finds urlpatterns to extract all URLs

specified in the various Django apps of the site.  Also fixed the
documentation of the TemplatedForm factory to be more correct.
 - Legacy-Id: 227
This commit is contained in:
Henrik Levkowetz 2007-06-04 19:26:16 +00:00
parent 4e25407e25
commit 8ff20e3127
2 changed files with 26 additions and 7 deletions

View file

@ -3,13 +3,33 @@ import re
import django.test.simple import django.test.simple
from django.test import TestCase from django.test import TestCase
from ietf.urls import urlpatterns
import ietf.settings import ietf.settings
import ietf.urls
def run_tests(module_list, verbosity=1, extra_tests=[]): def run_tests(module_list, verbosity=1, extra_tests=[]):
module_list.append(ietf.tests) module_list.append(ietf.tests)
return django.test.simple.run_tests(module_list, verbosity, extra_tests) return django.test.simple.run_tests(module_list, verbosity, extra_tests)
def get_patterns(module):
all = []
try:
patterns = module.urlpatterns
except AttributeError:
patterns = []
for item in patterns:
try:
subpatterns = get_patterns(item.urlconf_module)
except:
subpatterns = [""]
for sub in subpatterns:
if not sub:
all.append(item.regex.pattern)
elif sub.startswith("^"):
all.append(item.regex.pattern + sub[1:])
else:
all.append(item.regex.pattern + ".*" + sub)
return all
class UrlTestCase(TestCase): class UrlTestCase(TestCase):
def setUp(self): def setUp(self):
from django.test.client import Client from django.test.client import Client
@ -38,7 +58,7 @@ class UrlTestCase(TestCase):
def testCoverage(self): def testCoverage(self):
covered = [] covered = []
patterns = [pattern.regex.pattern for pattern in urlpatterns] patterns = get_patterns(ietf.urls)
for code, testurl, goodurl in self.testurls: for code, testurl, goodurl in self.testurls:
for pattern in patterns: for pattern in patterns:
if re.match(pattern, testurl[1:]): if re.match(pattern, testurl[1:]):
@ -48,7 +68,7 @@ class UrlTestCase(TestCase):
#self.assertEqual(set(patterns), set(covered), "Not all the #self.assertEqual(set(patterns), set(covered), "Not all the
#application URLs has test cases. The missing are: %s" % (list(set(patterns) - set(covered)))) #application URLs has test cases. The missing are: %s" % (list(set(patterns) - set(covered))))
if not set(patterns) == set(covered): if not set(patterns) == set(covered):
print "Not all the application URLs has test cases. The missing are: %s" % (list(set(patterns) - set(covered))) print "Not all the application URLs has test cases. The missing are: %s" % ("\n ".join(list(set(patterns) - set(covered))))
def testUrls(self): def testUrls(self):
for code, testurl, goodurl in self.testurls: for code, testurl, goodurl in self.testurls:

View file

@ -3,14 +3,13 @@ from django.utils.html import escape
def makeTemplatedForm(template=None): def makeTemplatedForm(template=None):
"""Create a form class which formats its fields using the provided template """Create a form class which formats its fields using the provided template
The template is provided with a dictionary containing the following keys, value The template is provided with a dictionary containing the following key-value
pairs: pairs:
"label": field label, if any, "label": field label, if any,
"errors": list of errors, if any, "errors": list of errors, if any,
"field": widget rendering for an unbound form / field value for a bound form, "text": widget rendering for an unbound form / field value for a bound form,
"help_text": field help text, if any "help_text": field help text, if any
""" """
from django.template import loader from django.template import loader
import django.newforms as forms import django.newforms as forms
@ -18,7 +17,7 @@ def makeTemplatedForm(template=None):
class TemplatedForm(forms.BaseForm): class TemplatedForm(forms.BaseForm):
_template = template _template = template
def __getitem__(self, name): def __getitem__(self, name):
"Returns a BoundField with the given name." "Returns a rendered field with the given name."
#syslog.syslog("FormattingForm.__getitem__(%s)" % (name, )) #syslog.syslog("FormattingForm.__getitem__(%s)" % (name, ))
try: try:
field = self.fields[name] field = self.fields[name]