|
7 | 7 | from graphene.types.inputobjecttype import InputObjectType
|
8 | 8 |
|
9 | 9 | from ...types import DjangoObjectType
|
10 |
| -from ..models import MyFakeModel, MyFakeModelWithDate, MyFakeModelWithPassword |
| 10 | +from ..models import ( |
| 11 | + MyFakeModel, |
| 12 | + MyFakeModelWithDate, |
| 13 | + MyFakeModelWithPassword, |
| 14 | + MyFakeModelWithChoiceField, |
| 15 | +) |
11 | 16 | from ..mutation import SerializerMutation
|
12 | 17 |
|
13 | 18 |
|
@@ -268,6 +273,39 @@ class Meta:
|
268 | 273 | assert result.days_since_last_edit == 4
|
269 | 274 |
|
270 | 275 |
|
| 276 | +def test_perform_mutate_success_with_enum_choice_field(): |
| 277 | + class ListViewChoiceFieldSerializer(serializers.ModelSerializer): |
| 278 | + choice_type = serializers.ChoiceField( |
| 279 | + choices=[(x.name, x.value) for x in MyFakeModelWithChoiceField.ChoiceType], |
| 280 | + required=False, |
| 281 | + ) |
| 282 | + |
| 283 | + class Meta: |
| 284 | + model = MyFakeModelWithChoiceField |
| 285 | + fields = "__all__" |
| 286 | + |
| 287 | + class SomeCreateSerializerMutation(SerializerMutation): |
| 288 | + class Meta: |
| 289 | + serializer_class = ListViewChoiceFieldSerializer |
| 290 | + |
| 291 | + choice_type = { |
| 292 | + "choice_type": SomeCreateSerializerMutation.Input.choice_type.type.get("ASDF") |
| 293 | + } |
| 294 | + name = MyFakeModelWithChoiceField.ChoiceType.ASDF.name |
| 295 | + result = SomeCreateSerializerMutation.mutate_and_get_payload( |
| 296 | + None, mock_info(), **choice_type |
| 297 | + ) |
| 298 | + assert result.errors is None |
| 299 | + assert result.choice_type == name |
| 300 | + kwargs = SomeCreateSerializerMutation.get_serializer_kwargs( |
| 301 | + None, mock_info(), **choice_type |
| 302 | + ) |
| 303 | + assert kwargs["data"]["choice_type"] == name |
| 304 | + assert 1 == MyFakeModelWithChoiceField.objects.count() |
| 305 | + item = MyFakeModelWithChoiceField.objects.first() |
| 306 | + assert item.choice_type == name |
| 307 | + |
| 308 | + |
271 | 309 | def test_mutate_and_get_payload_error():
|
272 | 310 | class MyMutation(SerializerMutation):
|
273 | 311 | class Meta:
|
|
0 commit comments