Commits

Benjamin Pollack committed 44b2601

add tests for plain object serialization

Comments (0)

Files changed (4)

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
+from models import TestModel, ExpressiveTestModel, Comment, InheritedModel, PlainOldObject
 from forms import EchoForm
 from test_project.apps.testapp import signals
 
         else:
             return super(AbstractHandler, self).read(request)
 
+class PlainOldObjectHandler(BaseHandler):
+    allowed_methods = ('GET',)
+    fields = ('type', 'field')
+    model = PlainOldObject
+    
+    def read(self, request):
+        return self.model()
 
 class EchoHandler(BaseHandler):
     allowed_methods = ('GET', )

tests/test_project/apps/testapp/models.py

     some_other = models.CharField(max_length=32, default='something else')
     
     class Meta:
-        db_table = 'testing_abstracts'
+        db_table = 'testing_abstracts'
+
+class PlainOldObject(object):
+    def __emittable__(self):
+        return {'type': 'plain',
+                'field': 'a field'}

tests/test_project/apps/testapp/tests.py

         resp = self.client.get('/api/echo', data)
         self.assertEquals(resp.status_code, 200)
         self.assertEquals(data, simplejson.loads(resp.content))
+
+class PlainOldObject(MainTests):
+    def test_plain_object_serialization(self):
+        resp = self.client.get('/api/popo')
+        self.assertEquals(resp.status_code, 200)
+        self.assertEquals({'type': 'plain', 'field': 'a field'}, simplejson.loads(resp.content))
+        

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
+from test_project.apps.testapp.handlers import EntryHandler, ExpressiveHandler, AbstractHandler, EchoHandler, PlainOldObjectHandler
 
 auth = HttpBasicAuthentication(realm='TestApplication')
 
 expressive = Resource(handler=ExpressiveHandler, authentication=auth)
 abstract = Resource(handler=AbstractHandler, authentication=auth)
 echo = Resource(handler=EchoHandler)
+popo = Resource(handler=PlainOldObjectHandler)
 
 
 urlpatterns = patterns('',
     url(r'^abstract/(?P<id_>\d+)\.(?P<emitter_format>.+)$', abstract),
 
     url(r'^echo$', echo),
+    
+    url(r'^popo$', popo),
 )