Coverage for tests/test_cliCmdQueryDatasetTypes.py: 37%
63 statements
« prev ^ index » next coverage.py v6.4.4, created at 2022-08-26 09:24 +0000
« prev ^ index » next coverage.py v6.4.4, created at 2022-08-26 09:24 +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 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 query-collections command.
23"""
25import unittest
27from astropy.table import Table as AstropyTable
28from lsst.daf.butler.cli.butler import cli
29from lsst.daf.butler.cli.cmd import query_dataset_types
30from lsst.daf.butler.cli.utils import LogCliRunner, clickResultMsg
31from lsst.daf.butler.tests import CliCmdTestBase
32from lsst.daf.butler.tests.utils import ButlerTestHelper, readTable
33from numpy import array
36class QueryDatasetTypesCmdTest(CliCmdTestBase, unittest.TestCase):
38 mockFuncName = "lsst.daf.butler.cli.cmd.commands.script.queryDatasetTypes"
40 @staticmethod
41 def defaultExpected():
42 return dict(repo=None, verbose=False, glob=(), components=None)
44 @staticmethod
45 def command():
46 return query_dataset_types
48 def test_minimal(self):
49 """Test only required parameters."""
50 self.run_test(["query-dataset-types", "here"], self.makeExpected(repo="here"))
52 def test_requiredMissing(self):
53 """Test that if the required parameter is missing it fails"""
54 self.run_missing(["query-dataset-types"], r"Error: Missing argument ['\"]REPO['\"].")
56 def test_all(self):
57 """Test all parameters."""
58 self.run_test(
59 ["query-dataset-types", "here", "--verbose", "foo*", "--components"],
60 self.makeExpected(repo="here", verbose=True, glob=("foo*",), components=True),
61 )
62 self.run_test(
63 ["query-dataset-types", "here", "--verbose", "foo*", "--no-components"],
64 self.makeExpected(repo="here", verbose=True, glob=("foo*",), components=False),
65 )
68class QueryDatasetTypesScriptTest(ButlerTestHelper, unittest.TestCase):
69 def testQueryDatasetTypes(self):
70 self.maxDiff = None
71 datasetName = "test"
72 instrumentDimension = "instrument"
73 visitDimension = "visit"
74 storageClassName = "StructuredDataDict"
75 expectedNotVerbose = AstropyTable((("test",),), names=("name",))
76 runner = LogCliRunner()
77 with runner.isolated_filesystem():
78 result = runner.invoke(cli, ["create", "here"])
79 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
80 # Create the dataset type.
81 result = runner.invoke(
82 cli,
83 [
84 "register-dataset-type",
85 "here",
86 datasetName,
87 storageClassName,
88 instrumentDimension,
89 visitDimension,
90 ],
91 )
92 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
93 # Okay to create it again identically.
94 result = runner.invoke(
95 cli,
96 [
97 "register-dataset-type",
98 "here",
99 datasetName,
100 storageClassName,
101 instrumentDimension,
102 visitDimension,
103 ],
104 )
105 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
106 # Not okay to create a different version of it.
107 result = runner.invoke(
108 cli, ["register-dataset-type", "here", datasetName, storageClassName, instrumentDimension]
109 )
110 self.assertNotEqual(result.exit_code, 0, clickResultMsg(result))
111 # Not okay to try to create a component dataset type.
112 result = runner.invoke(
113 cli, ["register-dataset-type", "here", "a.b", storageClassName, instrumentDimension]
114 )
115 self.assertNotEqual(result.exit_code, 0, clickResultMsg(result))
116 # check not-verbose output:
117 result = runner.invoke(cli, ["query-dataset-types", "here"])
118 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
119 self.assertAstropyTablesEqual(readTable(result.output), expectedNotVerbose)
120 # check glob output:
121 result = runner.invoke(cli, ["query-dataset-types", "here", "t*"])
122 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
123 self.assertAstropyTablesEqual(readTable(result.output), expectedNotVerbose)
124 # check verbose output:
125 result = runner.invoke(cli, ["query-dataset-types", "here", "--verbose"])
126 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
127 expected = AstropyTable(
128 array(
129 (
130 "test",
131 "['band', 'instrument', 'physical_filter', 'visit']",
132 storageClassName,
133 )
134 ),
135 names=("name", "dimensions", "storage class"),
136 )
137 self.assertAstropyTablesEqual(readTable(result.output), expected)
139 # Now remove and check that it was removed
140 # First a non-existent one
141 result = runner.invoke(cli, ["remove-dataset-type", "here", "unreal"])
142 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
144 # Now one we now has been registered
145 result = runner.invoke(cli, ["remove-dataset-type", "here", datasetName])
146 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
148 # and check that it has gone
149 result = runner.invoke(cli, ["query-dataset-types", "here"])
150 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
151 self.assertIn("No results", result.output)
154if __name__ == "__main__": 154 ↛ 155line 154 didn't jump to line 155, because the condition on line 154 was never true
155 unittest.main()