Added tests for list_fields handler attribute

This commit is contained in:
James Emerton 2009-07-17 10:39:22 -07:00
Родитель f2551e5a68
Коммит c934ccdca7
4 изменённых файлов: 59 добавлений и 4 удалений

Просмотреть файл

@ -3,7 +3,7 @@ from django.core.paginator import Paginator
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
@ -76,3 +76,9 @@ class EchoHandler(BaseHandler):
@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')

Просмотреть файл

@ -28,4 +28,10 @@ class InheritedModel(AbstractModel):
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)

Просмотреть файл

@ -15,7 +15,7 @@ except ImportError:
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):
@ -371,3 +371,42 @@ class PlainOldObject(MainTests):
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)

Просмотреть файл

@ -2,7 +2,7 @@ from django.conf.urls.defaults import *
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')
@ -11,6 +11,7 @@ expressive = Resource(handler=ExpressiveHandler, authentication=auth)
abstract = Resource(handler=AbstractHandler, authentication=auth)
echo = Resource(handler=EchoHandler)
popo = Resource(handler=PlainOldObjectHandler)
list_fields = Resource(handler=ListFieldsHandler)
urlpatterns = patterns('',
@ -30,6 +31,9 @@ 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),
)