Skip to content

Commit

Permalink
Merge pull request #118 from mikeywaites/feature/min-max
Browse files Browse the repository at this point in the history
bounds checking for Integer
  • Loading branch information
mikeywaites committed Aug 10, 2016
2 parents 901d7ef + 62fef1e commit 7a5323d
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 3 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.0-rc2
1.0.0-rc3
14 changes: 14 additions & 0 deletions kim/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
'none_not_allowed': 'This field cannot be null',
'invalid_choice': 'invalid choice',
'duplicates': 'duplicates found',
'out_of_bounds': 'value out of allowed range',
}


Expand Down Expand Up @@ -296,6 +297,18 @@ class UserMapper(Mapper):
serialize_pipeline = StringSerializePipeline


class IntegerFieldOpts(FieldOpts):
"""Custom FieldOpts class that provides additional config options for
:class:`.Integer`.
"""

def __init__(self, **kwargs):
self.max = kwargs.pop('max', None)
self.min = kwargs.pop('min', None)
super(IntegerFieldOpts, self).__init__(**kwargs)


class Integer(Field):
""":class:`.Integer` represents a value that must be valid
when passed to int()
Expand All @@ -312,6 +325,7 @@ class UserMapper(Mapper):
"""

opts_class = IntegerFieldOpts
marshal_pipeline = IntegerMarshalPipeline
serialize_pipeline = IntegerSerializePipeline

Expand Down
2 changes: 1 addition & 1 deletion kim/pipelines/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def run(self, **opts):
return session.output


@pipe()
@pipe(run_if_none=True)
def get_data_from_name(session):
"""extracts a specific key from data using field.name. This pipe is
typically used as the entry point to a chain of input pipes.
Expand Down
21 changes: 20 additions & 1 deletion kim/pipelines/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,29 @@ def is_valid_integer(session):
raise session.field.invalid(error_type='type_error')


@pipe()
def bounds_check(session):
"""pipe used to determine if a value can be coerced to an int
:param session: Kim pipeline session instance
"""

max_ = session.field.opts.max
min_ = session.field.opts.min

if max_ is not None and session.data > max_:
raise session.field.invalid(error_type='out_of_bounds')
if min_ is not None and session.data < min_:
raise session.field.invalid(error_type='out_of_bounds')

return session.data


class IntegerMarshalPipeline(MarshalPipeline):

validation_pipes = \
[is_valid_integer, is_valid_choice] + MarshalPipeline.validation_pipes
[is_valid_integer, is_valid_choice, bounds_check] + MarshalPipeline.validation_pipes


class IntegerSerializePipeline(SerializePipeline):
Expand Down
29 changes: 29 additions & 0 deletions tests/test_pipelines/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,17 @@ def test_marshal_read_only_integer():
assert output == {}


def test_marshal_default():

field = Integer(name='name', default=5)

output = {}
mapper_session = get_mapper_session(data={}, output=output)

field.marshal(mapper_session)
assert output == {'name': 5}


def test_is_valid_choice():

field = Integer(name='type', choices=[1, 2])
Expand All @@ -94,6 +105,24 @@ def test_is_valid_choice():
assert output == {'type': 1}


def test_min_max():

field = Integer(name='age', min=20, max=35)
output = {}

mapper_session = get_mapper_session(data={'age': 15}, output=output)
with pytest.raises(FieldInvalid):
field.marshal(mapper_session)

mapper_session = get_mapper_session(data={'age': 40}, output=output)
with pytest.raises(FieldInvalid):
field.marshal(mapper_session)

mapper_session = get_mapper_session(data={'age': 25}, output=output)
field.marshal(mapper_session)
assert output == {'age': 25}


def test_is_valid_decimal_pipe():
"""test piping data through is_valid_decimal.
"""
Expand Down

0 comments on commit 7a5323d

Please sign in to comment.