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#!/usr/bin/env python 

2 

3# 

4# LSST Data Management System 

5# Copyright 2008, 2009, 2010 LSST Corporation. 

6# 

7# This product includes software developed by the 

8# LSST Project (http://www.lsst.org/). 

9# 

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

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

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

13# (at your option) any later version. 

14# 

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

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

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

18# GNU General Public License for more details. 

19# 

20# You should have received a copy of the LSST License Statement and 

21# the GNU General Public License along with this program. If not, 

22# see <http://www.lsstcorp.org/LegalNotices/>. 

23# 

24 

25""" 

26Comprehensive tests reading and retrieving data of all types 

27""" 

28 

29import os 

30import unittest 

31 

32import lsst.utils 

33import lsst.utils.tests 

34from lsst.pex.policy import Policy 

35 

36proddir = lsst.utils.getPackageDir('pex_policy') 

37 

38 

39class GetTestCase(unittest.TestCase): 

40 

41 def setUp(self): 

42 self.policyfile = os.path.join(proddir, "examples", "types.paf") 

43 self.policy = Policy.createPolicy(self.policyfile, False) 

44 

45 def tearDown(self): 

46 del self.policy 

47 

48 def testGet(self): 

49 p = self.policy 

50 self.assertEqual(p.get("int"), 0) 

51 self.assertIs(p.get("true"), True) 

52 self.assertIs(p.get("false"), False) 

53 self.assertAlmostEqual(p.get("dbl"), -0.05, 8) 

54 self.assertEqual(p.get("str"), "birthday") 

55 self.assertTrue(p.isFile("file"), 

56 "Unexpected: 'file' is not a PolicyFile") 

57 self.assertIsNotNone(p.get("file"), "file value returned as None") 

58 self.assertEqual(p.get("file").getPath(), "CacheManager_dict.paf") 

59 self.assertTrue(p.isPolicy("pol"), "Unexpected: 'pol' is not a Policy") 

60 sp = p.get("pol") 

61 self.assertEqual(sp.get("int"), 2) 

62 

63 def testGetIntArray(self): 

64 self.assertTrue(self.policy.isInt("int")) 

65 v = self.policy.getArray("int") 

66 self.assertIsInstance(v, list, "array value not returned as a list") 

67 

68 truth = [-11, 0, 3, 42, -11, 0, 3, 42, 0, 0] 

69 self.assertEqual(len(v), len(truth), "wrong number of values in array") 

70 for i in range(len(truth)): 

71 self.assertEqual(v[i], truth[i], 

72 "wrong array element at index %d: %d != %d" % 

73 (i, v[i], truth[i])) 

74 

75 def testGetBoolArray(self): 

76 self.assertTrue(self.policy.isBool("true")) 

77 v = self.policy.getArray("true") 

78 self.assertIsInstance(v, list, "array value not returned as a list") 

79 

80 truth = [True] 

81 self.assertEqual(len(v), len(truth), "wrong number of values in array") 

82 for i in range(len(truth)): 

83 self.assertEqual(v[i], truth[i], 

84 "wrong array element at index %i: %s != %s" % 

85 (i, v[i], truth[i])) 

86 

87 self.assertTrue(self.policy.isBool("false")) 

88 v = self.policy.getArray("false") 

89 self.assertIsInstance(v, list, "array value not returned as a list") 

90 

91 truth = [False] 

92 self.assertEqual(len(v), len(truth), "wrong number of values in array") 

93 for i in range(len(truth)): 

94 self.assertEqual(v[i], truth[i], 

95 "wrong array element at index %i: %s != %s" % 

96 (i, v[i], truth[i])) 

97 

98 def testGetDoublArray(self): 

99 self.assertTrue(self.policy.isDouble("dbl")) 

100 v = self.policy.getArray("dbl") 

101 self.assertIsInstance(v, list, "array value not returned as a list") 

102 

103 truth = [-1.0, -65.78, -14.0, -0.12, -0.12, 1.0, 65.78, 14.0, 0.12, 

104 0.12, 1.0, 65.78, 14.0, 0.12, 0.12, -1.0e10, -65780000.0, 

105 -0.014, -0.12e14, 0.12e-11, 14.0, 0.12, 0.12, 1.0, 65.78, 

106 14.0, 50000, -0.05] 

107 self.assertEqual(len(v), len(truth), 

108 "wrong number of values in array: %i != %i" % 

109 (len(v), len(truth))) 

110 for i in range(len(truth)): 

111 self.assertAlmostEqual(v[i], truth[i], 8, 

112 "wrong array element at index %d: %g != %g" % 

113 (i, v[i], truth[i])) 

114 

115 def testGetStringArray(self): 

116 self.assertTrue(self.policy.isString("str")) 

117 v = self.policy.getArray("str") 

118 self.assertIsInstance(v, list, "array value not returned as a list") 

119 

120 truth = ["word", "two words", "quoted ' words", 'quoted " words', 

121 "a very long, multi-line description", "happy", "birthday"] 

122 self.assertEqual(len(v), len(truth), "wrong number of values in array") 

123 for i in range(len(truth)): 

124 self.assertEqual(v[i], truth[i], 

125 "wrong array element at index %d: %s != %s" % 

126 (i, v[i], truth[i])) 

127 

128 def testGetEmptyString(self): 

129 p = self.policy 

130 self.assertEqual(p.get("empty"), '') 

131 s = p.getArray("empty") 

132 self.assertEqual(len(s), 5) 

133 self.assertEqual(s[0], ' description ') 

134 self.assertEqual(s[1], ' ') 

135 self.assertEqual(s[2], ' ') 

136 self.assertEqual(s[3], ' ') 

137 self.assertEqual(s[4], '') 

138 

139 def testGetFileArray(self): 

140 self.assertTrue(self.policy.isFile("file")) 

141 v = self.policy.getArray("file") 

142 self.assertIsNotNone(v, "file array returned as None") 

143 

144 # this is be fixed in another ticket 

145 self.assertIsInstance(v, list, "array value not returned as a list") 

146 

147 truth = ["EventTransmitter_policy.paf", "CacheManager_dict.paf"] 

148 self.assertEqual(len(v), len(truth), "wrong number of values in array") 

149 for i in range(len(truth)): 

150 self.assertEqual(v[i].getPath(), truth[i], 

151 "wrong array element at index %d: %s != %s" % 

152 (i, v[i], truth[i])) 

153 

154 def testGetPolicyArray(self): 

155 self.assertTrue(self.policy.isPolicy("pol")) 

156 v = self.policy.getArray("pol") 

157 # this is be fixed in another ticket 

158 self.assertIsInstance(v, list, "array value not returned as a list") 

159 

160 self.assertEqual(len(v), 2, "wrong number of values in array") 

161 self.assertEqual(v[0].get("int"), 1) 

162 self.assertEqual(v[1].get("int"), 2) 

163 self.assertAlmostEqual(v[0].get("dbl"), 0.0003, 8) 

164 self.assertAlmostEqual(v[1].get("dbl"), -5.2, 8) 

165 

166 

167class TestMemory(lsst.utils.tests.MemoryTestCase): 

168 pass 

169 

170 

171__all__ = "GetTestCase".split() 

172 

173 

174def setup_module(module): 

175 lsst.utils.tests.init() 

176 

177 

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

179 lsst.utils.tests.init() 

180 unittest.main()