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

#!/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/>. 

# 

import unittest 

import lsst.utils.tests 

import lsst.pex.config as pexConfig 

 

 

class Config1(pexConfig.Config): 

d1 = pexConfig.DictField("d1", keytype=str, itemtype=int, default={"hi": 4}, itemCheck=lambda x: x > 0) 

d2 = pexConfig.DictField("d2", keytype=str, itemtype=str, default=None) 

d3 = pexConfig.DictField("d3", keytype=float, itemtype=float, optional=True, itemCheck=lambda x: x > 0) 

d4 = pexConfig.DictField("d4", keytype=str, itemtype=None, default={}) 

 

 

class DictFieldTest(unittest.TestCase): 

def testConstructor(self): 

try: 

class BadKeytype(pexConfig.Config): 

d = pexConfig.DictField("...", keytype=list, itemtype=int) 

except Exception: 

pass 

else: 

raise SyntaxError("Unsupported keyptype DictFields should not be allowed") 

 

try: 

class BadItemtype(pexConfig.Config): 

d = pexConfig.DictField("...", keytype=int, itemtype=dict) 

except Exception: 

pass 

else: 

raise SyntaxError("Unsupported itemtype DictFields should not be allowed") 

 

try: 

class BadItemCheck(pexConfig.Config): 

d = pexConfig.DictField("...", keytype=int, itemtype=int, itemCheck=4) 

except Exception: 

pass 

else: 

raise SyntaxError("Non-callable itemCheck DictFields should not be allowed") 

 

try: 

class BadDictCheck(pexConfig.Config): 

d = pexConfig.DictField("...", keytype=int, itemtype=int, dictCheck=4) 

except Exception: 

pass 

else: 

raise SyntaxError("Non-callable dictCheck DictFields should not be allowed") 

 

def testAssignment(self): 

c = Config1() 

self.assertRaises(pexConfig.FieldValidationError, setattr, c, "d1", {3: 3}) 

self.assertRaises(pexConfig.FieldValidationError, setattr, c, "d1", {"a": 0}) 

self.assertRaises(pexConfig.FieldValidationError, setattr, c, "d1", [1.2, 3, 4]) 

c.d1 = None 

c.d1 = {"a": 1, "b": 2} 

self.assertRaises(pexConfig.FieldValidationError, setattr, c, "d3", {"hi": True}) 

c.d3 = {4: 5} 

self.assertEqual(c.d3, {4.: 5.}) 

d = {"a": None, "b": 4, "c": "foo"} 

c.d4 = d 

self.assertEqual(c.d4, d) 

c.d4["a"] = 12 

c.d4[u"b"] = "three" 

c.d4["c"] = None 

self.assertEqual(c.d4["a"], 12) 

self.assertEqual(c.d4["b"], "three") 

self.assertEqual(c.d4["c"], None) 

self.assertRaises(pexConfig.FieldValidationError, setattr, c, "d4", {"hi": [1, 2, 3]}) 

 

def testValidate(self): 

c = Config1() 

self.assertRaises(pexConfig.FieldValidationError, Config1.validate, c) 

 

c.d2 = {"a": "b"} 

c.validate() 

 

def testInPlaceModification(self): 

c = Config1() 

self.assertRaises(pexConfig.FieldValidationError, c.d1.__setitem__, 2, 0) 

self.assertRaises(pexConfig.FieldValidationError, c.d1.__setitem__, "hi", 0) 

c.d1["hi"] = 10 

self.assertEqual(c.d1, {"hi": 10}) 

 

c.d3 = {} 

c.d3[4] = 5 

self.assertEqual(c.d3, {4.: 5.}) 

 

def testNoArbitraryAttributes(self): 

c = Config1() 

self.assertRaises(pexConfig.FieldValidationError, setattr, c.d1, "should", "fail") 

 

def testEquality(self): 

"""Test DictField.__eq__ 

 

We create two dicts, with the keys explicitly added in a different order 

and test their equality. 

""" 

keys1 = ['A', 'B', 'C'] 

keys2 = ['X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e'] 

 

c1 = Config1() 

c1.d4 = {k: "" for k in keys1} 

for k in keys2: 

c1.d4[k] = "" 

 

c2 = Config1() 

for k in keys2 + keys1: 

c2.d4[k] = "" 

 

self.assertTrue(pexConfig.compareConfigs('test', c1, c2)) 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

lsst.utils.tests.init() 

unittest.main()