Coverage for tests/test_cliCmdIngestFiles.py: 29%

58 statements  

« prev     ^ index     » next       coverage.py v7.2.5, created at 2023-05-02 18:18 -0700

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 json 

26import os 

27import unittest 

28 

29from astropy.table import Table 

30from lsst.daf.butler import Butler 

31from lsst.daf.butler.cli.butler import cli 

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

33from lsst.daf.butler.tests import MetricsExample 

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

35 

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

37 

38 

39class CliIngestFilesTest(unittest.TestCase, ButlerTestHelper): 

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

41 

42 def setUp(self): 

43 self.root = makeTestTempDir(TESTDIR) 

44 self.addCleanup(removeTestTempDir, self.root) 

45 

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

47 

48 self.root2 = makeTestTempDir(TESTDIR) 

49 self.addCleanup(removeTestTempDir, self.root2) 

50 

51 # Create some test output files to be ingested 

52 self.files = [] 

53 self.datasets = [] 

54 for i in range(2): 

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

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

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

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

59 self.datasets.append(data) 

60 self.files.append(outfile) 

61 

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

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

64 self.visits = [423, 424] 

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

66 

67 def testIngestRelativePath(self): 

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

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

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

71 self.assertIngest(table, options) 

72 

73 def testIngestAbsoluteWithDataId(self): 

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

75 table = Table( 

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

77 ) 

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

79 self.assertIngest(table, options) 

80 

81 def testIngestRelativeWithDataId(self): 

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

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

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

85 self.assertIngest(table, options) 

86 

87 def assertIngest(self, table, options): 

88 runner = LogCliRunner() 

89 with runner.isolated_filesystem(): 

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

91 table.write(table_file) 

92 

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

94 result = runner.invoke( 

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

96 ) 

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

98 

99 butler = Butler(self.root) 

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

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

102 

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

104 butler_data = butler.get( 

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

106 ) 

107 self.assertEqual(butler_data, data) 

108 

109 

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

111 unittest.main()