Coverage for tests/test_formatters.py: 96%

63 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-10-09 11:19 +0000

1#!/usr/bin/env python 

2# This file is part of atmospec. 

3# 

4# Developed for the LSST Data Management System. 

5# This product includes software developed by the LSST Project 

6# (https://www.lsst.org). 

7# See the COPYRIGHT file at the top-level directory of this distribution 

8# for details of code ownership. 

9# 

10# This program is free software: you can redistribute it and/or modify 

11# it under the terms of the GNU General Public License as published by 

12# the Free Software Foundation, either version 3 of the License, or 

13# (at your option) any later version. 

14# 

15# This program is distributed in the hope that it will be useful, 

16# but WITHOUT ANY WARRANTY; without even the implied warranty of 

17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

18# GNU General Public License for more details. 

19# 

20# You should have received a copy of the GNU General Public License 

21# along with this program. If not, see <https://www.gnu.org/licenses/>. 

22"""Test cases for atmospec Spectractor formatters.""" 

23 

24import unittest 

25from unittest import mock 

26 

27import lsst.utils 

28import lsst.utils.tests 

29from lsst.atmospec.formatters import ( 

30 SpectractorSpectrumFormatter, 

31 SpectractorImageFormatter, 

32 SpectractorFitParametersFormatter, 

33) 

34 

35 

36class FormattersTestCase(lsst.utils.tests.TestCase): 

37 """A test case for the Spectractor formatters. 

38 

39 The formatter methods do not depend on any instance state, so instances 

40 are created with ``object.__new__`` to avoid the ``FormatterV2`` 

41 constructor, which requires a full ``FileDescriptor``/``DatasetRef``. 

42 """ 

43 

44 def testImport(self): 

45 import lsst.atmospec.formatters as formatters # noqa: F401 

46 

47 def test_classAttributes(self): 

48 for cls, ext in ( 

49 (SpectractorSpectrumFormatter, '.fits'), 

50 (SpectractorImageFormatter, '.fits'), 

51 (SpectractorFitParametersFormatter, '.json'), 

52 ): 

53 self.assertEqual(cls.default_extension, ext) 

54 self.assertIsNone(cls.unsupported_parameters) 

55 self.assertTrue(cls.can_read_from_local_file) 

56 

57 def test_spectrum_read_from_local_file(self): 

58 formatter = object.__new__(SpectractorSpectrumFormatter) 

59 sentinel = mock.MagicMock(name="Spectrum instance") 

60 with mock.patch("lsst.atmospec.formatters.Spectrum", 

61 return_value=sentinel) as mockSpectrum: 

62 result = formatter.read_from_local_file("/some/path.fits") 

63 mockSpectrum.assert_called_once_with("/some/path.fits") 

64 self.assertIs(result, sentinel) 

65 

66 def test_spectrum_write_local_file(self): 

67 formatter = object.__new__(SpectractorSpectrumFormatter) 

68 dataset = mock.MagicMock(name="Spectrum dataset") 

69 uri = mock.MagicMock() 

70 uri.ospath = "/out/path.fits" 

71 formatter.write_local_file(dataset, uri) 

72 dataset.save_spectrum.assert_called_once_with("/out/path.fits") 

73 

74 def test_image_read_from_local_file(self): 

75 formatter = object.__new__(SpectractorImageFormatter) 

76 sentinel = mock.MagicMock(name="Image instance") 

77 with mock.patch("lsst.atmospec.formatters.Image", 

78 return_value=sentinel) as mockImage: 

79 result = formatter.read_from_local_file("/some/path.fits") 

80 mockImage.assert_called_once_with("/some/path.fits") 

81 self.assertIs(result, sentinel) 

82 

83 def test_image_write_local_file(self): 

84 formatter = object.__new__(SpectractorImageFormatter) 

85 dataset = mock.MagicMock(name="Image dataset") 

86 uri = mock.MagicMock() 

87 uri.ospath = "/out/image.fits" 

88 formatter.write_local_file(dataset, uri) 

89 dataset.save_image.assert_called_once_with("/out/image.fits") 

90 

91 def test_fitparameters_read_from_local_file(self): 

92 formatter = object.__new__(SpectractorFitParametersFormatter) 

93 sentinel = mock.MagicMock(name="fit parameters") 

94 with mock.patch("lsst.atmospec.formatters.read_fitparameter_json", 

95 return_value=sentinel) as mockRead: 

96 result = formatter.read_from_local_file("/some/params.json") 

97 mockRead.assert_called_once_with("/some/params.json") 

98 self.assertIs(result, sentinel) 

99 

100 def test_fitparameters_write_local_file(self): 

101 formatter = object.__new__(SpectractorFitParametersFormatter) 

102 dataset = mock.MagicMock(name="fit parameters dataset") 

103 uri = mock.MagicMock() 

104 uri.ospath = "/out/params.json" 

105 with mock.patch("lsst.atmospec.formatters.write_fitparameter_json") as mockWrite: 

106 formatter.write_local_file(dataset, uri) 

107 mockWrite.assert_called_once_with("/out/params.json", dataset) 

108 

109 

110class TestMemory(lsst.utils.tests.MemoryTestCase): 

111 pass 

112 

113 

114def setup_module(module): 

115 lsst.utils.tests.init() 

116 

117 

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

119 lsst.utils.tests.init() 

120 unittest.main()