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

3# Copyright 2015 LSST Corporation. 

4# 

5# This product includes software developed by the 

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

7# 

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

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

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

11# (at your option) any later version. 

12# 

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

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

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

16# GNU General Public License for more details. 

17# 

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

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

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

21# 

22 

23import collections.abc 

24import os 

25import shutil 

26import unittest 

27import tempfile 

28 

29import lsst.daf.persistence 

30import lsst.pex.policy 

31import lsst.utils.tests 

32 

33pafPolicyPath = os.path.join(os.path.dirname(__file__), 'pexToButlerPolicy', 'policy.paf') 

34ROOT = os.path.abspath(os.path.dirname(__file__)) 

35 

36 

37class PolicyTestCase(unittest.TestCase): 

38 """A test case for the butler policy to verify that it can load a pex policy properly.""" 

39 

40 def setUp(self): 

41 self.testDir = tempfile.mkdtemp(dir=ROOT, prefix='testPexPolicyToButlerPolicy-') 

42 

43 def tearDown(self): 

44 if os.path.exists(self.testDir): 

45 shutil.rmtree(self.testDir) 

46 

47 def testPafReader(self): 

48 """Test that Butler Policy can read a paf file and the keys compare the same as when the same file is 

49 read as a pex Policy.""" 

50 pexPolicy = lsst.pex.policy.Policy.createPolicy(pafPolicyPath) 

51 policy = lsst.daf.persistence.Policy(pafPolicyPath) 

52 

53 # go back through the newly created Butler Policy, and verify that values match the paf Policy 

54 for name in policy.names(): 

55 if pexPolicy.isArray(name): 

56 pexVal = pexPolicy.getArray(name) 

57 else: 

58 pexVal = pexPolicy.get(name) 

59 val = policy[name] 

60 if isinstance(val, lsst.daf.persistence.Policy): 

61 self.assertEqual(pexPolicy.getValueType(name), pexPolicy.POLICY) 

62 else: 

63 self.assertEqual(val, pexVal) 

64 

65 for name in pexPolicy.names(): 

66 if pexPolicy.getValueType(name) == pexPolicy.POLICY: 

67 self.assertIsInstance(policy.get(name), lsst.daf.persistence.Policy) 

68 else: 

69 if pexPolicy.isArray(name): 

70 pexVal = pexPolicy.getArray(name) 

71 else: 

72 pexVal = pexPolicy.get(name) 

73 self.assertEqual(pexVal, policy.get(name)) 

74 

75 # verify a known value, just for sanity: 

76 self.assertEqual(policy.get('exposures.raw.template'), 'raw/raw_v%(visit)d_f%(filter)s.fits.gz') 

77 

78 def testGetStringArray(self): 

79 pexPolicy = lsst.pex.policy.Policy.createPolicy(pafPolicyPath) 

80 self.assertIsInstance(pexPolicy, lsst.pex.policy.Policy) 

81 policy = lsst.daf.persistence.Policy(pafPolicyPath) 

82 s = policy.asArray('exposures.fcr.tables') 

83 self.assertEqual(s, ['raw', 'raw_visit', 'raw_skyTile']) 

84 

85 def testDumpAndLoad(self): 

86 """Load a paf file to a Butler Policy, and dump the loaded policy as a yaml file. 

87 Read the yaml file back in, and then compare all the keys & values to a copy of the paf file loaded 

88 as a pex policy, verify they compare equal. 

89 """ 

90 policy = lsst.daf.persistence.Policy(pafPolicyPath) 

91 yamlPolicyFile = os.path.join(self.testDir, 'policy.yaml') 

92 policy.dumpToFile(os.path.join(self.testDir, 'policy.yaml')) 

93 self.assertTrue(os.path.exists(os.path.join(self.testDir, 'policy.yaml'))) 

94 

95 # test that the data went through the entire wringer correctly - verify the 

96 # original pex data matches the lsst.daf.persistence.Policy data 

97 yamlPolicy = lsst.daf.persistence.Policy(yamlPolicyFile) 

98 yamlNames = yamlPolicy.names() 

99 yamlNames.sort() 

100 pexPolicy = lsst.pex.policy.Policy.createPolicy(pafPolicyPath) 

101 pexNames = pexPolicy.names() 

102 pexNames.sort() 

103 self.assertEqual(yamlNames, pexNames) 

104 for name in yamlNames: 

105 if not isinstance(yamlPolicy[name], lsst.daf.persistence.Policy): 

106 yamlPolicyVal = yamlPolicy[name] 

107 if isinstance(yamlPolicyVal, collections.abc.Iterable) and \ 

108 not isinstance(yamlPolicyVal, str): 

109 self.assertEqual(yamlPolicyVal, pexPolicy.getArray(name)) 

110 else: 

111 self.assertEqual(yamlPolicyVal, pexPolicy.get(name)) 

112 

113 

114class MemoryTester(lsst.utils.tests.MemoryTestCase): 

115 pass 

116 

117 

118def setup_module(module): 

119 lsst.utils.tests.init() 

120 

121 

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

123 lsst.utils.tests.init() 

124 unittest.main()