Coverage for tests/test_preExecInit.py: 15%

69 statements  

« prev     ^ index     » next       coverage.py v7.5.1, created at 2024-05-08 02:55 -0700

1# This file is part of ctrl_mpexec. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (https://www.lsst.org). 

6# See the COPYRIGHT file at the top-level directory of this distribution 

7# for details of code ownership. 

8# 

9# This software is dual licensed under the GNU General Public License and also 

10# under a 3-clause BSD license. Recipients may choose which of these licenses 

11# to use; please see the files gpl-3.0.txt and/or bsd_license.txt, 

12# respectively. If you choose the GPL option then the following text applies 

13# (but note that there is still no warranty even if you opt for BSD instead): 

14# 

15# This program is free software: you can redistribute it and/or modify 

16# it under the terms of the GNU General Public License as published by 

17# the Free Software Foundation, either version 3 of the License, or 

18# (at your option) any later version. 

19# 

20# This program is distributed in the hope that it will be useful, 

21# but WITHOUT ANY WARRANTY; without even the implied warranty of 

22# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

23# GNU General Public License for more details. 

24# 

25# You should have received a copy of the GNU General Public License 

26# along with this program. If not, see <https://www.gnu.org/licenses/>. 

27 

28"""Simple unit test for PreExecInit class. 

29""" 

30 

31import contextlib 

32import shutil 

33import tempfile 

34import unittest 

35 

36from lsst.ctrl.mpexec import PreExecInit 

37from lsst.pipe.base.tests.simpleQGraph import AddTaskFactoryMock, makeSimpleQGraph 

38 

39 

40@contextlib.contextmanager 

41def temporaryDirectory(): 

42 """Context manager that creates and destroys temporary directory. 

43 

44 Difference from `tempfile.TemporaryDirectory` is that it ignores errors 

45 when deleting a directory, which may happen with some filesystems. 

46 

47 Yields 

48 ------ 

49 `str` 

50 The temporary directory. 

51 """ 

52 tmpdir = tempfile.mkdtemp() 

53 yield tmpdir 

54 shutil.rmtree(tmpdir, ignore_errors=True) 

55 

56 

57class PreExecInitTestCase(unittest.TestCase): 

58 """A test case for PreExecInit.""" 

59 

60 def test_saveInitOutputs(self): 

61 taskFactory = AddTaskFactoryMock() 

62 for extendRun in (False, True): 

63 with self.subTest(extendRun=extendRun): 

64 with temporaryDirectory() as tmpdir: 

65 butler, qgraph = makeSimpleQGraph(root=tmpdir) 

66 preExecInit = PreExecInit(butler=butler, taskFactory=taskFactory, extendRun=extendRun) 

67 preExecInit.saveInitOutputs(qgraph) 

68 

69 def test_saveInitOutputs_twice(self): 

70 taskFactory = AddTaskFactoryMock() 

71 for extendRun in (False, True): 

72 with self.subTest(extendRun=extendRun): 

73 with temporaryDirectory() as tmpdir: 

74 butler, qgraph = makeSimpleQGraph(root=tmpdir) 

75 preExecInit = PreExecInit(butler=butler, taskFactory=taskFactory, extendRun=extendRun) 

76 preExecInit.saveInitOutputs(qgraph) 

77 if extendRun: 

78 # will ignore this 

79 preExecInit.saveInitOutputs(qgraph) 

80 else: 

81 # Second time it will fail 

82 with self.assertRaises(Exception): 

83 preExecInit.saveInitOutputs(qgraph) 

84 

85 def test_saveConfigs(self): 

86 for extendRun in (False, True): 

87 with self.subTest(extendRun=extendRun): 

88 with temporaryDirectory() as tmpdir: 

89 butler, qgraph = makeSimpleQGraph(root=tmpdir) 

90 preExecInit = PreExecInit(butler=butler, taskFactory=None, extendRun=extendRun) 

91 preExecInit.saveConfigs(qgraph) 

92 

93 def test_saveConfigs_twice(self): 

94 for extendRun in (False, True): 

95 with self.subTest(extendRun=extendRun): 

96 with temporaryDirectory() as tmpdir: 

97 butler, qgraph = makeSimpleQGraph(root=tmpdir) 

98 preExecInit = PreExecInit(butler=butler, taskFactory=None, extendRun=extendRun) 

99 preExecInit.saveConfigs(qgraph) 

100 if extendRun: 

101 # will ignore this 

102 preExecInit.saveConfigs(qgraph) 

103 else: 

104 # Second time it will fail 

105 with self.assertRaises(Exception): 

106 preExecInit.saveConfigs(qgraph) 

107 

108 def test_savePackageVersions(self): 

109 for extendRun in (False, True): 

110 with self.subTest(extendRun=extendRun): 

111 with temporaryDirectory() as tmpdir: 

112 butler, qgraph = makeSimpleQGraph(root=tmpdir) 

113 preExecInit = PreExecInit(butler=butler, taskFactory=None, extendRun=extendRun) 

114 preExecInit.savePackageVersions(qgraph) 

115 

116 def test_savePackageVersions_twice(self): 

117 for extendRun in (False, True): 

118 with self.subTest(extendRun=extendRun): 

119 with temporaryDirectory() as tmpdir: 

120 butler, qgraph = makeSimpleQGraph(root=tmpdir) 

121 preExecInit = PreExecInit(butler=butler, taskFactory=None, extendRun=extendRun) 

122 preExecInit.savePackageVersions(qgraph) 

123 if extendRun: 

124 # If this is the same packages then it should not 

125 # attempt to save. 

126 preExecInit.savePackageVersions(qgraph) 

127 else: 

128 # second time it will fail 

129 with self.assertRaises(Exception): 

130 preExecInit.savePackageVersions(qgraph) 

131 

132 

133if __name__ == "__main__": 

134 unittest.main()