Coverage for tests/test_cliCmdQueryDataIds.py: 49%
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"""
25import os
26import unittest
28from astropy.table import Table as AstropyTable
29from lsst.daf.butler import Butler, script
30from lsst.daf.butler.tests.utils import ButlerTestHelper, MetricTestRepo, makeTestTempDir, removeTestTempDir
31from numpy import array
33TESTDIR = os.path.abspath(os.path.dirname(__file__))
36class QueryDataIdsTest(unittest.TestCase, ButlerTestHelper):
38 mockFuncName = "lsst.daf.butler.cli.cmd.commands.script.queryDataIds"
40 @staticmethod
41 def _queryDataIds(repo, dimensions=(), collections=(), datasets=None, where=None):
42 """Helper to populate the call to script.queryDataIds with default
43 values."""
44 return script.queryDataIds(
45 repo=repo,
46 dimensions=dimensions,
47 collections=collections,
48 datasets=datasets,
49 where=where,
50 order_by=None,
51 limit=0,
52 offset=0,
53 )
55 def setUp(self):
56 self.root = makeTestTempDir(TESTDIR)
57 self.repo = MetricTestRepo(
58 root=self.root, configFile=os.path.join(TESTDIR, "config/basic/butler.yaml")
59 )
61 def tearDown(self):
62 removeTestTempDir(self.root)
64 def testDimensions(self):
65 """Test getting a dimension."""
66 res = self._queryDataIds(self.root, dimensions=("visit",))
67 expected = AstropyTable(
68 array((("R", "DummyCamComp", "d-r", 1, 423), ("R", "DummyCamComp", "d-r", 1, 424))),
69 names=("band", "instrument", "physical_filter", "visit_system", "visit"),
70 )
71 self.assertAstropyTablesEqual(res, expected)
73 def testNull(self):
74 "Test asking for nothing."
75 res = self._queryDataIds(self.root)
76 self.assertEqual(res, None)
78 def testWhere(self):
79 """Test with a WHERE constraint."""
80 res = self._queryDataIds(
81 self.root, dimensions=("visit",), where="instrument='DummyCamComp' AND visit=423"
82 )
83 expected = AstropyTable(
84 array((("R", "DummyCamComp", "d-r", 1, 423),)),
85 names=("band", "instrument", "physical_filter", "visit_system", "visit"),
86 )
87 self.assertAstropyTablesEqual(res, expected)
89 def testDatasetsAndCollections(self):
90 """Test constraining via datasets and collections."""
92 # Add a dataset in a different collection
93 self.butler = Butler(self.root, run="foo")
94 self.repo.butler.registry.insertDimensionData(
95 "visit",
96 {
97 "instrument": "DummyCamComp",
98 "id": 425,
99 "name": "fourtwentyfive",
100 "physical_filter": "d-r",
101 "visit_system": 1,
102 },
103 )
104 self.repo.addDataset(dataId={"instrument": "DummyCamComp", "visit": 425}, run="foo")
106 # Verify the new dataset is not found in the "ingest/run" collection.
107 res = self._queryDataIds(
108 repo=self.root, dimensions=("visit",), collections=("ingest/run",), datasets="test_metric_comp"
109 )
110 expected = AstropyTable(
111 array((("R", "DummyCamComp", "d-r", 1, 423), ("R", "DummyCamComp", "d-r", 1, 424))),
112 names=("band", "instrument", "physical_filter", "visit_system", "visit"),
113 )
114 self.assertAstropyTablesEqual(res, expected)
116 # Verify the new dataset is found in the "foo" collection.
117 res = self._queryDataIds(
118 repo=self.root, dimensions=("visit",), collections=("foo",), datasets="test_metric_comp"
119 )
120 expected = AstropyTable(
121 array((("R", "DummyCamComp", "d-r", 1, 425),)),
122 names=("band", "instrument", "physical_filter", "visit_system", "visit"),
123 )
124 self.assertAstropyTablesEqual(res, expected)
127if __name__ == "__main__": 127 ↛ 128line 127 didn't jump to line 128, because the condition on line 127 was never true
128 unittest.main()