Hide keyboard shortcuts

Hot-keys 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/>. 

21 

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

23""" 

24 

25import click 

26import unittest 

27import yaml 

28 

29from lsst.daf.butler import Butler, DatasetType, StorageClass 

30from lsst.daf.butler.cli.butler import cli 

31from lsst.daf.butler.cli.cmd import query_dataset_types 

32from lsst.daf.butler.cli.utils import clickResultMsg 

33from lsst.daf.butler.tests import CliCmdTestBase 

34 

35 

36class QueryDatasetTypesCmdTest(CliCmdTestBase, unittest.TestCase): 

37 

38 defaultExpected = dict(repo=None, 

39 verbose=False, 

40 glob=(), 

41 components=None) 

42 

43 command = query_dataset_types 

44 

45 def test_minimal(self): 

46 """Test only required parameters. 

47 """ 

48 self.run_test(["query-dataset-types", "here"], 

49 self.makeExpected(repo="here")) 

50 

51 def test_requiredMissing(self): 

52 """Test that if the required parameter is missing it fails""" 

53 self.run_missing(["query-dataset-types"], 'Error: Missing argument "REPO".') 

54 

55 def test_all(self): 

56 """Test all parameters.""" 

57 self.run_test(["query-dataset-types", "here", "--verbose", "foo*", "--components"], 

58 self.makeExpected(repo="here", verbose=True, glob=("foo*", ), components=True)) 

59 self.run_test(["query-dataset-types", "here", "--verbose", "foo*", "--no-components"], 

60 self.makeExpected(repo="here", verbose=True, glob=("foo*", ), components=False)) 

61 

62 

63class QueryDatasetTypesScriptTest(unittest.TestCase): 

64 

65 def testQueryDatasetTypes(self): 

66 self.maxDiff = None 

67 datasetName = "test" 

68 instrumentDimension = "instrument" 

69 visitDimension = "visit" 

70 storageClassName = "testDatasetType" 

71 expectedNotVerbose = {"datasetTypes": [datasetName]} 

72 runner = click.testing.CliRunner() 

73 with runner.isolated_filesystem(): 

74 butlerCfg = Butler.makeRepo("here") 

75 butler = Butler(butlerCfg, writeable=True) 

76 storageClass = StorageClass(storageClassName) 

77 butler.registry.storageClasses.registerStorageClass(storageClass) 

78 dimensions = butler.registry.dimensions.extract((instrumentDimension, visitDimension)) 

79 datasetType = DatasetType(datasetName, dimensions, storageClass) 

80 butler.registry.registerDatasetType(datasetType) 

81 # check not-verbose output: 

82 result = runner.invoke(cli, ["query-dataset-types", "here"]) 

83 self.assertEqual(result.exit_code, 0, clickResultMsg(result)) 

84 self.assertEqual(expectedNotVerbose, yaml.safe_load(result.output)) 

85 # check glob output: 

86 result = runner.invoke(cli, ["query-dataset-types", "here", "t*"]) 

87 self.assertEqual(result.exit_code, 0, clickResultMsg(result)) 

88 self.assertEqual(expectedNotVerbose, yaml.safe_load(result.output)) 

89 # check verbose output: 

90 result = runner.invoke(cli, ["query-dataset-types", "here", "--verbose"]) 

91 self.assertEqual(result.exit_code, 0, clickResultMsg(result)) 

92 response = yaml.safe_load(result.output) 

93 # output dimension names contain all required dimensions, more than 

94 # the registered dimensions, so verify the expected components 

95 # individually. 

96 self.assertEqual(response["datasetTypes"][0]["name"], datasetName) 

97 self.assertEqual(response["datasetTypes"][0]["storageClass"], storageClassName) 

98 self.assertIn(instrumentDimension, response["datasetTypes"][0]["dimensions"]) 

99 self.assertIn(visitDimension, response["datasetTypes"][0]["dimensions"]) 

100 

101 

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

103 unittest.main()