Skip to content

Commit

Permalink
fix: mimic psycopg/postgres behaviour of excluding infinity
Browse files Browse the repository at this point in the history
  • Loading branch information
hartungstenio committed Jun 22, 2023
1 parent f4b978b commit 044cc82
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
6 changes: 6 additions & 0 deletions drf_extra_fields/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
22 changes: 11 additions & 11 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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},
Expand Down Expand Up @@ -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"},
Expand Down Expand Up @@ -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"},
Expand Down Expand Up @@ -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.},
Expand Down Expand Up @@ -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': '[)'},
Expand All @@ -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': '[)'}),
({}, {}),
Expand Down Expand Up @@ -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': '[)'},
Expand All @@ -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()
Expand All @@ -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": {}})
Expand Down

0 comments on commit 044cc82

Please sign in to comment.