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 os 

25import unittest 

26 

27import lsst.utils.tests 

28 

29from lsst.pex.policy import Policy, UrnPolicyFile, BadNameError 

30import lsst.pex.exceptions 

31 

32# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- 

33 

34# TODO: test cross-package loading more thoroughly -- mix up the packages and 

35# repositories in a deeply nested and linked policy file. 

36 

37 

38class UrnPolicyFileTestCase(unittest.TestCase): 

39 examplesDir = None 

40 

41 def assertRaiseLCE(self, excClass, excMsg, callableObj, failMsg, *args, **kwargs): 

42 """ 

43 Expect callableObj(args, kwargs) to raise an exception of type excClass, 

44 and carres a message that contains excMsg. 

45 

46 excClass: the subclass of LsstCppException we expect to see 

47 excMsg: a substring of the message it should carry 

48 callableObj: the thing that, when called, should raise an exception 

49 failMsg: the assertion message if this fails 

50 args, kwargs (optional): arguments to pass to callableObj 

51 """ 

52 try: 

53 callableObj(*args, **kwargs) 

54 except excClass as e: 

55 self.assertGreater(str(e).find(excMsg), 0, 

56 failMsg + ": expected to see the message \"" + excMsg + 

57 "\"; actual message was \"" + str(e) + "\".") 

58 else: 

59 self.fail(failMsg + ": did not raise " + excClass) 

60 

61 def getExamples(self, filename=None): 

62 if not self.examplesDir: 

63 # XXX is this really the best way to find the src_dir? 

64 pexPolicyDir = lsst.utils.getPackageDir('pex_policy') 

65 self.examplesDir = os.path.join(pexPolicyDir, "examples") 

66 if filename: 

67 return os.path.join(self.examplesDir, filename) 

68 else: 

69 return self.examplesDir 

70 

71 def testReference(self): 

72 addr = "pex_policy:examples:EventTransmitter_policy.paf" 

73 p = Policy() 

74 

75 p.set("transmitter.logVerbosity", "not") 

76 UrnPolicyFile(addr).load(p) 

77 self.assertEqual(p.get("transmitter.logVerbosity"), "debug") 

78 

79 p.set("transmitter.logVerbosity", "not") 

80 UrnPolicyFile("urn:eupspkg:" + addr).load(p) 

81 self.assertEqual(p.get("transmitter.logVerbosity"), "debug") 

82 

83 p.set("transmitter.logVerbosity", "not") 

84 UrnPolicyFile("@@" + addr).load(p) 

85 self.assertEqual(p.get("transmitter.logVerbosity"), "debug") 

86 

87 def testIndirect(self): 

88 urn = "@urn:eupspkg:pex_policy:tests/urn:indirect_parent_good.paf" 

89 p = Policy(urn) 

90 self.assertEqual(p.get("urn_full.name"), "Simple Policy") 

91 self.assertEqual(p.get("urn_brief.name"), "Simple Policy") 

92 self.assertEqual(p.get("urn_mixed_case.name"), "Simple Policy") 

93 self.assertEqual(p.get("local.foo"), "bar") 

94 

95 p = Policy() 

96 UrnPolicyFile("pex_policy:tests/urn:level_1.paf").load(p) 

97 self.assertEqual(p.get("foo.bar.baz.qux.quux"), "schmazzle") 

98 

99 def testLoading(self): 

100 p = Policy("urn:eupspkg:pex_policy:tests/urn:level_1.paf") 

101 self.assertEqual(p.get("foo.bar.baz.qux.quux"), "schmazzle") 

102 

103 self.assertRaiseLCE(BadNameError, "Wrong number of terms", 

104 Policy, "URN too short", 

105 "urn:eupspkg:foo.paf") 

106 self.assertRaiseLCE(lsst.pex.exceptions.IoError, "failure opening Policy file", 

107 Policy, "URN abbrev '@' not allowed in constructor", 

108 "@pex_policy:tests/urn:level_1.paf") 

109 

110 urn = "urn:eupspkg:pex_policy:tests/dictionary:defaults_dictionary_good.paf" 

111 self.assertRaiseLCE(lsst.pex.exceptions.IoError, "/./defaults_dictionary_indirect", 

112 Policy.createPolicyFromUrn, 

113 "doesn't support loading undecorated DictionaryFile", 

114 urn) 

115 urn = "urn:eupspkg:pex_policy:tests/dictionary:defaults_dictionary_partial.paf" 

116 p = Policy.createPolicyFromUrn(urn) 

117 # make sure all reference types worked 

118 # self.assertEqual(p.get("indirect.string_type"), "foo") 

119 # self.assertEqual(p.get("indirect2.string_type"), "foo") 

120 self.assertEqual(p.get("indirect3.string_type"), "foo") 

121 self.assertEqual(p.get("indirect4.string_type"), "foo") 

122 

123 def testTypos(self): 

124 base = "pex_policy:tests/urn:indirect_parent_typo_" 

125 self.assertRaiseLCE(lsst.pex.exceptions.IoError, "failure opening Policy file", 

126 UrnPolicyFile(base + "1.paf").load, "Typo in URN", 

127 Policy()) 

128 self.assertRaiseLCE(lsst.pex.exceptions.IoError, "failure opening Policy file", 

129 UrnPolicyFile(base + "2.paf").load, "Typo in URN", 

130 Policy()) 

131 

132 def testRepos(self): 

133 # when the repository is mis-specified, local files cannot be loaded 

134 upf = UrnPolicyFile("pex_policy:tests:urn/indirect_parent_good.paf") 

135 # we expect it to look in <package>/tests/simple.paf 

136 pexPolicyDir = lsst.utils.getPackageDir('pex_policy') 

137 expectedFile = pexPolicyDir + "/tests/simple.paf" 

138 self.assertRaiseLCE(lsst.pex.exceptions.IoError, 

139 "failure opening Policy file: " + expectedFile, 

140 upf.load, "Wrong repository dir.", Policy()) 

141 

142 # a PAF file designed to have "tests" as it repository 

143 p = Policy() 

144 UrnPolicyFile("pex_policy:tests:urn/local_tests_repos.paf").load(p) 

145 self.assertEqual(p.get("local.polish"), "fancy") 

146 

147 

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

149 pass 

150 

151 

152def setup_module(module): 

153 lsst.utils.tests.init() 

154 

155 

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

157 lsst.utils.tests.init() 

158 unittest.main()