Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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 

40 def test_shadowing(self): 

41 

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

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 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()