Skip to content

Commit

Permalink
Merge pull request #148 from Hipo/fix-range-field
Browse files Browse the repository at this point in the history
Fix dict object does not have isempty attribute error
  • Loading branch information
alicertel authored Nov 28, 2020
2 parents 1a00711 + 3c00ebc commit 480b6b9
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 9 deletions.
24 changes: 17 additions & 7 deletions drf_extra_fields/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,13 +221,23 @@ def to_representation(self, value):
"""
Range instances -> dicts of primitive datatypes.
"""
if value.isempty:
return {'empty': True}
lower = self.child.to_representation(value.lower) if value.lower is not None else None
upper = self.child.to_representation(value.upper) if value.upper is not None else None
return {'lower': lower,
'upper': upper,
'bounds': value._bounds}
if isinstance(value, dict):
if not value:
return value

lower = value.get("lower")
upper = value.get("upper")
bounds = value.get("bounds")
else:
if value.isempty:
return {'empty': True}
lower = value.lower
upper = value.upper
bounds = value._bounds

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}

def get_initial(self):
initial = super().get_initial()
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

setup(
name='drf-extra-fields',
version='3.0.3',
version='3.0.4',
packages=['drf_extra_fields',
'drf_extra_fields.runtests'],
include_package_data=True,
Expand Down
73 changes: 72 additions & 1 deletion tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import django
import imghdr
import os
from decimal import Decimal

import pytest
import pytz
Expand Down Expand Up @@ -445,6 +446,7 @@ class FieldValues:
"""
Base class for testing valid and invalid input values.
"""

def test_valid_inputs(self):
"""
Ensure that valid values return the expected validated data.
Expand Down Expand Up @@ -479,7 +481,6 @@ class TestIntegerRangeField(FieldValues):
Values for `ListField` with CharField as child.
"""
serializer_class = IntegerRangeSerializer

valid_inputs = [
({'lower': '1', 'upper': 2, 'bounds': '[)'},
NumericRange(**{'lower': 1, 'upper': 2, 'bounds': '[)'})),
Expand All @@ -504,6 +505,15 @@ class TestIntegerRangeField(FieldValues):
{'lower': 1, 'upper': 2, 'bounds': '[)'}),
(NumericRange(**{'empty': True}), {'empty': True}),
(NumericRange(), {'bounds': '[)', 'lower': None, 'upper': None}),
({'lower': '1', 'upper': 2, 'bounds': '[)'},
{'lower': 1, 'upper': 2, 'bounds': '[)'}),
({'lower': 1, 'upper': 2},
{'lower': 1, 'upper': 2, 'bounds': None}),
({'lower': 1},
{'lower': 1, 'upper': None, 'bounds': None}),
({'upper': 1},
{'lower': None, 'upper': 1, 'bounds': None}),
({}, {}),
]
field = IntegerRangeField()

Expand Down Expand Up @@ -543,6 +553,15 @@ class TestDecimalRangeField(FieldValues):
{'lower': '1.1', 'upper': '2', 'bounds': '[)'}),
(NumericRange(**{'empty': True}), {'empty': True}),
(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"},
{'lower': "1.1", 'upper': "2.3", 'bounds': None}),
({'lower': 1},
{'lower': "1", 'upper': None, 'bounds': None}),
({'upper': 1},
{'lower': None, 'upper': "1", 'bounds': None}),
({}, {}),
]
field = DecimalRangeField()

Expand Down Expand Up @@ -585,6 +604,15 @@ class TestDecimalRangeFieldWithChildAttribute(FieldValues):
{'lower': '1.10', 'upper': '2.00', 'bounds': '[)'}),
(NumericRange(**{'empty': True}), {'empty': True}),
(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"},
{'lower': "1.10", 'upper': "2.30", 'bounds': None}),
({'lower': 1},
{'lower': "1.00", 'upper': None, 'bounds': None}),
({'upper': 1},
{'lower': None, 'upper': "1.00", 'bounds': None}),
({}, {}),
]


Expand Down Expand Up @@ -619,6 +647,15 @@ class TestFloatRangeField(FieldValues):
{'lower': 1.1, 'upper': 2, 'bounds': '[)'}),
(NumericRange(**{'empty': True}), {'empty': True}),
(NumericRange(), {'bounds': '[)', 'lower': None, 'upper': None}),
({'lower': '1', 'upper': 2., 'bounds': '[)'},
{'lower': 1., 'upper': 2., 'bounds': '[)'}),
({'lower': 1., 'upper': 2.},
{'lower': 1, 'upper': 2, 'bounds': None}),
({'lower': 1},
{'lower': 1, 'upper': None, 'bounds': None}),
({'upper': 1},
{'lower': None, 'upper': 1, 'bounds': None}),
({}, {}),
]
field = FloatRangeField()

Expand Down Expand Up @@ -681,6 +718,23 @@ class TestDateTimeRangeField(TestCase, FieldValues):
{'empty': True}),
(DateTimeTZRange(),
{'bounds': '[)', 'lower': None, 'upper': None}),
({'lower': '2001-01-01T13:00:00Z',
'upper': '2001-02-02T13:00:00Z',
'bounds': '[)'},
{'lower': '2001-01-01T13:00:00Z',
'upper': '2001-02-02T13:00:00Z',
'bounds': '[)'}),
({'lower': datetime.datetime(2001, 1, 1, 13, 00, tzinfo=pytz.utc),
'upper': datetime.datetime(2001, 2, 2, 13, 00, tzinfo=pytz.utc),
'bounds': '[)'},
{'lower': '2001-01-01T13:00:00Z',
'upper': '2001-02-02T13:00:00Z',
'bounds': '[)'}),
({'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': '[)'}),
({}, {}),
]
field = DateTimeRangeField()

Expand Down Expand Up @@ -741,6 +795,23 @@ class TestDateRangeField(FieldValues):
(DateRange(**{'empty': True}),
{'empty': True}),
(DateRange(), {'bounds': '[)', 'lower': None, 'upper': None}),
({'lower': '2001-01-01',
'upper': '2001-02-02',
'bounds': '[)'},
{'lower': '2001-01-01',
'upper': '2001-02-02',
'bounds': '[)'}),
({'lower': datetime.date(2001, 1, 1),
'upper': datetime.date(2001, 2, 2),
'bounds': '[)'},
{'lower': '2001-01-01',
'upper': '2001-02-02',
'bounds': '[)'}),
({'upper': '2001-02-02', 'bounds': '[)'},
{'lower': None, 'upper': '2001-02-02', 'bounds': '[)'}),
({'lower': '2001-01-01', 'bounds': '[)'},
{'lower': '2001-01-01', 'upper': None, 'bounds': '[)'}),
({}, {}),
]
field = DateRangeField()

Expand Down

0 comments on commit 480b6b9

Please sign in to comment.