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

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

611

612

613

614

615

616

617

618

619

620

621

622

623

624

625

626

627

628

629

630

631

632

633

634

635

636

637

638

639

640

641

642

643

644

645

646

647

648

649

650

651

652

653

654

655

656

657

658

659

#!/usr/bin/env python 

# 

# LSST Data Management System 

# Copyright 2008, 2009, 2010 LSST Corporation. 

# 

# This product includes software developed by the 

# LSST Project (http://www.lsst.org/). 

# 

# 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 LSST License Statement and 

# the GNU General Public License along with this program. If not, 

# see <http://www.lsstcorp.org/LegalNotices/>. 

# 

from builtins import str 

import os 

import unittest 

 

import lsst.utils.tests 

 

from lsst.pex.policy import Policy, Dictionary, PolicyFile, DefaultPolicyFile 

from lsst.pex.policy import ValidationError, DictionaryError 

import lsst.pex.exceptions 

 

 

class DictionaryTestCase(unittest.TestCase): 

testDictDir = None 

 

def assertRaiseLCE(self, excClass, excMsg, callableObj, failMsg, *args, **kwargs): 

""" 

Expect callableObj(args, kwargs) to raise an exception of type excClass, 

and carres a message that contains excMsg. 

 

excClass: the subclass of LsstCppException we expect to see 

excMsg: a substring of the message it should carry 

callableObj: the thing that, when called, should raise an exception 

failMsg: the assertion message if this fails 

args, kwargs (optional): arguments to pass to callableObj 

""" 

try: 

callableObj(*args, **kwargs) 

except excClass as e: 

self.assertGreater(str(e).find(excMsg), 0, 

failMsg + ": expected to see the message \"" + excMsg + 

"\"; actual message was \"" + str(e) + "\".") 

else: 

self.fail(failMsg + ": did not raise " + excClass) 

 

def assertValidationError(self, errorCode, callableObj, field, value): 

try: 

callableObj(field, value) 

except ValidationError as e: 

self.assertEqual(e.getErrors(field), errorCode) 

 

def getTestDictionary(self, filename=None): 

if not self.testDictDir: 

pexPolicyDir = lsst.utils.getPackageDir('pex_policy') 

self.testDictDir = os.path.join(pexPolicyDir, "tests", "dictionary") 

return os.path.join(self.testDictDir, filename) if filename else self.testDictDir 

 

def testDictionaryLoad(self): 

d = Dictionary() 

df = PolicyFile(self.getTestDictionary("simple_dictionary.paf")) 

self.assertFalse(d.isDictionary(), "false positive dictionary") 

df.load(d) 

self.assertTrue(d.isDictionary(), "failed to recognize a dictionary") 

 

def testBadDictionary(self): 

d = Dictionary(self.getTestDictionary("dictionary_bad_policyfile.paf")) 

self.assertRaiseLCE(DictionaryError, "Illegal type: \"PolicyFile\"", 

d.makeDef("file_type").getType, 

"Dictionary specified PolicyFile type") 

 

d = Dictionary(self.getTestDictionary("dictionary_bad_unknown_type.paf")) 

self.assertRaiseLCE(DictionaryError, "Unknown type: \"NotAType\"", 

d.makeDef("something").getType, 

"Dictionary specifies unknown types") 

 

d = Dictionary(self.getTestDictionary("dictionary_bad_type_type.paf")) 

self.assertRaiseLCE(DictionaryError, "Expected string", 

d.makeDef("something").getType, 

"Expected string \"type\" type") 

 

self.assertRaiseLCE(DictionaryError, "property found at bad: min_occurs", 

Dictionary, 

"Dictionary has mispelled keyword \"min_occurs\".", 

self.getTestDictionary("dictionary_bad_keyword.paf")) 

 

dbmd = self.getTestDictionary("dictionary_bad_multiple_definitions.paf") 

self.assertRaiseLCE(DictionaryError, "expected a single", 

Dictionary, 

"Dictionary has two 'definitions' sections", 

dbmd) 

 

p = Policy(self.getTestDictionary("values_policy_good_1.paf")) 

d = Dictionary(self.getTestDictionary("dictionary_bad_multiple_min.paf")) 

self.assertRaiseLCE(DictionaryError, "Min value for int_ra", d.validate, 

"Two mins specified.", p) 

d = Dictionary(self.getTestDictionary("dictionary_bad_multiple_max.paf")) 

self.assertRaiseLCE(DictionaryError, "Max value for int_ra", d.validate, 

"Two maxes specified.", p) 

d = Dictionary(self.getTestDictionary("dictionary_bad_min_wrong_type.paf")) 

self.assertRaiseLCE(DictionaryError, 

"Wrong type for int_range_count_type min", 

d.validate, "Wrong min type.", p) 

d = Dictionary(self.getTestDictionary("dictionary_bad_max_wrong_type.paf")) 

self.assertRaiseLCE(DictionaryError, 

"Wrong type for int_range_count_type max", 

d.validate, "Wrong max type.", p) 

 

# conflict between minOccurs and maxOccurs 

d = Dictionary(self.getTestDictionary("conflict_occurs_dictionary.paf")) 

p = Policy(self.getTestDictionary("conflict_occurs_policy_1.paf")) 

ve = ValidationError("Dictionary_1.py", 1, "testBadDictionary") 

d.validate(p, ve.cpp) 

self.assertEqual(ve.getErrors("1to0"), ValidationError.TOO_MANY_VALUES) 

self.assertEqual(ve.getErrors("2to1"), ValidationError.NOT_AN_ARRAY) 

self.assertEqual(ve.getParamCount(), 2) 

p = Policy(self.getTestDictionary("conflict_occurs_policy_2.paf")) 

ve = ValidationError("Dictionary_1.py", 1, "testBadDictionary") 

d.validate(p, ve.cpp) 

self.assertEqual(ve.getErrors("1to0"), ValidationError.MISSING_REQUIRED) 

self.assertEqual(ve.getErrors("2to1"), ValidationError.TOO_MANY_VALUES) 

self.assertEqual(ve.getParamCount(), 2) 

 

def testSimpleValidate(self): 

d = Dictionary(self.getTestDictionary("simple_dictionary.paf")) 

p = Policy(self.getTestDictionary("simple_policy.paf")) 

ve = ValidationError("Dictionary_1.py", 0, "testSimpleValidate") 

d.validate(p, ve.cpp) 

self.assertEqual(ve.getErrors("name"), 0, "no errors in name") 

self.assertEqual(ve.getErrors("height"), 0, "no errors in height") 

self.assertEqual(ve.getErrors(), 0, "no errors overall") 

 

def testTypeValidation(self): 

d = Dictionary(self.getTestDictionary("types_dictionary.paf")) 

self.assertEqual(d.makeDef("undef_type").getType(), Policy.UNDEF, 

"UNDEF definition type") 

self.assertEqual(d.makeDef("bool_type").getType(), Policy.BOOL, 

"BOOL definition type") 

self.assertEqual(d.makeDef("int_type").getType(), Policy.INT, 

"INT definition type") 

self.assertEqual(d.makeDef("double_type").getType(), Policy.DOUBLE, 

"DOUBLE definition type") 

self.assertEqual(d.makeDef("string_type").getType(), Policy.STRING, 

"STRING definition type") 

self.assertEqual(d.makeDef("policy_type").getType(), Policy.POLICY, 

"POLICY definition type") 

self.assertEqual(d.makeDef("file_type").getType(), Policy.POLICY, 

"POLICY definition type (substituted for PolicyFile)") 

 

p = Policy(self.getTestDictionary("types_policy_good.paf")) 

 

ve = ValidationError("Dictionary_1.py", 0, "testTypeValidation") 

d.validate(p, ve.cpp) 

self.assertEqual(ve.getErrors("file_type"), ValidationError.NOT_LOADED, 

"require loadPolicyFiles before validating") 

self.assertEqual(ve.getErrors("undef_file"), ValidationError.NOT_LOADED, 

"require loadPolicyFiles before validating") 

self.assertEqual(ve.getErrors(), ValidationError.NOT_LOADED, "no other errors") 

self.assertEqual(ve.getParamCount(), 2, "no other errors") 

 

p.loadPolicyFiles(self.getTestDictionary(), True) 

ve = ValidationError("Dictionary_1.py", 0, "testTypeValidation") 

d.validate(p, ve.cpp) 

 

self.assertEqual(ve.getErrors("undef_type"), 0, "no errors with undef") 

self.assertEqual(ve.getErrors("int_type"), 0, "no errors with int") 

self.assertEqual(ve.getErrors("double_type"), 0, "no errors with double") 

self.assertEqual(ve.getErrors("bool_type"), 0, "no errors with bool") 

self.assertEqual(ve.getErrors("string_type"), 0, "no errors with string") 

self.assertEqual(ve.getErrors("policy_type"), 0, "no errors with policy") 

self.assertEqual(ve.getErrors("file_type"), 0, "no errors with file") 

self.assertEqual(ve.getErrors(), 0, "no errors overall") 

 

def testWrongType(self): 

d = Dictionary(self.getTestDictionary("types_dictionary.paf")) 

 

p = Policy(self.getTestDictionary("types_policy_bad_bool.paf")) 

ve = ValidationError("Dictionary_1.py", 0, "testWrongType") 

d.validate(p, ve.cpp) 

self.assertEqual(ve.getErrors(), ValidationError.WRONG_TYPE, "wrong type") 

self.assertEqual(ve.getParamCount(), 5, "number of errors") 

self.assertEqual(ve.getErrors("bool_type"), 0, "correct type") 

 

p = Policy(self.getTestDictionary("types_policy_bad_int.paf")) 

ve = ValidationError("Dictionary_1.py", 1, "testWrongType") 

d.validate(p, ve.cpp) 

self.assertEqual(ve.getErrors(), ValidationError.WRONG_TYPE, "wrong type") 

self.assertEqual(ve.getParamCount(), 5, "number of errors") 

self.assertEqual(ve.getErrors("int_type"), 0, "correct type") 

self.assertEqual(ve.getErrors("double_type"), ValidationError.WRONG_TYPE, 

"int can't pass as double") 

 

p = Policy(self.getTestDictionary("types_policy_bad_double.paf")) 

ve = ValidationError("Dictionary_1.py", 2, "testWrongType") 

d.validate(p, ve.cpp) 

self.assertEqual(ve.getErrors(), ValidationError.WRONG_TYPE, "wrong type") 

self.assertEqual(ve.getParamCount(), 5, "number of errors") 

self.assertEqual(ve.getErrors("double_type"), 0, "correct type") 

 

p = Policy(self.getTestDictionary("types_policy_bad_string.paf")) 

ve = ValidationError("Dictionary_1.py", 3, "testWrongType") 

d.validate(p, ve.cpp) 

self.assertEqual(ve.getErrors(), ValidationError.WRONG_TYPE, "wrong type") 

self.assertEqual(ve.getParamCount(), 5, "number of errors") 

self.assertEqual(ve.getErrors("string_type"), 0, "correct type") 

 

p = Policy(self.getTestDictionary("types_policy_bad_policy.paf")) 

ve = ValidationError("Dictionary_1.py", 4, "testWrongType") 

d.validate(p, ve.cpp) 

self.assertEqual(ve.getErrors(), ValidationError.WRONG_TYPE, "wrong type") 

self.assertEqual(ve.getParamCount(), 4, "number of errors") 

self.assertEqual(ve.getErrors("policy_type"), 0, "correct type") 

self.assertEqual(ve.getErrors("file_type"), 0, "correct type") 

 

p = Policy(self.getTestDictionary("types_policy_bad_file.paf")) 

ve = ValidationError("Dictionary_1.py", 5, "testWrongType") 

d.validate(p, ve.cpp) 

self.assertEqual(ve.getErrors(), ValidationError.NOT_LOADED, "not loaded") 

self.assertEqual(ve.getParamCount(), 6, "number of errors") 

self.assertEqual(ve.getErrors("bool_type"), ValidationError.NOT_LOADED, 

"not loaded") 

self.assertEqual(ve.getErrors("file_type"), ValidationError.NOT_LOADED, 

"not loaded") 

self.assertEqual(ve.getErrors("policy_type"), ValidationError.NOT_LOADED, 

"not loaded") 

p.loadPolicyFiles(self.getTestDictionary(), True) 

ve = ValidationError("Dictionary_1.py", 6, "testWrongType") 

d.validate(p, ve.cpp) 

self.assertEqual(ve.getErrors(), ValidationError.WRONG_TYPE, "wrong type") 

self.assertEqual(ve.getErrors("file_type"), 0, "correct type") 

self.assertEqual(ve.getErrors("policy_type"), 0, "correct type") 

self.assertEqual(ve.getParamCount(), 4, "wrong type") 

 

def testValues(self): 

d = Dictionary(self.getTestDictionary("values_dictionary.paf")) 

d.check() 

p = Policy(self.getTestDictionary("values_policy_good_1.paf")) 

ve = ValidationError("Dictionary_1.py", 0, "testValues") 

d.validate(p, ve.cpp) 

self.assertEqual(ve.getParamCount(), 0) 

 

# policy: disallow allowed, min, max 

p = Policy(self.getTestDictionary("values_policy_bad_policy_set.paf")) 

ve = ValidationError("Dictionary_1.py", 1, "testValues") 

d.validate(p, ve.cpp) 

self.assertEqual(ve.getErrors("policy_set_type"), 

ValidationError.VALUE_DISALLOWED) 

p = Policy(self.getTestDictionary("values_policy_bad_policy_max.paf")) 

ve = ValidationError("Dictionary_1.py", 2, "testValues") 

d.validate(p, ve.cpp) 

self.assertEqual(ve.getErrors("policy_max_type"), 

ValidationError.VALUE_OUT_OF_RANGE) 

p = Policy(self.getTestDictionary("values_policy_bad_policy_min.paf")) 

ve = ValidationError("Dictionary_1.py", 3, "testValues") 

d.validate(p, ve.cpp) 

self.assertEqual(ve.getErrors("policy_min_type"), 

ValidationError.VALUE_OUT_OF_RANGE) 

 

# minOccurs/maxOccurs 

p = Policy(self.getTestDictionary("values_policy_bad_occurs.paf")) 

ve = ValidationError("Dictionary_1.py", 1, "testValues") 

d.validate(p, ve.cpp) 

self.assertEqual(ve.getParamCount(), 6) 

self.assertEqual(ve.getErrors("bool_set_count_type"), 

ValidationError.TOO_MANY_VALUES) 

self.assertEqual(ve.getErrors("int_range_count_type"), 

ValidationError.MISSING_REQUIRED) 

self.assertEqual(ve.getErrors("double_range_count_type"), 

ValidationError.TOO_MANY_VALUES) 

self.assertEqual(ve.getErrors("string_count_type"), 

ValidationError.ARRAY_TOO_SHORT) 

self.assertEqual(ve.getErrors("disallowed"), 

ValidationError.TOO_MANY_VALUES) 

self.assertEqual(ve.getErrors("policy_count_type"), 

ValidationError.TOO_MANY_VALUES) 

 

# min 

p = Policy(self.getTestDictionary("values_policy_bad_min.paf")) 

ve = ValidationError("Dictionary_1.py", 1, "testValues") 

d.validate(p, ve.cpp) 

self.assertEqual(ve.getParamCount(), 4) 

self.assertEqual(ve.getErrors(), ValidationError.VALUE_OUT_OF_RANGE) 

self.assertEqual(ve.getErrors("string_count_type"), ValidationError.OK) 

self.assertEqual(ve.getErrors("policy_count_type"), ValidationError.OK) 

# max 

p = Policy(self.getTestDictionary("values_policy_bad_max.paf")) 

ve = ValidationError("Dictionary_1.py", 1, "testValues") 

d.validate(p, ve.cpp) 

self.assertEqual(ve.getParamCount(), 4) 

self.assertEqual(ve.getErrors(), ValidationError.VALUE_OUT_OF_RANGE) 

self.assertEqual(ve.getErrors("string_count_type"), ValidationError.OK) 

self.assertEqual(ve.getErrors("policy_count_type"), ValidationError.OK) 

 

# allowed 

p = Policy(self.getTestDictionary("values_policy_bad_allowed.paf")) 

ve = ValidationError("Dictionary_1.py", 1, "testValues") 

d.validate(p, ve.cpp) 

self.assertEqual(ve.getParamCount(), 4) 

self.assertEqual(ve.getErrors(), ValidationError.VALUE_DISALLOWED) 

self.assertEqual(ve.getErrors("int_range_count_type"), ValidationError.OK) 

self.assertEqual(ve.getErrors("string_count_type"), ValidationError.OK) 

self.assertEqual(ve.getErrors("policy_count_type"), ValidationError.OK) 

 

# allowed & min/max 

p = Policy(self.getTestDictionary("values_policy_bad_allowedminmax.paf")) 

ve = ValidationError("Dictionary_1.py", 1, "testValues") 

d.validate(p, ve.cpp) 

self.assertEqual(ve.getParamCount(), 2) 

self.assertEqual(ve.getErrors("int_range_set_type"), 

int(ValidationError.VALUE_DISALLOWED) + 

int(ValidationError.VALUE_OUT_OF_RANGE)) 

self.assertEqual(ve.getErrors("double_range_count_type"), 

int(ValidationError.TOO_MANY_VALUES) + 

int(ValidationError.VALUE_OUT_OF_RANGE)) 

ve = ValidationError("Dictionary_1.py", 1, "testValues") 

d.validate(p, ve.cpp) 

 

def testNested(self): 

self.assertRaiseLCE(DictionaryError, 

"policy_bad_subdef.dictionary is a string", 

Dictionary, "Malformed subdictionary", 

self.getTestDictionary("nested_dictionary_bad_1.paf")) 

 

p = Policy(self.getTestDictionary("nested_policy_good.paf")) 

self.assertRaiseLCE(DictionaryError, "Unknown Dictionary property", 

Dictionary, "Malformed subdictionary", 

self.getTestDictionary("nested_dictionary_bad_2.paf")) 

 

d = Dictionary(self.getTestDictionary("nested_dictionary_good.paf")) 

d.check() 

self.assertRaiseLCE(lsst.pex.exceptions.LogicError, "dictionaryFile needs to be loaded", 

d.validate, "dictionaryFile not loaded", p) 

self.assertFalse(d.hasSubDictionary("policy_1")) 

self.assertTrue(d.hasSubDictionary("policy_2")) 

self.assertFalse(d.hasSubDictionary("policy_load")) 

n = d.loadPolicyFiles(self.getTestDictionary(), True) 

self.assertTrue(d.hasSubDictionary("policy_load")) 

self.assertEqual(n, 1) # number of files loaded 

d.validate(p) 

 

ve = ValidationError("Dictionary_1.py", 1, "testNested") 

p = Policy(self.getTestDictionary("nested_policy_bad.paf")) 

d.validate(p, ve.cpp) 

self.assertEqual(ve.getErrors("policy_1"), ValidationError.WRONG_TYPE) 

self.assertEqual(ve.getErrors("policy_2.foo"), 

ValidationError.VALUE_DISALLOWED) 

self.assertEqual(ve.getErrors("policy_2.bar"), 

ValidationError.MISSING_REQUIRED) 

self.assertEqual(ve.getErrors("policy_3.baz.qux"), 

ValidationError.WRONG_TYPE) 

self.assertEqual(ve.getErrors("policy_3.baz.paisley"), 

ValidationError.MISSING_REQUIRED) 

self.assertEqual(ve.getErrors("policy_3.baz.paisley"), 

ValidationError.MISSING_REQUIRED) 

self.assertEqual(ve.getErrors("policy_load.height"), 

ValidationError.MISSING_REQUIRED) 

self.assertEqual(ve.getParamCount(), 6) 

 

# multiple nesting 

p = Policy(self.getTestDictionary("nested_policy_1.paf")) 

n = p.loadPolicyFiles(self.getTestDictionary()) 

self.assertEqual(n, 3) 

self.assertEqual(p.getString("1.2b.foo"), "bar") 

 

d = Dictionary(self.getTestDictionary("nested_dictionary_1.paf")) 

n = d.loadPolicyFiles(self.getTestDictionary()) 

self.assertEqual(n, 3) 

p = Policy(True, d) # load from defaults 

self.assertEqual(p.getString("1.2a.foo"), "bar") 

self.assertEqual(p.getString("1.2b.foo"), "bar") 

 

# error in child 

d = Dictionary(self.getTestDictionary("nested_dictionary_bad_child.paf")) 

d.loadPolicyFiles(self.getTestDictionary()) 

# this should really be caught during loadPolicyFiles(), above 

self.assertRaiseLCE(DictionaryError, "Unknown type: \"NotAType\"", 

d.makeDef("sub.something").getType, 

"Loaded sub-dictionary specified a bogus type") 

 

def testChildDef(self): 

# simple 

d = Dictionary(self.getTestDictionary("childdef_simple_dictionary.paf")) 

p = Policy(self.getTestDictionary("childdef_simple_policy_good.paf")) 

d.validate(p) 

p = Policy(self.getTestDictionary("childdef_simple_policy_bad.paf")) 

ve = ValidationError("Dictionary_1.py", 1, "testChildDef") 

d.validate(p, ve.cpp) 

self.assertEqual(ve.getErrors("baz"), ValidationError.WRONG_TYPE) 

 

# multiple childDefs (DictionaryError) 

d = Dictionary(self.getTestDictionary("childdef_dictionary_bad_multiple.paf")) 

self.assertRaiseLCE(DictionaryError, "Multiple childDef", 

d.validate, "Dictionary specifies unknown types", p) 

 

# complex 

d = Dictionary(self.getTestDictionary("childdef_complex_dictionary.paf")) 

p = Policy(self.getTestDictionary("childdef_complex_policy_good_1.paf")) 

d.validate(p) 

p = Policy(self.getTestDictionary("childdef_complex_policy_good_2.paf")) 

d.validate(p) 

p = Policy(self.getTestDictionary("childdef_complex_policy_bad_1.paf")) 

ve = ValidationError("Dictionary_1.py", 1, "testChildDef") 

d.validate(p, ve.cpp) 

self.assertEqual(ve.getErrors("joe"), ValidationError.NOT_AN_ARRAY) 

self.assertEqual(ve.getErrors("deb"), ValidationError.NOT_AN_ARRAY) 

self.assertEqual(ve.getErrors("bob"), ValidationError.NOT_AN_ARRAY) 

self.assertEqual(ve.getErrors("bob.bar"), ValidationError.NOT_AN_ARRAY) 

self.assertEqual(ve.getErrors("nested.helen.qux"), 

ValidationError.MISSING_REQUIRED) 

self.assertEqual(ve.getErrors("nested.marvin.rafael"), 

ValidationError.TOO_MANY_VALUES) 

self.assertEqual(ve.getErrors("disallowed.foo"), 

ValidationError.TOO_MANY_VALUES) 

self.assertEqual(ve.getParamCount(), 7) 

 

def testDefaults(self): 

p = Policy.createPolicy(self.getTestDictionary("defaults_dictionary_good.paf"), 

"", True) 

self.assertEqual(p.valueCount("bool_set_count"), 1) 

self.assertIs(p.getBool("bool_set_count"), True) 

self.assertEqual(p.valueCount("int_range_count"), 3) 

self.assertEqual(p.getDouble("deep.sub_double"), 1.) 

self.assertEqual(p.get("indirect4.string_type"), "foo") 

 

try: 

p = Policy.createPolicy(self.getTestDictionary("defaults_dictionary_bad_1.paf"), 

"", True) 

except ValidationError as ve: 

self.assertEqual(ve.getErrors("double"), 

ValidationError.WRONG_TYPE) 

self.assertEqual(ve.getErrors("int_range_count"), 

ValidationError.NOT_AN_ARRAY) 

self.assertEqual(ve.getErrors("bool_set_count"), 

ValidationError.TOO_MANY_VALUES) 

self.assertEqual(ve.getErrors("deep.sub_double"), 

ValidationError.WRONG_TYPE) 

self.assertEqual(ve.getParamCount(), 4) 

 

# test setting and adding when created with a dictionary 

def testSetAdd(self): 

p = Policy.createPolicy(self.getTestDictionary("defaults_dictionary_good.paf"), 

"", True) 

self.assertTrue(p.canValidate()) 

self.assertValidationError(ValidationError.TOO_MANY_VALUES, 

p.add, "bool_set_count", True) 

self.assertEqual(p.valueCount("bool_set_count"), 1) 

self.assertValidationError(ValidationError.VALUE_DISALLOWED, 

p.set, "bool_set_count", False) 

self.assertIs(p.getBool("bool_set_count"), True) 

p.set("int_range_count", -7) 

self.assertValidationError(ValidationError.VALUE_OUT_OF_RANGE, 

p.add, "int_range_count", 10) 

# add & set don't check against minOccurs, but validate() does 

try: 

p.validate() 

except ValidationError as ve: 

self.assertEqual(ve.getErrors("int_range_count"), 

ValidationError.NOT_AN_ARRAY) 

self.assertEqual(ve.getErrors("required"), 

ValidationError.MISSING_REQUIRED) 

p.add("int_range_count", -8) 

p.set("required", "foo") 

p.validate() 

 

def testSelfValidation(self): 

# assign a dictionary after creation 

p = Policy(self.getTestDictionary("types_policy_good.paf")) 

p.loadPolicyFiles(self.getTestDictionary(), True) 

typesDict = Dictionary(self.getTestDictionary("types_dictionary.paf")) 

valuesDict = Dictionary(self.getTestDictionary("values_dictionary.paf")) 

self.assertFalse(p.canValidate()) 

p.setDictionary(typesDict) 

self.assertTrue(p.canValidate()) 

p.validate() 

p.set("bool_type", True) 

self.assertValidationError( 

ValidationError.WRONG_TYPE, p.set, "bool_type", "foo") 

 

# create with dictionary 

p = Policy.createPolicy(self.getTestDictionary("types_dictionary.paf"), "", True) 

self.assertTrue(p.canValidate()) 

p.set("bool_type", True) 

self.assertValidationError( 

ValidationError.WRONG_TYPE, p.set, "bool_type", "foo") 

 

# assign a dictionary after invalid modifications 

p = Policy() 

p.set("bool_type", "foo") 

p.setDictionary(typesDict) 

ve = ValidationError("Dictionary_1.py", 1, "testSelfValidation") 

p.validate(ve.cpp) 

self.assertEqual(ve.getErrors("bool_type"), ValidationError.WRONG_TYPE) 

try: 

p.validate() 

except ValidationError as ve: 

self.assertEqual(ve.getErrors("bool_type"), ValidationError.WRONG_TYPE) 

self.assertEqual(ve.getParamCount(), 1) 

p.set("bool_type", True) 

p.set("int_type", 1) 

p.validate() 

 

# switch dictionaries 

oldD = p.getDictionary() 

p.setDictionary(valuesDict) 

try: 

p.validate() 

except ValidationError as ve: 

self.assertEqual(ve.getErrors("bool_type"), 

ValidationError.UNKNOWN_NAME) 

p.set("string_range_type", "moo") 

try: 

p.set("string_range_type", "victor") 

except ValidationError as ve: 

self.assertEqual(ve.getErrors("string_range_type"), 

ValidationError.VALUE_OUT_OF_RANGE) 

p.setDictionary(oldD) 

p.remove("string_range_type") 

p.validate() 

 

def testMergeDefaults(self): 

# from a non-trivial dictionary 

p = Policy(self.getTestDictionary("defaults_policy_partial.paf")) 

p.set("required", "foo") 

d = Dictionary(self.getTestDictionary("defaults_dictionary_good.paf")) 

d.loadPolicyFiles(self.getTestDictionary(), True) 

self.assertEqual(p.nameCount(), 2) 

p.mergeDefaults(d) 

self.assertEqual(p.valueCount("int_range_count"), 3) 

self.assertEqual(p.nameCount(), 7) 

 

# from a policy that's really a dictionary 

p = Policy() 

pd = Policy(self.getTestDictionary("defaults_dictionary_indirect.paf")) 

p.mergeDefaults(pd) 

self.assertEqual(p.getString("string_type"), "foo") 

self.assertTrue(p.getDictionary().isDictionary()) 

 

# from a policy that's really a non-trivial dictionary 

p = Policy(self.getTestDictionary("defaults_policy_partial.paf")) 

p.set("required", "foo") 

pd = Policy(self.getTestDictionary("defaults_dictionary_policy.paf")) 

pd.loadPolicyFiles(self.getTestDictionary(), True) 

self.assertEqual(p.nameCount(), 2) 

p.mergeDefaults(pd) 

self.assertEqual(p.valueCount("int_range_count"), 3) 

self.assertEqual(p.nameCount(), 5) 

 

# ensure post-load validation 

p.set("int_range_count", -5) 

self.assertValidationError(ValidationError.UNKNOWN_NAME, 

p.add, "unknown", 0) 

 

# test throwing validation 

p = Policy(self.getTestDictionary("defaults_policy_partial.paf")) 

try: 

p.mergeDefaults(pd) 

except ValidationError as ve: 

self.assertEqual(ve.getErrors("required"), 

ValidationError.MISSING_REQUIRED) 

 

# non-throwing validation 

p = Policy(self.getTestDictionary("defaults_policy_partial.paf")) 

ve = ValidationError("Dictionary_1.py", 1, "testMergeDefaults") 

p.mergeDefaults(pd, False, ve.cpp) 

self.assertEqual(ve.getErrors("required"), ValidationError.MISSING_REQUIRED) 

self.assertEqual(ve.getParamCount(), 1) 

 

# non-retention 

p = Policy(self.getTestDictionary("defaults_policy_partial.paf")) 

p.set("required", "foo") 

p.mergeDefaults(pd, False) 

# make sure validate() fails gracefully when no dictionary present 

self.assertRaiseLCE(DictionaryError, "No dictionary", 

p.validate, "No dictionary assigned") 

p.add("unknown", 0) # would be rejected if dictionary was kept 

 

# deep merge from a Policy that's not a Dictionary 

p = Policy(self.getTestDictionary("defaults_policy_partial.paf")) 

p.mergeDefaults(Policy(self.getTestDictionary("defaults_policy_most.paf"))) 

self.assertEqual(p.nameCount(), 3) 

self.assertIs(p.getBool("bool_set_count"), True) 

self.assertEqual(p.getString("indirect.string_type"), "bar") 

 

# propagation of a Dictionary from one Policy to another via mergeDefaults 

d = Dictionary(self.getTestDictionary("defaults_dictionary_complete.paf")) 

d.loadPolicyFiles(self.getTestDictionary()) 

pEmpty = Policy() 

pEmpty.mergeDefaults(d) 

self.assertTrue(pEmpty.canValidate()) 

pPartial = Policy(self.getTestDictionary("defaults_policy_partial.paf")) 

pPartial.mergeDefaults(pEmpty) 

self.assertTrue(pPartial.canValidate(), "Dictionary handed off via mergeDefaults.") 

 

# test the sample code at http://dev.lsstcorp.org/trac/wiki/PolicyHowto 

def testSampleCode(self): 

policyFile = DefaultPolicyFile("pex_policy", "defaults_dictionary_complete.paf", 

"tests/dictionary") 

defaults = Policy.createPolicy(policyFile, policyFile.getRepositoryPath(), True) 

policy = Policy() 

policy.mergeDefaults(defaults) 

self.assertTrue(policy.canValidate()) 

 

policy = Policy() 

policy.mergeDefaults(defaults, False) 

self.assertFalse(policy.canValidate()) 

 

# even if not keeping it around for future validation, validate the merge now 

policy = Policy() 

policy.set("bad", "worse") 

self.assertValidationError(ValidationError.UNKNOWN_NAME, 

policy.mergeDefaults, defaults, False) 

self.assertFalse(policy.canValidate()) 

 

# test operations on an empty sub-dictionary (ticket 1210) 

def testEmptySubdict(self): 

d = Dictionary(self.getTestDictionary("empty_subdictionary.paf")) 

p = Policy() 

p.set("empty_required", Policy(self.getTestDictionary("simple_policy.paf"))) 

p.mergeDefaults(d) 

self.assertEqual(p.get("empty_sub_with_default.foo"), "bar") 

p.setDictionary(d) 

# this works because there is a definition for "empty_sub_with_default.foo" 

p.set("empty_sub_with_default.foo", "baz") 

 

p2 = Policy() 

p2.set("foo", "baz") 

p.set("empty_sub_no_default", p2) 

# this fails because Policy tries to makeDef("empty_sub_no_default.foo") 

# which fails because there's only a definition for "empty_sub_no_default", 

# but it doesn't contain any sub-definitions 

# p.set("empty_sub_no_default.foo", "baz") 

self.assertRaiseLCE(DictionaryError, 

"empty_sub_no_default.dictionary not found", 

p.set, "Empty policy definition -- if this fails, " 

"it means a known bug has been fixed. That's good.", 

"empty_sub_no_default.foo", "baz") 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

lsst.utils.tests.init() 

unittest.main()