Coverage for tests/test_configurableField.py: 30%
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
31import lsst.pex.config as pexConf
34class Config1(pexConf.Config):
35 f = pexConf.Field("f", dtype=float, default=5, check=lambda x: x > 0)
38class Target1:
39 ConfigClass = Config1
41 def __init__(self, config):
42 self.f = config.f
45def Target2(config):
46 return config.f
49class Config2(pexConf.Config):
50 c1 = pexConf.ConfigurableField("c1", target=Target1)
51 c2 = pexConf.ConfigurableField("c2", target=Target2, ConfigClass=Config1, default=Config1(f=3))
54class ConfigurableFieldTest(unittest.TestCase):
55 def testConstructor(self):
56 try:
58 class BadTarget(pexConf.Config):
59 d = pexConf.ConfigurableField("...", target=None)
61 except Exception:
62 pass
63 else:
64 raise SyntaxError("Uncallable targets should not be allowed")
66 try:
68 class NoConfigClass(pexConf.Config):
69 d = pexConf.ConfigurableField("...", target=Target2)
71 except Exception:
72 pass
73 else:
74 raise SyntaxError("Missing ConfigClass should not be allowed")
76 try:
78 class BadConfigClass(pexConf.Config):
79 d = pexConf.DictField("...", target=Target2, ConfigClass=Target2)
81 except Exception:
82 pass
83 else:
84 raise SyntaxError("ConfigClass that are not subclasses of Config should not be allowed")
86 def testBasics(self):
87 c = Config2()
88 self.assertEqual(c.c1.f, 5)
89 self.assertEqual(c.c2.f, 3)
91 self.assertEqual(type(c.c1.apply()), Target1)
92 self.assertEqual(c.c1.apply().f, 5)
93 self.assertEqual(c.c2.apply(), 3)
95 c.c2.retarget(Target1)
96 self.assertEqual(c.c2.f, 3)
97 self.assertEqual(type(c.c2.apply()), Target1)
98 self.assertEqual(c.c2.apply().f, 3)
100 c.c1.f = 2
101 self.assertEqual(c.c1.f, 2)
102 self.assertRaises(pexConf.FieldValidationError, setattr, c.c1, "f", 0)
104 c.c1 = Config1(f=10)
105 self.assertEqual(c.c1.f, 10)
107 c.c1 = Config1
108 self.assertEqual(c.c1.f, 5)
110 f = Config2(**dict(c.items()))
111 self.assertEqual(f.c1.f, c.c1.f)
112 self.assertEqual(f.c1.target, c.c1.target)
113 self.assertEqual(f.c2.target, c.c2.target)
114 self.assertEqual(f.c2.f, c.c2.f)
116 c.c2.f = 1
117 c.c1.f = 100
118 f.update(**dict(c.items()))
119 self.assertEqual(f.c1.f, c.c1.f)
120 self.assertEqual(f.c1.target, c.c1.target)
121 self.assertEqual(f.c2.target, c.c2.target)
122 self.assertEqual(f.c2.f, c.c2.f)
124 def testValidate(self):
125 c = Config2()
126 self.assertRaises(pexConf.FieldValidationError, setattr, c.c1, "f", 0)
128 c.validate()
130 def testPersistence(self):
131 c = Config2()
132 c.c2.retarget(Target1)
133 c.c2.f = 10
134 c.save("test.py")
136 r = Config2()
137 r.load("test.py")
138 os.remove("test.py")
140 self.assertEqual(c.c2.f, r.c2.f)
141 self.assertEqual(c.c2.target, r.c2.target)
144if __name__ == "__main__": 144 ↛ 145line 144 didn't jump to line 145, because the condition on line 144 was never true
145 unittest.main()