-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtest_cliCmdIngestFiles.py
119 lines (97 loc) · 4.81 KB
/
test_cliCmdIngestFiles.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# This file is part of daf_butler.
#
# Developed for the LSST Data Management System.
# This product includes software developed by the LSST Project
# (http://www.lsst.org).
# See the COPYRIGHT file at the top-level directory of this distribution
# for details of code ownership.
#
# This software is dual licensed under the GNU General Public License and also
# under a 3-clause BSD license. Recipients may choose which of these licenses
# to use; please see the files gpl-3.0.txt and/or bsd_license.txt,
# respectively. If you choose the GPL option then the following text applies
# (but note that there is still no warranty even if you opt for BSD instead):
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""Unit tests for daf_butler CLI ingest-files command."""
import json
import os
import unittest
from astropy.table import Table
from lsst.daf.butler import Butler
from lsst.daf.butler.cli.butler import cli
from lsst.daf.butler.cli.utils import LogCliRunner, clickResultMsg
from lsst.daf.butler.tests import MetricsExample
from lsst.daf.butler.tests.utils import ButlerTestHelper, MetricTestRepo, makeTestTempDir, removeTestTempDir
TESTDIR = os.path.abspath(os.path.dirname(__file__))
class CliIngestFilesTest(unittest.TestCase, ButlerTestHelper):
"""Test ingest-files command line."""
configFile = os.path.join(TESTDIR, "config/basic/butler.yaml")
def setUp(self):
self.root = makeTestTempDir(TESTDIR)
self.addCleanup(removeTestTempDir, self.root)
self.testRepo = MetricTestRepo(self.root, configFile=self.configFile)
self.root2 = makeTestTempDir(TESTDIR)
self.addCleanup(removeTestTempDir, self.root2)
# Create some test output files to be ingested
self.files = []
self.datasets = []
for i in range(2):
data = MetricsExample(summary={"int": i, "string": f"{self.id()}_{i}"})
outfile = f"test{i}.json"
with open(os.path.join(self.root2, outfile), "w") as fd:
json.dump(data._asdict(), fd)
self.datasets.append(data)
self.files.append(outfile)
# The values for the visit and instrument dimensions must correspond
# to values in the test repo that was created for this test.
self.visits = [423, 424]
self.instruments = ["DummyCamComp"] * 2
def testIngestRelativePath(self):
"""Ingest using relative path with prefix."""
table = Table([self.files, self.visits, self.instruments], names=["Files", "visit", "instrument"])
options = ("--prefix", self.root2)
self.assertIngest(table, options)
def testIngestAbsoluteWithDataId(self):
"""Ingest with absolute path and factored out dataId override."""
table = Table(
[[os.path.join(self.root2, f) for f in self.files], self.visits], names=["Files", "visit"]
)
options = ("--data-id", f"instrument={self.instruments[0]}")
self.assertIngest(table, options)
def testIngestRelativeWithDataId(self):
"""Ingest with relative path and factored out dataId override."""
table = Table([self.files, self.visits], names=["Files", "visit"])
options = ("--data-id", f"instrument={self.instruments[0]}", "--prefix", self.root2)
self.assertIngest(table, options)
def assertIngest(self, table, options):
runner = LogCliRunner()
with runner.isolated_filesystem():
table_file = os.path.join(self.root2, f"table_{self.id()}.csv")
table.write(table_file)
run = f"u/user/{self.id()}"
result = runner.invoke(
cli, ["ingest-files", *options, self.root, "test_metric_comp", run, table_file]
)
self.assertEqual(result.exit_code, 0, clickResultMsg(result))
butler = Butler.from_config(self.root)
refs = list(butler.registry.queryDatasets("test_metric_comp", collections=run))
self.assertEqual(len(refs), 2)
for i, data in enumerate(self.datasets):
butler_data = butler.get(
"test_metric_comp", visit=self.visits[i], instrument=self.instruments[i], collections=run
)
self.assertEqual(butler_data, data)
if __name__ == "__main__":
unittest.main()