Coverage for tests/test_extensions.py: 37%

52 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-07-12 11:01 -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 self.assertIsInstance(obsinfo, ObservationInfo) 

58 self.assertEqual(obsinfo.ext_foo, FOO) 

59 self.assertEqual(obsinfo.ext_number, NUMBER) 

60 

61 def test_basic(self): 

62 """Test construction of extended ObservationInfo.""" 

63 # Behaves like the original 

64 self.assert_observation_info(self.obsinfo) 

65 

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

67 self.assertEqual(copy, self.obsinfo) 

68 

69 with self.assertRaises(AttributeError): 

70 # Variable is read-only 

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

72 

73 with self.assertRaises(KeyError): 

74 # Can't specify extension value without declaring extensions 

75 makeObservationInfo(foo="foobar") 

76 

77 with self.assertRaises(TypeError): 

78 # Type checking is applied, like in the original 

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

80 

81 def test_pickle(self): 

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

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

84 self.assert_observation_info(obsinfo) 

85 

86 def test_simple(self): 

87 """Test that simple representation works.""" 

88 simple = self.obsinfo.to_simple() 

89 self.assertIn("ext_foo", simple) 

90 self.assertIn("ext_number", simple) 

91 obsinfo = ObservationInfo.from_simple(simple) 

92 self.assert_observation_info(obsinfo) 

93 

94 def test_json(self): 

95 """Test that JSON representation works.""" 

96 json = self.obsinfo.to_json() 

97 self.assertIn("ext_foo", json) 

98 self.assertIn("ext_number", json) 

99 obsinfo = ObservationInfo.from_json(json) 

100 self.assert_observation_info(obsinfo) 

101 

102 

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

104 unittest.main()