Coverage for tests/test_workspace.py: 31%

76 statements  

« prev     ^ index     » next       coverage.py v7.2.5, created at 2023-05-07 02:11 -0700

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# 

23 

24import os 

25import shutil 

26import tempfile 

27import unittest 

28from urllib.request import url2pathname 

29 

30import lsst.utils.tests 

31import lsst.daf.butler 

32from lsst.ap.verify.workspace import WorkspaceGen3 

33 

34 

35class WorkspaceGen3TestSuite(lsst.utils.tests.TestCase): 

36 

37 def setUp(self): 

38 # Use realpath to avoid link problems 

39 self._testWorkspace = os.path.realpath(tempfile.mkdtemp()) 

40 self._testbed = WorkspaceGen3(self._testWorkspace) 

41 

42 def tearDown(self): 

43 shutil.rmtree(self._testWorkspace, ignore_errors=True) 

44 

45 def testRepr(self): 

46 # Required to match constructor call 

47 self.assertEqual(repr(self._testbed), "WorkspaceGen3(" + repr(self._testWorkspace) + ")") 

48 

49 def testEq(self): 

50 copied = WorkspaceGen3(self._testWorkspace) 

51 self.assertEqual(self._testbed, copied) 

52 

53 with tempfile.TemporaryDirectory() as temp: 

54 different = WorkspaceGen3(temp) 

55 self.assertNotEqual(self._testbed, different) 

56 self.assertNotEqual(copied, different) 

57 

58 def _assertInDir(self, path, baseDir): 

59 """Test that ``path`` is a subpath of ``baseDir``. 

60 """ 

61 _canonPath = os.path.abspath(os.path.realpath(path)) 

62 _canonDir = os.path.abspath(os.path.realpath(baseDir)) 

63 ancestor = os.path.commonprefix([_canonPath, _canonDir]) 

64 self.assertEqual(ancestor, _canonDir) 

65 

66 def _assertNotInDir(self, path, baseDir): 

67 """Test that ``path`` is not a subpath of ``baseDir``. 

68 """ 

69 _canonPath = os.path.abspath(os.path.realpath(path)) 

70 _canonDir = os.path.abspath(os.path.realpath(baseDir)) 

71 ancestor = os.path.commonprefix([_canonPath, _canonDir]) 

72 self.assertNotEqual(ancestor, _canonDir) 

73 

74 def testMakeDir(self): 

75 """Verify that a Workspace creates the workspace directory if it does not exist. 

76 """ 

77 newPath = '_temp3' # can't use mkdtemp because creation is what we're testing 

78 shutil.rmtree(newPath, ignore_errors=True) 

79 self.assertFalse(os.path.exists(newPath), 'Workspace directory must not exist before test.') 

80 

81 try: 

82 WorkspaceGen3(newPath) 

83 self.assertTrue(os.path.exists(newPath), 'Workspace directory must exist.') 

84 finally: 

85 shutil.rmtree(newPath, ignore_errors=True) 

86 

87 def testDirectories(self): 

88 """Verify that a WorkspaceGen3 creates subdirectories in the target directory. 

89 

90 The exact locations are not tested, as they are likely to change. 

91 """ 

92 # Workspace should report all paths as absolute 

93 root = os.path.abspath(os.path.realpath(self._testWorkspace)) 

94 self.assertEqual(self._testbed.workDir, root) 

95 self._assertInDir(self._testbed.configDir, root) 

96 self._assertInDir(self._testbed.pipelineDir, root) 

97 # Workspace spec allows these to be URIs or paths, whatever the Butler accepts 

98 self._assertInDir(url2pathname(self._testbed.repo), root) 

99 

100 def testDatabase(self): 

101 """Verify that a WorkspaceGen3 requests a database file in the target 

102 directory, but not in any repository. 

103 """ 

104 root = self._testWorkspace 

105 self._assertInDir(self._testbed.dbLocation, root) 

106 # Workspace spec allows these to be URIs or paths, whatever the Butler accepts 

107 self._assertNotInDir(self._testbed.dbLocation, url2pathname(self._testbed.repo)) 

108 

109 def testAlerts(self): 

110 """Verify that a WorkspaceGen3 requests an alert dump in the target 

111 directory, but not in any repository. 

112 """ 

113 root = self._testWorkspace 

114 self._assertInDir(self._testbed.alertLocation, root) 

115 # Workspace spec allows these to be URIs or paths, whatever the Butler accepts 

116 self._assertNotInDir(self._testbed.alertLocation, url2pathname(self._testbed.repo)) 

117 

118 def testWorkButler(self): 

119 """Verify that the Gen 3 Butler is available if and only if the repository is set up. 

120 """ 

121 with self.assertRaises(RuntimeError): 

122 self._testbed.workButler 

123 lsst.daf.butler.Butler.makeRepo(self._testbed.repo) 

124 # workButler definition currently requires valid collections; this 

125 # should be an implementation detail. 

126 registry = lsst.daf.butler.Butler(self._testbed.repo, writeable=True).registry 

127 registry.registerCollection('refcats', lsst.daf.butler.CollectionType.RUN) 

128 registry.registerCollection('skymaps', lsst.daf.butler.CollectionType.RUN) 

129 registry.registerCollection('templates/foo', lsst.daf.butler.CollectionType.RUN) 

130 

131 # Can't really test Butler's state, so just make sure it exists 

132 self.assertTrue(self._testbed.workButler.isWriteable()) 

133 

134 def testAnalysisButler(self): 

135 """Verify that the Gen 3 Butler is available if and only if the repository is set up. 

136 """ 

137 with self.assertRaises(RuntimeError): 

138 self._testbed.analysisButler 

139 lsst.daf.butler.Butler.makeRepo(self._testbed.repo) 

140 # Can't really test Butler's state, so just make sure it exists 

141 self.assertFalse(self._testbed.analysisButler.isWriteable()) 

142 

143 

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

145 pass 

146 

147 

148def setup_module(module): 

149 lsst.utils.tests.init() 

150 

151 

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

153 lsst.utils.tests.init() 

154 unittest.main()