File: testlist.py

package info (click to toggle)
python-param 2.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,048 kB
  • sloc: python: 17,980; makefile: 3
file content (280 lines) | stat: -rw-r--r-- 8,105 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import re
import unittest

import param
import pytest

from .utils import check_defaults
# TODO: I copied the tests from testobjectselector, although I
# struggled to understand some of them. Both files should be reviewed
# and cleaned up together.

# TODO: tests copied from testobjectselector could use assertRaises
# context manager (and could be updated in testobjectselector too).

class TestListParameters(unittest.TestCase):

    def setUp(self):
        super().setUp()
        class P(param.Parameterized):
            e = param.List([5,6,7], item_type=int)
            l = param.List(["red","green","blue"], item_type=str, bounds=(0,10))
            m = param.List([1, 2, 3], bounds=(3, None))
            n = param.List([1], bounds=(None, 3))

        self.P = P

    def _check_defaults(self, p):
        assert p.default == []
        assert p.allow_None is False
        assert p.class_ is None
        assert p.item_type is None
        assert p.bounds == (0, None)
        assert p.instantiate is True

    def test_defaults_class(self):
        class P(param.Parameterized):
            l = param.List()

        check_defaults(P.param.l, label='L', skip=['instantiate'])
        self._check_defaults(P.param.l)

    def test_defaults_inst(self):
        class P(param.Parameterized):
            l = param.List()

        p = P()

        check_defaults(p.param.l, label='L', skip=['instantiate'])
        self._check_defaults(p.param.l)

    def test_defaults_unbound(self):
        l = param.List()

        check_defaults(l, label=None, skip=['instantiate'])
        self._check_defaults(l)

    def test_default_None(self):
        class Q(param.Parameterized):
            r = param.List(default=[])  #  Also check None)

    def test_set_object_constructor(self):
        p = self.P(e=[6])
        self.assertEqual(p.e, [6])

    def test_set_object_outside_bounds(self):
        p = self.P()
        with pytest.raises(
            ValueError,
            match=re.escape("List parameter 'P.l' length must be between 0 and 10 (inclusive), not 11.")
        ):
            p.l = [6] * 11

    def test_set_object_outside_lower_bounds(self):
        p = self.P()
        with pytest.raises(
            ValueError,
            match=re.escape("List parameter 'P.m' length must be at least 3, not 2.")
        ):
            p.m = [6, 7]

    def test_set_object_outside_upper_bounds(self):
        p = self.P()
        with pytest.raises(
            ValueError,
            match=re.escape("List parameter 'P.n' length must be at most 3, not 4.")
        ):
            p.n = [6] * 4

    def test_set_object_wrong_type(self):
        p = self.P()
        with pytest.raises(
            TypeError,
            match=re.escape("List parameter 'P.e' items must be instances of <class 'int'>, not <class 'str'>.")
        ):
            p.e=['s']

    def test_set_object_not_None(self):
        p = self.P(e=[6])
        with pytest.raises(
            ValueError,
            match=re.escape("List parameter 'P.e' must be a list, not an object of <class 'NoneType'>.")
        ):
            p.e = None

    def test_inheritance_behavior1(self):
        class A(param.Parameterized):
            p = param.List()

        class B(A):
            p = param.List()

        assert B.param.p.default == []
        assert B.param.p.instantiate is True
        assert B.param.p.bounds == (0, None)

        b = B()

        assert b.param.p.default == []
        assert b.param.p.instantiate is True
        assert b.param.p.bounds == (0, None)

    def test_inheritance_behavior2(self):
        class A(param.Parameterized):
            p = param.List(default=[0, 1])

        class B(A):
            p = param.List()

        # B inherits default from A
        assert B.param.p.default == [0 ,1]
        assert B.param.p.instantiate is True
        assert B.param.p.bounds == (0, None)

        b = B()

        assert b.param.p.default == [0, 1]
        assert b.param.p.instantiate is True
        assert b.param.p.bounds == (0, None)

    def test_inheritance_behavior3(self):
        class A(param.Parameterized):
            p = param.List(default=[0, 1], bounds=(1, 10))

        class B(A):
            p = param.List()

        # B inherits default and bounds from A
        assert B.param.p.default == [0, 1]
        assert B.param.p.instantiate is True
        assert B.param.p.bounds == (1, 10)

        b = B()

        assert b.param.p.default == [0, 1]
        assert b.param.p.instantiate is True
        assert b.param.p.bounds == (1, 10)

    def test_inheritance_behavior4(self):
        class A(param.Parameterized):
            p = param.List(default=[0], item_type=int)

        class B(A):
            p = param.List()

        # B inherit item_type
        assert B.param.p.default == [0]
        assert B.param.p.instantiate is True
        assert B.param.p.bounds == (0, None)
        assert B.param.p.item_type == int

        b = B()

        assert b.param.p.default == [0]
        assert b.param.p.instantiate is True
        assert b.param.p.bounds == (0, None)
        assert b.param.p.item_type == int

    def test_inheritance_behavior5(self):
        class A(param.Parameterized):
            p = param.List(default=[0, 1], allow_None=True)

        class B(A):
            p = param.List()

        # B does not inherit allow_None
        assert B.param.p.default == [0, 1]
        assert B.param.p.allow_None is False
        assert B.param.p.instantiate is True
        assert B.param.p.bounds == (0, None)

        b = B()

        assert b.param.p.default == [0, 1]
        assert b.param.p.allow_None is False
        assert b.param.p.instantiate is True
        assert b.param.p.bounds == (0, None)

    def test_inheritance_behavior6(self):
        class A(param.Parameterized):
            p = param.List(default=[0, 1], bounds=(1, 10))

        class B(A):
            p = param.List(default=[0, 1, 2, 3])

        assert B.param.p.default == [0, 1, 2, 3]
        assert B.param.p.instantiate is True
        assert B.param.p.bounds == (1, 10)

        b = B()

        assert b.param.p.default == [0, 1, 2, 3]
        assert b.param.p.instantiate is True
        assert b.param.p.bounds == (1, 10)


class TestHookListParameters(unittest.TestCase):

    def setUp(self):
        super().setUp()
        class P(param.Parameterized):
            e = param.HookList([abs])
            l = param.HookList(bounds=(0,10))

        self.P = P

    def _check_defaults(self, p):
        assert p.default == []
        assert p.allow_None is False
        assert p.class_ is None
        assert p.item_type is None
        assert p.bounds == (0, None)
        assert p.instantiate is True

    def test_defaults_class(self):
        class P(param.Parameterized):
            l = param.HookList()

        check_defaults(P.param.l, label='L', skip=['instantiate'])
        self._check_defaults(P.param.l)

    def test_defaults_inst(self):
        class P(param.Parameterized):
            l = param.HookList()

        p = P()

        check_defaults(p.param.l, label='L', skip=['instantiate'])
        self._check_defaults(p.param.l)

    def test_defaults_unbound(self):
        l = param.HookList()

        check_defaults(l, label=None, skip=['instantiate'])
        self._check_defaults(l)

    def test_default_None(self):
        class Q(param.Parameterized):
            r = param.HookList(default=[])  #  Also check None)

    def test_set_object_constructor(self):
        p = self.P(e=[abs])
        self.assertEqual(p.e, [abs])

    def test_set_object_outside_bounds(self):
        p = self.P()
        with pytest.raises(ValueError):
            p.l = [abs] * 11

    def test_set_object_wrong_type_foo(self):
        p = self.P()
        with pytest.raises(
            ValueError,
            match=re.escape("HookList parameter 'P.e' items must be callable, not 's'.")
        ):
            p.e = ['s']

    def test_set_object_not_None(self):
        p = self.P()
        with pytest.raises(ValueError):
            p.e = None