-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtest_number_validator.py
66 lines (53 loc) · 1.95 KB
/
test_number_validator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import unittest
from pyvalid.validators import NumberValidator
class NumberValidatorTestCase(unittest.TestCase):
def test_number_type(self):
validator = NumberValidator(number_type=int)
self.assertTrue(validator(12))
self.assertFalse(validator(10.56))
validator = NumberValidator(number_type=float)
self.assertTrue(validator(10.56))
self.assertFalse(validator(12))
def test_min_val(self):
validator = NumberValidator(min_val=-3.14)
self.assertTrue(validator(3.14))
self.assertTrue(validator(-3.14))
self.assertFalse(validator(-273.15))
self.assertFalse(None)
def test_max_val(self):
validator = NumberValidator(max_val=42)
self.assertTrue(validator(8))
self.assertTrue(validator(0))
self.assertFalse(validator(512))
self.assertFalse(None)
def test_in_range(self):
validator = NumberValidator(
in_range=[2**x for x in range(16)]
)
self.assertTrue(validator(1))
self.assertTrue(validator(256.0))
self.assertFalse(validator(0))
self.assertFalse(None)
def test_not_in_range(self):
validator = NumberValidator(
not_in_range=[2**x for x in range(16)]
)
self.assertTrue(validator(0))
self.assertTrue(validator(-1))
self.assertFalse(validator(2))
self.assertFalse(None)
def test_mixed(self):
with self.assertRaises(ValueError):
NumberValidator(min_val=100, max_val=50)
validator = NumberValidator(
min_val=0, max_val=2**16,
not_in_range=[2**x for x in range(16)]
)
self.assertTrue(validator(0))
self.assertTrue(validator(2**16 - 0.1))
self.assertFalse(validator(2**16 + 0.1))
self.assertFalse(validator(8))
self.assertFalse(validator(-8))
self.assertFalse(None)
if __name__ == '__main__':
unittest.main()