Coverage for tests/test_single_quantum_executor.py: 15%

68 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2025-10-14 01:42 -0700

1# This file is part of pipe_base. 

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 

28from __future__ import annotations 

29 

30import os 

31import unittest 

32 

33from lsst.pipe.base.single_quantum_executor import SingleQuantumExecutor 

34from lsst.pipe.base.tests.mocks import InMemoryRepo 

35 

36TESTDIR = os.path.abspath(os.path.dirname(__file__)) 

37 

38 

39class SingleQuantumExecutorTestCase(unittest.TestCase): 

40 """Tests for SingleQuantumExecutor implementation.""" 

41 

42 def test_simple_execute(self) -> None: 

43 """Run execute() method in simplest setup.""" 

44 helper = InMemoryRepo("base.yaml") 

45 helper.add_task() 

46 qgraph = helper.make_quantum_graph_builder().build(attach_datastore_records=False) 

47 executor, butler = helper.make_single_quantum_executor() 

48 nQuanta = 1 

49 nodes = list(qgraph) 

50 self.assertEqual(len(nodes), nQuanta) 

51 node = nodes[0] 

52 executor.execute(node.task_node, node.quantum) 

53 # There must be one dataset of task's output connection 

54 self.assertEqual(len(butler.get_datasets("dataset_auto1")), 1) 

55 

56 def test_skip_existing_execute(self) -> None: 

57 """Run execute() method twice, with skip_existing_in.""" 

58 helper = InMemoryRepo("base.yaml") 

59 helper.add_task() 

60 qgraph = helper.make_quantum_graph_builder().build(attach_datastore_records=False) 

61 executor, butler = helper.make_single_quantum_executor() 

62 nQuanta = 1 

63 nodes = list(qgraph) 

64 self.assertEqual(len(nodes), nQuanta) 

65 node = nodes[0] 

66 executor.execute(node.task_node, node.quantum) 

67 

68 outputs1 = butler.get_datasets("dataset_auto1") 

69 self.assertEqual(len(outputs1), 1) 

70 ref1, obj1 = outputs1.popitem() 

71 

72 # Re-run it with skip_existing, it should not run. Note that if it did 

73 # run (and called 'butler.put') that would raise an exception. 

74 executor = SingleQuantumExecutor(limited_butler_factory=butler.factory, skip_existing=True) 

75 executor.execute(node.task_node, node.quantum) 

76 

77 outputs2 = butler.get_datasets("dataset_auto1") 

78 self.assertEqual(len(outputs2), 1) 

79 ref2, obj2 = outputs2.popitem() 

80 self.assertEqual(ref1, ref2) 

81 # Objects should be the same (but not identities, because the butler 

82 # will copy them). 

83 self.assertEqual(obj1, obj2) 

84 

85 def test_clobber_outputs_execute(self) -> None: 

86 """Run execute() method twice, with clobber_outputs.""" 

87 helper = InMemoryRepo("base.yaml") 

88 helper.add_task() 

89 qgraph = helper.make_quantum_graph_builder().build(attach_datastore_records=False) 

90 executor, butler = helper.make_single_quantum_executor() 

91 nQuanta = 1 

92 nodes = list(qgraph) 

93 self.assertEqual(len(nodes), nQuanta) 

94 node = nodes[0] 

95 executor.execute(node.task_node, node.quantum) 

96 

97 outputs1 = butler.get_datasets("dataset_auto1") 

98 self.assertEqual(len(outputs1), 1) 

99 ref1, obj1 = outputs1.popitem() 

100 

101 # Remove the dataset ourself, and replace it with something 

102 # different so we can check later whether it got replaced. 

103 butler.pruneDatasets([ref1], disassociate=False, unstore=True, purge=False) 

104 obj1.quantum = None 

105 butler.put(obj1, ref1) 

106 

107 # Re-run it with clobber_outputs and skip_existing, it should not 

108 # clobber but should skip instead. 

109 executor = SingleQuantumExecutor( 

110 limited_butler_factory=butler.factory, skip_existing=True, clobber_outputs=True 

111 ) 

112 executor.execute(node.task_node, node.quantum) 

113 outputs2 = butler.get_datasets("dataset_auto1") 

114 self.assertEqual(len(outputs2), 1) 

115 ref2, obj2 = outputs2.popitem() 

116 self.assertEqual(ref1, ref2) 

117 self.assertEqual(obj1, obj2) 

118 

119 # Re-run it with clobber_outputs but without skip_existing_in, it 

120 # should clobber. 

121 executor = SingleQuantumExecutor(limited_butler_factory=butler.factory, clobber_outputs=True) 

122 executor.execute(node.task_node, node.quantum) 

123 outputs3 = butler.get_datasets("dataset_auto1") 

124 self.assertEqual(len(outputs3), 1) 

125 ref3, obj3 = outputs3.popitem() 

126 self.assertEqual(ref1, ref3) 

127 self.assertNotEqual(obj1, obj3) 

128 

129 

130if __name__ == "__main__": 

131 unittest.main()