Skip to content

Commit 787cf42

Browse files
Test errors are raised when unsupported types are serialized
1 parent 8dd9200 commit 787cf42

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

datahub/documents/serializers.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,13 @@ class RelatedObjectRelatedField(serializers.RelatedField):
6262

6363
def to_representation(self, instance):
6464
"""Convert model instance to built-in Python (JSON friendly) data types."""
65-
content_type = ContentType.objects.get_for_model(instance)
6665
if isinstance(instance, (Company)):
6766
return {
6867
'id': str(instance.id),
6968
'name': instance.name,
7069
}
71-
return Exception(f'Unexpected type of related object: {content_type.model}')
70+
content_type = ContentType.objects.get_for_model(instance)
71+
raise Exception(f'Unexpected type of related object: {format_content_type(content_type)}')
7272

7373

7474
class GenericDocumentRetrieveSerializer(serializers.ModelSerializer):

datahub/documents/test/test_serializers.py

+41
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import pytest
22

3+
from django.contrib.contenttypes.models import ContentType
4+
5+
from datahub.company.test.factories import CompanyFactory
6+
from datahub.documents.models import GenericDocument
37
from datahub.documents.serializers import (
48
GenericDocumentRetrieveSerializer,
59
SharePointDocumentSerializer,
@@ -12,6 +16,7 @@
1216
assert_retrieved_generic_document,
1317
assert_retrieved_sharepoint_document,
1418
)
19+
from datahub.investment.project.test.factories import InvestmentProjectFactory
1520

1621

1722
pytestmark = pytest.mark.django_db
@@ -33,3 +38,39 @@ def test_serializing_instance_returns_expected_fields(self):
3338
generic_document = CompanySharePointDocumentFactory()
3439
serializer = GenericDocumentRetrieveSerializer(generic_document)
3540
assert_retrieved_generic_document(generic_document, serializer.data)
41+
42+
def test_serializer_raises_error_if_unsupported_document_type(self):
43+
unsupported_document = CompanySharePointDocumentFactory()
44+
unsupported_document_type = ContentType.objects.get_for_model(unsupported_document)
45+
46+
company = CompanyFactory()
47+
company_type = ContentType.objects.get_for_model(company)
48+
49+
generic_document = GenericDocument.objects.create(
50+
document_type=unsupported_document_type,
51+
document_object_id=unsupported_document.id,
52+
related_object_type=company_type,
53+
related_object_id=company.id,
54+
)
55+
with pytest.raises(Exception):
56+
serializer = GenericDocumentRetrieveSerializer(generic_document)
57+
serializer.data
58+
59+
def test_serializer_raises_error_if_unsupported_related_object_type(self):
60+
document = SharePointDocumentFactory()
61+
document_type = ContentType.objects.get_for_model(document)
62+
63+
unsupported_related_object = InvestmentProjectFactory()
64+
unsupported_related_object_type = ContentType.objects.get_for_model(
65+
unsupported_related_object,
66+
)
67+
68+
generic_document = GenericDocument.objects.create(
69+
document_type=document_type,
70+
document_object_id=document.id,
71+
related_object_type=unsupported_related_object_type,
72+
related_object_id=unsupported_related_object.id,
73+
)
74+
with pytest.raises(Exception):
75+
serializer = GenericDocumentRetrieveSerializer(generic_document)
76+
serializer.data

0 commit comments

Comments
 (0)