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.datasetKey)
48 def testRepr(self):
49 # Required to match constructor call
50 self.assertEqual(repr(self._testbed), "Dataset(" + repr(self.datasetKey) + ")")
52 def testDatasets(self):
53 """Verify that a Dataset knows its supported datasets.
54 """
55 datasets = Dataset.getSupportedDatasets()
56 self.assertIn(DatasetTestSuite.datasetKey, datasets) # assumed by other tests
58 def testBadDataset(self):
59 """Verify that Dataset construction fails gracefully on unsupported datasets.
60 """
61 with self.assertRaises(ValueError):
62 Dataset("TotallyBogusDataset")
64 def testDirectories(self):
65 """Verify that a Dataset reports the desired directory structure.
66 """
67 root = self._testbed.datasetRoot
68 self.assertEqual(self._testbed.rawLocation, os.path.join(root, 'raw'))
69 self.assertEqual(self._testbed.calibLocation, os.path.join(root, 'calib'))
70 self.assertEqual(self._testbed.templateLocation, os.path.join(root, 'templates'))
71 self.assertEqual(self._testbed.refcatsLocation, os.path.join(root, 'refcats'))
73 def testObsPackage(self):
74 """Verify that a Dataset knows its associated obs package and camera.
75 """
76 self.assertEqual(self._testbed.obsPackage, DatasetTestSuite.obsPackage)
77 self.assertEqual(self._testbed.camera, DatasetTestSuite.camera)
78 self.assertEqual(self._testbed.instrument.getName(), DatasetTestSuite.gen3Camera)
80 def testOutput(self):
81 """Verify that a Dataset can create an output repository as desired.
82 """
83 testDir = tempfile.mkdtemp()
84 outputDir = os.path.join(testDir, 'goodOut')
85 calibRepoDir = outputDir
87 try:
88 self._testbed.makeCompatibleRepo(outputDir, calibRepoDir)
89 self.assertTrue(os.path.exists(outputDir), 'Output directory must exist.')
90 self.assertTrue(os.listdir(outputDir), 'Output directory must not be empty.')
91 self.assertTrue(os.path.exists(os.path.join(outputDir, '_mapper')) or
92 os.path.exists(os.path.join(outputDir, 'repositoryCfg.yaml')),
93 'Output directory must have a _mapper or repositoryCfg.yaml file.')
94 finally:
95 if os.path.exists(testDir):
96 shutil.rmtree(testDir, ignore_errors=True)
98 def testExistingOutput(self):
99 """Verify that a Dataset can handle pre-existing output directories,
100 including directories made by external code.
101 """
102 testDir = tempfile.mkdtemp()
103 outputDir = os.path.join(testDir, 'badOut')
104 calibRepoDir = outputDir
106 try:
107 os.makedirs(outputDir)
108 output = os.path.join(outputDir, 'foo.txt')
109 with open(output, 'w') as dummy:
110 dummy.write('This is a test!')
112 self._testbed.makeCompatibleRepo(outputDir, calibRepoDir)
113 self.assertTrue(os.path.exists(outputDir), 'Output directory must exist.')
114 self.assertTrue(os.listdir(outputDir), 'Output directory must not be empty.')
115 self.assertTrue(os.path.exists(os.path.join(outputDir, '_mapper')) or
116 os.path.exists(os.path.join(outputDir, 'repositoryCfg.yaml')),
117 'Output directory must have a _mapper or repositoryCfg.yaml file.')
118 finally:
119 if os.path.exists(testDir):
120 shutil.rmtree(testDir, ignore_errors=True)
122 def _checkOutputGen3(self, repo):
123 """Perform various integrity checks on a repository.
125 Parameters
126 ----------
127 repo : `str`
128 The repository to test. Currently only filesystem repositories
129 are supported.
130 """
131 self.assertTrue(os.path.exists(repo), 'Output directory must exist.')
132 # Call to Butler will fail if repo is corrupted
133 butler = dafButler.Butler(repo)
134 self.assertIn("LSSTCam-imSim/calib", butler.registry.queryCollections())
136 def testOutputGen3(self):
137 """Verify that a Dataset can create an output repository as desired.
138 """
139 testDir = tempfile.mkdtemp()
140 outputDir = os.path.join(testDir, 'goodOut')
142 try:
143 self._testbed.makeCompatibleRepoGen3(outputDir)
144 self._checkOutputGen3(outputDir)
145 finally:
146 if os.path.exists(testDir):
147 shutil.rmtree(testDir, ignore_errors=True)
149 def testExistingOutputGen3(self):
150 """Verify that a Dataset can handle pre-existing output directories,
151 including directories made by external code.
152 """
153 testDir = tempfile.mkdtemp()
154 outputDir = os.path.join(testDir, 'badOut')
156 try:
157 self._testbed.makeCompatibleRepoGen3(outputDir)
158 self._checkOutputGen3(outputDir)
159 self._testbed.makeCompatibleRepoGen3(outputDir)
160 self._checkOutputGen3(outputDir)
161 finally:
162 if os.path.exists(testDir):
163 shutil.rmtree(testDir, ignore_errors=True)
166class MemoryTester(lsst.utils.tests.MemoryTestCase):
167 pass
170def setup_module(module):
171 lsst.utils.tests.init()
174if __name__ == "__main__": 174 ↛ 175line 174 didn't jump to line 175, because the condition on line 174 was never true
175 lsst.utils.tests.init()
176 unittest.main()