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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

#!/usr/bin/env python 

# 

# LSST Data Management System 

# Copyright 2008, 2009, 2010 LSST Corporation. 

# 

# This product includes software developed by the 

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

# 

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

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

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

# (at your option) any later version. 

# 

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

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

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

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

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

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

# 

 

from builtins import str 

import os 

import unittest 

 

import lsst.utils.tests 

 

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

import lsst.pex.exceptions 

 

# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- 

 

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

# repositories in a deeply nested and linked policy file. 

 

 

class UrnPolicyFileTestCase(unittest.TestCase): 

examplesDir = None 

 

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

""" 

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

and carres a message that contains excMsg. 

 

excClass: the subclass of LsstCppException we expect to see 

excMsg: a substring of the message it should carry 

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

failMsg: the assertion message if this fails 

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

""" 

try: 

callableObj(*args, **kwargs) 

except excClass as e: 

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

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

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

else: 

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

 

def getExamples(self, filename=None): 

if not self.examplesDir: 

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

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

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

if filename: 

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

else: 

return self.examplesDir 

 

def testReference(self): 

addr = "pex_policy:examples:EventTransmitter_policy.paf" 

p = Policy() 

 

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

UrnPolicyFile(addr).load(p) 

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

 

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

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

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

 

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

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

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

 

def testIndirect(self): 

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

p = Policy(urn) 

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

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

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

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

 

p = Policy() 

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

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

 

def testLoading(self): 

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

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

 

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

Policy, "URN too short", 

"urn:eupspkg:foo.paf") 

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

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

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

 

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

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

Policy.createPolicyFromUrn, 

"doesn't support loading undecorated DictionaryFile", 

urn) 

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

p = Policy.createPolicyFromUrn(urn) 

# make sure all reference types worked 

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

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

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

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

 

def testTypos(self): 

base = "pex_policy:tests/urn:indirect_parent_typo_" 

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

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

Policy()) 

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

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

Policy()) 

 

def testRepos(self): 

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

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

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

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

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

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

"failure opening Policy file: " + expectedFile, 

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

 

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

p = Policy() 

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

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

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

lsst.utils.tests.init() 

unittest.main()