Coverage for tests/test_cliCmdIngestFiles.py: 27%
56 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-12 10:56 -0700
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-12 10:56 -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/>.
22"""Unit tests for daf_butler CLI ingest-files command.
23"""
25import json
26import os
27import unittest
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
36TESTDIR = os.path.abspath(os.path.dirname(__file__))
39class CliIngestFilesTest(unittest.TestCase, ButlerTestHelper):
40 """Test ingest-files command line."""
42 configFile = os.path.join(TESTDIR, "config/basic/butler.yaml")
44 def setUp(self):
45 self.root = makeTestTempDir(TESTDIR)
46 self.addCleanup(removeTestTempDir, self.root)
48 self.testRepo = MetricTestRepo(self.root, configFile=self.configFile)
50 self.root2 = makeTestTempDir(TESTDIR)
51 self.addCleanup(removeTestTempDir, self.root2)
53 # Create some test output files to be ingested
54 self.files = []
55 self.datasets = []
56 for i in range(2):
57 data = MetricsExample(summary={"int": i, "string": f"{self.id()}_{i}"})
58 outfile = f"test{i}.json"
59 with open(os.path.join(self.root2, outfile), "w") as fd:
60 json.dump(data._asdict(), fd)
61 self.datasets.append(data)
62 self.files.append(outfile)
64 # The values for the visit and instrument dimensions must correspond
65 # to values in the test repo that was created for this test.
66 self.visits = [423, 424]
67 self.instruments = ["DummyCamComp"] * 2
69 def testIngestRelativePath(self):
70 """Ingest using relative path with prefix."""
71 table = Table([self.files, self.visits, self.instruments], names=["Files", "visit", "instrument"])
72 options = ("--prefix", self.root2)
73 self.assertIngest(table, options)
75 def testIngestAbsoluteWithDataId(self):
76 """Ingest with absolute path and factored out dataId override."""
77 table = Table(
78 [[os.path.join(self.root2, f) for f in self.files], self.visits], names=["Files", "visit"]
79 )
80 options = ("--data-id", f"instrument={self.instruments[0]}")
81 self.assertIngest(table, options)
83 def testIngestRelativeWithDataId(self):
84 """Ingest with relative path and factored out dataId override."""
85 table = Table([self.files, self.visits], names=["Files", "visit"])
86 options = ("--data-id", f"instrument={self.instruments[0]}", "--prefix", self.root2)
87 self.assertIngest(table, options)
89 def assertIngest(self, table, options):
90 runner = LogCliRunner()
91 with runner.isolated_filesystem():
92 table_file = os.path.join(self.root2, f"table_{self.id()}.csv")
93 table.write(table_file)
95 run = f"u/user/{self.id()}"
96 result = runner.invoke(
97 cli, ["ingest-files", *options, self.root, "test_metric_comp", run, table_file]
98 )
99 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
101 butler = Butler(self.root)
102 refs = list(butler.registry.queryDatasets("test_metric_comp", collections=run))
103 self.assertEqual(len(refs), 2)
105 for i, data in enumerate(self.datasets):
106 butler_data = butler.get(
107 "test_metric_comp", visit=self.visits[i], instrument=self.instruments[i], collections=run
108 )
109 self.assertEqual(butler_data, data)
112if __name__ == "__main__":
113 unittest.main()