Coverage for tests/test_cliCmdQueryDatasetTypes.py: 33%

63 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-02-22 03:05 -0800

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 unittest 

26 

27from astropy.table import Table as AstropyTable 

28from lsst.daf.butler.cli.butler import cli 

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

30from lsst.daf.butler.cli.utils import LogCliRunner, clickResultMsg 

31from lsst.daf.butler.tests import CliCmdTestBase 

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

33from numpy import array 

34 

35 

36class QueryDatasetTypesCmdTest(CliCmdTestBase, unittest.TestCase): 

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

38 

39 @staticmethod 

40 def defaultExpected(): 

41 return dict(repo=None, verbose=False, glob=(), components=None) 

42 

43 @staticmethod 

44 def command(): 

45 return query_dataset_types 

46 

47 def test_minimal(self): 

48 """Test only required parameters.""" 

49 self.run_test(["query-dataset-types", "here"], 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"], r"Error: Missing argument ['\"]REPO['\"].") 

54 

55 def test_all(self): 

56 """Test all parameters.""" 

57 self.run_test( 

58 ["query-dataset-types", "here", "--verbose", "foo*", "--components"], 

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

60 ) 

61 self.run_test( 

62 ["query-dataset-types", "here", "--verbose", "foo*", "--no-components"], 

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

64 ) 

65 

66 

67class QueryDatasetTypesScriptTest(ButlerTestHelper, unittest.TestCase): 

68 def testQueryDatasetTypes(self): 

69 self.maxDiff = None 

70 datasetName = "test" 

71 instrumentDimension = "instrument" 

72 visitDimension = "visit" 

73 storageClassName = "StructuredDataDict" 

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

75 runner = LogCliRunner() 

76 with runner.isolated_filesystem(): 

77 result = runner.invoke(cli, ["create", "here"]) 

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

79 # Create the dataset type. 

80 result = runner.invoke( 

81 cli, 

82 [ 

83 "register-dataset-type", 

84 "here", 

85 datasetName, 

86 storageClassName, 

87 instrumentDimension, 

88 visitDimension, 

89 ], 

90 ) 

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

92 # Okay to create it again identically. 

93 result = runner.invoke( 

94 cli, 

95 [ 

96 "register-dataset-type", 

97 "here", 

98 datasetName, 

99 storageClassName, 

100 instrumentDimension, 

101 visitDimension, 

102 ], 

103 ) 

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

105 # Not okay to create a different version of it. 

106 result = runner.invoke( 

107 cli, ["register-dataset-type", "here", datasetName, storageClassName, instrumentDimension] 

108 ) 

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

110 # Not okay to try to create a component dataset type. 

111 result = runner.invoke( 

112 cli, ["register-dataset-type", "here", "a.b", storageClassName, instrumentDimension] 

113 ) 

114 self.assertNotEqual(result.exit_code, 0, clickResultMsg(result)) 

115 # check not-verbose output: 

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

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

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

119 # check glob output: 

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

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

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

123 # check verbose output: 

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

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

126 expected = AstropyTable( 

127 array( 

128 ( 

129 "test", 

130 "['band', 'instrument', 'physical_filter', 'visit']", 

131 storageClassName, 

132 ) 

133 ), 

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

135 ) 

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

137 

138 # Now remove and check that it was removed 

139 # First a non-existent one 

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

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

142 

143 # Now one we now has been registered 

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

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

146 

147 # and check that it has gone 

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

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

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

151 

152 

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

154 unittest.main()