Coverage for tests/test_preExecInit.py: 15%

71 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2022-10-11 08:57 +0000

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 AddTaskFactoryMock, makeSimpleQGraph 

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 def test_saveInitOutputs(self): 

50 taskFactory = AddTaskFactoryMock() 

51 for extendRun in (False, True): 

52 with self.subTest(extendRun=extendRun): 

53 with temporaryDirectory() as tmpdir: 

54 butler, qgraph = makeSimpleQGraph(root=tmpdir) 

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

56 preExecInit.saveInitOutputs(qgraph) 

57 

58 def test_saveInitOutputs_twice(self): 

59 taskFactory = AddTaskFactoryMock() 

60 for extendRun in (False, True): 

61 with self.subTest(extendRun=extendRun): 

62 with temporaryDirectory() as tmpdir: 

63 butler, qgraph = makeSimpleQGraph(root=tmpdir) 

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

65 preExecInit.saveInitOutputs(qgraph) 

66 if extendRun: 

67 # will ignore this 

68 preExecInit.saveInitOutputs(qgraph) 

69 else: 

70 # Second time it will fail 

71 with self.assertRaises(Exception): 

72 preExecInit.saveInitOutputs(qgraph) 

73 

74 def test_saveConfigs(self): 

75 for extendRun in (False, True): 

76 with self.subTest(extendRun=extendRun): 

77 with temporaryDirectory() as tmpdir: 

78 butler, qgraph = makeSimpleQGraph(root=tmpdir) 

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

80 preExecInit.saveConfigs(qgraph) 

81 

82 def test_saveConfigs_twice(self): 

83 for extendRun in (False, True): 

84 with self.subTest(extendRun=extendRun): 

85 with temporaryDirectory() as tmpdir: 

86 butler, qgraph = makeSimpleQGraph(root=tmpdir) 

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

88 preExecInit.saveConfigs(qgraph) 

89 if extendRun: 

90 # will ignore this 

91 preExecInit.saveConfigs(qgraph) 

92 else: 

93 # Second time it will fail 

94 with self.assertRaises(Exception): 

95 preExecInit.saveConfigs(qgraph) 

96 

97 def test_savePackageVersions(self): 

98 for extendRun in (False, True): 

99 with self.subTest(extendRun=extendRun): 

100 with temporaryDirectory() as tmpdir: 

101 butler, qgraph = makeSimpleQGraph(root=tmpdir) 

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

103 preExecInit.savePackageVersions(qgraph) 

104 

105 def test_savePackageVersions_twice(self): 

106 for extendRun in (False, True): 

107 with self.subTest(extendRun=extendRun): 

108 with temporaryDirectory() as tmpdir: 

109 butler, qgraph = makeSimpleQGraph(root=tmpdir) 

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

111 preExecInit.savePackageVersions(qgraph) 

112 if extendRun: 

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

114 # attempt to save. 

115 preExecInit.savePackageVersions(qgraph) 

116 else: 

117 # second time it will fail 

118 with self.assertRaises(Exception): 

119 preExecInit.savePackageVersions(qgraph) 

120 

121 

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

123 unittest.main()