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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

# This file is part of ctrl_mpexec. 

# 

# Developed for the LSST Data Management System. 

# This product includes software developed by the LSST Project 

# (https://www.lsst.org). 

# See the COPYRIGHT file at the top-level directory of this distribution 

# for details of code ownership. 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 3 of the License, or 

# (at your option) any later version. 

# 

# This program is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

# You should have received a copy of the GNU General Public License 

# along with this program. If not, see <https://www.gnu.org/licenses/>. 

 

"""Simple unit test for parser module. 

""" 

 

from argparse import ArgumentParser 

import unittest 

 

import lsst.utils.tests 

import lsst.ctrl.mpexec.cmdLineParser as parser_mod 

 

 

class _Error(Exception): 

pass 

 

 

class _NoExitParser(ArgumentParser): 

"""Special parser subclass which does not exit on errors or help. 

""" 

 

def exit(self, status=0, message=None): 

pass 

 

def error(self, message): 

raise _Error(message) 

 

 

class CmdLineParserTestCase(unittest.TestCase): 

"""A test case for parser module 

""" 

 

def setUp(self): 

pass 

 

def tearDown(self): 

pass 

 

def testPipelineAction(self): 

"""Test for a _PipelineAction and _pipe_action 

""" 

 

parser = _NoExitParser() 

parser.add_argument("-t", dest="pipeline_actions", action='append', 

type=parser_mod._ACTION_ADD_TASK) 

 

PipelineAction = parser_mod._PipelineAction 

args = parser.parse_args("-t task".split()) 

self.assertEqual(args.pipeline_actions, [PipelineAction("new_task", None, "task")]) 

 

args = parser.parse_args("-t task:label".split()) 

self.assertEqual(args.pipeline_actions, [PipelineAction("new_task", "label", "task")]) 

 

args = parser.parse_args("-t task".split()) 

self.assertEqual(args.pipeline_actions, [PipelineAction("new_task", None, "task")]) 

 

# something that is not syntaxically correct (not a literal) 

with self.assertRaises(_Error): 

parser.parse_args("-t task -s task:CannotEval".split()) 

 

# Literal but of the wrong type 

with self.assertRaises(_Error): 

parser.parse_args("-t task -s task:[1,2]".split()) 

 

# dictionary but not all strings 

with self.assertRaises(_Error): 

parser.parse_args("-t task -s task:{'x':1}".split()) 

 

def testInputCollectionType(self): 

"""Test for a _inputCollectionType 

""" 

 

parser = _NoExitParser() 

parser.add_argument("-i", dest="input", type=parser_mod._inputCollectionType, 

default={}) 

 

args = parser.parse_args("".split()) 

self.assertEqual(args.input, {}) 

 

args = parser.parse_args("-i coll".split()) 

self.assertEqual(args.input, {"": ["coll"]}) 

 

args = parser.parse_args("-i coll1,coll2,coll3".split()) 

self.assertEqual(args.input, {"": ["coll1", "coll2", "coll3"]}) 

 

args = parser.parse_args("-i ds:coll".split()) 

self.assertEqual(args.input, {"ds": ["coll"]}) 

 

args = parser.parse_args("-i ds1:coll1,ds2:coll2,ds2:coll3".split()) 

self.assertEqual(args.input, {"ds1": ["coll1"], 

"ds2": ["coll2", "coll3"]}) 

 

args = parser.parse_args("-i coll1,ds1:coll1,coll2,ds2:coll2,ds2:coll3,coll3".split()) 

self.assertEqual(args.input, {"": ["coll1", "coll2", "coll3"], 

"ds1": ["coll1"], 

"ds2": ["coll2", "coll3"]}) 

 

def testOutputCollectionType(self): 

"""Test for a _outputCollectionType 

""" 

 

parser = _NoExitParser() 

parser.add_argument("-o", dest="output", type=parser_mod._outputCollectionType, 

default={}) 

 

args = parser.parse_args("".split()) 

self.assertEqual(args.output, {}) 

 

args = parser.parse_args("-o coll".split()) 

self.assertEqual(args.output, {"": "coll"}) 

 

with self.assertRaises(_Error): 

args = parser.parse_args("-o coll1,coll2,coll3".split()) 

 

args = parser.parse_args("-o ds:coll".split()) 

self.assertEqual(args.output, {"ds": "coll"}) 

 

args = parser.parse_args("-o ds1:coll1,ds2:coll2,ds3:coll3".split()) 

self.assertEqual(args.output, {"ds1": "coll1", 

"ds2": "coll2", 

"ds3": "coll3"}) 

 

args = parser.parse_args("-o coll1,ds2:coll2".split()) 

self.assertEqual(args.output, {"": "coll1", 

"ds2": "coll2"}) 

 

with self.assertRaises(_Error): 

args = parser.parse_args("-o coll1,ds2:coll2,coll3".split()) 

 

def testCmdLineParser(self): 

"""Test for parser_mod.CmdLineParser 

""" 

parser = parser_mod.makeParser(parser_class=_NoExitParser) 

 

# this should result in error 

self.assertRaises(_Error, parser.parse_args) 

 

# know attributes to appear in parser output for all subcommands 

common_options = "loglevel longlog enableLsstDebug subcommand subparser".split() 

 

# test for the set of options defined in each command 

args = parser.parse_args( 

""" 

build -t cmd 

""".split()) 

build_options = """pipeline pipeline_actions order_pipeline 

save_pipeline pipeline_dot show""".split() 

self.assertEqual(set(vars(args).keys()), set(common_options + build_options)) 

self.assertEqual(args.subcommand, 'build') 

 

args = parser.parse_args( 

""" 

qgraph -t cmd 

""".split()) 

qgraph_options = build_options + """qgraph data_query butler_config 

input output skip_existing clobber_output 

save_qgraph qgraph_dot save_single_quanta""".split() 

self.assertEqual(set(vars(args).keys()), set(common_options + qgraph_options)) 

self.assertEqual(args.subcommand, 'qgraph') 

 

args = parser.parse_args( 

""" 

run -t taskname 

""".split()) 

run_options = qgraph_options + """register_dataset_types skip_init_writes 

init_only clobberConfig clobberVersions noBackupConfig noVersions 

processes profile timeout doraise""".split() 

self.assertEqual(set(vars(args).keys()), set(common_options + run_options)) 

self.assertEqual(args.subcommand, 'run') 

 

def testCmdLineTasks(self): 

 

parser = parser_mod.makeParser(parser_class=_NoExitParser) 

 

PipelineAction = parser_mod._PipelineAction 

 

# default options 

args = parser.parse_args( 

""" 

run -t taskname 

""".split()) 

self.assertFalse(args.clobberConfig) 

self.assertFalse(args.clobberVersions) 

self.assertFalse(args.enableLsstDebug) 

self.assertFalse(args.doraise) 

self.assertEqual(args.input, {}) 

self.assertEqual(args.loglevel, []) 

self.assertFalse(args.longlog) 

self.assertFalse(args.noBackupConfig) 

self.assertFalse(args.noVersions) 

self.assertEqual(args.output, {}) 

self.assertEqual(args.processes, 1) 

self.assertIsNone(args.profile) 

self.assertIsNone(args.timeout) 

self.assertEqual(args.pipeline_actions, [PipelineAction("new_task", None, "taskname")]) 

self.assertEqual(args.show, []) 

self.assertIsNotNone(args.subparser) 

self.assertIsNone(args.pipeline) 

 

# bunch of random options 

args = parser.parse_args( 

""" 

run 

--clobber-config 

--clobber-versions 

--debug 

--doraise 

--input inputColl 

--loglevel DEBUG -L component=trace 

--longlog 

--no-backup-config 

--no-versions 

--output outputColl 

-j 66 

--profile profile.out 

--timeout 10.10 

-t taskname:label 

--show config 

--show config=Task.* 

-c label:a=b 

-C label:filename1 

-c label:c=d -c label:e=f 

-C label:filename2 -C label:filename3 

--skip-existing 

""".split()) 

self.assertTrue(args.clobberConfig) 

self.assertTrue(args.clobberVersions) 

self.assertTrue(args.enableLsstDebug) 

self.assertTrue(args.doraise) 

self.assertEqual(args.input, {"": ["inputColl"]}) 

self.assertEqual(args.loglevel, [(None, 'DEBUG'), ('component', 'TRACE')]) 

self.assertTrue(args.longlog) 

self.assertTrue(args.noBackupConfig) 

self.assertTrue(args.noVersions) 

self.assertEqual(args.output, {"": "outputColl"}) 

self.assertEqual(args.processes, 66) 

self.assertEqual(args.profile, 'profile.out') 

self.assertEqual(args.timeout, 10.10) 

self.assertEqual(args.show, ['config', 'config=Task.*']) 

self.assertEqual(args.pipeline_actions, [PipelineAction("new_task", "label", "taskname"), 

PipelineAction("config", "label", "a=b"), 

PipelineAction("configfile", "label", "filename1"), 

PipelineAction("config", "label", "c=d"), 

PipelineAction("config", "label", "e=f"), 

PipelineAction("configfile", "label", "filename2"), 

PipelineAction("configfile", "label", "filename3")]) 

self.assertIsNone(args.pipeline) 

self.assertIsNone(args.qgraph) 

self.assertFalse(args.order_pipeline) 

self.assertIsNone(args.save_pipeline) 

self.assertIsNone(args.save_qgraph) 

self.assertIsNone(args.pipeline_dot) 

self.assertIsNone(args.qgraph_dot) 

self.assertTrue(args.skip_existing) 

 

# multiple tasks plus more options (-q should be exclusive with 

# some other options but we do not check it during parsing (yet)) 

args = parser.parse_args( 

""" 

run 

-p pipeline.yaml 

-g qgraph.pickle 

-t task1 

-t task2:label2 

-t task3 

-t task4 

--show config 

-c task1:a=b 

-C task1:filename1 

-c label2:c=d -c label2:e=f 

-C task3:filename2 -C task3:filename3 

--show config=Task.* 

-C task4:filename4 -c task4:x=y 

--order-pipeline 

--save-pipeline=newpipe.yaml 

--save-qgraph=newqgraph.pickle 

--pipeline-dot pipe.dot 

--qgraph-dot qgraph.dot 

""".split()) 

self.assertEqual(args.show, ['config', 'config=Task.*']) 

self.assertEqual(args.pipeline_actions, [PipelineAction("new_task", None, "task1"), 

PipelineAction("new_task", "label2", "task2"), 

PipelineAction("new_task", None, "task3"), 

PipelineAction("new_task", None, "task4"), 

PipelineAction("config", "task1", "a=b"), 

PipelineAction("configfile", "task1", "filename1"), 

PipelineAction("config", "label2", "c=d"), 

PipelineAction("config", "label2", "e=f"), 

PipelineAction("configfile", "task3", "filename2"), 

PipelineAction("configfile", "task3", "filename3"), 

PipelineAction("configfile", "task4", "filename4"), 

PipelineAction("config", "task4", "x=y")]) 

self.assertEqual(args.pipeline, "pipeline.yaml") 

self.assertEqual(args.qgraph, "qgraph.pickle") 

self.assertTrue(args.order_pipeline) 

self.assertEqual(args.save_pipeline, "newpipe.yaml") 

self.assertEqual(args.save_qgraph, "newqgraph.pickle") 

self.assertEqual(args.pipeline_dot, "pipe.dot") 

self.assertEqual(args.qgraph_dot, "qgraph.dot") 

 

# check that exclusive options generate error 

with self.assertRaises(_Error): 

parser.parse_args("qgraph -p pipeline.yaml --skip-existing --clobber-output".split()) 

 

def testCmdLinePipeline(self): 

 

parser = parser_mod.makeParser(parser_class=_NoExitParser) 

 

PipelineAction = parser_mod._PipelineAction 

 

args = parser.parse_args( 

""" 

run -p pipeline 

--show config 

--show config=Task.* 

""".split()) 

self.assertEqual(args.show, ['config', 'config=Task.*']) 

self.assertEqual(args.pipeline, 'pipeline') 

self.assertEqual(args.pipeline_actions, []) 

 

args = parser.parse_args("run -p pipeline -t task".split()) 

self.assertEqual(args.pipeline, 'pipeline') 

self.assertEqual(args.pipeline_actions, [PipelineAction("new_task", None, "task")]) 

 

 

class MyMemoryTestCase(lsst.utils.tests.MemoryTestCase): 

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

352 ↛ 353line 352 didn't jump to line 353, because the condition on line 352 was never trueif __name__ == "__main__": 

lsst.utils.tests.init() 

unittest.main()