Coverage for tests/test_cliCmdIngestFiles.py: 30%

58 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2022-12-01 19:55 +0000

1# This file is part of daf_butler. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (http://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 <http://www.gnu.org/licenses/>. 

21 

22"""Unit tests for daf_butler CLI ingest-files command. 

23""" 

24 

25import os 

26import json 

27import unittest 

28from astropy.table import Table 

29 

30from lsst.daf.butler.cli.butler import cli 

31from lsst.daf.butler.cli.utils import clickResultMsg, LogCliRunner 

32from lsst.daf.butler import Butler 

33from lsst.daf.butler.tests import MetricsExample 

34from lsst.daf.butler.tests.utils import ButlerTestHelper, makeTestTempDir, MetricTestRepo, removeTestTempDir 

35 

36 

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

38 

39 

40class CliIngestFilesTest(unittest.TestCase, ButlerTestHelper): 

41 

42 configFile = os.path.join(TESTDIR, "config/basic/butler.yaml") 

43 

44 def setUp(self): 

45 self.root = makeTestTempDir(TESTDIR) 

46 self.addCleanup(removeTestTempDir, self.root) 

47 

48 self.testRepo = MetricTestRepo(self.root, 

49 configFile=self.configFile) 

50 

51 self.root2 = makeTestTempDir(TESTDIR) 

52 self.addCleanup(removeTestTempDir, self.root2) 

53 

54 # Create some test output files to be ingested 

55 self.files = [] 

56 self.datasets = [] 

57 for i in range(2): 

58 data = MetricsExample(summary={"int": i, "string": f"{self.id()}_{i}"}) 

59 outfile = f"test{i}.json" 

60 with open(os.path.join(self.root2, outfile), "w") as fd: 

61 json.dump(data._asdict(), fd) 

62 self.datasets.append(data) 

63 self.files.append(outfile) 

64 

65 # The values for the visit and instrument dimensions must correspond 

66 # to values in the test repo that was created for this test. 

67 self.visits = [423, 424] 

68 self.instruments = ["DummyCamComp"] * 2 

69 

70 def testIngestRelativePath(self): 

71 """Ingest using relative path with prefix.""" 

72 table = Table([self.files, self.visits, self.instruments], 

73 names=["Files", "visit", "instrument"]) 

74 options = ("--prefix", self.root2) 

75 self.assertIngest(table, options) 

76 

77 def testIngestAbsoluteWithDataId(self): 

78 """Ingest with absolute path and factored out dataId override.""" 

79 table = Table([[os.path.join(self.root2, f) for f in self.files], self.visits], 

80 names=["Files", "visit"]) 

81 options = ("--data-id", f"instrument={self.instruments[0]}") 

82 self.assertIngest(table, options) 

83 

84 def testIngestRelativeWithDataId(self): 

85 """Ingest with relative path and factored out dataId override.""" 

86 table = Table([self.files, self.visits], 

87 names=["Files", "visit"]) 

88 options = ("--data-id", f"instrument={self.instruments[0]}", "--prefix", self.root2) 

89 self.assertIngest(table, options) 

90 

91 def assertIngest(self, table, options): 

92 runner = LogCliRunner() 

93 with runner.isolated_filesystem(): 

94 

95 table_file = os.path.join(self.root2, f"table_{self.id()}.csv") 

96 table.write(table_file) 

97 

98 run = f"u/user/{self.id()}" 

99 result = runner.invoke(cli, ["ingest-files", *options, 

100 self.root, "test_metric_comp", run, table_file]) 

101 self.assertEqual(result.exit_code, 0, clickResultMsg(result)) 

102 

103 butler = Butler(self.root) 

104 refs = list(butler.registry.queryDatasets("test_metric_comp", collections=run)) 

105 self.assertEqual(len(refs), 2) 

106 

107 for i, data in enumerate(self.datasets): 

108 butler_data = butler.get("test_metric_comp", visit=self.visits[i], 

109 instrument=self.instruments[i], 

110 collections=run) 

111 self.assertEqual(butler_data, data) 

112 

113 

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

115 unittest.main()