Skip to content

Commit

Permalink
Add tests to clarify allow_null behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
mfogel committed Jul 7, 2024
1 parent bb20f86 commit dbe227d
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions tests/test_serializer_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ class _TimeZoneSerializer(serializers.Serializer):

yield _TimeZoneSerializer

@pytest.fixture
def TimeZoneSerializerAllowNull(use_pytz):
class _TimeZoneSerializer(serializers.Serializer):
# pylint: disable=abstract-method
tz = TimeZoneSerializerField(use_pytz=use_pytz, allow_null=True)

yield _TimeZoneSerializer


def test_invalid_str(TimeZoneSerializer, invalid_tz):
serializer = TimeZoneSerializer(data={"tz": invalid_tz})
Expand Down Expand Up @@ -41,3 +49,14 @@ def test_valid_with_timezone_object(TimeZoneSerializer, pst, pst_tz):
assert serializer.is_valid()
assert serializer.data["tz"] == pst
assert serializer.validated_data["tz"] == pst_tz


def test_allow_null(TimeZoneSerializer, TimeZoneSerializerAllowNull):
serializer_disallow_null = TimeZoneSerializer(data={"tz": None})
serializer_allow_null = TimeZoneSerializerAllowNull(data={"tz": None})
assert serializer_disallow_null.is_valid() is False
assert serializer_disallow_null.data == {"tz": None}
assert serializer_disallow_null.validated_data == {}
assert serializer_allow_null.is_valid()
assert serializer_allow_null.data == {"tz": None}
assert serializer_allow_null.validated_data == {"tz": None}

0 comments on commit dbe227d

Please sign in to comment.