添加Announcement的API测试,即url中的announcement_list_api,增加公告编辑及相应的API测试

This commit is contained in:
hohoTT 2015-08-06 19:07:46 +08:00
parent 290212d408
commit 1675eed67d
4 changed files with 73 additions and 3 deletions

View File

@ -97,6 +97,7 @@ class UsernameCheckAPIView(APIView):
else: else:
return serializer_invalid_response(serializer) return serializer_invalid_response(serializer)
class EmailCheckAPIView(APIView): class EmailCheckAPIView(APIView):
def post(self, request): def post(self, request):
""" """

View File

@ -22,3 +22,9 @@ class AnnouncementSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = Announcement model = Announcement
class EditAnnouncementSerializer(serializers.Serializer):
id = serializers.IntegerField()
title = serializers.CharField(max_length=50)
content = serializers.CharField(max_length=10000)
visible = serializers.BooleanField()

View File

@ -4,9 +4,10 @@ from django.core.urlresolvers import reverse
from rest_framework.test import APITestCase, APIClient from rest_framework.test import APITestCase, APIClient
from account.models import User from account.models import User
from announcement.models import Announcement
class AnnouncementAPITest(APITestCase): class AnnouncementAdminAPITest(APITestCase):
def setUp(self): def setUp(self):
self.client = APIClient() self.client = APIClient()
self.url = reverse("announcement_admin_api") self.url = reverse("announcement_admin_api")
@ -14,6 +15,7 @@ class AnnouncementAPITest(APITestCase):
user.set_password("test") user.set_password("test")
user.save() user.save()
# 以下是发布公告的测试
def test_invalid_format(self): def test_invalid_format(self):
self.client.login(username="test", password="test") self.client.login(username="test", password="test")
data = {"title": "test1"} data = {"title": "test1"}
@ -24,4 +26,42 @@ class AnnouncementAPITest(APITestCase):
self.client.login(username="test", password="test") self.client.login(username="test", password="test")
data = {"title": "title0", "content": "content0"} data = {"title": "title0", "content": "content0"}
response = self.client.post(self.url, data=data) response = self.client.post(self.url, data=data)
self.assertEqual(response.data, {"code": 0, "data": u"公告发布成功!"}) self.assertEqual(response.data, {"code": 0, "data": u"公告发布成功!"})
def test_post_invalid_data(self):
self.client.login(username="test", password="test")
data = {"title": "test"}
response = self.client.post(self.url, data=data)
self.assertEqual(response.data["code"], 1)
# 以下是编辑公告的测试
def test_put_invalid_data(self):
self.client.login(username="test", password="test")
data = {"title": "test0", "content": "test0", "visible": "True"}
response = self.client.put(self.url, data=data)
self.assertEqual(response.data["code"], 1)
def test_announcement_does_not_exist(self):
announcement = Announcement.objects.create(title="aa",
content="AA",
created_by=User.objects.get(username="test"))
data = {"id": announcement.id + 1, "title": "11", "content": "22", "visible": True}
response = self.client.put(self.url, data=data)
self.assertEqual(response.data, {"code": 1, "data": u"该公告不存在!"})
def test_success_edit_announcement(self):
announcement = Announcement.objects.create(title="bb",
content="BB",
created_by=User.objects.get(username="test"))
data = {"id": announcement.id, "title": "11", "content": "22", "visible": True}
response = self.client.put(self.url, data=data)
self.assertEqual(response.data["code"], 0)
class AnnouncementAPITest(APITestCase):
def setUp(self):
self.client = APIClient()
self.url = reverse("announcement_list_api")
def test_success_get_data(self):
self.assertEqual(self.client.get(self.url).data["code"], 0)

View File

@ -6,7 +6,8 @@ from utils.shortcuts import serializer_invalid_response, error_response, success
from account.models import User from account.models import User
from utils.shortcuts import paginate from utils.shortcuts import paginate
from .models import Announcement from .models import Announcement
from .serializers import CreateAnnouncementSerializer, AnnouncementSerializer from .serializers import (CreateAnnouncementSerializer, AnnouncementSerializer,
EditAnnouncementSerializer)
class AnnouncementAdminAPIView(APIView): class AnnouncementAdminAPIView(APIView):
@ -26,6 +27,28 @@ class AnnouncementAdminAPIView(APIView):
else: else:
return serializer_invalid_response(serializer) return serializer_invalid_response(serializer)
def put(self, request):
"""
公告编辑json api接口
---
request_serializer: EditAnnouncementSerializer
response_serializer: AnnouncementSerializer
"""
serializer = EditAnnouncementSerializer(data=request.DATA)
if serializer.is_valid():
data = serializer.data
try:
announcement = Announcement.objects.get(id=data["id"])
except Announcement.DoesNotExist:
return error_response(u"该公告不存在!")
announcement.title = data["title"]
announcement.content = data["content"]
announcement.visible = data["visible"]
announcement.save()
return success_response(AnnouncementSerializer(announcement).data)
else:
return serializer_invalid_response(serializer)
class AnnouncementAPIView(APIView): class AnnouncementAPIView(APIView):
def get(self, request): def get(self, request):