Coverage for tests/test_cliCmdQueryDataIds.py: 47%

41 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2022-12-02 02:01 +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/>. 

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 

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

39 

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 ) 

54 

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 ) 

60 

61 def tearDown(self): 

62 removeTestTempDir(self.root) 

63 

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", 423), ("R", "DummyCamComp", "d-r", 424))), 

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

70 ) 

71 self.assertAstropyTablesEqual(res, expected) 

72 

73 def testNull(self): 

74 "Test asking for nothing." 

75 res = self._queryDataIds(self.root) 

76 self.assertEqual(res, None) 

77 

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", 423),)), 

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

86 ) 

87 self.assertAstropyTablesEqual(res, expected) 

88 

89 def testDatasetsAndCollections(self): 

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

91 

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 }, 

102 ) 

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

104 

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

106 res = self._queryDataIds( 

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

108 ) 

109 expected = AstropyTable( 

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

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

112 ) 

113 self.assertAstropyTablesEqual(res, expected) 

114 

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

116 res = self._queryDataIds( 

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

118 ) 

119 expected = AstropyTable( 

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

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

122 ) 

123 self.assertAstropyTablesEqual(res, expected) 

124 

125 

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

127 unittest.main()