Coverage for tests/test_extensions.py: 37%

52 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-04-13 02:37 -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 name = "dummy" 

25 supported_instrument = "dummy" 

26 extensions = dict( 

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

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

29 ) 

30 _const_map = { 

31 "ext_foo": FOO, 

32 } 

33 

34 @classmethod 

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

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

37 

38 def to_ext_number(self): 

39 """Return the combination on my luggage""" 

40 return NUMBER 

41 

42 

43class ExtensionsTestCase(unittest.TestCase): 

44 def setUp(self): 

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

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

47 # about missing translations. Catch them. 

48 with self.assertWarns(UserWarning): 

49 self.obsinfo = ObservationInfo(self.header) 

50 

51 def assert_observation_info(self, obsinfo): 

52 """Check that the `ObservationInfo` is as expected""" 

53 self.assertIsInstance(obsinfo, ObservationInfo) 

54 self.assertEqual(obsinfo.ext_foo, FOO) 

55 self.assertEqual(obsinfo.ext_number, NUMBER) 

56 

57 def test_basic(self): 

58 """Test construction of extended ObservationInfo""" 

59 # Behaves like the original 

60 self.assert_observation_info(self.obsinfo) 

61 

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

63 self.assertEqual(copy, self.obsinfo) 

64 

65 with self.assertRaises(AttributeError): 

66 # Variable is read-only 

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

68 

69 with self.assertRaises(KeyError): 

70 # Can't specify extension value without declaring extensions 

71 makeObservationInfo(foo="foobar") 

72 

73 with self.assertRaises(TypeError): 

74 # Type checking is applied, like in the original 

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

76 

77 def test_pickle(self): 

78 """Test that pickling works on ObservationInfo with extensions""" 

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

80 self.assert_observation_info(obsinfo) 

81 

82 def test_simple(self): 

83 """Test that simple representation works""" 

84 simple = self.obsinfo.to_simple() 

85 self.assertIn("ext_foo", simple) 

86 self.assertIn("ext_number", simple) 

87 obsinfo = ObservationInfo.from_simple(simple) 

88 self.assert_observation_info(obsinfo) 

89 

90 def test_json(self): 

91 """Test that JSON representation works""" 

92 json = self.obsinfo.to_json() 

93 self.assertIn("ext_foo", json) 

94 self.assertIn("ext_number", json) 

95 obsinfo = ObservationInfo.from_json(json) 

96 self.assert_observation_info(obsinfo) 

97 

98 

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

100 unittest.main()