int datatype must not accept fractional floats

- added also test for this

Change-Id: I1c3ad92bc10131d6cd096496230222c999f0097e
Reviewed-on: https://forge.frm2.tum.de/review/c/sine2020/secop/playground/+/22737
Tested-by: JenkinsCodeReview <bjoern_pedersen@frm2.tum.de>
Reviewed-by: Markus Zolliker <markus.zolliker@psi.ch>
This commit is contained in:
zolliker 2020-03-19 15:58:35 +01:00
parent 434ef4a423
commit 8c9d5d4e5c
2 changed files with 8 additions and 2 deletions

View File

@ -259,10 +259,11 @@ class IntRange(DataType):
def __call__(self, value):
try:
fvalue = float(value)
value = int(value)
if not (self.min <= value <= self.max) or int(value) != value:
if not self.min <= value <= self.max or round(fvalue) != fvalue:
raise BadValueError('%r should be an int between %d and %d' %
(value, self.min, self.max))
(value, self.min, self.max))
return value
except Exception:
raise BadValueError('Can not convert %r to int' % value)

View File

@ -109,8 +109,13 @@ def test_IntRange():
dt('XX')
with pytest.raises(ValueError):
dt([19, 'X'])
with pytest.raises(ValueError):
dt(1.3)
with pytest.raises(ValueError):
dt('1.3')
dt(1)
dt(0)
dt('1')
with pytest.raises(ProgrammingError):
IntRange('xc', 'Yx')