File: test_compound.py

package info (click to toggle)
python-formencode 2.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,856 kB
  • sloc: python: 6,772; makefile: 130; sh: 96; javascript: 61
file content (104 lines) | stat: -rw-r--r-- 3,065 bytes parent folder | download
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import unittest

from formencode import compound, Invalid
from formencode.validators import DictConverter


class TestCompoundValidator(unittest.TestCase):

    def setUp(self):
        self.validator = compound.CompoundValidator()

    def test_repr(self):
        r = repr(self.validator)
        self.assertNotIn('validatorArgs', r)
        self.assertIn('validators=[]', r)

    def test_to_python(self):
        self.assertRaises(NotImplementedError,
            self.validator.to_python, 1)

    def test_from_python(self):
        self.assertRaises(NotImplementedError,
            self.validator.from_python, 1)

    def test_clone(self):
        clone = self.validator()
        self.assertEqual(type(clone), type(self.validator))


class TestAllCompoundValidator(unittest.TestCase):

    def setUp(self):
        self.validator = compound.All(
            validators=[DictConverter({2: 1}), DictConverter({3: 2})])

    def test_repr(self):
        r = repr(self.validator)
        self.assertNotIn('validatorArgs', r)
        self.assertEqual(r.count('DictConverter'), 2)

    def test_to_python(self):
        self.assertEqual(self.validator.to_python(3), 1)

    def test_from_python(self):
        self.assertEqual(self.validator.from_python(1), 3)

    def test_clone(self):
        clone = self.validator()
        self.assertEqual(clone.to_python(3), 1)


class TestAnyCompoundValidator(unittest.TestCase):

    def setUp(self):
        self.validator = compound.Any(
            validators=[DictConverter({2: 'c'}), DictConverter({2: 'b'}),
                DictConverter({1: 'b'})])

    def test_repr(self):
        r = repr(self.validator)
        self.assertNotIn('validatorArgs', r)
        self.assertEqual(r.count('DictConverter'), 3)

    def test_to_python(self):
        # Should stop before 'c' coming from the right.
        self.assertEqual(self.validator.to_python(2), 'b')

    def test_from_python(self):
        # Should stop before 1 coming from the left.
        self.assertEqual(self.validator.from_python('b'), 2)

    def test_to_python_error(self):
        try:
            self.validator.to_python(3)
        except Invalid as e:
            self.assertIn('Enter a value from: 2', str(e))
        else:
            self.fail('Invalid should be raised when no validator succeeds.')

    def test_clone(self):
        clone = self.validator()
        self.assertEqual(clone.to_python(2), 'b')


class TestPipeCompoundValidator(unittest.TestCase):

    def setUp(self):
        self.validator = compound.Pipe(
            validators=[DictConverter({1: 2}), DictConverter({2: 3})])

    def test_repr(self):
        r = repr(self.validator)
        self.assertNotIn('validatorArgs', r)
        self.assertEqual(r.count('DictConverter'), 2)

    def test_to_python(self):
        self.assertEqual(self.validator.to_python(1), 3)

    def test_from_python(self):
        self.assertEqual(self.validator.from_python(3), 1)

    def test_clone(self):
        clone = self.validator()
        self.assertEqual(clone.to_python(1), 3)