Coverage for tests/test_yaml.py: 18%
89 statements
« prev ^ index » next coverage.py v6.5.0, created at 2022-10-12 02:20 -0700
« prev ^ index » next coverage.py v6.5.0, created at 2022-10-12 02:20 -0700
1# This file is part of daf_base
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 program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <http://www.gnu.org/licenses/>.
22"""Test YAML serialization"""
24import os
25import unittest
27try:
28 import yaml
29except ImportError:
30 yaml = None
32import lsst.daf.base
34TESTDIR = os.path.abspath(os.path.dirname(__file__))
37class YAMLTestCase(unittest.TestCase):
39 @unittest.skipIf(yaml is None, "yaml module not installed")
40 def setUp(self):
41 # In pyyaml >= 5.1 we prefer to use FullLoader
42 try:
43 self.yamlLoader = yaml.SafeLoader
44 except AttributeError:
45 self.yamlLoader = yaml.Loader
47 def testYamlPS(self):
48 ps = lsst.daf.base.PropertySet()
49 ps.setBool("bool", True)
50 ps.setShort("short", 42)
51 ps.setInt("int", 2008)
52 ps.setLongLong("int64_t", 0xfeeddeadbeef)
53 ps.setFloat("float", 3.14159)
54 ps.setDouble("double", 2.718281828459045)
55 ps.set("char*", "foo")
56 ps.setString("string", "bar")
57 ps.set("char*", u"foo")
58 ps.setString("string", u"bar")
59 ps.set("int2", 2009)
60 ps.set("dt", lsst.daf.base.DateTime("20090402T072639.314159265Z", lsst.daf.base.DateTime.UTC))
61 ps.set("blank", "")
62 ps.set("undef", None)
64 ps2 = yaml.load(yaml.dump(ps), Loader=self.yamlLoader)
65 self.assertIsInstance(ps2, lsst.daf.base.PropertySet)
66 self.assertEqual(ps, ps2)
68 def testYamlPL(self):
69 apl = lsst.daf.base.PropertyList()
70 apl.setBool("bool", True)
71 apl.setShort("short", 42)
72 apl.setInt("int", 2008)
73 apl.setLongLong("int64_t", 0xfeeddeadbeef)
74 apl.setFloat("float", 3.14159)
75 apl.setDouble("double", 2.718281828459045)
76 apl.set("char*", "foo")
77 apl.setString("string", "bar")
78 apl.set("int2", 2009)
79 apl.set("dt", lsst.daf.base.DateTime("20090402T072639.314159265Z", lsst.daf.base.DateTime.UTC))
80 apl.set("undef", None)
82 apl2 = yaml.load(yaml.dump(apl), Loader=self.yamlLoader)
83 self.assertIsInstance(apl2, lsst.daf.base.PropertyList)
84 self.assertEqual(apl, apl2)
86 def testYamlNest(self):
87 """Test nested property sets
88 """
89 ps = lsst.daf.base.PropertySet()
90 ps.setBool("bool", True)
91 ps.setShort("short", 42)
92 ps.setInt("int", 2008)
94 ps2 = lsst.daf.base.PropertySet()
95 ps2.setString("string", "foo")
96 ps2.setString("string2", "bar")
98 ps.setPropertySet("ps", ps2)
100 ps3 = yaml.load(yaml.dump(ps), Loader=self.yamlLoader)
101 self.assertEqual(ps3, ps)
102 self.assertEqual(ps3.getPropertySet("ps"), ps.getPropertySet("ps"))
104 # Now for a PropertyList
105 apl = lsst.daf.base.PropertyList()
106 apl.setBool("bool", True)
107 apl.setShort("short", 42)
108 apl.setInt("int", 2008)
109 apl.add("withcom", "string", "a comment")
110 apl.setPropertySet("ps", ps3)
112 apl2 = yaml.load(yaml.dump(apl), Loader=self.yamlLoader)
113 self.assertEqual(apl2, apl)
115 # Add the PropertyList to the PropertySet to ensure that the
116 # correct type is returned and no loss of comments
117 ps.setPropertySet("newpl", apl)
118 apl3 = yaml.load(yaml.dump(ps), Loader=self.yamlLoader)
119 self.assertEqual(apl3, ps)
121 def testYamlDateTime(self):
122 ts = lsst.daf.base.DateTime("2004-03-01T12:39:45.1Z", lsst.daf.base.DateTime.UTC)
123 ts2 = yaml.load(yaml.dump(ts), Loader=self.yamlLoader)
124 self.assertIsInstance(ts2, lsst.daf.base.DateTime)
125 self.assertEqual(ts, ts2)
127 def testLoader(self):
128 """Test loading of reference YAML files
129 """
130 # Old and new serialization of a propertyList
131 with open(os.path.join(TESTDIR, "data", "fitsheader-tuple.yaml")) as fd:
132 old = yaml.load(fd, Loader=yaml.Loader)
133 with open(os.path.join(TESTDIR, "data", "fitsheader.yaml")) as fd:
134 new = yaml.load(fd, Loader=self.yamlLoader)
135 self.assertIsInstance(new, lsst.daf.base.PropertyList)
136 self.assertIsInstance(old, lsst.daf.base.PropertyList)
137 self.assertEqual(old, new)
140if __name__ == '__main__': 140 ↛ 141line 140 didn't jump to line 141, because the condition on line 140 was never true
141 unittest.main()