test_Configurables.py 2.27 KB
Newer Older
1
2
# Prepare dummy configurables
from GaudiKernel.Configurable import ConfigurableAlgorithm, Configurable
3
from GaudiKernel.DataObjectHandleBase import DataObjectHandleBase
4

Marco Clemencic's avatar
Marco Clemencic committed
5

6
class MyAlg(ConfigurableAlgorithm):
Gitlab CI's avatar
Gitlab CI committed
7
8
9
    __slots__ = {
        'Text': 'some text',
        'Int': 23,
10
        'DataHandle': DataObjectHandleBase('Location', 'R')
Gitlab CI's avatar
Gitlab CI committed
11
    }
Marco Clemencic's avatar
Marco Clemencic committed
12
13

    def getDlls(self):
14
        return 'Dummy'
Marco Clemencic's avatar
Marco Clemencic committed
15
16

    def getType(self):
17
18
        return 'MyAlg'

Marco Clemencic's avatar
Marco Clemencic committed
19

20
21
22
def _clean_confs():
    Configurable.allConfigurables.clear()

Marco Clemencic's avatar
Marco Clemencic committed
23

24
25
26
27
def test_no_settings():
    a = MyAlg()
    assert a.getValuedProperties() == {}

Marco Clemencic's avatar
Marco Clemencic committed
28

29
30
31
32
def test_correct():
    a = MyAlg()
    a.Int = 42
    a.Text = 'value'
33
34
    a.DataHandle = '/Event/X'
    assert a.getValuedProperties() == {
Gitlab CI's avatar
Gitlab CI committed
35
36
        'Int': 42,
        'Text': 'value',
37
        'DataHandle': DataObjectHandleBase('/Event/X', 'R')
Gitlab CI's avatar
Gitlab CI committed
38
    }
39

Marco Clemencic's avatar
Marco Clemencic committed
40

41
42
def test_str_from_datahandle():
    a = MyAlg()
43
    a.Text = DataObjectHandleBase('value', 'R')
44
45
46
    assert a.getProp('Text') == 'value'


47
48
49
50
51
52
53
54
55
def test_invalid_value():
    a = MyAlg()
    try:
        a.Int = 'value'
        assert False, 'exception expected'
    except AssertionError:
        raise
    except ValueError:
        pass
56
    except Exception as x:
Marco Clemencic's avatar
Marco Clemencic committed
57
58
        assert False, 'ValueError exception expected, got %s' % type(
            x).__name__
59
60
61
62
63
64
65
66

    try:
        a.Text = [123]
        assert False, 'exception expected'
    except AssertionError:
        raise
    except ValueError:
        pass
67
    except Exception as x:
Marco Clemencic's avatar
Marco Clemencic committed
68
69
70
        assert False, 'ValueError exception expected, got %s' % type(
            x).__name__

71
72
73
74
75
76
77
78
79
80
81
    try:
        a.DataHandle = [123]
        assert False, 'exception expected'
    except AssertionError:
        raise
    except ValueError:
        pass
    except Exception, x:
        assert False, 'ValueError exception expected, got %s' % type(
            x).__name__

82
83
84
85
86
87
88
89
90
91

def test_invalid_key():
    a = MyAlg()
    try:
        a.Dummy = 'abc'
        assert False, 'exception expected'
    except AssertionError:
        raise
    except AttributeError:
        pass
92
    except Exception as x:
Marco Clemencic's avatar
Marco Clemencic committed
93
94
95
        assert False, 'AttributeError exception expected, got %s' % type(
            x).__name__

96
97
98
99
100

# ensure that all tests start from clean configuration
for _f in dir():
    if _f.startswith('test'):
        setattr(locals()[_f], 'setup', _clean_confs)