|
| 1 | +from django.test import TestCase |
| 2 | +from django.utils import timezone |
| 3 | + |
| 4 | +from rest_framework_json_api.utils import format_relation_name |
| 5 | +from rest_framework_json_api.serializers import ResourceIdentifierObjectSerializer |
| 6 | + |
| 7 | +from example.models import Blog, Entry, Author |
| 8 | + |
| 9 | + |
| 10 | +class TestResourceIdentifierObjectSerializer(TestCase): |
| 11 | + def setUp(self): |
| 12 | + self.blog = Blog.objects.create(name='Some Blog', tagline="It's a blog") |
| 13 | + self.entry = Entry.objects.create( |
| 14 | + blog=self.blog, |
| 15 | + headline='headline', |
| 16 | + body_text='body_text', |
| 17 | + pub_date=timezone.now(), |
| 18 | + mod_date=timezone.now(), |
| 19 | + n_comments=0, |
| 20 | + n_pingbacks=0, |
| 21 | + rating=3 |
| 22 | + ) |
| 23 | + for i in range(1,6): |
| 24 | + name = 'some_author{}'.format(i) |
| 25 | + self.entry.authors.add( |
| 26 | + Author.objects.create(name=name, email='{}@example.org'.format(name)) |
| 27 | + ) |
| 28 | + |
| 29 | + def test_data_in_correct_format_when_instantiated_with_blog_object(self): |
| 30 | + serializer = ResourceIdentifierObjectSerializer(instance=self.blog) |
| 31 | + |
| 32 | + expected_data = {'type': format_relation_name('Blog'), 'id': str(self.blog.id)} |
| 33 | + |
| 34 | + assert serializer.data == expected_data |
| 35 | + |
| 36 | + def test_data_in_correct_format_when_instantiated_with_entry_object(self): |
| 37 | + serializer = ResourceIdentifierObjectSerializer(instance=self.entry) |
| 38 | + |
| 39 | + expected_data = {'type': format_relation_name('Entry'), 'id': str(self.entry.id)} |
| 40 | + |
| 41 | + assert serializer.data == expected_data |
| 42 | + |
| 43 | + def test_deserialize_primitive_data_blog(self): |
| 44 | + initial_data = { |
| 45 | + 'type': format_relation_name('Blog'), |
| 46 | + 'id': str(self.blog.id) |
| 47 | + } |
| 48 | + serializer = ResourceIdentifierObjectSerializer(data=initial_data, model_class=Blog) |
| 49 | + |
| 50 | + self.assertTrue(serializer.is_valid(), msg=serializer.errors) |
| 51 | + assert serializer.validated_data == self.blog |
| 52 | + |
| 53 | + def test_data_in_correct_format_when_instantiated_with_queryset(self): |
| 54 | + qs = Author.objects.all() |
| 55 | + serializer = ResourceIdentifierObjectSerializer(instance=qs, many=True) |
| 56 | + |
| 57 | + type_string = format_relation_name('Author') |
| 58 | + author_pks = Author.objects.values_list('pk', flat=True) |
| 59 | + expected_data = [{'type': type_string, 'id': str(pk)} for pk in author_pks] |
| 60 | + |
| 61 | + assert serializer.data == expected_data |
| 62 | + |
| 63 | + def test_deserialize_many(self): |
| 64 | + type_string = format_relation_name('Author') |
| 65 | + author_pks = Author.objects.values_list('pk', flat=True) |
| 66 | + initial_data = [{'type': type_string, 'id': str(pk)} for pk in author_pks] |
| 67 | + |
| 68 | + serializer = ResourceIdentifierObjectSerializer(data=initial_data, model_class=Author, many=True) |
| 69 | + |
| 70 | + self.assertTrue(serializer.is_valid(), msg=serializer.errors) |
| 71 | + |
| 72 | + print(serializer.data) |
| 73 | + |
0 commit comments