Coverage for tests/test_configurableField.py: 30%
85 statements
« prev ^ index » next coverage.py v6.4.2, created at 2022-08-04 02:21 -0700
« prev ^ index » next coverage.py v6.4.2, created at 2022-08-04 02:21 -0700
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 pickle
30import unittest
32import lsst.pex.config as pexConf
35class Config1(pexConf.Config):
36 f = pexConf.Field("f", dtype=float, default=5, check=lambda x: x > 0)
39class Target1:
40 ConfigClass = Config1
42 def __init__(self, config):
43 self.f = config.f
46def Target2(config):
47 return config.f
50class Config2(pexConf.Config):
51 c1 = pexConf.ConfigurableField("c1", target=Target1)
52 c2 = pexConf.ConfigurableField("c2", target=Target2, ConfigClass=Config1, default=Config1(f=3))
55class ConfigurableFieldTest(unittest.TestCase):
56 def testConstructor(self):
57 try:
59 class BadTarget(pexConf.Config):
60 d = pexConf.ConfigurableField("...", target=None)
62 except Exception:
63 pass
64 else:
65 raise SyntaxError("Uncallable targets should not be allowed")
67 try:
69 class NoConfigClass(pexConf.Config):
70 d = pexConf.ConfigurableField("...", target=Target2)
72 except Exception:
73 pass
74 else:
75 raise SyntaxError("Missing ConfigClass should not be allowed")
77 try:
79 class BadConfigClass(pexConf.Config):
80 d = pexConf.DictField("...", target=Target2, ConfigClass=Target2)
82 except Exception:
83 pass
84 else:
85 raise SyntaxError("ConfigClass that are not subclasses of Config should not be allowed")
87 def testBasics(self):
88 c = Config2()
89 self.assertEqual(c.c1.f, 5)
90 self.assertEqual(c.c2.f, 3)
92 self.assertEqual(type(c.c1.apply()), Target1)
93 self.assertEqual(c.c1.apply().f, 5)
94 self.assertEqual(c.c2.apply(), 3)
96 c.c2.retarget(Target1)
97 self.assertEqual(c.c2.f, 3)
98 self.assertEqual(type(c.c2.apply()), Target1)
99 self.assertEqual(c.c2.apply().f, 3)
101 c.c1.f = 2
102 self.assertEqual(c.c1.f, 2)
103 self.assertRaises(pexConf.FieldValidationError, setattr, c.c1, "f", 0)
105 c.c1 = Config1(f=10)
106 self.assertEqual(c.c1.f, 10)
108 c.c1 = Config1
109 self.assertEqual(c.c1.f, 5)
111 f = Config2(**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 c.c2.f = 1
118 c.c1.f = 100
119 f.update(**dict(c.items()))
120 self.assertEqual(f.c1.f, c.c1.f)
121 self.assertEqual(f.c1.target, c.c1.target)
122 self.assertEqual(f.c2.target, c.c2.target)
123 self.assertEqual(f.c2.f, c.c2.f)
125 def testValidate(self):
126 c = Config2()
127 self.assertRaises(pexConf.FieldValidationError, setattr, c.c1, "f", 0)
129 c.validate()
131 def testPersistence(self):
132 c = Config2()
133 c.c2.retarget(Target1)
134 c.c2.f = 10
135 c.save("test.py")
137 r = Config2()
138 r.load("test.py")
139 os.remove("test.py")
141 self.assertEqual(c.c2.f, r.c2.f)
142 self.assertEqual(c.c2.target, r.c2.target)
144 def testNoPickle(self):
145 """Test that pickle support is disabled for the proxy container."""
146 c = Config2()
147 with self.assertRaises(pexConf.UnexpectedProxyUsageError):
148 pickle.dumps(c.c2)
151if __name__ == "__main__": 151 ↛ 152line 151 didn't jump to line 152, because the condition on line 151 was never true
152 unittest.main()