Coverage for tests/test_cliCmdIngestFiles.py: 27%

56 statements  

« prev     ^ index     » next       coverage.py v7.4.0, created at 2024-01-16 10:44 +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""" 

30 

31import json 

32import os 

33import unittest 

34 

35from astropy.table import Table 

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 

56 self.root2 = makeTestTempDir(TESTDIR) 

57 self.addCleanup(removeTestTempDir, self.root2) 

58 

59 # Create some test output files to be ingested 

60 self.files = [] 

61 self.datasets = [] 

62 for i in range(2): 

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

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

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

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

67 self.datasets.append(data) 

68 self.files.append(outfile) 

69 

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

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

72 self.visits = [423, 424] 

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

74 

75 def testIngestRelativePath(self): 

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

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

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

79 self.assertIngest(table, options) 

80 

81 def testIngestAbsoluteWithDataId(self): 

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

83 table = Table( 

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

85 ) 

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

87 self.assertIngest(table, options) 

88 

89 def testIngestRelativeWithDataId(self): 

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

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

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

93 self.assertIngest(table, options) 

94 

95 def assertIngest(self, table, options): 

96 runner = LogCliRunner() 

97 with runner.isolated_filesystem(): 

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

99 table.write(table_file) 

100 

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

102 result = runner.invoke( 

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

104 ) 

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

106 

107 butler = Butler.from_config(self.root) 

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

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

110 

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

112 butler_data = butler.get( 

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

114 ) 

115 self.assertEqual(butler_data, data) 

116 

117 

118if __name__ == "__main__": 

119 unittest.main()