Coverage for tests/test_shadowing.py: 35%

40 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2022-11-09 05:57 +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 def to_instrument(self): 

19 return "BaseInstrument" 

20 

21 

22class ConstTranslator(StubTranslator): 

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

24 

25 

26class TrivialTranslator(ConstTranslator): 

27 # This should not pick up the _const_map from parent class 

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

29 

30 

31class ExplicitTranslator(TrivialTranslator): 

32 # The explicit method should override the parent implementations 

33 # and not inherit the _trivial_map from parent. 

34 def to_instrument(self): 

35 return "InstrumentE" 

36 

37 

38class TranslatorShadowing(unittest.TestCase): 

39 def test_shadowing(self): 

40 

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

42 

43 class ShadowTranslator(StubTranslator): 

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

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

46 

47 def to_instrument(self): 

48 return "Instrument3" 

49 

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

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

52 

53 s = ShadowTranslator({}) 

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

55 

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

57 

58 class ShadowTranslator(StubTranslator): 

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

60 

61 def to_instrument(self): 

62 return "Instrument3" 

63 

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

65 

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

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

68 

69 def test_auto_maps1(self): 

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

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

72 

73 def test_auto_maps2(self): 

74 t = ExplicitTranslator({}) 

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

76 

77 

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

79 unittest.main()