Coverage for tests/test_translate_header.py: 14%

83 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-03-20 03:54 -0700

1# This file is part of astro_metadata_translator. 

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 LICENSE file at the top-level directory of this distribution 

7# for details of code ownership. 

8# 

9# Use of this source code is governed by a 3-clause BSD-style 

10# license that can be found in the LICENSE file. 

11 

12import io 

13import os.path 

14import unittest 

15 

16from astro_metadata_translator.bin.translate import translate_or_dump_headers 

17 

18TESTDIR = os.path.abspath(os.path.dirname(__file__)) 

19TESTDATA = os.path.join(TESTDIR, "data") 

20 

21 

22class TestTranslateHeader(unittest.TestCase): 

23 """Test that the astrometadata translate and dump logic works.""" 

24 

25 def _readlines(self, stream): 

26 """Return the lines written to the stream. 

27 

28 Parameters 

29 ---------- 

30 stream : `io.StringIO` 

31 The stream to read. 

32 

33 Returns 

34 ------- 

35 lines : `list` of `str` 

36 The lines contained in the stream. 

37 """ 

38 stream.seek(0) 

39 return [ll.rstrip() for ll in stream.readlines()] 

40 

41 def test_translate_header(self): 

42 """Translate some header files.""" 

43 with io.StringIO() as out: 

44 with io.StringIO() as err: 

45 okay, failed = translate_or_dump_headers( 

46 [TESTDATA], 

47 r"^fitsheader.*yaml$", 

48 0, 

49 False, 

50 outstream=out, 

51 errstream=err, 

52 output_mode="none", 

53 ) 

54 self.assertEqual(self._readlines(out), []) 

55 lines = self._readlines(err) 

56 self.assertEqual(len(lines), 10) 

57 self.assertTrue(lines[0].startswith("Analyzing"), f"Line: '{lines[0]}'") 

58 

59 self.assertEqual(len(okay), 10) 

60 self.assertEqual(len(failed), 0) 

61 

62 def test_translate_header_table(self): 

63 """Translate some header files with table output.""" 

64 with io.StringIO() as out: 

65 with io.StringIO() as err: 

66 okay, failed = translate_or_dump_headers( 

67 [TESTDATA], r"^fitsheader.*yaml$", 0, False, outstream=out, errstream=err 

68 ) 

69 output = self._readlines(out) 

70 self.assertTrue(output[0].startswith("ObsId")) 

71 self.assertTrue(output[1].startswith("-------")) 

72 self.assertEqual(len(output), 12) 

73 errlines = self._readlines(err) 

74 self.assertEqual(len(errlines), 0) 

75 

76 self.assertEqual(len(okay), 10) 

77 self.assertEqual(len(failed), 0) 

78 

79 def test_translate_header_fails(self): 

80 """Translate some header files that fail.""" 

81 with io.StringIO() as out: 

82 with io.StringIO() as err: 

83 okay, failed = translate_or_dump_headers( 

84 [TESTDATA], r"^.*yaml$", 0, False, outstream=out, errstream=err, output_mode="none" 

85 ) 

86 

87 out_lines = self._readlines(out) 

88 self.assertEqual(len(out_lines), len(failed)) 

89 self.assertTrue(out_lines[0].startswith("Failure processing"), f"Line: '{out_lines[0]}'") 

90 self.assertIn("not a mapping", out_lines[0], f"Line: '{out_lines[0]}'") 

91 

92 err_lines = self._readlines(err) 

93 self.assertEqual(len(err_lines), 13) # The number of files analyzed 

94 self.assertTrue(err_lines[0].startswith("Analyzing"), f"Line: '{err_lines[0]}'") 

95 

96 # Form message to issue if the test fails. 

97 newline = "\n" # f-string can not accept \ in string. 

98 msg = f"""Converted successfully: 

99{newline.join(okay)} 

100Failed conversions: 

101{newline.join(failed)} 

102Standard output: 

103{newline.join(out_lines)} 

104""" 

105 self.assertEqual((len(okay), len(failed)), (10, 3), msg=msg) 

106 

107 def test_translate_header_traceback(self): 

108 """Translate some header files that fail and trigger traceback.""" 

109 with io.StringIO() as out: 

110 with io.StringIO() as err: 

111 okay, failed = translate_or_dump_headers( 

112 [TESTDATA], r"^.*yaml$", 0, True, outstream=out, errstream=err, output_mode="none" 

113 ) 

114 

115 lines = self._readlines(out) 

116 self.assertGreaterEqual(len(lines), 22, "\n".join(lines)) 

117 self.assertTrue(lines[0].startswith("Traceback"), f"Line '{lines[0]}'") 

118 

119 lines = self._readlines(err) 

120 self.assertGreaterEqual(len(lines), 13, "\n".join(lines)) 

121 self.assertTrue(lines[0].startswith("Analyzing"), f"Line: '{lines[0]}'") 

122 

123 self.assertEqual(len(okay), 10) 

124 self.assertEqual(len(failed), 3) 

125 

126 def test_translate_header_dump(self): 

127 """Check that a header is dumped.""" 

128 with io.StringIO() as out: 

129 with io.StringIO() as err: 

130 okay, failed = translate_or_dump_headers( 

131 [os.path.join(TESTDATA, "fitsheader-decam.yaml")], 

132 r"^fitsheader.*yaml$", 

133 0, 

134 False, 

135 outstream=out, 

136 errstream=err, 

137 output_mode="yaml", 

138 ) 

139 

140 lines = self._readlines(out) 

141 # Look for a DECam header in the output 

142 header = "\n".join(lines) 

143 self.assertIn("DTINSTRU", header) 

144 

145 lines = self._readlines(err) 

146 self.assertEqual(len(lines), 1) 

147 self.assertTrue(lines[0], "Analyzing tests/data/fitsheader-decam.yaml...") 

148 

149 self.assertEqual(len(okay), 1) 

150 self.assertEqual(len(failed), 0) 

151 

152 def test_translate_header_loud(self): 

153 """Check that ObservationInfo content is displayed.""" 

154 with io.StringIO() as out: 

155 with io.StringIO() as err: 

156 okay, failed = translate_or_dump_headers( 

157 [os.path.join(TESTDATA, "fitsheader-decam.yaml")], 

158 r"^fitsheader.*yaml$", 

159 0, 

160 False, 

161 outstream=out, 

162 errstream=err, 

163 output_mode="verbose", 

164 ) 

165 

166 lines = self._readlines(out) 

167 # Look for the translated DECam header in the output 

168 self.assertEqual(lines[2], "datetime_begin: 2013-09-01T06:02:55.754") 

169 

170 lines = self._readlines(err) 

171 self.assertEqual(len(lines), 1) 

172 self.assertTrue(lines[0], "Analyzing tests/data/fitsheader-decam.yaml...") 

173 

174 self.assertEqual(len(okay), 1) 

175 self.assertEqual(len(failed), 0) 

176 

177 

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

179 unittest.main()