Skip to content

Commit

Permalink
1) Added Yeild flag(decimal/string)
Browse files Browse the repository at this point in the history
2) Fixed tests naming convention
  • Loading branch information
nareshnootoo committed Sep 28, 2016
1 parent e6b1b31 commit 2135bd3
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 18 deletions.
49 changes: 36 additions & 13 deletions voluptuous/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,7 @@ def fn(arg):

assert_raises(Invalid, fn, 1)


def test_number_validation_with_string():
""" test with Number with string"""
schema = Schema({"number" : Number(precision=6, scale=2)})
Expand All @@ -569,7 +570,8 @@ def test_number_validation_with_string():
else:
assert False, "Did not raise Invalid for String"

def test_number_validation_with_invalid_precision_scale():

def test_number_validation_with_invalid_precision_invalid_scale():
""" test with Number with invalid precision and scale"""
schema = Schema({"number" : Number(precision=6, scale=2)})
try:
Expand All @@ -580,33 +582,38 @@ def test_number_validation_with_invalid_precision_scale():
else:
assert False, "Did not raise Invalid for String"

def test_number_validation_with_valid_number():

def test_number_validation_with_valid_precision_scale():
""" test with Number with valid precision and scale"""
schema = Schema({"number" : Number(precision=6, scale=2)})
schema = Schema({"number" : Number(precision=6, scale=2, yield_decimal=True)})
out_ = schema({"number": '1234.00'})
assert_equal(float(out_.get("number")), 1234.00)


def test_number_when_precision_scale_none():
""" test with Number with no precision and scale"""
schema = Schema({"number" : Number()})
schema = Schema({"number" : Number(yield_decimal=True)})
out_ = schema({"number": '12345678901234'})
assert_equal(out_.get("number"), 12345678901234)

def test_number_when_precision_none_n_valid_scale_1():

def test_number_when_precision_none_n_valid_scale_case1():
""" test with Number with no precision and valid scale case 1"""
schema = Schema({"number" : Number(scale=2)})
schema = Schema({"number" : Number(scale=2, yield_decimal=True)})
out_ = schema({"number": '123456789.34'})
assert_equal(float(out_.get("number")), 123456789.34)

def test_number_when_precision_none_n_valid_scale_2():

def test_number_when_precision_none_n_valid_scale_case2():
""" test with Number with no precision and valid scale case 2 with zero in decimal part"""
schema = Schema({"number" : Number(scale=2)})
schema = Schema({"number" : Number(scale=2, yield_decimal=True)})
out_ = schema({"number": '123456789012.00'})
assert_equal(float(out_.get("number")), 123456789012.00)


def test_number_when_precision_none_n_invalid_scale():
""" test with Number with no precision and invalid scale"""
schema = Schema({"number" : Number(scale=2)})
schema = Schema({"number" : Number(scale=2, yield_decimal=True)})
try:
schema({"number": '12345678901.234'})
except MultipleInvalid as e:
Expand All @@ -615,15 +622,17 @@ def test_number_when_precision_none_n_invalid_scale():
else:
assert False, "Did not raise Invalid for String"

def test_number_when_scale_none_n_valid_precision():

def test_number_when_valid_precision_n_scale_none():
""" test with Number with no precision and valid scale"""
schema = Schema({"number" : Number(precision=14)})
schema = Schema({"number" : Number(precision=14, yield_decimal=True)})
out_ = schema({"number": '1234567.8901234'})
assert_equal(float(out_.get("number")), 1234567.8901234)

def test_number_when_scale_none_n_invalid_precision():

def test_number_when_invalid_precision_n_scale_none():
""" test with Number with no precision and invalid scale"""
schema = Schema({"number" : Number(precision=14)})
schema = Schema({"number" : Number(precision=14, yield_decimal=True)})
try:
schema({"number": '12345674.8901234'})
except MultipleInvalid as e:
Expand All @@ -632,3 +641,17 @@ def test_number_when_scale_none_n_invalid_precision():
else:
assert False, "Did not raise Invalid for String"


def test_number_validation_with_valid_precision_scale_yield_decimal_none():
""" test with Number with valid precision, scale and no yield_decimal"""
schema = Schema({"number" : Number(precision=6, scale=2)})
out_ = schema({"number": '1234.00'})
assert_equal(out_.get("number"), '1234.00')


def test_number_validation_with_valid_precision_scale_yield_decimal_false():
""" test with Number with valid precision, scale and no yield_decimal"""
schema = Schema({"number" : Number(precision=6, scale=2, yield_decimal=False)})
out_ = schema({"number": '1234.00'})
assert_equal(out_.get("number"), '1234.00')

16 changes: 11 additions & 5 deletions voluptuous/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -799,27 +799,31 @@ def __repr__(self):

class Number(object):
"""
Verify the maximum number of digits that are present in the number(Precision),
Verify the number of digits that are present in the number(Precision),
and the decimal places(Scale)
:raises Invalid: If the value does not match the provided Precision and Scale.
>>> schema = Schema(Number(precision=6, scale=2))
>>> schema('1234.01')
1234.01
>>> schema = Schema(Number(precision=6, scale=2, yield_decimal=True))
>>> schema('1234.01')
Decimal('1234.01')
"""

def __init__(self, precision=None, scale=None, msg=None):
def __init__(self, precision=None, scale=None, msg=None, yield_decimal=False):
self.precision = precision
self.scale = scale
self.msg = msg
self.yield_decimal = yield_decimal

def __call__(self, v):
"""
:param v: is a number enclosed with string
:return: Decimal number
"""
precision, scale, number = self._get_precision_scale(v)
precision, scale, decimal_num = self._get_precision_scale(v)

if self.precision is not None and self.scale is not None and\
precision != self.precision and scale != self.scale:
Expand All @@ -831,7 +835,10 @@ def __call__(self, v):
if self.scale is not None and scale != self.scale :
raise Invalid(self.msg or "Scale must be equal to %s"%self.scale)

return number
if self.yield_decimal:
return decimal_num
else:
return v

def __repr__(self):
return ('Number(precision=%s, scale=%s, msg=%s)' % (self.precision, self.scale, self.msg))
Expand All @@ -841,7 +848,6 @@ def _get_precision_scale(self, number):
:param number:
:return: tuple(precision, scale, decimal_number)
"""

try:
decimal_num = Decimal(number)
except InvalidOperation:
Expand Down

0 comments on commit 2135bd3

Please sign in to comment.