Hide keyboard shortcuts

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# 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 os.path 

13import unittest 

14import io 

15 

16from astro_metadata_translator.bin.translateheader import process_files 

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 translate_header CLI 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 = process_files([TESTDATA], r"^fitsheader.*yaml$", 0, False, 

46 outstream=out, errstream=err, output_mode="none") 

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

48 lines = self._readlines(err) 

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

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

51 

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

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

54 

55 def test_translate_header_table(self): 

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

57 with io.StringIO() as out: 

58 with io.StringIO() as err: 

59 okay, failed = process_files([TESTDATA], r"^fitsheader.*yaml$", 0, False, 

60 outstream=out, errstream=err) 

61 output = self._readlines(out) 

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

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

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

65 errlines = self._readlines(err) 

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

67 

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

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

70 

71 def test_translate_header_fails(self): 

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

73 with io.StringIO() as out: 

74 with io.StringIO() as err: 

75 okay, failed = process_files([TESTDATA], r"^.*yaml$", 0, False, 

76 outstream=out, errstream=err, output_mode="none") 

77 

78 lines = self._readlines(out) 

79 self.assertEqual(len(lines), len(failed)) 

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

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

82 

83 lines = self._readlines(err) 

84 self.assertEqual(len(lines), 13) 

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

86 

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

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

89 

90 def test_translate_header_traceback(self): 

91 """Translate some header files that fail and trigger traceback""" 

92 with io.StringIO() as out: 

93 with io.StringIO() as err: 

94 okay, failed = process_files([TESTDATA], r"^.*yaml$", 0, True, 

95 outstream=out, errstream=err, output_mode="none") 

96 

97 lines = self._readlines(out) 

98 self.assertEqual(len(lines), 26) 

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

100 

101 lines = self._readlines(err) 

102 self.assertEqual(len(lines), 13) 

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

104 

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

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

107 

108 def test_translate_header_dump(self): 

109 """Check that a header is dumped""" 

110 with io.StringIO() as out: 

111 with io.StringIO() as err: 

112 okay, failed = process_files([os.path.join(TESTDATA, "fitsheader-decam.yaml")], 

113 r"^fitsheader.*yaml$", 0, False, 

114 outstream=out, errstream=err, output_mode="yaml") 

115 

116 lines = self._readlines(out) 

117 # Look for a DECam header in the output 

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

119 self.assertIn("DTINSTRU", header) 

120 

121 lines = self._readlines(err) 

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

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

124 

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

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

127 

128 def test_translate_header_loud(self): 

129 """Check that ObservationInfo content is displayed""" 

130 with io.StringIO() as out: 

131 with io.StringIO() as err: 

132 okay, failed = process_files([os.path.join(TESTDATA, "fitsheader-decam.yaml")], 

133 r"^fitsheader.*yaml$", 0, False, 

134 outstream=out, errstream=err, output_mode="verbose") 

135 

136 lines = self._readlines(out) 

137 # Look for the translated DECam header in the output 

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

139 

140 lines = self._readlines(err) 

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

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

143 

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

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

146 

147 

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

149 unittest.main()