Ticket #9310: named_404_with_test.diff

File named_404_with_test.diff, 7.0 KB (added by Tobias McNulty, 14 years ago)

REALLY updated the patch to latest trunk

  • django/core/urlresolvers.py

    diff -r da07240fd0ca django/core/urlresolvers.py
    a b  
    252252                except Resolver404, e:
    253253                    sub_tried = e.args[0].get('tried')
    254254                    if sub_tried is not None:
    255                         tried.extend([(pattern.regex.pattern + '   ' + t) for t in sub_tried])
     255                        tried.extend([[pattern] + t for t in sub_tried])
    256256                    else:
    257                         tried.append(pattern.regex.pattern)
     257                        tried.append([pattern])
    258258                else:
    259259                    if sub_match:
    260260                        sub_match_dict = dict([(smart_str(k), v) for k, v in match.groupdict().items()])
     
    262262                        for k, v in sub_match.kwargs.iteritems():
    263263                            sub_match_dict[smart_str(k)] = v
    264264                        return ResolverMatch(sub_match.func, sub_match.args, sub_match_dict, sub_match.url_name, self.app_name or sub_match.app_name, [self.namespace] + sub_match.namespaces)
    265                     tried.append(pattern.regex.pattern)
     265                    tried.append([pattern])
    266266            raise Resolver404({'tried': tried, 'path': new_path})
    267267        raise Resolver404({'path' : path})
    268268
  • django/views/debug.py

    diff -r da07240fd0ca django/views/debug.py
    a b  
    778778      </p>
    779779      <ol>
    780780        {% for pattern in urlpatterns %}
    781           <li>{{ pattern }}</li>
     781          <li>
     782            {% for pat in pattern %}
     783                {{ pat.regex.pattern }}
     784                {% if forloop.last and pat.name %}[name='{{ pat.name }}']{% endif %}
     785            {% endfor %}
     786          </li>
    782787        {% endfor %}
    783788      </ol>
    784789      <p>The current URL, <code>{{ request_path|escape }}</code>, didn't match any of these.</p>
  • new file tests/regressiontests/urlpatterns_reverse/included_named_urls.py

    diff -r da07240fd0ca tests/regressiontests/urlpatterns_reverse/included_named_urls.py
    - +  
     1from django.conf.urls.defaults import *
     2from views import empty_view
     3
     4urlpatterns = patterns('',
     5    url(r'^$', empty_view, name="named-url3"),
     6    url(r'^extra/(?P<extra>\w+)/$', empty_view, name="named-url4"),
     7    url(r'^(?P<one>\d+)|(?P<two>\d+)/$', empty_view),
     8    (r'^included/', include('regressiontests.urlpatterns_reverse.included_named_urls2')),
     9)
     10
  • new file tests/regressiontests/urlpatterns_reverse/included_named_urls2.py

    diff -r da07240fd0ca tests/regressiontests/urlpatterns_reverse/included_named_urls2.py
    - +  
     1from django.conf.urls.defaults import *
     2from views import empty_view
     3
     4urlpatterns = patterns('',
     5    url(r'^$', empty_view, name="named-url5"),
     6    url(r'^extra/(?P<extra>\w+)/$', empty_view, name="named-url6"),
     7    url(r'^(?P<one>\d+)|(?P<two>\d+)/$', empty_view),
     8)
     9
  • new file tests/regressiontests/urlpatterns_reverse/named_urls.py

    diff -r da07240fd0ca tests/regressiontests/urlpatterns_reverse/named_urls.py
    - +  
     1from django.conf.urls.defaults import *
     2from views import empty_view
     3
     4urlpatterns = patterns('',
     5    url(r'^$', empty_view, name="named-url1"),
     6    url(r'^extra/(?P<extra>\w+)/$', empty_view, name="named-url2"),
     7    url(r'^(?P<one>\d+)|(?P<two>\d+)/$', empty_view),
     8    (r'^included/', include('regressiontests.urlpatterns_reverse.included_named_urls')),
     9)
  • tests/regressiontests/urlpatterns_reverse/tests.py

    diff -r da07240fd0ca tests/regressiontests/urlpatterns_reverse/tests.py
    a b  
    1818
    1919from django.conf import settings
    2020from django.core.exceptions import ImproperlyConfigured
    21 from django.core.urlresolvers import reverse, resolve, NoReverseMatch, Resolver404, ResolverMatch
     21from django.core.urlresolvers import reverse, resolve, NoReverseMatch,\
     22                                     Resolver404, ResolverMatch,\
     23                                     RegexURLResolver, RegexURLPattern
    2224from django.http import HttpResponseRedirect, HttpResponsePermanentRedirect
    2325from django.shortcuts import redirect
    2426from django.test import TestCase
     
    172174        self.assertRaises(Resolver404, resolve, 'a')
    173175        self.assertRaises(Resolver404, resolve, '\\')
    174176        self.assertRaises(Resolver404, resolve, '.')
     177       
     178    def test_404_tried_urls_have_names(self):
     179        """
     180        Verifies that the list of URLs that come back from a Resolver404
     181        exception contains a list in the right format for printing out in
     182        the DEBUG 404 page with both the patterns and URL names, if available.
     183        """
     184        urls = 'regressiontests.urlpatterns_reverse.named_urls'
     185        # this list matches the expected URL types and names returned when
     186        # you try to resolve a non-existent URL in the first level of included
     187        # URLs in named_urls.py (e.g., '/included/non-existent-url')
     188        url_types_names = [
     189            [{'type': RegexURLPattern, 'name': 'named-url1'}],
     190            [{'type': RegexURLPattern, 'name': 'named-url2'}],
     191            [{'type': RegexURLPattern, 'name': None}],
     192            [{'type': RegexURLResolver}, {'type': RegexURLPattern, 'name': 'named-url3'}],
     193            [{'type': RegexURLResolver}, {'type': RegexURLPattern, 'name': 'named-url4'}],
     194            [{'type': RegexURLResolver}, {'type': RegexURLPattern, 'name': None}],
     195            [{'type': RegexURLResolver}, {'type': RegexURLResolver}],
     196        ]
     197        try:
     198            resolve('/included/non-existent-url', urlconf=urls)
     199            self.fail('resolve did not raise a 404')
     200        except Resolver404, e:
     201            # make sure we at least matched the root ('/') url resolver:
     202            self.assertTrue('tried' in e.args[0])
     203            tried = e.args[0]['tried']
     204            self.assertEqual(len(e.args[0]['tried']), len(url_types_names), 'Wrong number of tried URLs returned.  Expected %s, got %s.' % (len(url_types_names), len(e.args[0]['tried'])))
     205            for tried, expected in zip(e.args[0]['tried'], url_types_names):
     206                for t, e in zip(tried, expected):
     207                    self.assertTrue(isinstance(t, e['type']), '%s is not an instance of %s' % (t, e['type']))
     208                    if 'name' in e:
     209                        if not e['name']:
     210                            self.assertTrue(t.name is None, 'Expected no URL name but found %s.' % t.name)
     211                        else:
     212                            self.assertEqual(t.name, e['name'], 'Wrong URL name.  Expected "%s", got "%s".' % (e['name'], t.name))
    175213
    176214class ReverseShortcutTests(TestCase):
    177215    urls = 'regressiontests.urlpatterns_reverse.urls'
Back to Top