Coverage for tests/test_bpsconfig.py: 27%

100 statements  

« prev     ^ index     » next       coverage.py v6.4, created at 2022-05-26 09:58 +0000

1# This file is part of ctrl_bps. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (https://www.lsst.org). 

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

7# for details of code ownership. 

8# 

9# This program is free software: you can redistribute it and/or modify 

10# it under the terms of the GNU General Public License as published by 

11# the Free Software Foundation, either version 3 of the License, or 

12# (at your option) any later version. 

13# 

14# This program is distributed in the hope that it will be useful, 

15# but WITHOUT ANY WARRANTY; without even the implied warranty of 

16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

17# GNU General Public License for more details. 

18# 

19# You should have received a copy of the GNU General Public License 

20# along with this program. If not, see <https://www.gnu.org/licenses/>. 

21import os 

22import unittest 

23 

24import yaml 

25from lsst.ctrl.bps import BpsConfig 

26from lsst.daf.butler.core.config import Config 

27 

28TESTDIR = os.path.abspath(os.path.dirname(__file__)) 

29 

30 

31class TestBpsConfigConstructor(unittest.TestCase): 

32 def setUp(self): 

33 self.filename = os.path.join(TESTDIR, "data/config.yaml") 

34 with open(self.filename, "r") as f: 

35 self.dictionary = yaml.safe_load(f) 

36 

37 def tearDown(self): 

38 pass 

39 

40 def testFromFilename(self): 

41 """Test initialization from a file.""" 

42 config = BpsConfig(self.filename) 

43 self.assertIn("foo", config) 

44 

45 def testFromDict(self): 

46 """Test initialization from a dictionary.""" 

47 config = BpsConfig(self.dictionary) 

48 self.assertIn("bar", config) 

49 

50 def testFromConfig(self): 

51 """Test initialization from other Config object.""" 

52 c = Config(self.dictionary) 

53 config = BpsConfig(c) 

54 self.assertIn("baz", config) 

55 

56 def testFromBpsConfig(self): 

57 """Test initialization from other BpsConfig object.""" 

58 c = BpsConfig(self.dictionary) 

59 config = BpsConfig(c) 

60 self.assertIn("foo", config) 

61 

62 def testInvalidArg(self): 

63 """Test if exception is raised for an argument of unsupported type.""" 

64 sequence = ["wibble", "wobble", "wubble", "flob"] 

65 with self.assertRaises(RuntimeError): 

66 BpsConfig(sequence) 

67 

68 

69class TestBpsConfigSearch(unittest.TestCase): 

70 def setUp(self): 

71 filename = os.path.join(TESTDIR, "data/config.yaml") 

72 self.config = BpsConfig(filename, search_order=["baz", "bar", "foo"]) 

73 os.environ["GARPLY"] = "garply" 

74 

75 def tearDown(self): 

76 del os.environ["GARPLY"] 

77 

78 def testSectionSearchOrder(self): 

79 """Test if sections are searched in the prescribed order.""" 

80 key = "qux" 

81 found, value = self.config.search(key) 

82 self.assertEqual(found, True) 

83 self.assertEqual(value, 2) 

84 

85 def testCurrentValues(self): 

86 """Test if a current value overrides of the one in configuration.""" 

87 found, value = self.config.search("qux", opt={"curvals": {"qux": -3}}) 

88 self.assertEqual(found, True) 

89 self.assertEqual(value, -3) 

90 

91 def testSearchobjValues(self): 

92 """Test if a serachobj value overrides of the one in configuration.""" 

93 options = {"searchobj": {"qux": 4}} 

94 found, value = self.config.search("qux", opt=options) 

95 self.assertEqual(found, True) 

96 self.assertEqual(value, 4) 

97 

98 def testSubsectionSearch(self): 

99 options = {"curvals": {"curr_baz": "garply"}} 

100 found, value = self.config.search("qux", opt=options) 

101 self.assertEqual(found, True) 

102 self.assertEqual(value, 3) 

103 

104 def testDefault(self): 

105 """Test if a default value is properly set.""" 

106 found, value = self.config.search("plugh", opt={"default": 4}) 

107 self.assertEqual(found, True) 

108 self.assertEqual(value, 4) 

109 

110 def testVariables(self): 

111 """Test combinations of expandEnvVars, replaceEnvVars, 

112 and replaceVars.""" 

113 test_opt = {"expandEnvVars": False, "replaceEnvVars": False, "replaceVars": False} 

114 found, value = self.config.search("grault", opt=test_opt) 

115 self.assertEqual(found, True) 

116 self.assertEqual(value, "${GARPLY}/waldo/{qux:03}") 

117 

118 test_opt = {"expandEnvVars": False, "replaceEnvVars": False, "replaceVars": True} 

119 found, value = self.config.search("grault", opt=test_opt) 

120 self.assertEqual(found, True) 

121 self.assertEqual(value, "${GARPLY}/waldo/002") 

122 

123 test_opt = {"expandEnvVars": False, "replaceEnvVars": True, "replaceVars": False} 

124 found, value = self.config.search("grault", opt=test_opt) 

125 self.assertEqual(found, True) 

126 self.assertEqual(value, "<ENV:GARPLY>/waldo/{qux:03}") 

127 

128 test_opt = {"expandEnvVars": False, "replaceEnvVars": True, "replaceVars": True} 

129 found, value = self.config.search("grault", opt=test_opt) 

130 self.assertEqual(found, True) 

131 self.assertEqual(value, "<ENV:GARPLY>/waldo/002") 

132 

133 test_opt = {"expandEnvVars": True, "replaceEnvVars": False, "replaceVars": False} 

134 found, value = self.config.search("grault", opt=test_opt) 

135 self.assertEqual(found, True) 

136 self.assertEqual(value, "garply/waldo/{qux:03}") 

137 

138 test_opt = {"expandEnvVars": True, "replaceEnvVars": False, "replaceVars": True} 

139 found, value = self.config.search("grault", opt=test_opt) 

140 self.assertEqual(found, True) 

141 self.assertEqual(value, "garply/waldo/002") 

142 

143 test_opt = {"expandEnvVars": True, "replaceEnvVars": True, "replaceVars": False} 

144 found, value = self.config.search("grault", opt=test_opt) 

145 self.assertEqual(found, True) 

146 self.assertEqual(value, "garply/waldo/{qux:03}") 

147 

148 test_opt = {"expandEnvVars": True, "replaceEnvVars": True, "replaceVars": True} 

149 found, value = self.config.search("grault", opt=test_opt) 

150 self.assertEqual(found, True) 

151 self.assertEqual(found, True) 

152 self.assertEqual(value, "garply/waldo/002") 

153 

154 def testRequired(self): 

155 """Test if exception is raised if a required setting is missing.""" 

156 with self.assertRaises(KeyError): 

157 self.config.search("fred", opt={"required": True}) 

158 

159 

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

161 unittest.main()