Commits

Jesper Nøhr committed 4ce0f31

adding 2 tests with abstract base models

Comments (0)

Files changed (5)

examples/blogserver/api/handlers.py

 
 class AnonymousBlogpostHandler(AnonymousBaseHandler):
     model = Blogpost
-    fields = ('title', 'content', 'created_on')
+    fields = ('id', 'title', 'content', 'created_on')
 
 class BlogpostHandler(BaseHandler):
     model = Blogpost

tests/test_project/apps/testapp/handlers.py

 from piston.handler import BaseHandler
 from piston.utils import rc, validate
 
-from models import TestModel, ExpressiveTestModel, Comment
+from models import TestModel, ExpressiveTestModel, Comment, InheritedModel
 
 class EntryHandler(BaseHandler):
     model = TestModel
             return rc.CREATED
         else:
             super(ExpressiveTestModel, self).create(request)
+            
+            
+class AbstractHandler(BaseHandler):
+    fields = ('id', 'some_other', 'some_field')
+    model = InheritedModel
+    
+    def read(self, request, id_=None):
+        if id_:
+            return self.model.objects.get(pk=id_)
+        else:
+            return super(AbstractHandler, self).read(request)
             

tests/test_project/apps/testapp/models.py

 class Comment(models.Model):
     parent = models.ForeignKey(ExpressiveTestModel, related_name='comments')
     content = models.TextField()
-    
+
+class AbstractModel(models.Model):
+    some_field = models.CharField(max_length=32, default='something here')
+    
+    class Meta:
+        abstract = True
+        
+class InheritedModel(AbstractModel):
+    some_other = models.CharField(max_length=32, default='something else')
+    
+    class Meta:
+        db_table = 'testing_abstracts'

tests/test_project/apps/testapp/tests.py

 
 import base64
 
-from test_project.apps.testapp.models import TestModel, ExpressiveTestModel, Comment
+from test_project.apps.testapp.models import TestModel, ExpressiveTestModel, Comment, InheritedModel
 
 class MainTests(TestCase):
     def setUp(self):
                 HTTP_AUTHORIZATION=self.auth_string).content
         self.assertEquals(expected, result)
 
+class AbstractBaseClassTests(MainTests):
+    def init_delegate(self):
+        self.ab1 = InheritedModel()
+        self.ab1.save()
+        self.ab2 = InheritedModel()
+        self.ab2.save()
+        
+    def test_field_presence(self):
+        result = self.client.get('/api/abstract.json',
+                HTTP_AUTHORIZATION=self.auth_string).content
+                
+        expected = '[{"id": 1, "some_other": "something else", "some_field": "something here"}, {"id": 2, "some_other": "something else", "some_field": "something here"}]'
+        
+        self.assertEquals(result, expected)
+
+    def test_specific_id(self):
+        ids = (1, 2)
+        be = '{"id": %d, "some_other": "something else", "some_field": "something here"}'
+        
+        for id_ in ids:
+            result = self.client.get('/api/abstract/%d.json' % id_,
+                    HTTP_AUTHORIZATION=self.auth_string).content
+                    
+            expected = be % id_
+                    
+            self.assertEquals(result, expected)
+
 class IncomingExpressiveTests(MainTests):
     def init_delegate(self):
         e1 = ExpressiveTestModel(title="foo", content="bar")

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