aboutsummaryrefslogtreecommitdiffstats
path: root/src/testapp
diff options
context:
space:
mode:
authorTom Christie2011-01-04 17:42:23 +0000
committerTom Christie2011-01-04 17:42:23 +0000
commitf144b769fedd421f3ec24dfd3a4f10c681192337 (patch)
treea3ebc0fe0cdd38aad6e3a40f04d62b49fbee51d5 /src/testapp
parent48c7171aa05cd69ab8d9cd6f3a8eed52f18792a4 (diff)
downloaddjango-rest-framework-f144b769fedd421f3ec24dfd3a4f10c681192337.tar.bz2
Lots of good form validation and default actions
Diffstat (limited to 'src/testapp')
-rw-r--r--src/testapp/models.py30
-rw-r--r--src/testapp/tests.py87
-rw-r--r--src/testapp/urls.py3
-rw-r--r--src/testapp/views.py37
4 files changed, 127 insertions, 30 deletions
diff --git a/src/testapp/models.py b/src/testapp/models.py
index 71a83623..75304c9c 100644
--- a/src/testapp/models.py
+++ b/src/testapp/models.py
@@ -1,3 +1,31 @@
from django.db import models
+import uuid
-# Create your models here.
+def uuid_str():
+ return str(uuid.uuid1())
+
+class ExampleModel(models.Model):
+ num = models.IntegerField(default=2, choices=((1,'one'), (2, 'two')))
+ hidden_num = models.IntegerField(verbose_name='Something', help_text='HELP')
+ text = models.TextField(blank=False)
+ another = models.CharField(max_length=10)
+
+
+class ExampleContainer(models.Model):
+ """Container. Has a key, a name, and some internal data, and contains a set of items."""
+ key = models.CharField(primary_key=True, default=uuid_str, max_length=36, editable=False)
+ name = models.CharField(max_length=256)
+ internal = models.IntegerField(default=0)
+
+ @models.permalink
+ def get_absolute_url(self):
+ return ('testapp.views.ContainerInstance', [self.key])
+
+
+class ExampleItem(models.Model):
+ """Item. Belongs to a container and has an index number and a note.
+ Items are uniquely identified by their container and index number."""
+ container = models.ForeignKey(ExampleContainer, related_name='items')
+ index = models.IntegerField()
+ note = models.CharField(max_length=1024)
+ unique_together = (container, index) \ No newline at end of file
diff --git a/src/testapp/tests.py b/src/testapp/tests.py
index c4e7dee3..0e2cde63 100644
--- a/src/testapp/tests.py
+++ b/src/testapp/tests.py
@@ -9,7 +9,8 @@ from django.test import TestCase
from django.core.urlresolvers import reverse
from testapp import views
import json
-from rest.utils import xml2dict, dict2xml
+#from rest.utils import xml2dict, dict2xml
+
class AcceptHeaderTests(TestCase):
def assert_accept_mimetype(self, mimetype, expect=None, expect_match=True):
@@ -45,6 +46,10 @@ class AcceptHeaderTests(TestCase):
def test_invalid_accept_header_returns_406(self):
resp = self.client.get(reverse(views.ReadOnlyResource), HTTP_ACCEPT='invalid/invalid')
self.assertEquals(resp.status_code, 406)
+
+ def test_prefer_specific(self):
+ self.fail("Test not implemented")
+
class AllowedMethodsTests(TestCase):
def test_reading_read_only_allowed(self):
@@ -63,6 +68,7 @@ class AllowedMethodsTests(TestCase):
resp = self.client.put(reverse(views.WriteOnlyResource), {})
self.assertEquals(resp.status_code, 200)
+
class EncodeDecodeTests(TestCase):
def setUp(self):
super(self.__class__, self).setUp()
@@ -70,36 +76,71 @@ class EncodeDecodeTests(TestCase):
def test_encode_form_decode_json(self):
content = self.input
- resp = self.client.put(reverse(views.WriteOnlyResource), content, HTTP_ACCEPT='application/json')
+ resp = self.client.put(reverse(views.WriteOnlyResource), content)
output = json.loads(resp.content)
self.assertEquals(self.input, output)
def test_encode_json_decode_json(self):
content = json.dumps(self.input)
- resp = self.client.put(reverse(views.WriteOnlyResource), content, 'application/json', HTTP_ACCEPT='application/json')
+ resp = self.client.put(reverse(views.WriteOnlyResource), content, 'application/json')
output = json.loads(resp.content)
self.assertEquals(self.input, output)
- def test_encode_xml_decode_json(self):
- content = dict2xml(self.input)
- resp = self.client.put(reverse(views.WriteOnlyResource), content, 'application/json', HTTP_ACCEPT='application/json')
+ #def test_encode_xml_decode_json(self):
+ # content = dict2xml(self.input)
+ # resp = self.client.put(reverse(views.WriteOnlyResource), content, 'application/json', HTTP_ACCEPT='application/json')
+ # output = json.loads(resp.content)
+ # self.assertEquals(self.input, output)
+
+ #def test_encode_form_decode_xml(self):
+ # content = self.input
+ # resp = self.client.put(reverse(views.WriteOnlyResource), content, HTTP_ACCEPT='application/xml')
+ # output = xml2dict(resp.content)
+ # self.assertEquals(self.input, output)
+
+ #def test_encode_json_decode_xml(self):
+ # content = json.dumps(self.input)
+ # resp = self.client.put(reverse(views.WriteOnlyResource), content, 'application/json', HTTP_ACCEPT='application/xml')
+ # output = xml2dict(resp.content)
+ # self.assertEquals(self.input, output)
+
+ #def test_encode_xml_decode_xml(self):
+ # content = dict2xml(self.input)
+ # resp = self.client.put(reverse(views.WriteOnlyResource), content, 'application/json', HTTP_ACCEPT='application/xml')
+ # output = xml2dict(resp.content)
+ # self.assertEquals(self.input, output)
+
+class ModelTests(TestCase):
+ def test_create_container(self):
+ content = json.dumps({'name': 'example'})
+ resp = self.client.post(reverse(views.ContainerFactory), content, 'application/json')
output = json.loads(resp.content)
- self.assertEquals(self.input, output)
-
- def test_encode_form_decode_xml(self):
- content = self.input
- resp = self.client.put(reverse(views.WriteOnlyResource), content, HTTP_ACCEPT='application/xml')
- output = xml2dict(resp.content)
- self.assertEquals(self.input, output)
+ self.assertEquals(resp.status_code, 201)
+ self.assertEquals(output['name'], 'example')
+ self.assertEquals(set(output.keys()), set(('absolute_uri', 'name', 'key')))
- def test_encode_json_decode_xml(self):
- content = json.dumps(self.input)
- resp = self.client.put(reverse(views.WriteOnlyResource), content, 'application/json', HTTP_ACCEPT='application/xml')
- output = xml2dict(resp.content)
- self.assertEquals(self.input, output)
+class CreatedModelTests(TestCase):
+ def setUp(self):
+ content = json.dumps({'name': 'example'})
+ resp = self.client.post(reverse(views.ContainerFactory), content, 'application/json', HTTP_ACCEPT='application/json')
+ self.container = json.loads(resp.content)
- def test_encode_xml_decode_xml(self):
- content = dict2xml(self.input)
- resp = self.client.put(reverse(views.WriteOnlyResource), content, 'application/json', HTTP_ACCEPT='application/xml')
- output = xml2dict(resp.content)
- self.assertEquals(self.input, output) \ No newline at end of file
+ def test_read_container(self):
+ resp = self.client.get(self.container["absolute_uri"])
+ self.assertEquals(resp.status_code, 200)
+ container = json.loads(resp.content)
+ self.assertEquals(container, self.container)
+
+ def test_delete_container(self):
+ resp = self.client.delete(self.container["absolute_uri"])
+ self.assertEquals(resp.status_code, 204)
+ self.assertEquals(resp.content, '')
+
+ def test_update_container(self):
+ self.container['name'] = 'new'
+ content = json.dumps(self.container)
+ resp = self.client.put(self.container["absolute_uri"], content, 'application/json')
+ self.assertEquals(resp.status_code, 200)
+ container = json.loads(resp.content)
+ self.assertEquals(container, self.container)
+ \ No newline at end of file
diff --git a/src/testapp/urls.py b/src/testapp/urls.py
index bbdde8a3..b90590db 100644
--- a/src/testapp/urls.py
+++ b/src/testapp/urls.py
@@ -5,4 +5,7 @@ urlpatterns = patterns('testapp.views',
(r'^read-only$', 'ReadOnlyResource'),
(r'^write-only$', 'WriteOnlyResource'),
(r'^read-write$', 'ReadWriteResource'),
+ (r'^model$', 'ModelFormResource'),
+ (r'^container$', 'ContainerFactory'),
+ (r'^container/((?P<key>[^/]+))$', 'ContainerInstance'),
)
diff --git a/src/testapp/views.py b/src/testapp/views.py
index d9160af6..f121efa3 100644
--- a/src/testapp/views.py
+++ b/src/testapp/views.py
@@ -1,21 +1,24 @@
-from rest.resource import Resource
+from rest.resource import Resource, ModelResource
from testapp.forms import ExampleForm
+from testapp.models import ExampleModel, ExampleContainer
class RootResource(Resource):
"""This is my docstring
"""
- allowed_methods = ('GET',)
+ allowed_operations = ('read',)
def read(self, headers={}, *args, **kwargs):
return (200, {'read-only-api': self.reverse(ReadOnlyResource),
'write-only-api': self.reverse(WriteOnlyResource),
- 'read-write-api': self.reverse(ReadWriteResource)}, {})
+ 'read-write-api': self.reverse(ReadWriteResource),
+ 'model-api': self.reverse(ModelFormResource),
+ 'create-container': self.reverse(ContainerFactory)}, {})
class ReadOnlyResource(Resource):
"""This is my docstring
"""
- allowed_methods = ('GET',)
+ allowed_operations = ('read',)
def read(self, headers={}, *args, **kwargs):
return (200, {'ExampleString': 'Example',
@@ -26,13 +29,35 @@ class ReadOnlyResource(Resource):
class WriteOnlyResource(Resource):
"""This is my docstring
"""
- allowed_methods = ('PUT',)
+ allowed_operations = ('update',)
def update(self, data, headers={}, *args, **kwargs):
return (200, data, {})
class ReadWriteResource(Resource):
- allowed_methods = ('GET', 'PUT', 'DELETE')
+ allowed_operations = ('read', 'update', 'delete')
create_form = ExampleForm
update_form = ExampleForm
+
+
+class ModelFormResource(ModelResource):
+ allowed_operations = ('read', 'update', 'delete')
+ model = ExampleModel
+
+# Nice things: form validation is applied to any input type
+# html forms for output
+# output always serialized nicely
+class ContainerFactory(ModelResource):
+ allowed_operations = ('create',)
+ model = ExampleContainer
+ fields = ('absolute_uri', 'name', 'key')
+ form_fields = ('name',)
+
+
+class ContainerInstance(ModelResource):
+ allowed_operations = ('read', 'update', 'delete')
+ model = ExampleContainer
+ fields = ('absolute_uri', 'name', 'key')
+ form_fields = ('name',)
+