Coverage for tests/test_dataset.py : 27%

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
29import lsst.utils.tests
30import lsst.daf.butler as dafButler
31from lsst.ap.verify.dataset import Dataset
32from lsst.ap.verify.testUtils import DataTestCase
35class DatasetTestSuite(DataTestCase):
37 @classmethod
38 def setUpClass(cls):
39 super().setUpClass()
41 cls.obsPackage = 'obs_lsst'
42 cls.camera = 'imsim'
43 cls.gen3Camera = 'LSSTCam-imSim'
45 def setUp(self):
46 self._testbed = Dataset(DatasetTestSuite.testDataset)
48 def testRepr(self):
49 # Required to match constructor call
50 self.assertEqual(repr(self._testbed), "Dataset(" + repr(self.testDataset) + ")")
52 # TODO: remove in DM-29042
53 def testDatasets(self):
54 """Verify that a Dataset knows its supported datasets.
55 """
56 with self.assertWarns(FutureWarning):
57 datasets = Dataset.getSupportedDatasets()
58 self.assertIn(DatasetTestSuite.datasetKey, datasets) # assumed by other tests
60 # TODO: remove in DM-29042
61 def testOldDataset(self):
62 """Verify that Dataset construction warns on old-style dataset names.
63 """
64 with self.assertWarns(FutureWarning):
65 Dataset(DatasetTestSuite.datasetKey)
67 def testBadDataset(self):
68 """Verify that Dataset construction fails gracefully on nonexistent datasets.
69 """
70 with self.assertRaises(ValueError):
71 Dataset("ap_verify_totally_bogus")
73 def testDirectories(self):
74 """Verify that a Dataset reports the desired directory structure.
75 """
76 root = self._testbed.datasetRoot
77 self.assertEqual(self._testbed.rawLocation, os.path.join(root, 'raw'))
78 self.assertEqual(self._testbed.calibLocation, os.path.join(root, 'calib'))
79 self.assertEqual(self._testbed.templateLocation, os.path.join(root, 'templates'))
80 self.assertEqual(self._testbed.refcatsLocation, os.path.join(root, 'refcats'))
82 def testObsPackage(self):
83 """Verify that a Dataset knows its associated obs package and camera.
84 """
85 self.assertEqual(self._testbed.obsPackage, DatasetTestSuite.obsPackage)
86 self.assertEqual(self._testbed.camera, DatasetTestSuite.camera)
87 self.assertEqual(self._testbed.instrument.getName(), DatasetTestSuite.gen3Camera)
89 def testOutput(self):
90 """Verify that a Dataset can create an output repository as desired.
91 """
92 testDir = tempfile.mkdtemp()
93 outputDir = os.path.join(testDir, 'goodOut')
94 calibRepoDir = outputDir
96 try:
97 self._testbed.makeCompatibleRepo(outputDir, calibRepoDir)
98 self.assertTrue(os.path.exists(outputDir), 'Output directory must exist.')
99 self.assertTrue(os.listdir(outputDir), 'Output directory must not be empty.')
100 self.assertTrue(os.path.exists(os.path.join(outputDir, '_mapper')) or
101 os.path.exists(os.path.join(outputDir, 'repositoryCfg.yaml')),
102 'Output directory must have a _mapper or repositoryCfg.yaml file.')
103 finally:
104 if os.path.exists(testDir):
105 shutil.rmtree(testDir, ignore_errors=True)
107 def testExistingOutput(self):
108 """Verify that a Dataset can handle pre-existing output directories,
109 including directories made by external code.
110 """
111 testDir = tempfile.mkdtemp()
112 outputDir = os.path.join(testDir, 'badOut')
113 calibRepoDir = outputDir
115 try:
116 os.makedirs(outputDir)
117 output = os.path.join(outputDir, 'foo.txt')
118 with open(output, 'w') as dummy:
119 dummy.write('This is a test!')
121 self._testbed.makeCompatibleRepo(outputDir, calibRepoDir)
122 self.assertTrue(os.path.exists(outputDir), 'Output directory must exist.')
123 self.assertTrue(os.listdir(outputDir), 'Output directory must not be empty.')
124 self.assertTrue(os.path.exists(os.path.join(outputDir, '_mapper')) or
125 os.path.exists(os.path.join(outputDir, 'repositoryCfg.yaml')),
126 'Output directory must have a _mapper or repositoryCfg.yaml file.')
127 finally:
128 if os.path.exists(testDir):
129 shutil.rmtree(testDir, ignore_errors=True)
131 def _checkOutputGen3(self, repo):
132 """Perform various integrity checks on a repository.
134 Parameters
135 ----------
136 repo : `str`
137 The repository to test. Currently only filesystem repositories
138 are supported.
139 """
140 self.assertTrue(os.path.exists(repo), 'Output directory must exist.')
141 # Call to Butler will fail if repo is corrupted
142 butler = dafButler.Butler(repo)
143 self.assertIn("LSSTCam-imSim/calib", butler.registry.queryCollections())
145 def testOutputGen3(self):
146 """Verify that a Dataset can create an output repository as desired.
147 """
148 testDir = tempfile.mkdtemp()
149 outputDir = os.path.join(testDir, 'goodOut')
151 try:
152 self._testbed.makeCompatibleRepoGen3(outputDir)
153 self._checkOutputGen3(outputDir)
154 finally:
155 if os.path.exists(testDir):
156 shutil.rmtree(testDir, ignore_errors=True)
158 def testExistingOutputGen3(self):
159 """Verify that a Dataset can handle pre-existing output directories,
160 including directories made by external code.
161 """
162 testDir = tempfile.mkdtemp()
163 outputDir = os.path.join(testDir, 'badOut')
165 try:
166 self._testbed.makeCompatibleRepoGen3(outputDir)
167 self._checkOutputGen3(outputDir)
168 self._testbed.makeCompatibleRepoGen3(outputDir)
169 self._checkOutputGen3(outputDir)
170 finally:
171 if os.path.exists(testDir):
172 shutil.rmtree(testDir, ignore_errors=True)
175class MemoryTester(lsst.utils.tests.MemoryTestCase):
176 pass
179def setup_module(module):
180 lsst.utils.tests.init()
183if __name__ == "__main__": 183 ↛ 184line 183 didn't jump to line 184, because the condition on line 183 was never true
184 lsst.utils.tests.init()
185 unittest.main()