-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
/
Copy pathtest_issue.py
65 lines (58 loc) · 1.85 KB
/
test_issue.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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