Commits

James Emerton committed fa0e604

Added tests for list_fields handler attribute

  • Participants
  • Parent commits 7b3bf59

Comments (0)

Files changed (4)

File tests/test_project/apps/testapp/handlers.py

 from piston.handler import BaseHandler
 from piston.utils import rc, validate
 
-from models import TestModel, ExpressiveTestModel, Comment, InheritedModel, PlainOldObject
+from models import TestModel, ExpressiveTestModel, Comment, InheritedModel, PlainOldObject, ListFieldsModel
 from forms import EchoForm
 from test_project.apps.testapp import signals
 
     @validate(EchoForm, 'GET')
     def read(self, request):
         return {'msg': request.GET['msg']}
+
+class ListFieldsHandler(BaseHandler):
+    model = ListFieldsModel
+    fields = ('id','kind','variety','color')
+    list_fields = ('id','variety')
+

File tests/test_project/apps/testapp/models.py

 class PlainOldObject(object):
     def __emittable__(self):
         return {'type': 'plain',
-                'field': 'a field'}
+                'field': 'a field'}
+
+
+class ListFieldsModel(models.Model):
+    kind = models.CharField(max_length=15)
+    variety = models.CharField(max_length=15)
+    color = models.CharField(max_length=15)

File tests/test_project/apps/testapp/tests.py

 
 import urllib, base64
 
-from test_project.apps.testapp.models import TestModel, ExpressiveTestModel, Comment, InheritedModel
+from test_project.apps.testapp.models import TestModel, ExpressiveTestModel, Comment, InheritedModel, ListFieldsModel
 from test_project.apps.testapp import signals
 
 class MainTests(TestCase):
         self.assertEquals(resp.status_code, 200)
         self.assertEquals({'type': 'plain', 'field': 'a field'}, simplejson.loads(resp.content))
         
+
+
+class ListFieldsTest(MainTests):
+    def init_delegate(self):
+        ListFieldsModel(kind='fruit', variety='apple', color='green').save()
+        ListFieldsModel(kind='vegetable', variety='carrot', color='orange').save()
+        ListFieldsModel(kind='animal', variety='dog', color='brown').save()
+
+    def test_single_item(self):
+        expect = '''{
+    "color": "green", 
+    "kind": "fruit", 
+    "id": 1, 
+    "variety": "apple"
+}'''
+        resp = self.client.get('/api/list_fields/1')
+        self.assertEquals(resp.status_code, 200)
+        self.assertEquals(resp.content, expect)
+
+
+    def test_multiple_items(self):
+        expect = '''[
+    {
+        "id": 1, 
+        "variety": "apple"
+    }, 
+    {
+        "id": 2, 
+        "variety": "carrot"
+    }, 
+    {
+        "id": 3, 
+        "variety": "dog"
+    }
+]'''
+        resp = self.client.get('/api/list_fields')
+        self.assertEquals(resp.status_code, 200)
+        self.assertEquals(resp.content, expect)
+        

File tests/test_project/apps/testapp/urls.py

 from piston.resource import Resource
 from piston.authentication import HttpBasicAuthentication
 
-from test_project.apps.testapp.handlers import EntryHandler, ExpressiveHandler, AbstractHandler, EchoHandler, PlainOldObjectHandler
+from test_project.apps.testapp.handlers import EntryHandler, ExpressiveHandler, AbstractHandler, EchoHandler, PlainOldObjectHandler, ListFieldsHandler
 
 auth = HttpBasicAuthentication(realm='TestApplication')
 
 abstract = Resource(handler=AbstractHandler, authentication=auth)
 echo = Resource(handler=EchoHandler)
 popo = Resource(handler=PlainOldObjectHandler)
+list_fields = Resource(handler=ListFieldsHandler)
 
 
 urlpatterns = patterns('',
     url(r'^oauth/request_token$', 'piston.authentication.oauth_request_token'),
     url(r'^oauth/authorize$', 'piston.authentication.oauth_user_auth'),
     url(r'^oauth/access_token$', 'piston.authentication.oauth_access_token'),
+
+    url(r'^list_fields$', list_fields),
+    url(r'^list_fields/(?P<id>.+)$', list_fields),
     
     url(r'^popo$', popo),
 )