From 044cc82a34a6ed2cae0c74cea72dd0ea502b78a0 Mon Sep 17 00:00:00 2001 From: Christian Hartung Date: Thu, 22 Jun 2023 16:50:09 -0300 Subject: [PATCH] fix: mimic psycopg/postgres behaviour of excluding infinity --- drf_extra_fields/fields.py | 6 ++++++ tests/test_fields.py | 22 +++++++++++----------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/drf_extra_fields/fields.py b/drf_extra_fields/fields.py index 4135ba1..221f425 100644 --- a/drf_extra_fields/fields.py +++ b/drf_extra_fields/fields.py @@ -260,6 +260,12 @@ def to_representation(self, value): upper = value.upper bounds = value._bounds + if bounds: + if lower is None and bounds[0] == "[": + bounds = "(" + bounds[1] + if upper is None and bounds[1] == "]": + bounds = bounds[0] + ")" + return {'lower': self.child.to_representation(lower) if lower is not None else None, 'upper': self.child.to_representation(upper) if upper is not None else None, 'bounds': bounds} diff --git a/tests/test_fields.py b/tests/test_fields.py index 19f7c54..e5839a1 100644 --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -498,7 +498,7 @@ class TestIntegerRangeField(FieldValues): (NumericRange(**{'lower': '1', 'upper': '2'}), {'lower': 1, 'upper': 2, 'bounds': '[)'}), (NumericRange(**{'empty': True}), {'empty': True}), - (NumericRange(), {'bounds': '[)', 'lower': None, 'upper': None}), + (NumericRange(), {'bounds': '()', 'lower': None, 'upper': None}), ({'lower': '1', 'upper': 2, 'bounds': '[)'}, {'lower': 1, 'upper': 2, 'bounds': '[)'}), ({'lower': 1, 'upper': 2}, @@ -551,7 +551,7 @@ class TestIntegerRangeChildAllowNullField(FieldValues): (NumericRange(**{'lower': '1', 'upper': '2'}), {'lower': 1, 'upper': 2, 'bounds': '[)'}), (NumericRange(**{'empty': True}), {'empty': True}), - (NumericRange(), {'bounds': '[)', 'lower': None, 'upper': None}), + (NumericRange(), {'bounds': '()', 'lower': None, 'upper': None}), ({'lower': '1', 'upper': 2, 'bounds': '[)'}, {'lower': 1, 'upper': 2, 'bounds': '[)'}), ({'lower': 1, 'upper': 2}, @@ -590,7 +590,7 @@ class TestDecimalRangeField(FieldValues): (NumericRange(**{'lower': '1.1', 'upper': '2'}), {'lower': '1.1', 'upper': '2', 'bounds': '[)'}), (NumericRange(**{'empty': True}), {'empty': True}), - (NumericRange(), {'bounds': '[)', 'lower': None, 'upper': None}), + (NumericRange(), {'bounds': '()', 'lower': None, 'upper': None}), ({'lower': Decimal('1.1'), 'upper': "2.3", 'bounds': '[)'}, {'lower': "1.1", 'upper': "2.3", 'bounds': '[)'}), ({'lower': Decimal('1.1'), 'upper': "2.3"}, @@ -641,7 +641,7 @@ class TestDecimalRangeFieldWithChildAttribute(FieldValues): (NumericRange(**{'lower': '1.1', 'upper': '2'}), {'lower': '1.10', 'upper': '2.00', 'bounds': '[)'}), (NumericRange(**{'empty': True}), {'empty': True}), - (NumericRange(), {'bounds': '[)', 'lower': None, 'upper': None}), + (NumericRange(), {'bounds': '()', 'lower': None, 'upper': None}), ({'lower': Decimal('1.1'), 'upper': "2.3", 'bounds': '[)'}, {'lower': "1.10", 'upper': "2.30", 'bounds': '[)'}), ({'lower': Decimal('1.1'), 'upper': "2.3"}, @@ -684,7 +684,7 @@ class TestFloatRangeField(FieldValues): (NumericRange(**{'lower': '1.1', 'upper': '2'}), {'lower': 1.1, 'upper': 2, 'bounds': '[)'}), (NumericRange(**{'empty': True}), {'empty': True}), - (NumericRange(), {'bounds': '[)', 'lower': None, 'upper': None}), + (NumericRange(), {'bounds': '()', 'lower': None, 'upper': None}), ({'lower': '1', 'upper': 2., 'bounds': '[)'}, {'lower': 1., 'upper': 2., 'bounds': '[)'}), ({'lower': 1., 'upper': 2.}, @@ -755,7 +755,7 @@ class TestDateTimeRangeField(TestCase, FieldValues): (DateTimeTZRange(**{'empty': True}), {'empty': True}), (DateTimeTZRange(), - {'bounds': '[)', 'lower': None, 'upper': None}), + {'bounds': '()', 'lower': None, 'upper': None}), ({'lower': '2001-01-01T13:00:00Z', 'upper': '2001-02-02T13:00:00Z', 'bounds': '[)'}, @@ -769,7 +769,7 @@ class TestDateTimeRangeField(TestCase, FieldValues): 'upper': '2001-02-02T13:00:00Z', 'bounds': '[)'}), ({'upper': '2001-02-02T13:00:00Z', 'bounds': '[)'}, - {'lower': None, 'upper': '2001-02-02T13:00:00Z', 'bounds': '[)'}), + {'lower': None, 'upper': '2001-02-02T13:00:00Z', 'bounds': '()'}), ({'lower': '2001-01-01T13:00:00Z', 'bounds': '[)'}, {'lower': '2001-01-01T13:00:00Z', 'upper': None, 'bounds': '[)'}), ({}, {}), @@ -832,7 +832,7 @@ class TestDateRangeField(FieldValues): 'bounds': '[)'}), (DateRange(**{'empty': True}), {'empty': True}), - (DateRange(), {'bounds': '[)', 'lower': None, 'upper': None}), + (DateRange(), {'bounds': '()', 'lower': None, 'upper': None}), ({'lower': '2001-01-01', 'upper': '2001-02-02', 'bounds': '[)'}, @@ -846,9 +846,9 @@ class TestDateRangeField(FieldValues): 'upper': '2001-02-02', 'bounds': '[)'}), ({'upper': '2001-02-02', 'bounds': '[)'}, - {'lower': None, 'upper': '2001-02-02', 'bounds': '[)'}), + {'lower': None, 'upper': '2001-02-02', 'bounds': '()'}), ({'lower': '2001-01-01', 'bounds': '[)'}, - {'lower': '2001-01-01', 'upper': None, 'bounds': '[)'}), + {'lower': '2001-01-01', 'upper': None, 'bounds': '[)'}), ({}, {}), ] field = DateRangeField() @@ -864,7 +864,7 @@ def test_no_source_on_child(self): def test_initial_value_of_field(self): serializer = DateRangeSerializer() - assert serializer.data['range'] == {'lower': None, 'upper': None, 'bounds': '[)'} + assert serializer.data['range'] == {'lower': None, 'upper': None, 'bounds': '()'} def test_allow_empty(self): serializer = DateRangeWithAllowEmptyFalseSerializer(data={"range": {}})