From 7164aa177617c824111757ee86d7c372dfeaaac7 Mon Sep 17 00:00:00 2001 From: johnthagen Date: Mon, 22 Feb 2021 16:01:00 -0500 Subject: [PATCH] Add unit tests --- tests/test_serializers.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/test_serializers.py b/tests/test_serializers.py index 59236f8..cb132a4 100644 --- a/tests/test_serializers.py +++ b/tests/test_serializers.py @@ -1,6 +1,7 @@ import uuid import pytest +from enumfields.drf import EnumField from enumfields.drf.serializers import EnumSupportSerializerMixin from rest_framework import serializers @@ -42,6 +43,22 @@ def test_serialize(int_names): assert data['int_enum'] == data['int_enum_not_editable'] == IntegerEnum.B.value +@pytest.mark.parametrize('instance, representation', [ + ('', ''), + (None, None), + ('r', 'r'), + ('g', 'g'), + ('b', 'b'), +]) +def test_enumfield_to_representation(instance, representation): + assert EnumField(Color).to_representation(instance) == representation + + +def test_invalid_enumfield_to_representation(): + with pytest.raises(ValueError, match=r"Invalid value.*"): + assert EnumField(Color).to_representation('INVALID_ENUM_STRING') + + @pytest.mark.django_db @pytest.mark.parametrize('lenient_serializer', (False, True)) @pytest.mark.parametrize('lenient_data', (False, True))