Coverage for tests/test_preExecInit.py: 20%

71 statements  

« prev     ^ index     » next       coverage.py v7.1.0, created at 2023-02-05 18:04 -0800

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 program is free software: you can redistribute it and/or modify 

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

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

12# (at your option) any later version. 

13# 

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

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

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

17# GNU General Public License for more details. 

18# 

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

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

21 

22"""Simple unit test for PreExecInit class. 

23""" 

24 

25import contextlib 

26import shutil 

27import tempfile 

28import unittest 

29 

30from lsst.ctrl.mpexec import PreExecInit 

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

32 

33 

34@contextlib.contextmanager 

35def temporaryDirectory(): 

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

37 

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

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

40 """ 

41 tmpdir = tempfile.mkdtemp() 

42 yield tmpdir 

43 shutil.rmtree(tmpdir, ignore_errors=True) 

44 

45 

46class PreExecInitTestCase(unittest.TestCase): 

47 """A test case for PreExecInit 

48 """ 

49 

50 def test_saveInitOutputs(self): 

51 taskFactory = AddTaskFactoryMock() 

52 for extendRun in (False, True): 

53 with self.subTest(extendRun=extendRun): 

54 with temporaryDirectory() as tmpdir: 

55 butler, qgraph = makeSimpleQGraph(root=tmpdir) 

56 preExecInit = PreExecInit(butler=butler, taskFactory=taskFactory, 

57 extendRun=extendRun) 

58 preExecInit.saveInitOutputs(qgraph) 

59 

60 def test_saveInitOutputs_twice(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, 

67 extendRun=extendRun) 

68 preExecInit.saveInitOutputs(qgraph) 

69 if extendRun: 

70 # will ignore this 

71 preExecInit.saveInitOutputs(qgraph) 

72 else: 

73 # Second time it will fail 

74 with self.assertRaises(Exception): 

75 preExecInit.saveInitOutputs(qgraph) 

76 

77 def test_saveConfigs(self): 

78 for extendRun in (False, True): 

79 with self.subTest(extendRun=extendRun): 

80 with temporaryDirectory() as tmpdir: 

81 butler, qgraph = makeSimpleQGraph(root=tmpdir) 

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

83 preExecInit.saveConfigs(qgraph) 

84 

85 def test_saveConfigs_twice(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 if extendRun: 

93 # will ignore this 

94 preExecInit.saveConfigs(qgraph) 

95 else: 

96 # Second time it will fail 

97 with self.assertRaises(Exception): 

98 preExecInit.saveConfigs(qgraph) 

99 

100 def test_savePackageVersions(self): 

101 for extendRun in (False, True): 

102 with self.subTest(extendRun=extendRun): 

103 with temporaryDirectory() as tmpdir: 

104 butler, qgraph = makeSimpleQGraph(root=tmpdir) 

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

106 preExecInit.savePackageVersions(qgraph) 

107 

108 def test_savePackageVersions_twice(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 if extendRun: 

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

117 # attempt to save. 

118 preExecInit.savePackageVersions(qgraph) 

119 else: 

120 # second time it will fail 

121 with self.assertRaises(Exception): 

122 preExecInit.savePackageVersions(qgraph) 

123 

124 

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

126 unittest.main()