From 12908b149c446598682269f8df78290fa8268982 Mon Sep 17 00:00:00 2001 From: fbz Date: Sat, 5 Jul 2025 17:22:08 +0800 Subject: [PATCH] test: test TestMultipleChoiceField can json serializable --- tests/test_fields.py | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/tests/test_fields.py b/tests/test_fields.py index 7e68a0ea0..ced28038c 100644 --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -11,6 +11,8 @@ from zoneinfo import ZoneInfo import pytest +from rest_framework.utils import json + try: import pytz except ImportError: @@ -2193,7 +2195,30 @@ class TestMultipleChoiceField(FieldValues): field.partial = False assert field.get_value(QueryDict("")) == [] field.partial = True - assert field.get_value(QueryDict('')) == rest_framework.fields.empty + assert field.get_value(QueryDict("")) == rest_framework.fields.empty + + def test_valid_inputs_is_json_serializable(self): + for input_value, _ in get_items(self.valid_inputs): + validated = self.field.run_validation(input_value) + + try: + json.dumps(validated) + except TypeError as e: + assert ( + False + ), f"Validated output not JSON serializable: {repr(validated)}; Error: {e}" + + def test_output_is_json_serializable(self): + for output_value, _ in get_items(self.outputs): + representation = self.field.to_representation(output_value) + + try: + json.dumps(representation) + except TypeError as e: + assert False, ( + f"to_representation output not JSON serializable: " + f"{repr(representation)}; Error: {e}" + ) class TestEmptyMultipleChoiceField(FieldValues):