Coverage for tests/test_cliCmdQueryDatasetTypes.py: 34%
Shortcuts on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
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"""
25from astropy.table import Table as AstropyTable
26from numpy import array
27import unittest
29from lsst.daf.butler.cli.butler import cli
30from lsst.daf.butler.cli.cmd import query_dataset_types
31from lsst.daf.butler.cli.utils import clickResultMsg, LogCliRunner
32from lsst.daf.butler.tests import CliCmdTestBase
33from lsst.daf.butler.tests.utils import ButlerTestHelper, readTable
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,
43 verbose=False,
44 glob=(),
45 components=None)
47 @staticmethod
48 def command():
49 return query_dataset_types
51 def test_minimal(self):
52 """Test only required parameters.
53 """
54 self.run_test(["query-dataset-types", "here"],
55 self.makeExpected(repo="here"))
57 def test_requiredMissing(self):
58 """Test that if the required parameter is missing it fails"""
59 self.run_missing(["query-dataset-types"], r"Error: Missing argument ['\"]REPO['\"].")
61 def test_all(self):
62 """Test all parameters."""
63 self.run_test(["query-dataset-types", "here", "--verbose", "foo*", "--components"],
64 self.makeExpected(repo="here", verbose=True, glob=("foo*", ), components=True))
65 self.run_test(["query-dataset-types", "here", "--verbose", "foo*", "--no-components"],
66 self.makeExpected(repo="here", verbose=True, glob=("foo*", ), components=False))
69class QueryDatasetTypesScriptTest(ButlerTestHelper, unittest.TestCase):
71 def testQueryDatasetTypes(self):
72 self.maxDiff = None
73 datasetName = "test"
74 instrumentDimension = "instrument"
75 visitDimension = "visit"
76 storageClassName = "StructuredDataDict"
77 expectedNotVerbose = AstropyTable((("test",),), names=("name",))
78 runner = LogCliRunner()
79 with runner.isolated_filesystem():
80 result = runner.invoke(cli, ["create", "here"])
81 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
82 # Create the dataset type.
83 result = runner.invoke(cli, ["register-dataset-type", "here", datasetName,
84 storageClassName, instrumentDimension, visitDimension])
85 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
86 # Okay to create it again identically.
87 result = runner.invoke(cli, ["register-dataset-type", "here", datasetName,
88 storageClassName, instrumentDimension, visitDimension])
89 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
90 # Not okay to create a different version of it.
91 result = runner.invoke(cli, ["register-dataset-type", "here", datasetName,
92 storageClassName, instrumentDimension])
93 self.assertNotEqual(result.exit_code, 0, clickResultMsg(result))
94 # Not okay to try to create a component dataset type.
95 result = runner.invoke(cli, ["register-dataset-type", "here", "a.b",
96 storageClassName, instrumentDimension])
97 self.assertNotEqual(result.exit_code, 0, clickResultMsg(result))
98 # check not-verbose output:
99 result = runner.invoke(cli, ["query-dataset-types", "here"])
100 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
101 self.assertAstropyTablesEqual(readTable(result.output), expectedNotVerbose)
102 # check glob output:
103 result = runner.invoke(cli, ["query-dataset-types", "here", "t*"])
104 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
105 self.assertAstropyTablesEqual(readTable(result.output), expectedNotVerbose)
106 # check verbose output:
107 result = runner.invoke(cli, ["query-dataset-types", "here", "--verbose"])
108 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
109 expected = AstropyTable(array((
110 "test",
111 "['band', 'instrument', 'physical_filter', 'visit_system', 'visit']",
112 storageClassName)),
113 names=("name", "dimensions", "storage class"))
114 self.assertAstropyTablesEqual(readTable(result.output), expected)
116 # Now remove and check that it was removed
117 # First a non-existent one
118 result = runner.invoke(cli, ["remove-dataset-type", "here", "unreal"])
119 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
121 # Now one we now has been registered
122 result = runner.invoke(cli, ["remove-dataset-type", "here", datasetName])
123 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
125 # and check that it has gone
126 result = runner.invoke(cli, ["query-dataset-types", "here"])
127 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
128 self.assertIn("No results", result.output)
131if __name__ == "__main__": 131 ↛ 132line 131 didn't jump to line 132, because the condition on line 131 was never true
132 unittest.main()