Coverage for tests/test_taskFactory.py: 50%

62 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-03-28 03:02 -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 

28from __future__ import annotations 

29 

30import shutil 

31import tempfile 

32import unittest 

33from typing import TYPE_CHECKING 

34 

35import lsst.daf.butler.tests as butlerTests 

36import lsst.pex.config as pexConfig 

37from lsst.ctrl.mpexec import TaskFactory 

38from lsst.pipe.base import PipelineTaskConfig, PipelineTaskConnections, TaskDef, connectionTypes 

39 

40if TYPE_CHECKING: 

41 from lsst.daf.butler import Butler, DatasetRef 

42 

43# Storage class to use for tests of fakes. 

44_FAKE_STORAGE_CLASS = "StructuredDataDict" 

45 

46 

47class FakeConnections(PipelineTaskConnections, dimensions=set()): 

48 """Fake connections class used for testing.""" 

49 

50 initInput = connectionTypes.InitInput(name="fakeInitInput", doc="test", storageClass=_FAKE_STORAGE_CLASS) 

51 initOutput = connectionTypes.InitOutput( 

52 name="fakeInitOutput", doc="test", storageClass=_FAKE_STORAGE_CLASS 

53 ) 

54 input = connectionTypes.Input( 

55 name="fakeInput", doc="test", storageClass=_FAKE_STORAGE_CLASS, dimensions=set() 

56 ) 

57 output = connectionTypes.Output( 

58 name="fakeOutput", doc="test", storageClass=_FAKE_STORAGE_CLASS, dimensions=set() 

59 ) 

60 

61 

62class FakeConfig(PipelineTaskConfig, pipelineConnections=FakeConnections): 

63 """Config class used along with fake connections class.""" 

64 

65 widget = pexConfig.Field(dtype=float, doc="test", default=1.0) 

66 

67 

68def mockTaskClass(): 

69 """Record calls to ``__call__``. 

70 

71 A class placeholder that records calls to __call__. 

72 """ 

73 mock = unittest.mock.Mock(__name__="_TaskMock", _DefaultName="FakeTask", ConfigClass=FakeConfig) 

74 return mock 

75 

76 

77class TaskFactoryTestCase(unittest.TestCase): 

78 """Tests for `TaskFactory`.""" 

79 

80 @classmethod 

81 def setUpClass(cls) -> None: 

82 super().setUpClass() 

83 

84 tmp = tempfile.mkdtemp() 

85 cls.addClassCleanup(shutil.rmtree, tmp, ignore_errors=True) 

86 cls.repo = butlerTests.makeTestRepo(tmp) 

87 butlerTests.addDatasetType(cls.repo, "fakeInitInput", set(), _FAKE_STORAGE_CLASS) 

88 butlerTests.addDatasetType(cls.repo, "fakeInitOutput", set(), _FAKE_STORAGE_CLASS) 

89 butlerTests.addDatasetType(cls.repo, "fakeInput", set(), _FAKE_STORAGE_CLASS) 

90 butlerTests.addDatasetType(cls.repo, "fakeOutput", set(), _FAKE_STORAGE_CLASS) 

91 

92 def setUp(self) -> None: 

93 super().setUp() 

94 

95 self.factory = TaskFactory() 

96 self.constructor = mockTaskClass() 

97 

98 @staticmethod 

99 def _alteredConfig() -> FakeConfig: 

100 config = FakeConfig() 

101 config.widget = 42.0 

102 return config 

103 

104 @staticmethod 

105 def _dummyCatalog() -> dict: 

106 return {} 

107 

108 def _tempButler(self) -> tuple[Butler, dict[str, DatasetRef]]: 

109 butler = butlerTests.makeTestCollection(self.repo, uniqueId=self.id()) 

110 catalog = self._dummyCatalog() 

111 refs = {} 

112 for dataset_type in ("fakeInitInput", "fakeInitOutput", "fakeInput", "fakeOutput"): 

113 refs[dataset_type] = butler.put(catalog, dataset_type) 

114 return butler, refs 

115 

116 def testDefaultConfigLabel(self) -> None: 

117 task_def = TaskDef(taskClass=self.constructor, label=None, config=None) 

118 butler, _ = self._tempButler() 

119 self.factory.makeTask(taskDef=task_def, butler=butler, initInputRefs=[]) 

120 self.constructor.assert_called_with(config=FakeConfig(), initInputs={}, name="FakeTask") 

121 

122 def testAllArgs(self) -> None: 

123 config = self._alteredConfig() 

124 task_def = TaskDef(taskClass=self.constructor, label="no-name", config=config) 

125 butler, refs = self._tempButler() 

126 self.factory.makeTask(taskDef=task_def, butler=butler, initInputRefs=[refs["fakeInitInput"]]) 

127 catalog = butler.get("fakeInitInput") 

128 self.constructor.assert_called_with(config=config, initInputs={"initInput": catalog}, name="no-name") 

129 

130 

131if __name__ == "__main__": 

132 unittest.main()