Coverage for tests/test_cliCmdQueryDataIds.py: 47%

41 statements  

« prev     ^ index     » next       coverage.py v7.2.5, created at 2023-05-02 18:18 -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/>. 

21 

22"""Unit tests for daf_butler CLI query-collections command. 

23""" 

24 

25import os 

26import unittest 

27 

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 

32 

33TESTDIR = os.path.abspath(os.path.dirname(__file__)) 

34 

35 

36class QueryDataIdsTest(unittest.TestCase, ButlerTestHelper): 

37 mockFuncName = "lsst.daf.butler.cli.cmd.commands.script.queryDataIds" 

38 

39 @staticmethod 

40 def _queryDataIds(repo, dimensions=(), collections=(), datasets=None, where=None): 

41 """Helper to populate the call to script.queryDataIds with default 

42 values.""" 

43 return script.queryDataIds( 

44 repo=repo, 

45 dimensions=dimensions, 

46 collections=collections, 

47 datasets=datasets, 

48 where=where, 

49 order_by=None, 

50 limit=0, 

51 offset=0, 

52 ) 

53 

54 def setUp(self): 

55 self.root = makeTestTempDir(TESTDIR) 

56 self.repo = MetricTestRepo( 

57 root=self.root, configFile=os.path.join(TESTDIR, "config/basic/butler.yaml") 

58 ) 

59 

60 def tearDown(self): 

61 removeTestTempDir(self.root) 

62 

63 def testDimensions(self): 

64 """Test getting a dimension.""" 

65 res = self._queryDataIds(self.root, dimensions=("visit",)) 

66 expected = AstropyTable( 

67 array((("R", "DummyCamComp", "d-r", 423), ("R", "DummyCamComp", "d-r", 424))), 

68 names=("band", "instrument", "physical_filter", "visit"), 

69 ) 

70 self.assertAstropyTablesEqual(res, expected) 

71 

72 def testNull(self): 

73 "Test asking for nothing." 

74 res = self._queryDataIds(self.root) 

75 self.assertEqual(res, None) 

76 

77 def testWhere(self): 

78 """Test with a WHERE constraint.""" 

79 res = self._queryDataIds( 

80 self.root, dimensions=("visit",), where="instrument='DummyCamComp' AND visit=423" 

81 ) 

82 expected = AstropyTable( 

83 array((("R", "DummyCamComp", "d-r", 423),)), 

84 names=("band", "instrument", "physical_filter", "visit"), 

85 ) 

86 self.assertAstropyTablesEqual(res, expected) 

87 

88 def testDatasetsAndCollections(self): 

89 """Test constraining via datasets and collections.""" 

90 

91 # Add a dataset in a different collection 

92 self.butler = Butler(self.root, run="foo") 

93 self.repo.butler.registry.insertDimensionData( 

94 "visit", 

95 { 

96 "instrument": "DummyCamComp", 

97 "id": 425, 

98 "name": "fourtwentyfive", 

99 "physical_filter": "d-r", 

100 }, 

101 ) 

102 self.repo.addDataset(dataId={"instrument": "DummyCamComp", "visit": 425}, run="foo") 

103 

104 # Verify the new dataset is not found in the "ingest/run" collection. 

105 res = self._queryDataIds( 

106 repo=self.root, dimensions=("visit",), collections=("ingest/run",), datasets="test_metric_comp" 

107 ) 

108 expected = AstropyTable( 

109 array((("R", "DummyCamComp", "d-r", 423), ("R", "DummyCamComp", "d-r", 424))), 

110 names=("band", "instrument", "physical_filter", "visit"), 

111 ) 

112 self.assertAstropyTablesEqual(res, expected) 

113 

114 # Verify the new dataset is found in the "foo" collection. 

115 res = self._queryDataIds( 

116 repo=self.root, dimensions=("visit",), collections=("foo",), datasets="test_metric_comp" 

117 ) 

118 expected = AstropyTable( 

119 array((("R", "DummyCamComp", "d-r", 425),)), 

120 names=("band", "instrument", "physical_filter", "visit"), 

121 ) 

122 self.assertAstropyTablesEqual(res, expected) 

123 

124 

125if __name__ == "__main__": 125 ↛ 126line 125 didn't jump to line 126, because the condition on line 125 was never true

126 unittest.main()