diff options
| author | Tom Christie | 2011-05-04 09:21:17 +0100 |
|---|---|---|
| committer | Tom Christie | 2011-05-04 09:21:17 +0100 |
| commit | d373b3a067796b8e181be9368fa24e89c572c45e (patch) | |
| tree | eec24eb8f8813ce959511c3572a54c5ee645e227 /djangorestframework/tests | |
| parent | 8756664e064a18afc4713d921c318cd968f18433 (diff) | |
| download | django-rest-framework-d373b3a067796b8e181be9368fa24e89c572c45e.tar.bz2 | |
Decouple views and resources
Diffstat (limited to 'djangorestframework/tests')
| -rw-r--r-- | djangorestframework/tests/accept.py | 10 | ||||
| -rw-r--r-- | djangorestframework/tests/authentication.py | 6 | ||||
| -rw-r--r-- | djangorestframework/tests/breadcrumbs.py | 12 | ||||
| -rw-r--r-- | djangorestframework/tests/description.py | 28 | ||||
| -rw-r--r-- | djangorestframework/tests/files.py | 6 | ||||
| -rw-r--r-- | djangorestframework/tests/parsers.py | 22 | ||||
| -rw-r--r-- | djangorestframework/tests/reverse.py | 8 | ||||
| -rw-r--r-- | djangorestframework/tests/throttling.py | 6 | ||||
| -rw-r--r-- | djangorestframework/tests/validators.py | 29 | ||||
| -rw-r--r-- | djangorestframework/tests/views.py | 2 |
10 files changed, 66 insertions, 63 deletions
diff --git a/djangorestframework/tests/accept.py b/djangorestframework/tests/accept.py index b12dc757..c5a3f69e 100644 --- a/djangorestframework/tests/accept.py +++ b/djangorestframework/tests/accept.py @@ -1,6 +1,6 @@ from django.test import TestCase from djangorestframework.compat import RequestFactory -from djangorestframework.resource import Resource +from djangorestframework.views import BaseView # See: http://www.useragentstring.com/ @@ -19,15 +19,15 @@ class UserAgentMungingTest(TestCase): def setUp(self): - class MockResource(Resource): + class MockView(BaseView): permissions = () def get(self, request): return {'a':1, 'b':2, 'c':3} self.req = RequestFactory() - self.MockResource = MockResource - self.view = MockResource.as_view() + self.MockView = MockView + self.view = MockView.as_view() def test_munge_msie_accept_header(self): """Send MSIE user agent strings and ensure that we get an HTML response, @@ -42,7 +42,7 @@ class UserAgentMungingTest(TestCase): def test_dont_rewrite_msie_accept_header(self): """Turn off REWRITE_IE_ACCEPT_HEADER, send MSIE user agent strings and ensure that we get a JSON response if we set a */* accept header.""" - view = self.MockResource.as_view(REWRITE_IE_ACCEPT_HEADER=False) + view = self.MockView.as_view(REWRITE_IE_ACCEPT_HEADER=False) for user_agent in (MSIE_9_USER_AGENT, MSIE_8_USER_AGENT, diff --git a/djangorestframework/tests/authentication.py b/djangorestframework/tests/authentication.py index 248bd87a..04ac471a 100644 --- a/djangorestframework/tests/authentication.py +++ b/djangorestframework/tests/authentication.py @@ -6,19 +6,19 @@ from django.test import Client, TestCase from django.utils import simplejson as json from djangorestframework.compat import RequestFactory -from djangorestframework.resource import Resource +from djangorestframework.views import BaseView from djangorestframework import permissions import base64 -class MockResource(Resource): +class MockView(BaseView): permissions = ( permissions.IsAuthenticated, ) def post(self, request): return {'a':1, 'b':2, 'c':3} urlpatterns = patterns('', - (r'^$', MockResource.as_view()), + (r'^$', MockView.as_view()), ) diff --git a/djangorestframework/tests/breadcrumbs.py b/djangorestframework/tests/breadcrumbs.py index 2f9a7e9d..1fd75634 100644 --- a/djangorestframework/tests/breadcrumbs.py +++ b/djangorestframework/tests/breadcrumbs.py @@ -1,21 +1,21 @@ from django.conf.urls.defaults import patterns, url from django.test import TestCase from djangorestframework.utils.breadcrumbs import get_breadcrumbs -from djangorestframework.resource import Resource +from djangorestframework.views import BaseView -class Root(Resource): +class Root(BaseView): pass -class ResourceRoot(Resource): +class ResourceRoot(BaseView): pass -class ResourceInstance(Resource): +class ResourceInstance(BaseView): pass -class NestedResourceRoot(Resource): +class NestedResourceRoot(BaseView): pass -class NestedResourceInstance(Resource): +class NestedResourceInstance(BaseView): pass urlpatterns = patterns('', diff --git a/djangorestframework/tests/description.py b/djangorestframework/tests/description.py index d34e2d11..d5a1102f 100644 --- a/djangorestframework/tests/description.py +++ b/djangorestframework/tests/description.py @@ -1,5 +1,5 @@ from django.test import TestCase -from djangorestframework.resource import Resource +from djangorestframework.views import BaseView from djangorestframework.compat import apply_markdown from djangorestframework.utils.description import get_name, get_description @@ -32,23 +32,23 @@ MARKED_DOWN = """<h2>an example docstring</h2> <h2 id="hash_style_header">hash style header</h2>""" -class TestResourceNamesAndDescriptions(TestCase): +class TestViewNamesAndDescriptions(TestCase): def test_resource_name_uses_classname_by_default(self): """Ensure Resource names are based on the classname by default.""" - class MockResource(Resource): + class MockView(BaseView): pass - self.assertEquals(get_name(MockResource()), 'Mock Resource') + self.assertEquals(get_name(MockView()), 'Mock View') def test_resource_name_can_be_set_explicitly(self): """Ensure Resource names can be set using the 'name' class attribute.""" example = 'Some Other Name' - class MockResource(Resource): + class MockView(BaseView): name = example - self.assertEquals(get_name(MockResource()), example) + self.assertEquals(get_name(MockView()), example) def test_resource_description_uses_docstring_by_default(self): """Ensure Resource names are based on the docstring by default.""" - class MockResource(Resource): + class MockView(BaseView): """an example docstring ==================== @@ -64,28 +64,28 @@ class TestResourceNamesAndDescriptions(TestCase): # hash style header #""" - self.assertEquals(get_description(MockResource()), DESCRIPTION) + self.assertEquals(get_description(MockView()), DESCRIPTION) def test_resource_description_can_be_set_explicitly(self): """Ensure Resource descriptions can be set using the 'description' class attribute.""" example = 'Some other description' - class MockResource(Resource): + class MockView(BaseView): """docstring""" description = example - self.assertEquals(get_description(MockResource()), example) + self.assertEquals(get_description(MockView()), example) def test_resource_description_does_not_require_docstring(self): """Ensure that empty docstrings do not affect the Resource's description if it has been set using the 'description' class attribute.""" example = 'Some other description' - class MockResource(Resource): + class MockView(BaseView): description = example - self.assertEquals(get_description(MockResource()), example) + self.assertEquals(get_description(MockView()), example) def test_resource_description_can_be_empty(self): """Ensure that if a resource has no doctring or 'description' class attribute, then it's description is the empty string""" - class MockResource(Resource): + class MockView(BaseView): pass - self.assertEquals(get_description(MockResource()), '') + self.assertEquals(get_description(MockView()), '') def test_markdown(self): """Ensure markdown to HTML works as expected""" diff --git a/djangorestframework/tests/files.py b/djangorestframework/tests/files.py index 4dc3aa40..f0321cb3 100644 --- a/djangorestframework/tests/files.py +++ b/djangorestframework/tests/files.py @@ -1,7 +1,7 @@ from django.test import TestCase from django import forms from djangorestframework.compat import RequestFactory -from djangorestframework.resource import Resource +from djangorestframework.views import BaseView import StringIO class UploadFilesTests(TestCase): @@ -15,7 +15,7 @@ class UploadFilesTests(TestCase): class FileForm(forms.Form): file = forms.FileField - class MockResource(Resource): + class MockView(BaseView): permissions = () form = FileForm @@ -26,7 +26,7 @@ class UploadFilesTests(TestCase): file = StringIO.StringIO('stuff') file.name = 'stuff.txt' request = self.factory.post('/', {'file': file}) - view = MockResource.as_view() + view = MockView.as_view() response = view(request) self.assertEquals(response.content, '{"FILE_CONTENT": "stuff", "FILE_NAME": "stuff.txt"}') diff --git a/djangorestframework/tests/parsers.py b/djangorestframework/tests/parsers.py index 00ebc812..049ac741 100644 --- a/djangorestframework/tests/parsers.py +++ b/djangorestframework/tests/parsers.py @@ -2,12 +2,12 @@ .. >>> from djangorestframework.parsers import FormParser >>> from djangorestframework.compat import RequestFactory - >>> from djangorestframework.resource import Resource + >>> from djangorestframework.views import BaseView >>> from StringIO import StringIO >>> from urllib import urlencode >>> req = RequestFactory().get('/') - >>> some_resource = Resource() - >>> some_resource.request = req # Make as if this request had been dispatched + >>> some_view = BaseView() + >>> some_view.request = req # Make as if this request had been dispatched FormParser ============ @@ -24,7 +24,7 @@ Here is some example data, which would eventually be sent along with a post requ Default behaviour for :class:`parsers.FormParser`, is to return a single value for each parameter : - >>> FormParser(some_resource).parse(StringIO(inpt)) == {'key1': 'bla1', 'key2': 'blo1'} + >>> FormParser(some_view).parse(StringIO(inpt)) == {'key1': 'bla1', 'key2': 'blo1'} True However, you can customize this behaviour by subclassing :class:`parsers.FormParser`, and overriding :meth:`parsers.FormParser.is_a_list` : @@ -36,7 +36,7 @@ However, you can customize this behaviour by subclassing :class:`parsers.FormPar This new parser only flattens the lists of parameters that contain a single value. - >>> MyFormParser(some_resource).parse(StringIO(inpt)) == {'key1': 'bla1', 'key2': ['blo1', 'blo2']} + >>> MyFormParser(some_view).parse(StringIO(inpt)) == {'key1': 'bla1', 'key2': ['blo1', 'blo2']} True .. note:: The same functionality is available for :class:`parsers.MultipartParser`. @@ -61,7 +61,7 @@ The browsers usually strip the parameter completely. A hack to avoid this, and t :class:`parsers.FormParser` strips the values ``_empty`` from all the lists. - >>> MyFormParser(some_resource).parse(StringIO(inpt)) == {'key1': 'blo1'} + >>> MyFormParser(some_view).parse(StringIO(inpt)) == {'key1': 'blo1'} True Oh ... but wait a second, the parameter ``key2`` isn't even supposed to be a list, so the parser just stripped it. @@ -71,7 +71,7 @@ Oh ... but wait a second, the parameter ``key2`` isn't even supposed to be a lis ... def is_a_list(self, key, val_list): ... return key == 'key2' ... - >>> MyFormParser(some_resource).parse(StringIO(inpt)) == {'key1': 'blo1', 'key2': []} + >>> MyFormParser(some_view).parse(StringIO(inpt)) == {'key1': 'blo1', 'key2': []} True Better like that. Note that you can configure something else than ``_empty`` for the empty value by setting :attr:`parsers.FormParser.EMPTY_VALUE`. @@ -81,7 +81,7 @@ from tempfile import TemporaryFile from django.test import TestCase from djangorestframework.compat import RequestFactory from djangorestframework.parsers import MultipartParser -from djangorestframework.resource import Resource +from djangorestframework.views import BaseView from djangorestframework.utils.mediatypes import MediaType from StringIO import StringIO @@ -122,9 +122,9 @@ class TestMultipartParser(TestCase): def test_multipartparser(self): """Ensure that MultipartParser can parse multipart/form-data that contains a mix of several files and parameters.""" post_req = RequestFactory().post('/', self.body, content_type=self.content_type) - resource = Resource() - resource.request = post_req - parsed = MultipartParser(resource).parse(StringIO(self.body)) + view = BaseView() + view.request = post_req + parsed = MultipartParser(view).parse(StringIO(self.body)) self.assertEqual(parsed['key1'], 'val1') self.assertEqual(parsed.FILES['file1'].read(), 'blablabla') diff --git a/djangorestframework/tests/reverse.py b/djangorestframework/tests/reverse.py index 49939d0e..7026d4a4 100644 --- a/djangorestframework/tests/reverse.py +++ b/djangorestframework/tests/reverse.py @@ -3,10 +3,10 @@ from django.core.urlresolvers import reverse from django.test import TestCase from django.utils import simplejson as json -from djangorestframework.resource import Resource +from djangorestframework.views import BaseView -class MockResource(Resource): +class MockView(BaseView): """Mock resource which simply returns a URL, so that we can ensure that reversed URLs are fully qualified""" permissions = () @@ -14,8 +14,8 @@ class MockResource(Resource): return reverse('another') urlpatterns = patterns('', - url(r'^$', MockResource.as_view()), - url(r'^another$', MockResource.as_view(), name='another'), + url(r'^$', MockView.as_view()), + url(r'^another$', MockView.as_view(), name='another'), ) diff --git a/djangorestframework/tests/throttling.py b/djangorestframework/tests/throttling.py index 46383271..94d01428 100644 --- a/djangorestframework/tests/throttling.py +++ b/djangorestframework/tests/throttling.py @@ -3,11 +3,11 @@ from django.test import TestCase from django.utils import simplejson as json from djangorestframework.compat import RequestFactory -from djangorestframework.resource import Resource +from djangorestframework.views import BaseView from djangorestframework.permissions import Throttling -class MockResource(Resource): +class MockView(BaseView): permissions = ( Throttling, ) throttle = (3, 1) # 3 requests per second @@ -15,7 +15,7 @@ class MockResource(Resource): return 'foo' urlpatterns = patterns('', - (r'^$', MockResource.as_view()), + (r'^$', MockView.as_view()), ) diff --git a/djangorestframework/tests/validators.py b/djangorestframework/tests/validators.py index b6563db6..52a675d2 100644 --- a/djangorestframework/tests/validators.py +++ b/djangorestframework/tests/validators.py @@ -4,6 +4,8 @@ from django.test import TestCase from djangorestframework.compat import RequestFactory from djangorestframework.validators import BaseValidator, FormValidator, ModelFormValidator from djangorestframework.response import ErrorResponse +from djangorestframework.views import BaseView +from djangorestframework.resource import Resource class TestValidatorMixinInterfaces(TestCase): @@ -20,7 +22,7 @@ class TestDisabledValidations(TestCase): def test_disabled_form_validator_returns_content_unchanged(self): """If the view's form attribute is None then FormValidator(view).validate(content) should just return the content unmodified.""" - class DisabledFormView(object): + class DisabledFormView(BaseView): form = None view = DisabledFormView() @@ -30,7 +32,7 @@ class TestDisabledValidations(TestCase): def test_disabled_form_validator_get_bound_form_returns_none(self): """If the view's form attribute is None on then FormValidator(view).get_bound_form(content) should just return None.""" - class DisabledFormView(object): + class DisabledFormView(BaseView): form = None view = DisabledFormView() @@ -39,11 +41,10 @@ class TestDisabledValidations(TestCase): def test_disabled_model_form_validator_returns_content_unchanged(self): - """If the view's form and model attributes are None then + """If the view's form is None and does not have a Resource with a model set then ModelFormValidator(view).validate(content) should just return the content unmodified.""" - class DisabledModelFormView(object): + class DisabledModelFormView(BaseView): form = None - model = None view = DisabledModelFormView() content = {'qwerty':'uiop'} @@ -51,13 +52,12 @@ class TestDisabledValidations(TestCase): def test_disabled_model_form_validator_get_bound_form_returns_none(self): """If the form attribute is None on FormValidatorMixin then get_bound_form(content) should just return None.""" - class DisabledModelFormView(object): - form = None + class DisabledModelFormView(BaseView): model = None view = DisabledModelFormView() content = {'qwerty':'uiop'} - self.assertEqual(ModelFormValidator(view).get_bound_form(content), None)# + self.assertEqual(ModelFormValidator(view).get_bound_form(content), None) class TestNonFieldErrors(TestCase): """Tests against form validation errors caused by non-field errors. (eg as might be caused by some custom form validation)""" @@ -84,7 +84,7 @@ class TestNonFieldErrors(TestCase): except ErrorResponse, exc: self.assertEqual(exc.response.raw_content, {'errors': [MockForm.ERROR_TEXT]}) else: - self.fail('ResourceException was not raised') #pragma: no cover + self.fail('ErrorResponse was not raised') #pragma: no cover class TestFormValidation(TestCase): @@ -95,11 +95,11 @@ class TestFormValidation(TestCase): class MockForm(forms.Form): qwerty = forms.CharField(required=True) - class MockFormView(object): + class MockFormView(BaseView): form = MockForm validators = (FormValidator,) - class MockModelFormView(object): + class MockModelFormView(BaseView): form = MockForm validators = (ModelFormValidator,) @@ -264,9 +264,12 @@ class TestModelFormValidator(TestCase): @property def readonly(self): return 'read only' - - class MockView(object): + + class MockResource(Resource): model = MockModel + + class MockView(BaseView): + resource = MockResource self.validator = ModelFormValidator(MockView) diff --git a/djangorestframework/tests/views.py b/djangorestframework/tests/views.py index 9e2e893f..598712d2 100644 --- a/djangorestframework/tests/views.py +++ b/djangorestframework/tests/views.py @@ -3,7 +3,7 @@ from django.test import TestCase from django.test import Client -urlpatterns = patterns('djangorestframework.views', +urlpatterns = patterns('djangorestframework.utils.staticviews', url(r'^robots.txt$', 'deny_robots'), url(r'^favicon.ico$', 'favicon'), url(r'^accounts/login$', 'api_login'), |
