Coverage for tests/test_jobReporter.py: 34%

37 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-01 10:20 +0000

1# This file is part of verify. 

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 

22import os 

23import unittest 

24 

25import astropy.units as u 

26 

27import lsst.daf.butler as dafButler 

28import lsst.daf.butler.tests as butlerTests 

29import lsst.daf.butler.tests.utils as testUtils 

30 

31from lsst.verify import Measurement 

32from lsst.verify.bin.jobReporter import JobReporter 

33 

34 

35class JobReporterTestCase(unittest.TestCase): 

36 

37 @classmethod 

38 def setUpClass(cls): 

39 cls.root = testUtils.makeTestTempDir( 

40 os.path.abspath(os.path.dirname(__file__))) 

41 cls.addClassCleanup(testUtils.removeTestTempDir, cls.root) 

42 

43 # Can't use in-memory datastore because JobReporter creates a 

44 # new Butler from scratch. 

45 cls.repo = dafButler.Butler(dafButler.Butler.makeRepo(cls.root), 

46 writeable=True) 

47 

48 # White-box testing: must use real metrics, and provide datasets of 

49 # type metricvalue_*_*. 

50 butlerTests.addDataIdValue(cls.repo, "instrument", "NotACam") 

51 butlerTests.addDataIdValue(cls.repo, "detector", 101) 

52 # physical_filter needed for well-behaved visits 

53 butlerTests.addDataIdValue(cls.repo, "physical_filter", 

54 "k2021", band="k") 

55 butlerTests.addDataIdValue(cls.repo, "visit", 42) 

56 

57 # Dependency on verify_metrics, but not on the code for computing 

58 # these metrics. 

59 butlerTests.addDatasetType( 

60 cls.repo, 

61 "metricvalue_pipe_tasks_CharacterizeImageTime", 

62 {"instrument", "visit", "detector"}, 

63 "MetricValue") 

64 

65 # No shared setUp(); each test case is responsible for its own collections 

66 

67 def test_chain(self): 

68 """Test that running JobReporter on a chained collection retrieves the 

69 metric value closest to the head of the chain. 

70 """ 

71 metricName = "pipe_tasks.CharacterizeImageTime" 

72 id = {"instrument": "NotACam", "visit": 42, "detector": 101} 

73 chainName = "test_chain" 

74 # Note: relies on dict being ordered 

75 chain = {"test_chain_run2": Measurement(metricName, 13.5 * u.s), 

76 "test_chain_run1": Measurement(metricName, 5.0 * u.s), 

77 } 

78 

79 for collection, result in chain.items(): 

80 self.repo.registry.registerCollection( 

81 collection, dafButler.CollectionType.RUN) 

82 self.repo.put(result, 

83 "metricvalue_pipe_tasks_CharacterizeImageTime", 

84 id, 

85 run=collection) 

86 self.repo.registry.registerCollection( 

87 chainName, dafButler.CollectionType.CHAINED) 

88 self.repo.registry.setCollectionChain(chainName, chain.keys()) 

89 

90 reporter = JobReporter(repository=self.root, 

91 collection=chainName, 

92 metrics_package="pipe_tasks", 

93 spec=None, 

94 dataset_name="_tests") 

95 jobs = reporter.run() 

96 self.assertEqual(len(jobs), 1) # Only one data ID 

97 values = list(jobs.values())[0].measurements 

98 self.assertEqual(len(values), 1) # Only one metric 

99 self.assertEqual(values[metricName], chain["test_chain_run2"]) 

100 

101 

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

103 unittest.main()