Coverage for tests/test_packages.py : 16%

Hot-keys 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#!/usr/bin/env python
2#
3# LSST Data Management System
4# Copyright 2016 AURA/LSST.
5#
6# This product includes software developed by the
7# LSST Project (http://www.lsst.org/).
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 LSST License Statement and
20# the GNU General Public License along with this program. If not,
21# see <https://www.lsstcorp.org/LegalNotices/>.
22#
23import os
24import unittest
25import tempfile
27import lsst.base
30class PackagesTestCase(unittest.TestCase):
31 """Tests for package version collection
33 Unfortunately, we're somewhat limited in what we can test because
34 we only get the versions of things being used at runtime, and this
35 package sits rather low in the dependency chain so there's not
36 usually a lot of other packages available when this test gets run.
37 Therefore some of the tests are only checking that things don't
38 explode, since they are incapable of testing much more than that.
39 """
41 def testPython(self):
42 """Test that we get the right version for this python package"""
43 versions = lsst.base.getPythonPackages()
44 expected = (lsst.base.version.__version__)
45 self.assertEqual(versions["base"], expected)
47 def testEnvironment(self):
48 """Test getting versions from the environment
50 Unfortunately, none of the products that need their versions divined from the
51 environment are dependencies of this package, and so all we can do is test
52 that this doesn't fall over.
53 """
54 lsst.base.getEnvironmentPackages()
56 def testRuntime(self):
57 """Test getting versions from runtime libraries
59 Unfortunately, none of the products that we get runtime versions from are
60 dependencies of this package, and so all we can do is test that this doesn't
61 fall over.
62 """
63 lsst.base.getRuntimeVersions()
65 def _writeTempFile(self, packages, suffix):
66 """Write packages to a temp file using the supplied suffix and read
67 back.
68 """
69 # Can't use lsst.utils.tests.getTempFilePath because we're its dependency
70 temp = tempfile.NamedTemporaryFile(prefix="packages.", suffix=suffix, delete=False)
71 tempName = temp.name
72 temp.close() # We don't use the fd, just want a filename
73 try:
74 packages.write(tempName)
75 new = lsst.base.Packages.read(tempName)
76 finally:
77 os.unlink(tempName)
78 return new
80 def testPackages(self):
81 """Test the Packages class"""
82 packages = lsst.base.Packages.fromSystem()
84 # Test pickling and YAML
85 new = self._writeTempFile(packages, ".pickle")
86 new_pkl = self._writeTempFile(packages, ".pkl")
87 new_yaml = self._writeTempFile(packages, ".yaml")
89 self.assertIsInstance(new, lsst.base.Packages,
90 f"Checking type ({type(new)}) from pickle")
91 self.assertIsInstance(new_yaml, lsst.base.Packages,
92 f"Checking type ({type(new_yaml)}) from YAML")
93 self.assertEqual(new, packages)
94 self.assertEqual(new_pkl, new)
95 self.assertEqual(new, new_yaml)
97 with self.assertRaises(ValueError):
98 self._writeTempFile(packages, ".txt")
100 with self.assertRaises(ValueError):
101 # .txt extension is not recognized
102 lsst.base.Packages.read("something.txt")
104 # 'packages' and 'new' should have identical content
105 self.assertDictEqual(packages.difference(new), {})
106 self.assertDictEqual(packages.missing(new), {})
107 self.assertDictEqual(packages.extra(new), {})
108 self.assertEqual(len(packages), len(new))
110 # Check inverted comparisons
111 self.assertDictEqual(new.difference(packages), {})
112 self.assertDictEqual(new.missing(packages), {})
113 self.assertDictEqual(new.extra(packages), {})
115 # Now load an obscure python package and the list of packages should change
116 import smtpd # noqa Shouldn't be used by anything we've previously imported
117 new = lsst.base.Packages.fromSystem()
118 self.assertDictEqual(packages.difference(new), {}) # No inconsistencies
119 self.assertDictEqual(packages.extra(new), {}) # Nothing in 'packages' that's not in 'new'
120 missing = packages.missing(new)
121 self.assertGreater(len(missing), 0) # 'packages' should be missing some stuff in 'new'
122 self.assertIn("smtpd", missing)
124 # Inverted comparisons
125 self.assertDictEqual(new.difference(packages), {})
126 self.assertDictEqual(new.missing(packages), {}) # Nothing in 'new' that's not in 'packages'
127 extra = new.extra(packages)
128 self.assertGreater(len(extra), 0) # 'new' has extra stuff compared to 'packages'
129 self.assertIn("smtpd", extra)
131 packages.update(new) # Should now be identical
132 self.assertDictEqual(packages.difference(new), {})
133 self.assertDictEqual(packages.missing(new), {})
134 self.assertDictEqual(packages.extra(new), {})
135 self.assertEqual(len(packages), len(new))
137 # Serialize via bytes
138 for format in ("pickle", "yaml"):
139 asbytes = new.toBytes(format)
140 from_bytes = lsst.base.Packages.fromBytes(asbytes, format)
141 self.assertEqual(from_bytes, new)
143 with self.assertRaises(ValueError):
144 new.toBytes("unknown_format")
146 with self.assertRaises(ValueError):
147 lsst.base.Packages.fromBytes(from_bytes, "unknown_format")
149 with self.assertRaises(TypeError):
150 some_yaml = b"list: [1, 2]"
151 lsst.base.Packages.fromBytes(some_yaml, "yaml")
154if __name__ == "__main__": 154 ↛ 155line 154 didn't jump to line 155, because the condition on line 154 was never true
155 unittest.main()