Coverage for tests/test_shadowing.py: 35%

40 statements  

« prev     ^ index     » next       coverage.py v7.4.0, created at 2024-01-06 11:47 +0000

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 unittest 

13 

14from astro_metadata_translator import StubTranslator 

15 

16 

17class ShadowBase(StubTranslator): 

18 """Base class for testing shadowing.""" 

19 

20 def to_instrument(self): 

21 return "BaseInstrument" 

22 

23 

24class ConstTranslator(StubTranslator): 

25 """Simple translation class with a constant mapping.""" 

26 

27 _const_map = {"instrument": "InstrumentB"} 

28 

29 

30class TrivialTranslator(ConstTranslator): 

31 """Translator inheriting from the constant mapping class but with an 

32 override. 

33 

34 This class should not pick up the _const_map from parent class. 

35 """ 

36 

37 _trivial_map = {"instrument": "INSTRUME"} 

38 

39 

40class ExplicitTranslator(TrivialTranslator): 

41 """Translation class with explicit method inheriting from class 

42 with automatic translation methods. 

43 

44 The explicit method should override the parent implementations 

45 and not inherit the _trivial_map from parent. 

46 """ 

47 

48 def to_instrument(self): 

49 return "InstrumentE" 

50 

51 

52class TranslatorShadowing(unittest.TestCase): 

53 """Test that shadowed translations are detected.""" 

54 

55 def test_shadowing(self): 

56 with self.assertLogs("astro_metadata_translator", level="WARN") as cm: 

57 

58 class ShadowTranslator(StubTranslator): 

59 _const_map = {"instrument": "InstrumentC"} 

60 _trivial_map = {"instrument": "INSTRUME"} 

61 

62 def to_instrument(self): 

63 return "Instrument3" 

64 

65 self.assertIn("defined in both", cm.output[0]) 

66 self.assertIn("replaced by _const_map", cm.output[1]) 

67 

68 s = ShadowTranslator({}) 

69 self.assertEqual(s.to_instrument(), "InstrumentC") 

70 

71 with self.assertLogs("astro_metadata_translator", level="WARN") as cm: 

72 

73 class ShadowTranslator(StubTranslator): 

74 _trivial_map = {"instrument": "INSTRUME"} 

75 

76 def to_instrument(self): 

77 return "Instrument3" 

78 

79 self.assertIn("replaced by _trivial_map", cm.output[0]) 

80 

81 s = ShadowTranslator({"INSTRUME": "InstrumentT"}) 

82 self.assertEqual(s.to_instrument(), "InstrumentT") 

83 

84 def test_auto_maps1(self): 

85 t = TrivialTranslator({"INSTRUME": "InstrumentX"}) 

86 self.assertEqual(t.to_instrument(), "InstrumentX") 

87 

88 def test_auto_maps2(self): 

89 t = ExplicitTranslator({}) 

90 self.assertEqual(t.to_instrument(), "InstrumentE") 

91 

92 

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

94 unittest.main()