Coverage for tests/test_packages.py : 19%

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 self.assertIsInstance(new_yaml, lsst.base.Packages)
91 self.assertEqual(new, packages)
92 self.assertEqual(new_pkl, new)
93 self.assertEqual(new, new_yaml)
95 with self.assertRaises(ValueError):
96 self._writeTempFile(packages, ".txt")
98 with self.assertRaises(ValueError):
99 # .txt extension is not recognized
100 lsst.base.Packages.read("something.txt")
102 # 'packages' and 'new' should have identical content
103 self.assertDictEqual(packages.difference(new), {})
104 self.assertDictEqual(packages.missing(new), {})
105 self.assertDictEqual(packages.extra(new), {})
106 self.assertEqual(len(packages), len(new))
108 # Check inverted comparisons
109 self.assertDictEqual(new.difference(packages), {})
110 self.assertDictEqual(new.missing(packages), {})
111 self.assertDictEqual(new.extra(packages), {})
113 # Now load an obscure python package and the list of packages should change
114 import smtpd # noqa Shouldn't be used by anything we've previously imported
115 new = lsst.base.Packages.fromSystem()
116 self.assertDictEqual(packages.difference(new), {}) # No inconsistencies
117 self.assertDictEqual(packages.extra(new), {}) # Nothing in 'packages' that's not in 'new'
118 missing = packages.missing(new)
119 self.assertGreater(len(missing), 0) # 'packages' should be missing some stuff in 'new'
120 self.assertIn("smtpd", missing)
122 # Inverted comparisons
123 self.assertDictEqual(new.difference(packages), {})
124 self.assertDictEqual(new.missing(packages), {}) # Nothing in 'new' that's not in 'packages'
125 extra = new.extra(packages)
126 self.assertGreater(len(extra), 0) # 'new' has extra stuff compared to 'packages'
127 self.assertIn("smtpd", extra)
129 packages.update(new) # Should now be identical
130 self.assertDictEqual(packages.difference(new), {})
131 self.assertDictEqual(packages.missing(new), {})
132 self.assertDictEqual(packages.extra(new), {})
133 self.assertEqual(len(packages), len(new))
136if __name__ == "__main__": 136 ↛ 137line 136 didn't jump to line 137, because the condition on line 136 was never true
137 unittest.main()