diff options
| author | tom christie tom@tomchristie.com | 2010-12-30 01:02:11 +0000 |
|---|---|---|
| committer | tom christie tom@tomchristie.com | 2010-12-30 01:02:11 +0000 |
| commit | 8a12f89aaacfc0839d6ab1e62b4b5046930517ba (patch) | |
| tree | 3f0b4fd095d93c5b003832b5e19ecf7f670924ab /src/testapp | |
| parent | 650111dc8c0800e5b7d4c878c1d454657b68efca (diff) | |
| download | django-rest-framework-8a12f89aaacfc0839d6ab1e62b4b5046930517ba.tar.bz2 | |
Added tests
Diffstat (limited to 'src/testapp')
| -rw-r--r-- | src/testapp/__init__.py | 0 | ||||
| -rw-r--r-- | src/testapp/models.py | 3 | ||||
| -rw-r--r-- | src/testapp/tests.py | 54 | ||||
| -rw-r--r-- | src/testapp/urls.py | 8 | ||||
| -rw-r--r-- | src/testapp/views.py | 21 |
5 files changed, 86 insertions, 0 deletions
diff --git a/src/testapp/__init__.py b/src/testapp/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/src/testapp/__init__.py diff --git a/src/testapp/models.py b/src/testapp/models.py new file mode 100644 index 00000000..71a83623 --- /dev/null +++ b/src/testapp/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/src/testapp/tests.py b/src/testapp/tests.py new file mode 100644 index 00000000..5543cd96 --- /dev/null +++ b/src/testapp/tests.py @@ -0,0 +1,54 @@ +""" +This file demonstrates two different styles of tests (one doctest and one +unittest). These will both pass when you run "manage.py test". + +Replace these with more appropriate tests for your application. +""" + +from django.test import TestCase +from django.core.urlresolvers import reverse +from testapp.views import ReadOnlyResource, MirroringWriteResource + +class AcceptHeaderTests(TestCase): + def assert_accept_mimetype(self, mimetype, expect=None, expect_match=True): + """ + Assert that a request with given mimetype in the accept header, + gives a response with the appropriate content-type. + """ + if expect is None: + expect = mimetype + + resp = self.client.get(reverse(ReadOnlyResource), HTTP_ACCEPT=mimetype) + + if expect_match: + self.assertEquals(resp['content-type'], expect) + else: + self.assertNotEquals(resp['content-type'], expect) + + def test_accept_xml(self): + self.assert_accept_mimetype('application/xml') + + def test_accept_json(self): + self.assert_accept_mimetype('application/json') + + def test_accept_xml_prefered_to_json(self): + self.assert_accept_mimetype('application/xml,q=0.9;application/json,q=0.1', expect='application/xml') + + def test_accept_json_prefered_to_xml(self): + self.assert_accept_mimetype('application/json,q=0.9;application/xml,q=0.1', expect='application/json') + + def test_dont_accept_invalid(self): + self.assert_accept_mimetype('application/invalid', expect_match=False) + + def test_invalid_accept_header_returns_406(self): + resp = self.client.get(reverse(ReadOnlyResource), HTTP_ACCEPT='invalid/invalid') + self.assertEquals(resp.status_code, 406) + +class AllowedMethodsTests(TestCase): + def test_write_on_read_only_resource_returns_405(self): + resp = self.client.put(reverse(ReadOnlyResource), {}) + self.assertEquals(resp.status_code, 405) + + def test_read_on_write_only_resource_returns_405(self): + resp = self.client.get(reverse(MirroringWriteResource)) + self.assertEquals(resp.status_code, 405) diff --git a/src/testapp/urls.py b/src/testapp/urls.py new file mode 100644 index 00000000..a41c156b --- /dev/null +++ b/src/testapp/urls.py @@ -0,0 +1,8 @@ +from django.conf.urls.defaults import patterns +from testapp.views import ReadOnlyResource, MirroringWriteResource + + +urlpatterns = patterns('', + (r'^read-only$', ReadOnlyResource), + (r'^mirroring-write$', MirroringWriteResource), +) diff --git a/src/testapp/views.py b/src/testapp/views.py new file mode 100644 index 00000000..f0174414 --- /dev/null +++ b/src/testapp/views.py @@ -0,0 +1,21 @@ +from decimal import Decimal +from rest.resource import Resource + +class ReadOnlyResource(Resource): + """This is my docstring + """ + allowed_methods = ('GET',) + + def read(self, headers={}, *args, **kwargs): + return (200, {'ExampleString': 'Example', + 'ExampleInt': 1, + 'ExampleDecimal': 1.0}, {}) + + +class MirroringWriteResource(Resource): + """This is my docstring + """ + allowed_methods = ('PUT',) + + def create(self, data, headers={}, *args, **kwargs): + return (200, data, {}) |
