Coverage for tests/test_taskFactory.py: 50%

62 statements  

« prev     ^ index     » next       coverage.py v7.3.0, created at 2023-08-25 09:44 +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 

22from __future__ import annotations 

23 

24import shutil 

25import tempfile 

26import unittest 

27from typing import TYPE_CHECKING 

28 

29import lsst.daf.butler.tests as butlerTests 

30import lsst.pex.config as pexConfig 

31from lsst.ctrl.mpexec import TaskFactory 

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

33 

34if TYPE_CHECKING: 

35 from lsst.daf.butler import Butler, DatasetRef 

36 

37# Storage class to use for tests of fakes. 

38_FAKE_STORAGE_CLASS = "StructuredDataDict" 

39 

40 

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

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

43 

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

45 initOutput = connectionTypes.InitOutput( 

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

47 ) 

48 input = connectionTypes.Input( 

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

50 ) 

51 output = connectionTypes.Output( 

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

53 ) 

54 

55 

56class FakeConfig(PipelineTaskConfig, pipelineConnections=FakeConnections): 

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

58 

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

60 

61 

62def mockTaskClass(): 

63 """Record calls to ``__call__``. 

64 

65 A class placeholder that records calls to __call__. 

66 """ 

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

68 return mock 

69 

70 

71class TaskFactoryTestCase(unittest.TestCase): 

72 """Tests for `TaskFactory`.""" 

73 

74 @classmethod 

75 def setUpClass(cls) -> None: 

76 super().setUpClass() 

77 

78 tmp = tempfile.mkdtemp() 

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

80 cls.repo = butlerTests.makeTestRepo(tmp) 

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

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

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

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

85 

86 def setUp(self) -> None: 

87 super().setUp() 

88 

89 self.factory = TaskFactory() 

90 self.constructor = mockTaskClass() 

91 

92 @staticmethod 

93 def _alteredConfig() -> FakeConfig: 

94 config = FakeConfig() 

95 config.widget = 42.0 

96 return config 

97 

98 @staticmethod 

99 def _dummyCatalog() -> dict: 

100 return {} 

101 

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

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

104 catalog = self._dummyCatalog() 

105 refs = {} 

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

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

108 return butler, refs 

109 

110 def testDefaultConfigLabel(self) -> None: 

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

112 butler, _ = self._tempButler() 

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

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

115 

116 def testAllArgs(self) -> None: 

117 config = self._alteredConfig() 

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

119 butler, refs = self._tempButler() 

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

121 catalog = butler.get("fakeInitInput") 

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

123 

124 

125if __name__ == "__main__": 

126 unittest.main()