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# LSST Data Management System 

4# Copyright 2008, 2009, 2010 LSST Corporation. 

5# 

6# This product includes software developed by the 

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

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 LSST License Statement and 

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

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

22# 

23 

24import unittest 

25import os.path 

26 

27import lsst.utils.tests 

28from lsst.pex.policy import Policy, NameNotFound 

29import lsst.pex.exceptions 

30 

31 

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

33 

34 

35class PolicyTestCase(unittest.TestCase): 

36 def testPolicySetget(self): 

37 p = Policy() 

38 self.assertFalse(p.exists("foo"), "empty existence test failed") 

39 self.assertEqual(p.valueCount("foo.bar"), 0, 

40 "empty valueCount test failed") 

41 

42 self.assertRaises(lsst.pex.exceptions.Exception, p.getTypeInfo, "foo") 

43 

44 p.set("doall", "true") 

45 

46 # non-existence tests on a non-empty policy 

47 self.assertFalse(p.exists("foo"), 

48 "non-empty non-existence test failed") 

49 self.assertEqual(p.valueCount("foo.bar"), 0, 

50 "empty valueCount test failed") 

51 self.assertFalse(p.isInt("foo"), 

52 "non-empty non-existence type test failed") 

53 self.assertRaises(lsst.pex.exceptions.Exception, p.getTypeInfo, "foo") 

54 

55 # existence tests 

56 self.assertTrue(p.exists("doall"), "non-empty existence test failed") 

57 self.assertEqual(p.valueCount("doall"), 1, 

58 "single valueCount test failed") 

59 

60 self.assertRaises(lsst.pex.exceptions.Exception, p.getInt, "doall") 

61 self.assertRaises(lsst.pex.exceptions.Exception, p.getDoubleArray, "doall") 

62 

63 self.assertEqual(p.get("doall"), "true", 

64 "top-level getString failed") 

65 p.set("doall", "duh") 

66 self.assertEqual(p.get("doall"), "duh", 

67 "top-level getString failed") 

68 

69 # test array access 

70 ary = p.getArray("doall") 

71 self.assertEqual(len(ary), 1, 

72 "scalar property has more than one value") 

73 

74 self.assertEqual(ary[0], "duh", "scalar access via array failed") 

75 

76 p.add("doall", "never") 

77 self.assertEqual(p.valueCount("doall"), 2, 

78 "2-elem. valueCount test failed") 

79 self.assertEqual(p.get("doall"), "never", "top-level add failed") 

80 ary = p.getArray("doall") 

81 self.assertEqual(len(ary), 2, 

82 "scalar property has wrong number of values") 

83 self.assertEqual(ary[0], "duh", 

84 "scalar access via (2-el) array failed") 

85 self.assertEqual(ary[-1], "never", 

86 "scalar access via (2-el) array failed") 

87 

88 # test hierarchical access 

89 

90 # list names 

91 

92 # test types 

93 p.set("pint", 5) 

94 self.assertEqual(p.getInt("pint"), 5, "support for type int failed") 

95 self.assertIsInstance(p.get("pint"), int, 

96 "auto-typing for int failed") 

97 p.set("pdbl", 5.1) 

98 self.assertAlmostEqual(p.getDouble("pdbl"), 5.1, 7, 

99 "support for type double failed") 

100 self.assertEqual(type(p.get("pdbl")), type(5.1), 

101 "auto-typing for double failed") 

102 p.set("pbool", True) 

103 self.assertTrue(p.getBool("pbool"), "support for type bool failed") 

104 self.assertEqual(type(p.get("pbool")), type(True), 

105 "auto-typing for bool failed") 

106 p.add("pbool", False) 

107 

108 # test shallow & deep copies 

109 

110 # test raise NameNotFound if not present 

111 self.assertRaises(NameNotFound, p.get, "nonexistent") 

112 self.assertRaises(NameNotFound, p.getArray, "nonexistent") 

113 self.assertRaises(NameNotFound, p.getDouble, "nonexistent") 

114 

115 def testSimpleLoad(self): 

116 p = Policy.createPolicy(os.path.join(proddir, "examples", "EventTransmitter_policy.paf")) 

117 self.assertEqual(p.get("transmitter.serializationFormat"), "deluxe") 

118 p = None 

119 

120 def testEmptyPolicy(self): 

121 p = Policy() # noqa F841: unused variable 

122 

123 def testPolicyCopy(self): 

124 p = Policy.createPolicy(os.path.join(proddir, "examples", "EventTransmitter_policy.paf")) 

125 pp = Policy(p, True) 

126 self.assertEqual(p.get("transmitter.serializationFormat"), "deluxe") 

127 self.assertEqual(pp.getString("transmitter.serializationFormat"), "deluxe") 

128 p = None 

129 self.assertEqual(pp.getString("transmitter.serializationFormat"), "deluxe") 

130 

131 def testSetNothing(self): 

132 p = Policy() 

133 try: 

134 p.set("foo", None) 

135 self.assertTrue(False, "Setting value to None succeeded.") 

136 except RuntimeError: 

137 self.assertFalse(p.exists("foo")) 

138 

139 

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

141 pass 

142 

143 

144def setup_module(module): 

145 lsst.utils.tests.init() 

146 

147 

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

149 lsst.utils.tests.init() 

150 unittest.main()