diff options
| author | Kevin Stone | 2013-01-20 12:59:27 -0800 | 
|---|---|---|
| committer | Kevin Stone | 2013-01-20 13:03:38 -0800 | 
| commit | 71bd2faa792569c9f4c83a06904b927616bfdbf1 (patch) | |
| tree | 83045e97278206493c452313797395128a4633de | |
| parent | 2c76212e5454efa4d4d02c7051055c7957497d52 (diff) | |
| download | django-rest-framework-71bd2faa792569c9f4c83a06904b927616bfdbf1.tar.bz2 | |
Added test case for format_suffix_patterns to validate changes introduced with issue #593.
Signed-off-by: Kevin Stone <kevinastone@gmail.com>
| -rw-r--r-- | rest_framework/tests/urlpatterns.py | 75 | 
1 files changed, 75 insertions, 0 deletions
diff --git a/rest_framework/tests/urlpatterns.py b/rest_framework/tests/urlpatterns.py new file mode 100644 index 00000000..e96e7cf3 --- /dev/null +++ b/rest_framework/tests/urlpatterns.py @@ -0,0 +1,75 @@ +from collections import namedtuple + +from django.core import urlresolvers + +from django.test import TestCase +from django.test.client import RequestFactory + +from rest_framework.compat import patterns, url, include +from rest_framework.urlpatterns import format_suffix_patterns + + +# A container class for test paths for the test case +URLTestPath = namedtuple('URLTestPath', ['path', 'args', 'kwargs']) + + +def test_view(request, *args, **kwargs): +    pass + + +class FormatSuffixTests(TestCase): +    def _test_urlpatterns(self, urlpatterns, test_paths): +        factory = RequestFactory() +        try: +            urlpatterns = format_suffix_patterns(urlpatterns) +        except: +            self.fail("Failed to apply `format_suffix_patterns` on  the supplied urlpatterns") +        resolver = urlresolvers.RegexURLResolver(r'^/', urlpatterns) +        for test_path in test_paths: +            request = factory.get(test_path.path) +            try: +                callback, callback_args, callback_kwargs = resolver.resolve(request.path_info) +            except: +                self.fail("Failed to resolve URL: %s" % request.path_info) +            self.assertEquals(callback_args, test_path.args) +            self.assertEquals(callback_kwargs, test_path.kwargs) + +    def test_format_suffix(self): +        urlpatterns = patterns( +            '', +            url(r'^test$', test_view), +        ) +        test_paths = [ +            URLTestPath('/test', (), {}), +            URLTestPath('/test.api', (), {'format': 'api'}), +            URLTestPath('/test.asdf', (), {'format': 'asdf'}), +        ] +        self._test_urlpatterns(urlpatterns, test_paths) + +    def test_default_args(self): +        urlpatterns = patterns( +            '', +            url(r'^test$', test_view, {'foo': 'bar'}), +        ) +        test_paths = [ +            URLTestPath('/test', (), {'foo': 'bar', }), +            URLTestPath('/test.api', (), {'foo': 'bar', 'format': 'api'}), +            URLTestPath('/test.asdf', (), {'foo': 'bar', 'format': 'asdf'}), +        ] +        self._test_urlpatterns(urlpatterns, test_paths) + +    def test_included_urls(self): +        nested_patterns = patterns( +            '', +            url(r'^path$', test_view) +        ) +        urlpatterns = patterns( +            '', +            url(r'^test/', include(nested_patterns), {'foo': 'bar'}), +        ) +        test_paths = [ +            URLTestPath('/test/path', (), {'foo': 'bar', }), +            URLTestPath('/test/path.api', (), {'foo': 'bar', 'format': 'api'}), +            URLTestPath('/test/path.asdf', (), {'foo': 'bar', 'format': 'asdf'}), +        ] +        self._test_urlpatterns(urlpatterns, test_paths)  | 
