Coverage for tests/test_policy.py: 29%
80 statements
« prev ^ index » next coverage.py v7.5.0, created at 2024-04-25 00:02 -0700
« prev ^ index » next coverage.py v7.5.0, created at 2024-04-25 00:02 -0700
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#
23import os
24import unittest
26import lsst.daf.persistence as dafPersist
27import lsst.utils.tests
30class PolicyTestCase(unittest.TestCase):
31 """A test case for the butler policy"""
33 def setUp(self):
34 self.policy = dafPersist.Policy(
35 {'body': {'job': {'position': 'Developer', 'company': 'Microsoft'}, 'name': 'John'},
36 'error': False})
38 def testBasic(self):
39 p = dafPersist.Policy()
40 p['a'] = {1: 2}
41 self.assertEqual(p, {'a': {1: 2}})
42 p.update({'a': {3: 4}})
43 self.assertEqual(p, {'a': {1: 2, 3: 4}})
45 def testUpdateWithDict(self):
46 self.policy.update({'body': {'job': {'position': 'Manager'}}})
47 self.assertEqual(self.policy['body'],
48 {'job': {'position': 'Manager', 'company': 'Microsoft'}, 'name': 'John'})
49 self.policy.update({'body': {'name': {'first': 'John', 'last': 'Smith'}}})
50 self.assertEqual(self.policy['body'],
51 {'job': {'position': 'Manager', 'company': 'Microsoft'},
52 'name': {'first': 'John', 'last': 'Smith'}})
54 def testUpdateWithPolicy(self):
55 p1 = dafPersist.Policy({'body': {'job': {'position': 'Manager'}}})
56 self.policy.update(p1)
57 self.assertEqual(self.policy['body'],
58 {'job': {'position': 'Manager', 'company': 'Microsoft'}, 'name': 'John'})
60 def testGet(self):
61 self.assertEqual(self.policy['body'],
62 {'job': {'position': 'Developer', 'company': 'Microsoft'}, 'name': 'John'})
63 self.assertEqual(self.policy['body.job'], {'position': 'Developer', 'company': 'Microsoft'})
64 self.assertEqual(self.policy['body.job.position'], 'Developer')
66 def testSet(self):
67 # change an item
68 self.policy['body.job.company'] = 'SLAC'
69 self.assertEqual(self.policy['body'],
70 {'job': {'position': 'Developer', 'company': 'SLAC'}, 'name': 'John'})
71 # add an item
72 self.policy['body.job.salary'] = 100000
73 self.assertEqual(self.policy['body'],
74 {'job': {'position': 'Developer', 'company': 'SLAC', 'salary': 100000},
75 'name': 'John'})
77 def testNames(self):
78 names = self.policy.names()
79 names.sort()
80 expectedNames = ['body', 'body.job', 'body.job.position', 'body.job.company', 'body.name', 'error']
81 expectedNames.sort()
82 self.assertEqual(names, expectedNames)
84 def testCopyPolicy(self):
85 pol = dafPersist.Policy(self.policy)
86 self.assertEqual(pol, self.policy)
88 def testGetPolicy(self):
89 policy = self.policy['body']
90 self.assertEqual(policy, {'job': {'position': 'Developer', 'company': 'Microsoft'}, 'name': 'John'})
91 self.assertEqual(policy['job.position'], 'Developer') # note: verifies dot naming
92 self.assertIsInstance(policy, dafPersist.Policy)
94 def testDotsInBraces(self):
95 self.assertEqual(self.policy['body.job.company'], 'Microsoft')
97 def testMerge(self):
98 a = dafPersist.Policy()
99 b = dafPersist.Policy()
100 a['a.b.c'] = 1
101 b['a.b.c'] = 2
102 b['a.b.d'] = 3
103 a.merge(b)
104 self.assertEqual(a['a.b.c'], 1)
105 self.assertEqual(a['a.b.d'], 3)
107 # b should remain unchanged:
108 self.assertEqual(b['a.b.c'], 2)
109 self.assertEqual(b['a.b.d'], 3)
111 def testOpenDefaultPolicy(self):
112 policyFile = dafPersist.Policy.defaultPolicyFile('daf_persistence', 'testPolicy.yaml', 'tests')
113 policy = dafPersist.Policy(policyFile)
114 self.assertEqual(policy['exposures.raw.template'], 'foo/bar/baz.fits.gz')
116 def testDumpLoad(self):
117 self.policy.dumpToFile('testDumpFile.yaml')
118 loadedPolicy = dafPersist.Policy('testDumpFile.yaml')
119 self.assertEqual(self.policy, loadedPolicy)
120 os.remove('testDumpFile.yaml')
122 def testNonExistantPolicyAtPath(self):
123 # Test that loading yaml files that do not exist raises an IOError.
124 with self.assertRaises(IOError):
125 dafPersist.Policy("c:/nonExistantPath/policy.yaml")
127 def testInvalidPolicyFileType(self):
128 # We only support files with '.yaml' extension; check that trying to load a file with a
129 # different extension (in this case '.txt') raises a RuntimeError.
130 with self.assertRaises(RuntimeError):
131 dafPersist.Policy("c:/policy.txt")
134class TestMemory(lsst.utils.tests.MemoryTestCase):
135 pass
138def setup_module(module):
139 lsst.utils.tests.init()
142if __name__ == "__main__": 142 ↛ 143line 142 didn't jump to line 143, because the condition on line 142 was never true
143 lsst.utils.tests.init()
144 unittest.main()