File: testparamunion.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 (66 lines) | stat: -rw-r--r-- 1,837 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
"""
UnitTest for param_union helper
"""
import logging
import unittest

import param


class MyHandler(logging.StreamHandler):

    def __init__(self):
        super().__init__()
        self.records = []

    def emit(self, record):
        self.records.append(record)


class TestParamUnion(unittest.TestCase):

    def setUp(self):
        self.logger = param.get_logger()
        self.handler = MyHandler()
        self.logger.addHandler(self.handler)

    def tearDown(self):
        self.logger.removeHandler(self.handler)

    def test_param_union_values(self):
        class A(param.Parameterized):
            a = param.Number(1)
        class B(param.Parameterized):
            b = param.Number(2)
        class C(A, B):
            pass
        a = A()
        a.a = 10
        b = B()
        b.b = 5
        c_1 = C(**param.param_union(a))
        self.assertTrue(c_1.a == 10 and c_1.b == 2)
        c_2 = C(**param.param_union(b))
        self.assertTrue(c_2.a == 1 and c_2.b == 5)
        c_3 = C(**param.param_union(a, b))
        self.assertTrue(c_3.a == 10 and c_3.b == 5)
        c_4 = C(**param.param_union())
        self.assertTrue(c_4.a == 1 and c_4.b == 2)

    def test_param_union_warnings(self):
        class A(param.Parameterized):
            a = param.Number(1)
        a = A()
        A(**param.param_union(a))
        self.assertFalse(self.handler.records)
        A(**param.param_union())
        self.assertFalse(self.handler.records)
        A(**param.param_union(a, a))
        self.assertTrue(self.handler.records)
        self.handler.records.pop()
        A(**param.param_union(a, a, warn=False))
        self.assertFalse(self.handler.records)

    def test_param_union_raises_on_unexpected_kwarg(self):
        with self.assertRaises(TypeError):
            param.param_union(dumbdumbface=True)