Coverage for tests / test_cliCmdIngestFiles.py: 28%

57 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-04-24 08:17 +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 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 <http://www.gnu.org/licenses/>. 

27 

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

29 

30import json 

31import os 

32import unittest 

33 

34from astropy.table import Table 

35 

36from lsst.daf.butler import Butler 

37from lsst.daf.butler.cli.butler import cli 

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

39from lsst.daf.butler.tests import MetricsExample 

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

41 

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

43 

44 

45class CliIngestFilesTest(unittest.TestCase, ButlerTestHelper): 

46 """Test ingest-files command line.""" 

47 

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

49 

50 def setUp(self): 

51 self.root = makeTestTempDir(TESTDIR) 

52 self.addCleanup(removeTestTempDir, self.root) 

53 

54 self.testRepo = MetricTestRepo(self.root, configFile=self.configFile) 

55 self.enterContext(self.testRepo.butler) 

56 

57 self.root2 = makeTestTempDir(TESTDIR) 

58 self.addCleanup(removeTestTempDir, self.root2) 

59 

60 # Create some test output files to be ingested 

61 self.files = [] 

62 self.datasets = [] 

63 for i in range(2): 

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

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

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

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

68 self.datasets.append(data) 

69 self.files.append(outfile) 

70 

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

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

73 self.visits = [423, 424] 

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

75 

76 def testIngestRelativePath(self): 

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

78 table = Table([self.files, self.visits, self.instruments], names=["Files", "visit", "instrument"]) 

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

80 self.assertIngest(table, options) 

81 

82 def testIngestAbsoluteWithDataId(self): 

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

84 table = Table( 

85 [[os.path.join(self.root2, f) for f in self.files], self.visits], names=["Files", "visit"] 

86 ) 

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

88 self.assertIngest(table, options) 

89 

90 def testIngestRelativeWithDataId(self): 

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

92 table = Table([self.files, self.visits], names=["Files", "visit"]) 

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

94 self.assertIngest(table, options) 

95 

96 def assertIngest(self, table, options): 

97 runner = LogCliRunner() 

98 with runner.isolated_filesystem(): 

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

100 table.write(table_file) 

101 

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

103 result = runner.invoke( 

104 cli, ["ingest-files", *options, self.root, "test_metric_comp", run, table_file] 

105 ) 

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

107 

108 butler = Butler.from_config(self.root) 

109 self.enterContext(butler) 

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

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

112 

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

114 butler_data = butler.get( 

115 "test_metric_comp", visit=self.visits[i], instrument=self.instruments[i], collections=run 

116 ) 

117 self.assertEqual(butler_data, data) 

118 

119 

120if __name__ == "__main__": 

121 unittest.main()