mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-30 21:44:04 +03:00
66 lines
1.8 KiB
Python
66 lines
1.8 KiB
Python
from django.test import TestCase, override_settings
|
|
from django.urls import reverse
|
|
|
|
from rest_framework.test import APIClient
|
|
from rest_framework.utils import json
|
|
|
|
from . import models, serializers
|
|
|
|
|
|
class TestSerializer(TestCase):
|
|
def test_create(self):
|
|
item = models.Item.objects.create(name='test')
|
|
data = {
|
|
"items": [
|
|
{
|
|
"item": item.id,
|
|
"amount": 100,
|
|
}
|
|
],
|
|
}
|
|
serializer = serializers.SummarySerializer(data=data)
|
|
serializer.is_valid(raise_exception=True)
|
|
expected_data = {
|
|
"itemamount_set": [
|
|
{
|
|
"item": item,
|
|
"amount": 100,
|
|
}
|
|
],
|
|
}
|
|
assert serializer.validated_data == expected_data
|
|
serializer.save()
|
|
assert models.Summary.objects.count() == 1
|
|
|
|
|
|
@override_settings(ROOT_URLCONF='tests.issue.urls')
|
|
class TestIssueViewSet(TestCase):
|
|
def test_create(self):
|
|
api_client = APIClient()
|
|
item = models.Item.objects.create(name='test')
|
|
data = {
|
|
"items": [
|
|
{
|
|
"item": item.id,
|
|
"amount": 100,
|
|
}
|
|
],
|
|
}
|
|
response = api_client.post(reverse('summary-list'), data)
|
|
print(response.content)
|
|
assert response.status_code == 201
|
|
|
|
def test_create_with_json(self):
|
|
api_client = APIClient()
|
|
item = models.Item.objects.create(name='test')
|
|
data = {
|
|
"items": [
|
|
{
|
|
"item": item.id,
|
|
"amount": 100,
|
|
}
|
|
],
|
|
}
|
|
response = api_client.post(reverse('summary-list'), json.dumps(data), content_type='application/json')
|
|
assert response.status_code == 201
|