2012-10-04 16:58:28 +04:00
|
|
|
from django.conf.urls.defaults import patterns, url
|
2012-10-04 14:26:41 +04:00
|
|
|
from django.test import TestCase
|
|
|
|
from django.test.client import RequestFactory
|
|
|
|
from rest_framework import generics, status, serializers
|
2012-10-30 14:35:30 +04:00
|
|
|
from rest_framework.tests.models import Anchor, BasicModel, ManyToManyModel, BlogPost, BlogPostComment
|
2012-10-04 14:26:41 +04:00
|
|
|
|
|
|
|
factory = RequestFactory()
|
|
|
|
|
|
|
|
|
2012-10-30 14:35:30 +04:00
|
|
|
class BlogPostCommentSerializer(serializers.Serializer):
|
|
|
|
text = serializers.CharField()
|
|
|
|
blog_post_url = serializers.HyperlinkedRelatedField(source='blog_post', view_name='blogpost-detail', queryset=BlogPost.objects.all())
|
|
|
|
|
|
|
|
def restore_object(self, attrs, instance=None):
|
|
|
|
return BlogPostComment(**attrs)
|
|
|
|
|
|
|
|
|
2012-10-04 14:26:41 +04:00
|
|
|
class BasicList(generics.ListCreateAPIView):
|
|
|
|
model = BasicModel
|
|
|
|
model_serializer_class = serializers.HyperlinkedModelSerializer
|
|
|
|
|
|
|
|
|
|
|
|
class BasicDetail(generics.RetrieveUpdateDestroyAPIView):
|
|
|
|
model = BasicModel
|
|
|
|
model_serializer_class = serializers.HyperlinkedModelSerializer
|
|
|
|
|
|
|
|
|
2012-10-04 19:58:18 +04:00
|
|
|
class AnchorDetail(generics.RetrieveAPIView):
|
|
|
|
model = Anchor
|
|
|
|
model_serializer_class = serializers.HyperlinkedModelSerializer
|
|
|
|
|
|
|
|
|
|
|
|
class ManyToManyList(generics.ListAPIView):
|
|
|
|
model = ManyToManyModel
|
|
|
|
model_serializer_class = serializers.HyperlinkedModelSerializer
|
|
|
|
|
|
|
|
|
|
|
|
class ManyToManyDetail(generics.RetrieveAPIView):
|
|
|
|
model = ManyToManyModel
|
|
|
|
model_serializer_class = serializers.HyperlinkedModelSerializer
|
|
|
|
|
|
|
|
|
2012-10-30 14:35:30 +04:00
|
|
|
class BlogPostCommentListCreate(generics.ListCreateAPIView):
|
|
|
|
model = BlogPostComment
|
|
|
|
model_serializer_class = BlogPostCommentSerializer
|
|
|
|
|
|
|
|
|
|
|
|
class BlogPostDetail(generics.RetrieveAPIView):
|
|
|
|
model = BlogPost
|
|
|
|
|
2012-10-04 14:26:41 +04:00
|
|
|
urlpatterns = patterns('',
|
|
|
|
url(r'^basic/$', BasicList.as_view(), name='basicmodel-list'),
|
|
|
|
url(r'^basic/(?P<pk>\d+)/$', BasicDetail.as_view(), name='basicmodel-detail'),
|
2012-10-04 19:58:18 +04:00
|
|
|
url(r'^anchor/(?P<pk>\d+)/$', AnchorDetail.as_view(), name='anchor-detail'),
|
|
|
|
url(r'^manytomany/$', ManyToManyList.as_view(), name='manytomanymodel-list'),
|
|
|
|
url(r'^manytomany/(?P<pk>\d+)/$', ManyToManyDetail.as_view(), name='manytomanymodel-detail'),
|
2012-10-30 14:35:30 +04:00
|
|
|
url(r'^posts/(?P<pk>\d+)/$', BlogPostDetail.as_view(), name='blogpost-detail'),
|
|
|
|
url(r'^comments/$', BlogPostCommentListCreate.as_view(), name='blogpostcomment-list')
|
2012-10-04 14:26:41 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2012-10-04 19:58:18 +04:00
|
|
|
class TestBasicHyperlinkedView(TestCase):
|
2012-10-04 14:26:41 +04:00
|
|
|
urls = 'rest_framework.tests.hyperlinkedserializers'
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
"""
|
|
|
|
Create 3 BasicModel intances.
|
|
|
|
"""
|
|
|
|
items = ['foo', 'bar', 'baz']
|
|
|
|
for item in items:
|
|
|
|
BasicModel(text=item).save()
|
|
|
|
self.objects = BasicModel.objects
|
|
|
|
self.data = [
|
|
|
|
{'url': 'http://testserver/basic/%d/' % obj.id, 'text': obj.text}
|
|
|
|
for obj in self.objects.all()
|
|
|
|
]
|
|
|
|
self.list_view = BasicList.as_view()
|
|
|
|
self.detail_view = BasicDetail.as_view()
|
|
|
|
|
|
|
|
def test_get_list_view(self):
|
|
|
|
"""
|
|
|
|
GET requests to ListCreateAPIView should return list of objects.
|
|
|
|
"""
|
2012-10-04 19:58:18 +04:00
|
|
|
request = factory.get('/basic/')
|
|
|
|
response = self.list_view(request).render()
|
|
|
|
self.assertEquals(response.status_code, status.HTTP_200_OK)
|
|
|
|
self.assertEquals(response.data, self.data)
|
|
|
|
|
|
|
|
def test_get_detail_view(self):
|
|
|
|
"""
|
|
|
|
GET requests to ListCreateAPIView should return list of objects.
|
|
|
|
"""
|
|
|
|
request = factory.get('/basic/1')
|
|
|
|
response = self.detail_view(request, pk=1).render()
|
|
|
|
self.assertEquals(response.status_code, status.HTTP_200_OK)
|
|
|
|
self.assertEquals(response.data, self.data[0])
|
|
|
|
|
|
|
|
|
|
|
|
class TestManyToManyHyperlinkedView(TestCase):
|
|
|
|
urls = 'rest_framework.tests.hyperlinkedserializers'
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
"""
|
|
|
|
Create 3 BasicModel intances.
|
|
|
|
"""
|
|
|
|
items = ['foo', 'bar', 'baz']
|
|
|
|
anchors = []
|
|
|
|
for item in items:
|
|
|
|
anchor = Anchor(text=item)
|
|
|
|
anchor.save()
|
|
|
|
anchors.append(anchor)
|
|
|
|
|
|
|
|
manytomany = ManyToManyModel()
|
|
|
|
manytomany.save()
|
|
|
|
manytomany.rel.add(*anchors)
|
|
|
|
|
|
|
|
self.data = [{
|
|
|
|
'url': 'http://testserver/manytomany/1/',
|
|
|
|
'rel': [
|
|
|
|
'http://testserver/anchor/1/',
|
|
|
|
'http://testserver/anchor/2/',
|
|
|
|
'http://testserver/anchor/3/',
|
|
|
|
]
|
|
|
|
}]
|
|
|
|
self.list_view = ManyToManyList.as_view()
|
|
|
|
self.detail_view = ManyToManyDetail.as_view()
|
|
|
|
|
|
|
|
def test_get_list_view(self):
|
|
|
|
"""
|
|
|
|
GET requests to ListCreateAPIView should return list of objects.
|
|
|
|
"""
|
|
|
|
request = factory.get('/manytomany/')
|
2012-10-04 14:26:41 +04:00
|
|
|
response = self.list_view(request).render()
|
|
|
|
self.assertEquals(response.status_code, status.HTTP_200_OK)
|
|
|
|
self.assertEquals(response.data, self.data)
|
|
|
|
|
|
|
|
def test_get_detail_view(self):
|
|
|
|
"""
|
|
|
|
GET requests to ListCreateAPIView should return list of objects.
|
|
|
|
"""
|
2012-10-04 19:58:18 +04:00
|
|
|
request = factory.get('/manytomany/1/')
|
2012-10-04 14:26:41 +04:00
|
|
|
response = self.detail_view(request, pk=1).render()
|
|
|
|
self.assertEquals(response.status_code, status.HTTP_200_OK)
|
|
|
|
self.assertEquals(response.data, self.data[0])
|
2012-10-30 14:35:30 +04:00
|
|
|
|
|
|
|
|
|
|
|
class TestCreateWithForeignKeys(TestCase):
|
|
|
|
urls = 'rest_framework.tests.hyperlinkedserializers'
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
"""
|
|
|
|
Create a blog post
|
|
|
|
"""
|
|
|
|
self.post = BlogPost.objects.create(title="Test post")
|
|
|
|
self.create_view = BlogPostCommentListCreate.as_view()
|
|
|
|
|
|
|
|
def test_create_comment(self):
|
|
|
|
|
|
|
|
data = {
|
|
|
|
'text': 'A test comment',
|
2012-10-30 14:49:01 +04:00
|
|
|
'blog_post_url': 'http://testserver/posts/1/'
|
2012-10-30 14:35:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
request = factory.post('/comments/', data=data)
|
|
|
|
response = self.create_view(request).render()
|
|
|
|
self.assertEqual(response.status_code, 201)
|
|
|
|
self.assertEqual(self.post.blogpostcomment_set.count(), 1)
|
|
|
|
self.assertEqual(self.post.blogpostcomment_set.all()[0].text, 'A test comment')
|