test_configurable.py 3.64 KB
Newer Older
Marco Clemencic's avatar
Marco Clemencic committed
1
2
3
4
5
6
7
8
9
10
#####################################################################################
# (c) Copyright 1998-2019 CERN for the benefit of the LHCb and ATLAS collaborations #
#                                                                                   #
# This software is distributed under the terms of the Apache version 2 licence,     #
# copied verbatim in the file "LICENSE".                                            #
#                                                                                   #
# In applying this licence, CERN does not waive the privileges and immunities       #
# granted to it by virtue of its status as an Intergovernmental Organization        #
# or submit itself to any jurisdiction.                                             #
#####################################################################################
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
from GaudiConfig2.Configurables.TestConf import MyAlg, SimpleOptsAlgTool
from GaudiConfig2 import Configurable, useGlobalInstances
from nose.tools import raises, with_setup


def setup_func():
    Configurable.instances.clear()
    useGlobalInstances(True)


def teardown_func():
    Configurable.instances.clear()
    useGlobalInstances(False)


@with_setup(setup_func, teardown_func)
def test_configurable():

    from GaudiConfig2 import Configurable
    x = MyAlg()
    assert x._properties == {}
    assert not hasattr(x, 'name')

    x.name = 'x'
    assert x.name == 'x'
    x.name = 'x'
    assert Configurable.instances['x'] is x
    assert MyAlg.getInstance('x') is x

    assert MyAlg.getInstance('y') is not x
    y = Configurable.instances['y']
    assert y.name == 'y'
    assert MyAlg.getInstance('y') is y
    assert type(y) is MyAlg
    z = MyAlg('z')

    assert 'z' in Configurable.instances
    assert Configurable.getInstance('z') is z

    try:
        z.name = 'y'
        assert False, 'ValueError expected'
    except ValueError:
        pass
    z.name = 'a'
    assert 'a' in Configurable.instances
    assert 'z' not in Configurable.instances

    del z.name
    assert 'a' not in Configurable.instances

    try:
        z.name = 123
        assert False, 'TypeError expected'
    except TypeError:
        pass

    a = MyAlg(MyAlg.__cpp_type__)
    assert a.__opt_value__() == MyAlg.__cpp_type__

    a.name = 'abc'
    assert a.__opt_value__() == MyAlg.__cpp_type__ + '/abc'

74
    assert a.getFullJobOptName() == MyAlg.__cpp_type__ + '/abc'
75

76
    assert a.toStringProperty() == MyAlg.__cpp_type__ + '/abc'
Gitlab CI's avatar
Gitlab CI committed
77

78
79
80
81
82
83
84
85
    assert MyAlg.getGaudiType() == 'Algorithm'

    assert MyAlg.getType() == 'TestConf::MyAlg'

    assert MyAlg.getDefaultProperty("AStringProp") == 'text'

    assert MyAlg.getDefaultProperties()['ABoolProp'] == False

Gitlab CI's avatar
Gitlab CI committed
86

87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
def test_properties():
    p = MyAlg()

    assert p.NoValidProp is MyAlg._descriptors['NoValidProp'].default
    p.NoValidProp = 'something'
    assert p.NoValidProp == 'something'
    del p.NoValidProp
    assert p.NoValidProp is MyAlg._descriptors['NoValidProp'].default

    p = MyAlg(AnIntProp=42)
    assert p.AnIntProp == 42


@with_setup(setup_func, teardown_func)
def test_parent_handling():
    p = MyAlg('Dummy')

    t = SimpleOptsAlgTool('ATool', parent=p)
    assert t.name == 'Dummy.ATool'
106
107
    assert t.getName() == 'Dummy.ATool'
    assert t.getGaudiType() == 'AlgTool'
108
109
110

    t = SimpleOptsAlgTool('BTool', parent='Dummy.ATool')
    assert t.name == 'Dummy.ATool.BTool'
111
    assert t.getName() == 'Dummy.ATool.BTool'
112
113
114
115
116
117
118
119
120
121
122
123


@with_setup(setup_func, teardown_func)
@raises(AttributeError)
def test_parent_with_no_name():
    SimpleOptsAlgTool('ATool', parent=MyAlg())


@with_setup(setup_func, teardown_func)
@raises(TypeError)
def test_child_with_no_name():
    SimpleOptsAlgTool(parent=MyAlg('Dummy'))