Coverage for tests/test_taskFactory.py: 45%

62 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-09 02:48 -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 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 initInput = connectionTypes.InitInput(name="fakeInitInput", doc="test", storageClass=_FAKE_STORAGE_CLASS) 

43 initOutput = connectionTypes.InitOutput( 

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

45 ) 

46 input = connectionTypes.Input( 

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

48 ) 

49 output = connectionTypes.Output( 

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

51 ) 

52 

53 

54class FakeConfig(PipelineTaskConfig, pipelineConnections=FakeConnections): 

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

56 

57 

58def mockTaskClass(): 

59 """A class placeholder that records calls to __call__.""" 

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

61 return mock 

62 

63 

64class TaskFactoryTestCase(unittest.TestCase): 

65 @classmethod 

66 def setUpClass(cls) -> None: 

67 super().setUpClass() 

68 

69 tmp = tempfile.mkdtemp() 

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

71 cls.repo = butlerTests.makeTestRepo(tmp) 

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

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

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

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

76 

77 def setUp(self) -> None: 

78 super().setUp() 

79 

80 self.factory = TaskFactory() 

81 self.constructor = mockTaskClass() 

82 

83 @staticmethod 

84 def _alteredConfig() -> FakeConfig: 

85 config = FakeConfig() 

86 config.widget = 42.0 

87 return config 

88 

89 @staticmethod 

90 def _dummyCatalog() -> dict: 

91 return {} 

92 

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

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

95 catalog = self._dummyCatalog() 

96 refs = {} 

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

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

99 return butler, refs 

100 

101 def testDefaultConfigLabel(self) -> None: 

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

103 butler, _ = self._tempButler() 

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

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

106 

107 def testAllArgs(self) -> None: 

108 config = self._alteredConfig() 

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

110 butler, refs = self._tempButler() 

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

112 catalog = butler.get("fakeInitInput") 

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

114 

115 

116if __name__ == "__main__": 

117 unittest.main()