|
| 1 | +import pytest |
| 2 | + |
| 3 | +from django.contrib.auth import get_user_model |
| 4 | +from rest_framework import serializers |
| 5 | +from rest_framework.response import Response |
| 6 | +from rest_framework.views import APIView |
| 7 | +from rest_framework_json_api import utils |
| 8 | + |
| 9 | +pytestmark = pytest.mark.django_db |
| 10 | + |
| 11 | +class Resource(APIView): |
| 12 | + pass |
| 13 | + |
| 14 | +class ResourceSerializer(serializers.ModelSerializer): |
| 15 | + class Meta(): |
| 16 | + fields = ('username',) |
| 17 | + model = get_user_model() |
| 18 | + |
| 19 | +def test_get_resource_name(): |
| 20 | + view = Resource() |
| 21 | + context = {'view': view} |
| 22 | + assert 'resources' == utils.get_resource_name(context), 'derived from view' |
| 23 | + |
| 24 | + view.model = get_user_model() |
| 25 | + assert 'users' == utils.get_resource_name(context), 'derived from view model' |
| 26 | + |
| 27 | + view.resource_name = 'custom' |
| 28 | + assert 'custom' == utils.get_resource_name(context), 'manually set on view' |
| 29 | + |
| 30 | + view.response = Response(status=403) |
| 31 | + assert 'errors' == utils.get_resource_name(context), 'handles 4xx error' |
| 32 | + |
| 33 | + view.response = Response(status=500) |
| 34 | + assert 'errors' == utils.get_resource_name(context), 'handles 500 error' |
| 35 | + |
| 36 | + view = Resource() |
| 37 | + context = {'view': view} |
| 38 | + view.serializer_class = ResourceSerializer |
| 39 | + assert 'users' == utils.get_resource_name(context), 'derived from serializer' |
| 40 | + |
| 41 | + view.serializer_class.Meta.resource_name = 'rcustom' |
| 42 | + assert 'rcustom' == utils.get_resource_name(context), 'set on serializer' |
| 43 | + |
| 44 | +def test_format_keys(): |
| 45 | + underscored = { |
| 46 | + 'first_name': 'a', |
| 47 | + 'last_name': 'b', |
| 48 | + } |
| 49 | + |
| 50 | + output = {'firstName': 'a', 'lastName': 'b'} |
| 51 | + assert utils.format_keys(underscored, 'camelize') == output |
| 52 | + |
| 53 | + output = {'first-name': 'a', 'last-name': 'b'} |
| 54 | + assert utils.format_keys(underscored, 'dasherize') == output |
| 55 | + |
| 56 | + new_input = {'firstName': 'a', 'lastName': 'b'} |
| 57 | + assert utils.format_keys(new_input, 'underscore') == underscored |
| 58 | + |
| 59 | + output = [{'first-name': 'a', 'last-name': 'b'}] |
| 60 | + assert utils.format_keys([underscored], 'dasherize') == output |
| 61 | + |
| 62 | +def test_format_value(): |
| 63 | + assert utils.format_value('first_name', 'camelize') == 'FirstName' |
| 64 | + assert utils.format_value('first_name', 'dasherize') == 'first-name' |
| 65 | + assert utils.format_value('first-name', 'underscore') == 'first_name' |
| 66 | + |
| 67 | +def test_format_relation_name(): |
| 68 | + assert utils.format_relation_name('first_name', 'camelize') == 'FirstNames' |
| 69 | + |
| 70 | +def test_build_json_resource_obj(): |
| 71 | + resource = { |
| 72 | + 'pk': 1, |
| 73 | + 'username': 'Alice', |
| 74 | + } |
| 75 | + |
| 76 | + serializer = ResourceSerializer(data={'username': 'Alice'}) |
| 77 | + serializer.is_valid() |
| 78 | + resource_instance = serializer.save() |
| 79 | + |
| 80 | + output = { |
| 81 | + 'type': 'user', |
| 82 | + 'id': '1', |
| 83 | + 'attributes': { |
| 84 | + 'username': 'Alice' |
| 85 | + }, |
| 86 | + } |
| 87 | + |
| 88 | + assert utils.build_json_resource_obj( |
| 89 | + serializer.fields, resource, resource_instance, 'user') == output |
| 90 | + |
0 commit comments