Coverage for tests/test_configurableField.py: 25%
Shortcuts on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# This file is part of pex_config.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (http://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# This software is dual licensed under the GNU General Public License and also
10# under a 3-clause BSD license. Recipients may choose which of these licenses
11# to use; please see the files gpl-3.0.txt and/or bsd_license.txt,
12# respectively. If you choose the GPL option then the following text applies
13# (but note that there is still no warranty even if you opt for BSD instead):
14#
15# This program is free software: you can redistribute it and/or modify
16# it under the terms of the GNU General Public License as published by
17# the Free Software Foundation, either version 3 of the License, or
18# (at your option) any later version.
19#
20# This program is distributed in the hope that it will be useful,
21# but WITHOUT ANY WARRANTY; without even the implied warranty of
22# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23# GNU General Public License for more details.
24#
25# You should have received a copy of the GNU General Public License
26# along with this program. If not, see <http://www.gnu.org/licenses/>.
28import os
29import unittest
30import lsst.pex.config as pexConf
33class Config1(pexConf.Config):
34 f = pexConf.Field("f", dtype=float, default=5, check=lambda x: x > 0)
37class Target1:
38 ConfigClass = Config1
40 def __init__(self, config):
41 self.f = config.f
44def Target2(config):
45 return config.f
48class Config2(pexConf.Config):
49 c1 = pexConf.ConfigurableField("c1", target=Target1)
50 c2 = pexConf.ConfigurableField("c2", target=Target2, ConfigClass=Config1, default=Config1(f=3))
53class ConfigurableFieldTest(unittest.TestCase):
54 def testConstructor(self):
55 try:
56 class BadTarget(pexConf.Config):
57 d = pexConf.ConfigurableField("...", target=None)
58 except Exception:
59 pass
60 else:
61 raise SyntaxError("Uncallable targets should not be allowed")
63 try:
64 class NoConfigClass(pexConf.Config):
65 d = pexConf.ConfigurableField("...", target=Target2)
66 except Exception:
67 pass
68 else:
69 raise SyntaxError("Missing ConfigClass should not be allowed")
71 try:
72 class BadConfigClass(pexConf.Config):
73 d = pexConf.DictField("...", target=Target2, ConfigClass=Target2)
74 except Exception:
75 pass
76 else:
77 raise SyntaxError("ConfigClass that are not subclasses of Config should not be allowed")
79 def testBasics(self):
80 c = Config2()
81 self.assertEqual(c.c1.f, 5)
82 self.assertEqual(c.c2.f, 3)
84 self.assertEqual(type(c.c1.apply()), Target1)
85 self.assertEqual(c.c1.apply().f, 5)
86 self.assertEqual(c.c2.apply(), 3)
88 c.c2.retarget(Target1)
89 self.assertEqual(c.c2.f, 3)
90 self.assertEqual(type(c.c2.apply()), Target1)
91 self.assertEqual(c.c2.apply().f, 3)
93 c.c1.f = 2
94 self.assertEqual(c.c1.f, 2)
95 self.assertRaises(pexConf.FieldValidationError, setattr, c.c1, "f", 0)
97 c.c1 = Config1(f=10)
98 self.assertEqual(c.c1.f, 10)
100 c.c1 = Config1
101 self.assertEqual(c.c1.f, 5)
103 f = Config2(**dict(c.items()))
104 self.assertEqual(f.c1.f, c.c1.f)
105 self.assertEqual(f.c1.target, c.c1.target)
106 self.assertEqual(f.c2.target, c.c2.target)
107 self.assertEqual(f.c2.f, c.c2.f)
109 c.c2.f = 1
110 c.c1.f = 100
111 f.update(**dict(c.items()))
112 self.assertEqual(f.c1.f, c.c1.f)
113 self.assertEqual(f.c1.target, c.c1.target)
114 self.assertEqual(f.c2.target, c.c2.target)
115 self.assertEqual(f.c2.f, c.c2.f)
117 def testValidate(self):
118 c = Config2()
119 self.assertRaises(pexConf.FieldValidationError, setattr, c.c1, "f", 0)
121 c.validate()
123 def testPersistence(self):
124 c = Config2()
125 c.c2.retarget(Target1)
126 c.c2.f = 10
127 c.save("test.py")
129 r = Config2()
130 r.load("test.py")
131 os.remove("test.py")
133 self.assertEqual(c.c2.f, r.c2.f)
134 self.assertEqual(c.c2.target, r.c2.target)
137if __name__ == "__main__": 137 ↛ 138line 137 didn't jump to line 138, because the condition on line 137 was never true
138 unittest.main()