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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

# This file is part of astro_metadata_translator. 

# 

# Developed for the LSST Data Management System. 

# This product includes software developed by the LSST Project 

# (http://www.lsst.org). 

# See the LICENSE file at the top-level directory of this distribution 

# for details of code ownership. 

# 

# Use of this source code is governed by a 3-clause BSD-style 

# license that can be found in the LICENSE file. 

 

import unittest 

 

from astro_metadata_translator import StubTranslator 

 

 

class ShadowBase(StubTranslator): 

def to_instrument(self): 

return "BaseInstrument" 

 

 

class ConstTranslator(StubTranslator): 

_const_map = {"instrument": "InstrumentB"} 

 

 

class TrivialTranslator(ConstTranslator): 

# This should not pick up the _const_map from parent class 

_trivial_map = {"instrument": "INSTRUME"} 

 

 

class ExplicitTranslator(TrivialTranslator): 

# The explicit method should override the parent implementations 

# and not inherit the _trivial_map from parent. 

def to_instrument(self): 

return "InstrumentE" 

 

 

class TranslatorShadowing(unittest.TestCase): 

 

def test_shadowing(self): 

 

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

class ShadowTranslator(StubTranslator): 

_const_map = {"instrument": "InstrumentC"} 

_trivial_map = {"instrument": "INSTRUME"} 

 

def to_instrument(self): 

return "Instrument3" 

 

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

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

 

s = ShadowTranslator({}) 

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

 

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

class ShadowTranslator(StubTranslator): 

_trivial_map = {"instrument": "INSTRUME"} 

 

def to_instrument(self): 

return "Instrument3" 

 

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

 

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

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

 

def test_auto_maps1(self): 

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

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

 

def test_auto_maps2(self): 

t = ExplicitTranslator({}) 

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

 

 

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

unittest.main()