Coverage for tests/test_shadowing.py: 35%

40 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-01 02:38 -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 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 with self.assertLogs("astro_metadata_translator", level="WARN") as cm: 

41 

42 class ShadowTranslator(StubTranslator): 

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

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

45 

46 def to_instrument(self): 

47 return "Instrument3" 

48 

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

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

51 

52 s = ShadowTranslator({}) 

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

54 

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

56 

57 class ShadowTranslator(StubTranslator): 

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

59 

60 def to_instrument(self): 

61 return "Instrument3" 

62 

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

64 

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

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

67 

68 def test_auto_maps1(self): 

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

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

71 

72 def test_auto_maps2(self): 

73 t = ExplicitTranslator({}) 

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

75 

76 

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

78 unittest.main()