| 1 |
import datetime |
| 2 |
|
| 3 |
from django.test import TestCase, override_settings |
| 4 |
|
| 5 |
# todo: use from rest_framework when released |
| 6 |
from rest_framework.test import APIRequestFactory |
| 7 |
from .models import Comment |
| 8 |
|
| 9 |
|
| 10 |
factory = APIRequestFactory() |
| 11 |
|
| 12 |
|
| 13 |
@override_settings(ROOT_URLCONF='tests_app.tests.functional.mixins.detail_serializer_mixin.urls') |
| 14 |
class DetailSerializerMixinTest_serializer_detail_class(TestCase): |
| 15 |
|
| 16 |
def setUp(self): |
| 17 |
self.comment = Comment.objects.create( |
| 18 |
id=1, |
| 19 |
email='[email protected]', |
| 20 |
content='Hello world', |
| 21 |
created=datetime.datetime.now() |
| 22 |
) |
| 23 |
|
| 24 |
def test_serializer_class_response(self): |
| 25 |
resp = self.client.get('/comments/') |
| 26 |
expected = [{ |
| 27 |
'id': 1, |
| 28 |
'email': '[email protected]' |
| 29 |
}] |
| 30 |
self.assertEqual(resp.data, expected) |
| 31 |
|
| 32 |
def test_serializer_detail_class_response(self): |
| 33 |
resp = self.client.get('/comments/1/') |
| 34 |
expected = { |
| 35 |
'id': 1, |
| 36 |
'email': '[email protected]', |
| 37 |
'content': 'Hello world', |
| 38 |
} |
| 39 |
self.assertEqual(resp.data, expected, 'should use detail serializer for detail endpoint') |
| 40 |
|
| 41 |
def test_view_with_mixin_and_without__serializer_detail_class__should_raise_exception(self): |
| 42 |
msg = "'CommentWithoutDetailSerializerClassViewSet' should include a 'serializer_detail_class' attribute" |
| 43 |
self.assertRaisesMessage(AssertionError, msg, self.client.get, '/comments-2/') |
| 44 |
|
| 45 |
|
| 46 |
@override_settings(ROOT_URLCONF='tests_app.tests.functional.mixins.detail_serializer_mixin.urls') |
| 47 |
class DetailSerializerMixin_queryset_detail(TestCase): |
| 48 |
|
| 49 |
def setUp(self): |
| 50 |
self.comments = [ |
| 51 |
Comment.objects.create( |
| 52 |
id=1, |
| 53 |
email='[email protected]', |
| 54 |
content='Hello world', |
| 55 |
created=datetime.datetime.now() |
| 56 |
), |
| 57 |
Comment.objects.create( |
| 58 |
id=2, |
| 59 |
email='[email protected]', |
| 60 |
content='Hello world 2', |
| 61 |
created=datetime.datetime.now() |
| 62 |
), |
| 63 |
] |
| 64 |
|
| 65 |
def test_list_should_use_default_queryset_method(self): |
| 66 |
resp = self.client.get('/comments-3/') |
| 67 |
expected = [{ |
| 68 |
'id': 2, |
| 69 |
'email': '[email protected]' |
| 70 |
}] |
| 71 |
self.assertEqual(resp.data, expected) |
| 72 |
|
| 73 |
def test_detail_view_should_use_default_queryset_if_queryset_detail_not_specified(self): |
| 74 |
resp = self.client.get('/comments-3/1/') |
| 75 |
self.assertEqual(resp.status_code, 404) |
| 76 |
|
| 77 |
resp = self.client.get('/comments-3/2/') |
| 78 |
expected = { |
| 79 |
'id': 2, |
| 80 |
'email': '[email protected]', |
| 81 |
'content': 'Hello world 2', |
| 82 |
} |
| 83 |
self.assertEqual(resp.data, expected) |
| 84 |
|
| 85 |
def test_list_should_use_default_queryset_method_if_queryset_detail_specified(self): |
| 86 |
resp = self.client.get('/comments-4/') |
| 87 |
expected = [{ |
| 88 |
'id': 2, |
| 89 |
'email': '[email protected]' |
| 90 |
}] |
| 91 |
self.assertEqual(resp.data, expected) |
| 92 |
|
| 93 |
def test_detail_view_should_use_custom_queryset_if_queryset_detail_specified(self): |
| 94 |
resp = self.client.get('/comments-4/2/') |
| 95 |
self.assertEqual(resp.status_code, 404) |
| 96 |
|
| 97 |
resp = self.client.get('/comments-4/1/') |
| 98 |
expected = { |
| 99 |
'id': 1, |
| 100 |
'email': '[email protected]', |
| 101 |
'content': 'Hello world', |
| 102 |
} |
| 103 |
self.assertEqual(resp.data, expected) |
| 104 |
|
| 105 |
def test_nested_model_view_with_mixin_should_use_get_detail_queryset(self): |
| 106 |
""" |
| 107 |
Regression tests for https://github.com/chibisov/drf-extensions/pull/24 |
| 108 |
""" |
| 109 |
resp = self.client.get('/comments-5/1/') |
| 110 |
expected = { |
| 111 |
'id': 1, |
| 112 |
'email': '[email protected]', |
| 113 |
'content': 'Hello world', |
| 114 |
} |
| 115 |
self.assertEqual(resp.status_code, 200) |
| 116 |
self.assertEqual(resp.data, expected) |