Coverage for tests/test_shadowing.py: 35%
40 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-08-05 01:30 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-08-05 01:30 +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.
12import unittest
14from astro_metadata_translator import StubTranslator
17class ShadowBase(StubTranslator):
18 """Base class for testing shadowing."""
20 def to_instrument(self):
21 return "BaseInstrument"
24class ConstTranslator(StubTranslator):
25 """Simple translation class with a constant mapping."""
27 _const_map = {"instrument": "InstrumentB"}
30class TrivialTranslator(ConstTranslator):
31 """Translator inheriting from the constant mapping class but with an
32 override.
34 This class should not pick up the _const_map from parent class.
35 """
37 _trivial_map = {"instrument": "INSTRUME"}
40class ExplicitTranslator(TrivialTranslator):
41 """Translation class with explicit method inheriting from class
42 with automatic translation methods.
44 The explicit method should override the parent implementations
45 and not inherit the _trivial_map from parent.
46 """
48 def to_instrument(self):
49 return "InstrumentE"
52class TranslatorShadowing(unittest.TestCase):
53 """Test that shadowed translations are detected."""
55 def test_shadowing(self):
56 with self.assertLogs("astro_metadata_translator", level="WARN") as cm:
58 class ShadowTranslator(StubTranslator):
59 _const_map = {"instrument": "InstrumentC"}
60 _trivial_map = {"instrument": "INSTRUME"}
62 def to_instrument(self):
63 return "Instrument3"
65 self.assertIn("defined in both", cm.output[0])
66 self.assertIn("replaced by _const_map", cm.output[1])
68 s = ShadowTranslator({})
69 self.assertEqual(s.to_instrument(), "InstrumentC")
71 with self.assertLogs("astro_metadata_translator", level="WARN") as cm:
73 class ShadowTranslator(StubTranslator):
74 _trivial_map = {"instrument": "INSTRUME"}
76 def to_instrument(self):
77 return "Instrument3"
79 self.assertIn("replaced by _trivial_map", cm.output[0])
81 s = ShadowTranslator({"INSTRUME": "InstrumentT"})
82 self.assertEqual(s.to_instrument(), "InstrumentT")
84 def test_auto_maps1(self):
85 t = TrivialTranslator({"INSTRUME": "InstrumentX"})
86 self.assertEqual(t.to_instrument(), "InstrumentX")
88 def test_auto_maps2(self):
89 t = ExplicitTranslator({})
90 self.assertEqual(t.to_instrument(), "InstrumentE")
93if __name__ == "__main__": 93 ↛ 94line 93 didn't jump to line 94, because the condition on line 93 was never true
94 unittest.main()