Coverage for tests/test_extensions.py: 39%

52 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 pickle 

13import unittest 

14 

15from astro_metadata_translator import ObservationInfo, PropertyDefinition, StubTranslator, makeObservationInfo 

16 

17"""Test that extensions to the core set of properties works""" 

18 

19FOO = "bar" 

20NUMBER = 12345 

21 

22 

23class DummyTranslator(StubTranslator): 

24 """Test translation class that supports extensions.""" 

25 

26 name = "dummy" 

27 supported_instrument = "dummy" 

28 extensions = dict( 

29 number=PropertyDefinition("A number", "int", int), 

30 foo=PropertyDefinition("A string", "str", str), 

31 ) 

32 _const_map = { 

33 "ext_foo": FOO, 

34 } 

35 

36 @classmethod 

37 def can_translate(cls, header, filename=None): 

38 return "INSTRUME" in header and header["INSTRUME"] == "dummy" 

39 

40 def to_ext_number(self): 

41 """Return the combination on my luggage.""" 

42 return NUMBER 

43 

44 

45class ExtensionsTestCase(unittest.TestCase): 

46 """Test support for extended properties.""" 

47 

48 def setUp(self): 

49 self.header = dict(INSTRUME="dummy") 

50 # The test translator is incomplete so it will issue warnings 

51 # about missing translations. Catch them. 

52 with self.assertWarns(UserWarning): 

53 self.obsinfo = ObservationInfo(self.header) 

54 

55 def assert_observation_info(self, obsinfo): 

56 """Check that the `ObservationInfo` is as expected. 

57 

58 Parameters 

59 ---------- 

60 obsinfo : `ObservationInfo` 

61 Information to check. 

62 """ 

63 self.assertIsInstance(obsinfo, ObservationInfo) 

64 self.assertEqual(obsinfo.ext_foo, FOO) 

65 self.assertEqual(obsinfo.ext_number, NUMBER) 

66 

67 def test_basic(self): 

68 """Test construction of extended ObservationInfo.""" 

69 # Behaves like the original 

70 self.assert_observation_info(self.obsinfo) 

71 

72 copy = makeObservationInfo(extensions=DummyTranslator.extensions, ext_foo=FOO, ext_number=NUMBER) 

73 self.assertEqual(copy, self.obsinfo) 

74 

75 with self.assertRaises(AttributeError): 

76 # Variable is read-only 

77 self.obsinfo.ext_foo = "something completely different" 

78 

79 with self.assertRaises(KeyError): 

80 # Can't specify extension value without declaring extensions 

81 makeObservationInfo(foo="foobar") 

82 

83 with self.assertRaises(TypeError): 

84 # Type checking is applied, like in the original 

85 makeObservationInfo(extensions=DummyTranslator.extensions, ext_foo=98765) 

86 

87 def test_pickle(self): 

88 """Test that pickling works on ObservationInfo with extensions.""" 

89 obsinfo = pickle.loads(pickle.dumps(self.obsinfo)) 

90 self.assert_observation_info(obsinfo) 

91 

92 def test_simple(self): 

93 """Test that simple representation works.""" 

94 simple = self.obsinfo.to_simple() 

95 self.assertIn("ext_foo", simple) 

96 self.assertIn("ext_number", simple) 

97 obsinfo = ObservationInfo.from_simple(simple) 

98 self.assert_observation_info(obsinfo) 

99 

100 def test_json(self): 

101 """Test that JSON representation works.""" 

102 json = self.obsinfo.to_json() 

103 self.assertIn("ext_foo", json) 

104 self.assertIn("ext_number", json) 

105 obsinfo = ObservationInfo.from_json(json) 

106 self.assert_observation_info(obsinfo) 

107 

108 

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

110 unittest.main()