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 

25from astropy.table import Table as AstropyTable 

26from numpy import array 

27import unittest 

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

33from lsst.daf.butler.tests import CliCmdTestBase 

34from lsst.daf.butler.tests.utils import ButlerTestHelper, readTable 

35 

36 

37class QueryDatasetTypesCmdTest(CliCmdTestBase, unittest.TestCase): 

38 

39 mockFuncName = "lsst.daf.butler.cli.cmd.commands.script.queryDatasetTypes" 

40 

41 @staticmethod 

42 def defaultExpected(): 

43 return dict(repo=None, 

44 verbose=False, 

45 glob=(), 

46 components=None) 

47 

48 @staticmethod 

49 def command(): 

50 return query_dataset_types 

51 

52 def test_minimal(self): 

53 """Test only required parameters. 

54 """ 

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

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

57 

58 def test_requiredMissing(self): 

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

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

61 

62 def test_all(self): 

63 """Test all parameters.""" 

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

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

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

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

68 

69 

70class QueryDatasetTypesScriptTest(ButlerTestHelper, unittest.TestCase): 

71 

72 def testQueryDatasetTypes(self): 

73 self.maxDiff = None 

74 datasetName = "test" 

75 instrumentDimension = "instrument" 

76 visitDimension = "visit" 

77 storageClassName = "testDatasetType" 

78 expectedNotVerbose = AstropyTable((("test",),), names=("name",)) 

79 runner = LogCliRunner() 

80 with runner.isolated_filesystem(): 

81 butlerCfg = Butler.makeRepo("here") 

82 butler = Butler(butlerCfg, writeable=True) 

83 storageClass = StorageClass(storageClassName) 

84 butler.registry.storageClasses.registerStorageClass(storageClass) 

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

86 datasetType = DatasetType(datasetName, dimensions, storageClass) 

87 butler.registry.registerDatasetType(datasetType) 

88 # check not-verbose output: 

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

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

91 self.assertAstropyTablesEqual(readTable(result.output), expectedNotVerbose) 

92 # check glob output: 

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

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

95 self.assertAstropyTablesEqual(readTable(result.output), expectedNotVerbose) 

96 # check verbose output: 

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

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

99 expected = AstropyTable(array(( 

100 "test", 

101 "['band', 'instrument', 'physical_filter', 'visit_system', 'visit']", 

102 "testDatasetType")), 

103 names=("name", "dimensions", "storage class")) 

104 self.assertAstropyTablesEqual(readTable(result.output), expected) 

105 

106 # Now remove and check that it was removed 

107 # First a non-existent one 

108 result = runner.invoke(cli, ["remove-dataset-type", "here", "unreal"]) 

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

110 

111 # Now one we now has been registered 

112 result = runner.invoke(cli, ["remove-dataset-type", "here", datasetName]) 

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

114 

115 # and check that it has gone 

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

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

118 self.assertIn("No results", result.output) 

119 

120 

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

122 unittest.main()