Coverage for tests/test_workspace.py : 31%

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# This file is part of ap_verify.
3#
4# Developed for the LSST Data Management System.
5# This product includes software developed by the LSST Project
6# (http://www.lsst.org).
7# See the COPYRIGHT file at the top-level directory of this distribution
8# for details of code ownership.
9#
10# This program is free software: you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation, either version 3 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program. If not, see <http://www.gnu.org/licenses/>.
22#
24import os
25import shutil
26import tempfile
27import unittest
28from urllib.request import url2pathname
30import lsst.utils.tests
31import lsst.daf.butler
32from lsst.ap.verify.workspace import WorkspaceGen2, WorkspaceGen3
35class WorkspaceGen2TestSuite(lsst.utils.tests.TestCase):
37 def setUp(self):
38 self._testWorkspace = tempfile.mkdtemp()
39 self._testbed = WorkspaceGen2(self._testWorkspace)
41 def tearDown(self):
42 shutil.rmtree(self._testWorkspace, ignore_errors=True)
44 def _assertInDir(self, path, baseDir):
45 """Test that ``path`` is a subpath of ``baseDir``.
46 """
47 _canonPath = os.path.abspath(os.path.realpath(path))
48 _canonDir = os.path.abspath(os.path.realpath(baseDir))
49 ancestor = os.path.commonprefix([_canonPath, _canonDir])
50 self.assertEqual(ancestor, _canonDir)
52 def _assertNotInDir(self, path, baseDir):
53 """Test that ``path`` is not a subpath of ``baseDir``.
54 """
55 _canonPath = os.path.abspath(os.path.realpath(path))
56 _canonDir = os.path.abspath(os.path.realpath(baseDir))
57 ancestor = os.path.commonprefix([_canonPath, _canonDir])
58 self.assertNotEqual(ancestor, _canonDir)
60 def testMakeDir(self):
61 """Verify that a Workspace creates the workspace directory if it does not exist.
62 """
63 newPath = '_temp'
64 shutil.rmtree(newPath, ignore_errors=True)
65 self.assertFalse(os.path.exists(newPath), 'Workspace directory must not exist before test.')
67 try:
68 WorkspaceGen2(newPath)
69 self.assertTrue(os.path.exists(newPath), 'Workspace directory must exist.')
70 finally:
71 shutil.rmtree(newPath, ignore_errors=True)
73 @staticmethod
74 def _allRepos(workspace):
75 """An iterator over all repos exposed by a WorkspaceGen2.
76 """
77 yield workspace.dataRepo
78 yield workspace.calibRepo
79 yield workspace.templateRepo
80 yield workspace.outputRepo
82 def testDirectories(self):
83 """Verify that a WorkspaceGen2 creates repositories in the target directory.
85 The exact repository locations are not tested, as they are likely to change.
86 """
87 # Workspace should report all paths as absolute
88 root = os.path.abspath(os.path.realpath(self._testWorkspace))
89 self.assertEqual(self._testbed.workDir, root)
90 self._assertInDir(self._testbed.configDir, root)
91 for repo in self._allRepos(self._testbed):
92 # Workspace spec allows these to be URIs or paths, whatever the Butler accepts
93 self._assertInDir(url2pathname(repo), root)
95 def testDatabase(self):
96 """Verify that a WorkspaceGen2 requests a database file in the target
97 directory, but not in any repository.
98 """
99 root = self._testWorkspace
100 self._assertInDir(self._testbed.dbLocation, root)
101 for repo in self._allRepos(self._testbed):
102 # Workspace spec allows these to be URIs or paths, whatever the Butler accepts
103 self._assertNotInDir(self._testbed.dbLocation, url2pathname(repo))
106class WorkspaceGen3TestSuite(lsst.utils.tests.TestCase):
108 def setUp(self):
109 self._testWorkspace = tempfile.mkdtemp()
110 self._testbed = WorkspaceGen3(self._testWorkspace)
112 def tearDown(self):
113 shutil.rmtree(self._testWorkspace, ignore_errors=True)
115 def _assertInDir(self, path, baseDir):
116 """Test that ``path`` is a subpath of ``baseDir``.
117 """
118 _canonPath = os.path.abspath(os.path.realpath(path))
119 _canonDir = os.path.abspath(os.path.realpath(baseDir))
120 ancestor = os.path.commonprefix([_canonPath, _canonDir])
121 self.assertEqual(ancestor, _canonDir)
123 def _assertNotInDir(self, path, baseDir):
124 """Test that ``path`` is not a subpath of ``baseDir``.
125 """
126 _canonPath = os.path.abspath(os.path.realpath(path))
127 _canonDir = os.path.abspath(os.path.realpath(baseDir))
128 ancestor = os.path.commonprefix([_canonPath, _canonDir])
129 self.assertNotEqual(ancestor, _canonDir)
131 def testMakeDir(self):
132 """Verify that a Workspace creates the workspace directory if it does not exist.
133 """
134 newPath = '_temp'
135 shutil.rmtree(newPath, ignore_errors=True)
136 self.assertFalse(os.path.exists(newPath), 'Workspace directory must not exist before test.')
138 try:
139 WorkspaceGen3(newPath)
140 self.assertTrue(os.path.exists(newPath), 'Workspace directory must exist.')
141 finally:
142 shutil.rmtree(newPath, ignore_errors=True)
144 def testDirectories(self):
145 """Verify that a WorkspaceGen3 creates subdirectories in the target directory.
147 The exact locations are not tested, as they are likely to change.
148 """
149 # Workspace should report all paths as absolute
150 root = os.path.abspath(os.path.realpath(self._testWorkspace))
151 self.assertEqual(self._testbed.workDir, root)
152 self._assertInDir(self._testbed.configDir, root)
153 # Workspace spec allows these to be URIs or paths, whatever the Butler accepts
154 self._assertInDir(url2pathname(self._testbed.repo), root)
156 def testDatabase(self):
157 """Verify that a WorkspaceGen3 requests a database file in the target
158 directory, but not in any repository.
159 """
160 root = self._testWorkspace
161 self._assertInDir(self._testbed.dbLocation, root)
162 # Workspace spec allows these to be URIs or paths, whatever the Butler accepts
163 self._assertNotInDir(self._testbed.dbLocation, url2pathname(self._testbed.repo))
165 def testWorkButler(self):
166 """Verify that the Gen 3 Butler is available if and only if the repository is set up.
167 """
168 with self.assertRaises(RuntimeError):
169 self._testbed.workButler
170 lsst.daf.butler.Butler.makeRepo(self._testbed.repo)
171 # Can't really test Butler's state, so just make sure it exists
172 self.assertTrue(self._testbed.workButler.isWriteable())
174 def testAnalysisButler(self):
175 """Verify that the Gen 3 Butler is available if and only if the repository is set up.
176 """
177 with self.assertRaises(RuntimeError):
178 self._testbed.analysisButler
179 lsst.daf.butler.Butler.makeRepo(self._testbed.repo)
180 # Can't really test Butler's state, so just make sure it exists
181 self.assertFalse(self._testbed.analysisButler.isWriteable())
184class MemoryTester(lsst.utils.tests.MemoryTestCase):
185 pass
188def setup_module(module):
189 lsst.utils.tests.init()
192if __name__ == "__main__": 192 ↛ 193line 192 didn't jump to line 193, because the condition on line 192 was never true
193 lsst.utils.tests.init()
194 unittest.main()