Coverage for tests/test_packages.py: 16%

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

75 statements  

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 

26 

27import lsst.base 

28 

29 

30class PackagesTestCase(unittest.TestCase): 

31 """Tests for package version collection 

32 

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 """ 

40 

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) 

46 

47 def testEnvironment(self): 

48 """Test getting versions from the environment 

49 

50 Unfortunately, none of the products that need their versions divined 

51 from the environment are dependencies of this package, and so all we 

52 can do is test that this doesn't fall over. 

53 """ 

54 lsst.base.getEnvironmentPackages() 

55 

56 def testRuntime(self): 

57 """Test getting versions from runtime libraries 

58 

59 Unfortunately, none of the products that we get runtime versions from 

60 are dependencies of this package, and so all we can do is test that 

61 this doesn't fall over. 

62 """ 

63 lsst.base.getRuntimeVersions() 

64 

65 def testConda(self): 

66 """Test getting versions from conda environement 

67 

68 We do not rely on being run in a conda environment so all we can do is 

69 test that this doesn't fall over. 

70 """ 

71 lsst.base.getCondaPackages() 

72 

73 def _writeTempFile(self, packages, suffix): 

74 """Write packages to a temp file using the supplied suffix and read 

75 back. 

76 """ 

77 # Can't use lsst.utils.tests.getTempFilePath because we're its 

78 # dependency 

79 temp = tempfile.NamedTemporaryFile(prefix="packages.", suffix=suffix, delete=False) 

80 tempName = temp.name 

81 temp.close() # We don't use the fd, just want a filename 

82 try: 

83 packages.write(tempName) 

84 new = lsst.base.Packages.read(tempName) 

85 finally: 

86 os.unlink(tempName) 

87 return new 

88 

89 def testPackages(self): 

90 """Test the Packages class""" 

91 packages = lsst.base.Packages.fromSystem() 

92 

93 # Test pickling and YAML 

94 new = self._writeTempFile(packages, ".pickle") 

95 new_pkl = self._writeTempFile(packages, ".pkl") 

96 new_yaml = self._writeTempFile(packages, ".yaml") 

97 

98 self.assertIsInstance(new, lsst.base.Packages, 

99 f"Checking type ({type(new)}) from pickle") 

100 self.assertIsInstance(new_yaml, lsst.base.Packages, 

101 f"Checking type ({type(new_yaml)}) from YAML") 

102 self.assertEqual(new, packages) 

103 self.assertEqual(new_pkl, new) 

104 self.assertEqual(new, new_yaml) 

105 

106 with self.assertRaises(ValueError): 

107 self._writeTempFile(packages, ".txt") 

108 

109 with self.assertRaises(ValueError): 

110 # .txt extension is not recognized 

111 lsst.base.Packages.read("something.txt") 

112 

113 # 'packages' and 'new' should have identical content 

114 self.assertDictEqual(packages.difference(new), {}) 

115 self.assertDictEqual(packages.missing(new), {}) 

116 self.assertDictEqual(packages.extra(new), {}) 

117 self.assertEqual(len(packages), len(new)) 

118 

119 # Check inverted comparisons 

120 self.assertDictEqual(new.difference(packages), {}) 

121 self.assertDictEqual(new.missing(packages), {}) 

122 self.assertDictEqual(new.extra(packages), {}) 

123 

124 # Now load an obscure python package and the list of packages should 

125 # change 

126 # Shouldn't be used by anything we've previously imported 

127 import smtpd # noqa: F401 

128 new = lsst.base.Packages.fromSystem() 

129 self.assertDictEqual(packages.difference(new), {}) # No inconsistencies 

130 self.assertDictEqual(packages.extra(new), {}) # Nothing in 'packages' that's not in 'new' 

131 missing = packages.missing(new) 

132 self.assertGreater(len(missing), 0) # 'packages' should be missing some stuff in 'new' 

133 self.assertIn("smtpd", missing) 

134 

135 # Inverted comparisons 

136 self.assertDictEqual(new.difference(packages), {}) 

137 self.assertDictEqual(new.missing(packages), {}) # Nothing in 'new' that's not in 'packages' 

138 extra = new.extra(packages) 

139 self.assertGreater(len(extra), 0) # 'new' has extra stuff compared to 'packages' 

140 self.assertIn("smtpd", extra) 

141 

142 packages.update(new) # Should now be identical 

143 self.assertDictEqual(packages.difference(new), {}) 

144 self.assertDictEqual(packages.missing(new), {}) 

145 self.assertDictEqual(packages.extra(new), {}) 

146 self.assertEqual(len(packages), len(new)) 

147 

148 # Serialize via bytes 

149 for format in ("pickle", "yaml"): 

150 asbytes = new.toBytes(format) 

151 from_bytes = lsst.base.Packages.fromBytes(asbytes, format) 

152 self.assertEqual(from_bytes, new) 

153 

154 with self.assertRaises(ValueError): 

155 new.toBytes("unknown_format") 

156 

157 with self.assertRaises(ValueError): 

158 lsst.base.Packages.fromBytes(from_bytes, "unknown_format") 

159 

160 with self.assertRaises(TypeError): 

161 some_yaml = b"list: [1, 2]" 

162 lsst.base.Packages.fromBytes(some_yaml, "yaml") 

163 

164 

165if __name__ == "__main__": 165 ↛ 166line 165 didn't jump to line 166, because the condition on line 165 was never true

166 unittest.main()