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

1import json 

2import os 

3import sys 

4 

5import pytest 

6 

7sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/../../../../") 

8from sdc_etl_libs.sdc_data_schema.schema_exceptions import (DataSchemaCriticalError, DataSchemaFailedValidation) 

9from sdc_etl_libs.sdc_data_schema.schema_validation import (ResultTypeStatuses, SchemaValidation) 

10 

11def test_validation__key_allowed_values__list__good_value(): 

12 """ 

13 Ensure that if a key has a set "allowed_values" and a "type" of "list" that a given value or list of values passes 

14 if they all match with allowed values listed out in "criteria". 

15 ################################################################################################################### 

16 # key_av_1 = {"required": False, "data_type": str, "allowed_values": {"type": "list", "criteria": ["cat", "dog"]}} # 

17 # key_av_2 = { # 

18 # "required": False, # 

19 # "data_type": list, # 

20 # "list_value_opts": {"data_type": str}, # 

21 # "allowed_values": {"type": "list", "criteria": ["cat", "dog"]} # 

22 # } # 

23 ################################################################################################################### 

24 """ 

25 

26 # Test single good value 

27 ep_schema_raw = """ 

28 { 

29 "type": "test", 

30 "tag": "main_source", 

31 "info": { 

32 "type": "test", 

33 "access": { 

34 "type": "test_allowed_values", 

35 "test_details": { 

36 "key_av_1": "dog" 

37 } 

38 }, 

39 "opts": { 

40 } 

41 } 

42 } 

43 """ 

44 

45 ep_schema = json.loads(ep_schema_raw) 

46 validation = SchemaValidation() 

47 schema = validation.validate_schema(ep_schema, validation_type_="endpoint") 

48 

49 assert validation.ttl_errors == 0 

50 assert { 

51 'status': 'ERROR', 

52 'reason': 'BAD_VALUE', 

53 'section': 'endpoint:info:access:test_details', 

54 'note': '"key_av_1" value of "dog" is not allowed. Check allowed values.' 

55 } not in validation.results 

56 

57 # Test list of good values 

58 ep_schema_raw = """ 

59 { 

60 "type": "test", 

61 "tag": "main_source", 

62 "info": { 

63 "type": "test", 

64 "access": { 

65 "type": "test_allowed_values", 

66 "test_details": { 

67 "key_av_2": ["dog", "cat"] 

68 } 

69 }, 

70 "opts": { 

71 } 

72 } 

73 } 

74 """ 

75 

76 ep_schema = json.loads(ep_schema_raw) 

77 validation = SchemaValidation() 

78 schema = validation.validate_schema(ep_schema, validation_type_="endpoint") 

79 

80 assert validation.ttl_errors == 0 

81 assert { 

82 'status': 'ERROR', 

83 'reason': 'BAD_VALUE', 

84 'section': 'endpoint:info:access:test_details', 

85 'note': 'Value "dog" within list "key_av_2" is not allowed. Check allowed values.' 

86 } not in validation.results 

87 assert { 

88 'status': 'ERROR', 

89 'reason': 'BAD_VALUE', 

90 'section': 'endpoint:info:access:test_details', 

91 'note': 'Value "cat" within list "key_av_2" is not allowed. Check allowed values.' 

92 } not in validation.results 

93 

94 

95def test_validation__key_allowed_values__list__bad_value(): 

96 """ 

97 Ensure that if a key has a set "allowed_values" and a "type" of "list" that a given value or list of values records 

98 an ERROR and DataSchemaFailedValidation is raised if a value is invalid according to the allowed values in "criteria". 

99 ################################################################################################################### 

100 # key_av_1 = {"required": False, "data_type": str, "allowed_values": {"type": "list", "criteria": ["cat", "dog"]}} # 

101 # key_av_2 = { # 

102 # "required": False, # 

103 # "data_type": list, # 

104 # "list_value_opts": {"data_type": str}, # 

105 # "allowed_values": {"type": "list", "criteria": ["cat", "dog"]} # 

106 # } # 

107 ################################################################################################################### 

108 """ 

109 

110 # Test a single bad value 

111 ep_schema_raw = """ 

112 { 

113 "type": "test", 

114 "tag": "main_source", 

115 "info": { 

116 "type": "test", 

117 "access": { 

118 "type": "test_allowed_values", 

119 "test_details": { 

120 "key_av_1": "rabbit" 

121 } 

122 }, 

123 "opts": { 

124 } 

125 } 

126 } 

127 """ 

128 

129 ep_schema = json.loads(ep_schema_raw) 

130 validation = SchemaValidation() 

131 

132 with pytest.raises(DataSchemaFailedValidation): 

133 schema = validation.validate_schema(ep_schema, validation_type_="endpoint") 

134 

135 assert { 

136 'status': 'ERROR', 

137 'reason': 'BAD_VALUE', 

138 'section': 'endpoint:info:access:test_details', 

139 'note': '"key_av_1" value of "rabbit" is not allowed. Check allowed values.' 

140 } in validation.results 

141 

142 # Test a list that includes a bad value 

143 ep_schema_raw = """ 

144 { 

145 "type": "test", 

146 "tag": "main_source", 

147 "info": { 

148 "type": "test", 

149 "access": { 

150 "type": "test_allowed_values", 

151 "test_details": { 

152 "key_av_2": ["cat", "rabbit"] 

153 } 

154 }, 

155 "opts": { 

156 } 

157 } 

158 } 

159 """ 

160 

161 ep_schema = json.loads(ep_schema_raw) 

162 validation = SchemaValidation() 

163 

164 with pytest.raises(DataSchemaFailedValidation): 

165 schema = validation.validate_schema(ep_schema, validation_type_="endpoint") 

166 

167 assert { 

168 'status': 'ERROR', 

169 'reason': 'BAD_VALUE', 

170 'section': 'endpoint:info:access:test_details', 

171 'note': 'Value "rabbit" within list "key_av_2" is not allowed. Check allowed values.' 

172 } in validation.results 

173 

174 

175def test_validation__key_allowed_values__list__critical_error(): 

176 """ 

177 Ensure that a DataSchemaCriticalError is raised if a key has a set "allowed_values" and "criteria" is not a list. 

178 ################################################################################################################### 

179 # key_av_3 = {"required": False, "data_type": str, "allowed_values": {"type": "list", "criteria": "dog"}} # 

180 ################################################################################################################### 

181 """ 

182 

183 ep_schema_raw = """ 

184 { 

185 "type": "test", 

186 "tag": "main_source", 

187 "info": { 

188 "type": "test", 

189 "access": { 

190 "type": "test_allowed_values", 

191 "test_details": { 

192 "key_av_3": "dog" 

193 } 

194 }, 

195 "opts": { 

196 } 

197 } 

198 } 

199 """ 

200 

201 ep_schema = json.loads(ep_schema_raw) 

202 validation = SchemaValidation() 

203 

204 with pytest.raises(DataSchemaCriticalError): 

205 schema = validation.validate_schema(ep_schema, validation_type_="endpoint") 

206 

207def test_validation__key__allowed_values__range__good_value(): 

208 """ 

209 Ensure that if a key has a set "allowed_values" and a "type" of "range" that a given value or list of values passes 

210 if they all fall within the range defined in "criteria". 

211 ######################################################################################################################### 

212 # key_av_4 = {"required": False, "data_type": int, "allowed_values": {"type": "range", "criteria": {"min": 0, "max": 5}}} # 

213 # key_av_5 = { # 

214 # "required": False, # 

215 # "data_type": list, # 

216 # "list_value_opts": {"data_type": str}, # 

217 # "allowed_values": {"type": "range", "criteria": {"min": 0, "max": 5}} # 

218 # } # 

219 ######################################################################################################################### 

220 """ 

221 

222 # Test on a single value within range 

223 ep_schema_raw = """ 

224 { 

225 "type": "test", 

226 "tag": "main_source", 

227 "info": { 

228 "type": "test", 

229 "access": { 

230 "type": "test_allowed_values", 

231 "test_details": { 

232 "key_av_4": 3 

233 } 

234 }, 

235 "opts": { 

236 } 

237 } 

238 } 

239 """ 

240 

241 ep_schema = json.loads(ep_schema_raw) 

242 validation = SchemaValidation() 

243 schema = validation.validate_schema(ep_schema, validation_type_="endpoint") 

244 

245 assert validation.ttl_errors == 0 

246 assert { 

247 'status': 'ERROR', 

248 'reason': 'BAD_VALUE', 

249 'section': 'endpoint:info:access:test_details', 

250 'note': '"key_av_4" value of "3" is below the minimum range of values. Check allowed value range.' 

251 } not in validation.results 

252 assert { 

253 'status': 'ERROR', 

254 'reason': 'BAD_VALUE', 

255 'section': 'endpoint:info:access:test_details', 

256 'note': '"key_av_4" value of "3" is above the maximum range of values. Check allowed value range.' 

257 } not in validation.results 

258 

259 # Test on a list of values within range 

260 ep_schema_raw = """ 

261 { 

262 "type": "test", 

263 "tag": "main_source", 

264 "info": { 

265 "type": "test", 

266 "access": { 

267 "type": "test_allowed_values", 

268 "test_details": { 

269 "key_av_5": [2, 4] 

270 } 

271 }, 

272 "opts": { 

273 } 

274 } 

275 } 

276 """ 

277 

278 ep_schema = json.loads(ep_schema_raw) 

279 validation = SchemaValidation() 

280 schema = validation.validate_schema(ep_schema, validation_type_="endpoint") 

281 

282 assert validation.ttl_errors == 0 

283 assert { 

284 'status': 'ERROR', 

285 'reason': 'BAD_VALUE', 

286 'section': 'endpoint:info:access:test_details', 

287 'note': 'Value "2" within list "key_av_5" is below the minimum range of values. Check allowed value range.' 

288 } not in validation.results 

289 assert { 

290 'status': 'ERROR', 

291 'reason': 'BAD_VALUE', 

292 'section': 'endpoint:info:access:test_details', 

293 'note': 'Value "2" within list "key_av_5" is above the maximum range of values. Check allowed value range.' 

294 } not in validation.results 

295 assert { 

296 'status': 'ERROR', 

297 'reason': 'BAD_VALUE', 

298 'section': 'endpoint:info:access:test_details', 

299 'note': 'Value "4" within list "key_av_5" is below the minimum range of values. Check allowed value range.' 

300 } not in validation.results 

301 assert { 

302 'status': 'ERROR', 

303 'reason': 'BAD_VALUE', 

304 'section': 'endpoint:info:access:test_details', 

305 'note': 'Value "4" within list "key_av_5" is above the maximum range of values. Check allowed value range.' 

306 } not in validation.results 

307 

308 # Test range is inclusive on lower bound 

309 ep_schema_raw = """ 

310 { 

311 "type": "test", 

312 "tag": "main_source", 

313 "info": { 

314 "type": "test", 

315 "access": { 

316 "type": "test_allowed_values", 

317 "test_details": { 

318 "key_av_4": 0 

319 } 

320 }, 

321 "opts": { 

322 } 

323 } 

324 } 

325 """ 

326 

327 ep_schema = json.loads(ep_schema_raw) 

328 validation = SchemaValidation() 

329 schema = validation.validate_schema(ep_schema, validation_type_="endpoint") 

330 

331 assert validation.ttl_errors == 0 

332 assert { 

333 'status': 'ERROR', 

334 'reason': 'BAD_VALUE', 

335 'section': 'endpoint:info:access:test_details', 

336 'note': '"key_av_4" value of "0" is below the minimum range of values. Check allowed value range.' 

337 } not in validation.results 

338 

339 # Test range is inclusive on upper bound 

340 ep_schema_raw = """ 

341 { 

342 "type": "test", 

343 "tag": "main_source", 

344 "info": { 

345 "type": "test", 

346 "access": { 

347 "type": "test_allowed_values", 

348 "test_details": { 

349 "key_av_4": 5 

350 } 

351 }, 

352 "opts": { 

353 } 

354 } 

355 } 

356 """ 

357 

358 ep_schema = json.loads(ep_schema_raw) 

359 validation = SchemaValidation() 

360 schema = validation.validate_schema(ep_schema, validation_type_="endpoint") 

361 

362 assert validation.ttl_errors == 0 

363 assert { 

364 'status': 'ERROR', 

365 'reason': 'BAD_VALUE', 

366 'section': 'endpoint:info:access:test_details', 

367 'note': '"key_av_4" value of "5" is above the maximum range of values. Check allowed value range.' 

368 } not in validation.results 

369 

370 

371def test_validation__key__allowed_values__range__bad_value(): 

372 """ 

373 Ensure that if a key has a set "allowed_values" and a "type" of "range" that a given value or list of values records 

374 an ERROR and DataSchemaFailedValidation is raised if a value is invalid according to the allowed range in "criteria". 

375 ######################################################################################################################### 

376 # key_av_4 = {"required": False, "data_type": int, "allowed_values": {"type": "range", "criteria": {"min": 0, "max": 5}}} # 

377 # key_av_5 = { # 

378 # "required": False, # 

379 # "data_type": list, # 

380 # "list_value_opts": {"data_type": str}, # 

381 # "allowed_values": {"type": "range", "criteria": {"min": 0, "max": 5}} # 

382 # } # 

383 ######################################################################################################################### 

384 """ 

385 

386 # Test that single value below lower bound returns an error 

387 ep_schema_raw = """ 

388 { 

389 "type": "test", 

390 "tag": "main_source", 

391 "info": { 

392 "type": "test", 

393 "access": { 

394 "type": "test_allowed_values", 

395 "test_details": { 

396 "key_av_4": -1 

397 } 

398 }, 

399 "opts": { 

400 } 

401 } 

402 } 

403 """ 

404 

405 ep_schema = json.loads(ep_schema_raw) 

406 validation = SchemaValidation() 

407 

408 with pytest.raises(DataSchemaFailedValidation): 

409 schema = validation.validate_schema(ep_schema, validation_type_="endpoint") 

410 

411 assert { 

412 'status': 'ERROR', 

413 'reason': 'BAD_VALUE', 

414 'section': 'endpoint:info:access:test_details', 

415 'note': '"key_av_4" value of "-1" is below the minimum range of values. Check allowed value range.' 

416 } in validation.results 

417 

418 # Test that single value above upper bound returns an error 

419 ep_schema_raw = """ 

420 { 

421 "type": "test", 

422 "tag": "main_source", 

423 "info": { 

424 "type": "test", 

425 "access": { 

426 "type": "test_allowed_values", 

427 "test_details": { 

428 "key_av_4": 6 

429 } 

430 }, 

431 "opts": { 

432 } 

433 } 

434 } 

435 """ 

436 

437 ep_schema = json.loads(ep_schema_raw) 

438 validation = SchemaValidation() 

439 

440 with pytest.raises(DataSchemaFailedValidation): 

441 schema = validation.validate_schema(ep_schema, validation_type_="endpoint") 

442 

443 assert { 

444 'status': 'ERROR', 

445 'reason': 'BAD_VALUE', 

446 'section': 'endpoint:info:access:test_details', 

447 'note': '"key_av_4" value of "6" is above the maximum range of values. Check allowed value range.' 

448 } in validation.results 

449 

450 # Test that list containing value below lower bound returns an error 

451 ep_schema_raw = """ 

452 { 

453 "type": "test", 

454 "tag": "main_source", 

455 "info": { 

456 "type": "test", 

457 "access": { 

458 "type": "test_allowed_values", 

459 "test_details": { 

460 "key_av_5": [-1, 0] 

461 } 

462 }, 

463 "opts": { 

464 } 

465 } 

466 } 

467 """ 

468 

469 ep_schema = json.loads(ep_schema_raw) 

470 validation = SchemaValidation() 

471 

472 with pytest.raises(DataSchemaFailedValidation): 

473 schema = validation.validate_schema(ep_schema, validation_type_="endpoint") 

474 

475 assert { 

476 'status': 'ERROR', 

477 'reason': 'BAD_VALUE', 

478 'section': 'endpoint:info:access:test_details', 

479 'note': 'Value "-1" within list "key_av_5" is below the minimum range of values. Check allowed value range.' 

480 } in validation.results 

481 

482 # Test that list containing value above upper bound returns an error 

483 ep_schema_raw = """ 

484 { 

485 "type": "test", 

486 "tag": "main_source", 

487 "info": { 

488 "type": "test", 

489 "access": { 

490 "type": "test_allowed_values", 

491 "test_details": { 

492 "key_av_5": [5, 6] 

493 } 

494 }, 

495 "opts": { 

496 } 

497 } 

498 } 

499 """ 

500 

501 ep_schema = json.loads(ep_schema_raw) 

502 validation = SchemaValidation() 

503 

504 with pytest.raises(DataSchemaFailedValidation): 

505 schema = validation.validate_schema(ep_schema, validation_type_="endpoint") 

506 

507 assert { 

508 'status': 'ERROR', 

509 'reason': 'BAD_VALUE', 

510 'section': 'endpoint:info:access:test_details', 

511 'note': 'Value "6" within list "key_av_5" is above the maximum range of values. Check allowed value range.' 

512 } in validation.results 

513 

514 

515def test_validation__key__allowed_values__range__critical_error(): 

516 """ 

517 Ensure that a DataSchemaCriticalError is raised if any of the following occur when a key has a set "allowed_values" 

518 and a "type" of "range": 

519 - "criteria" is not a dictionary 

520 - neither "min" or "max" is defined in "criteria" 

521 - "min" is greater than "max" 

522 - a non-numerical data_type is used (except for a list of numerical values) 

523 ################################################################################################################################# 

524 # key_av_6 = {"required": False, "data_type": int, "allowed_values": {"type": "range", "criteria": [0, 6]}} # 

525 # key_av_7 = {"required": False, "data_type": int, "allowed_values": {"type": "range", "criteria": {"minimum": 0, "maximum": 5}}} # 

526 # key_av_8 = {"required": False, "data_type": int, "allowed_values": {"type": "range", "criteria": {"min": 1, "max": -1}}} # 

527 # key_av_9 = {"required": False, "data_type": str, "allowed_values": {"type": "range", "criteria": {"min": 0, "max": 5}}} # 

528 # key_av_10 = { # 

529 # "required": False, # 

530 # "data_type": list, # 

531 # "list_value_opts": {"data_type": str}, # 

532 # "allowed_values": {"type": "range", "criteria": {"min": 0, "max": 5}} # 

533 # } # 

534 ################################################################################################################################# 

535 """ 

536 

537 # Test that not having "criteria" as a dictionary will raise a critical error 

538 ep_schema_raw = """ 

539 { 

540 "type": "test", 

541 "tag": "main_source", 

542 "info": { 

543 "type": "test", 

544 "access": { 

545 "type": "test_allowed_values", 

546 "test_details": { 

547 "key_av_6": 3 

548 } 

549 }, 

550 "opts": { 

551 } 

552 } 

553 } 

554 """ 

555 

556 ep_schema = json.loads(ep_schema_raw) 

557 validation = SchemaValidation() 

558 

559 with pytest.raises(DataSchemaCriticalError): 

560 schema = validation.validate_schema(ep_schema, validation_type_="endpoint") 

561 

562 # Test that having neither "min" or "max" defined will raise a critical error 

563 ep_schema_raw = """ 

564 { 

565 "type": "test", 

566 "tag": "main_source", 

567 "info": { 

568 "type": "test", 

569 "access": { 

570 "type": "test_allowed_values", 

571 "test_details": { 

572 "key_av_7": 3 

573 } 

574 }, 

575 "opts": { 

576 } 

577 } 

578 } 

579 """ 

580 

581 ep_schema = json.loads(ep_schema_raw) 

582 validation = SchemaValidation() 

583 

584 with pytest.raises(DataSchemaCriticalError): 

585 schema = validation.validate_schema(ep_schema, validation_type_="endpoint") 

586 

587 # Test that having "min" greater than "max" will raise a critical error 

588 ep_schema_raw = """ 

589 { 

590 "type": "test", 

591 "tag": "main_source", 

592 "info": { 

593 "type": "test", 

594 "access": { 

595 "type": "test_allowed_values", 

596 "test_details": { 

597 "key_av_8": 0 

598 } 

599 }, 

600 "opts": { 

601 } 

602 } 

603 } 

604 """ 

605 

606 ep_schema = json.loads(ep_schema_raw) 

607 validation = SchemaValidation() 

608 

609 with pytest.raises(DataSchemaCriticalError): 

610 schema = validation.validate_schema(ep_schema, validation_type_="endpoint") 

611 

612 # Test that a non-numerical data type will raise a critical error 

613 ep_schema_raw = """ 

614 { 

615 "type": "test", 

616 "tag": "main_source", 

617 "info": { 

618 "type": "test", 

619 "access": { 

620 "type": "test_allowed_values", 

621 "test_details": { 

622 "key_av_9": "3" 

623 } 

624 }, 

625 "opts": { 

626 } 

627 } 

628 } 

629 """ 

630 

631 ep_schema = json.loads(ep_schema_raw) 

632 validation = SchemaValidation() 

633 

634 with pytest.raises(DataSchemaCriticalError): 

635 schema = validation.validate_schema(ep_schema, validation_type_="endpoint") 

636 

637 # Test that a non-numerical "data_type" in "list_value_opts" will raise a critical error 

638 ep_schema_raw = """ 

639 { 

640 "type": "test", 

641 "tag": "main_source", 

642 "info": { 

643 "type": "test", 

644 "access": { 

645 "type": "test_allowed_values", 

646 "test_details": { 

647 "key_av_10": ["3", "4"] 

648 } 

649 }, 

650 "opts": { 

651 } 

652 } 

653 } 

654 """ 

655 

656 ep_schema = json.loads(ep_schema_raw) 

657 validation = SchemaValidation() 

658 

659 with pytest.raises(DataSchemaCriticalError): 

660 schema = validation.validate_schema(ep_schema, validation_type_="endpoint") 

661 

662 

663def test_validation__key__allowed_values__regex__good_value(): 

664 """ 

665 Ensure that if a key has a set "allowed_values" and a "type" of "regex" that a given value or list of values passes 

666 if they all match the regex pattern defined in "criteria". 

667 ##################################################################################################################### 

668 # key_av_11 = {"required": False, "data_type": str, "allowed_values": {"type": "regex", "criteria": "query(?!\.sql)"}} # 

669 # key_av_12 = { # 

670 # "required": False, # 

671 # "data_type": list, # 

672 # "list_value_opts": {"data_type": str}, # 

673 # "allowed_values": {"type": "regex", "criteria": "query(?!\.sql)"} # 

674 # } # 

675 ##################################################################################################################### 

676 """ 

677 

678 # Test on a single good value 

679 ep_schema_raw = """ 

680 { 

681 "type": "test", 

682 "tag": "main_source", 

683 "info": { 

684 "type": "test", 

685 "access": { 

686 "type": "test_allowed_values", 

687 "test_details": { 

688 "key_av_11": "events_query" 

689 } 

690 }, 

691 "opts": { 

692 } 

693 } 

694 } 

695 """ 

696 

697 ep_schema = json.loads(ep_schema_raw) 

698 validation = SchemaValidation() 

699 schema = validation.validate_schema(ep_schema, validation_type_="endpoint") 

700 

701 assert validation.ttl_errors == 0 

702 assert { 

703 'status': 'ERROR', 

704 'reason': 'BAD_VALUE', 

705 'section': 'endpoint:info:access:test_details', 

706 'note': '"key_av_11" value of "events_query" does not match the given regex pattern. Check allowed regex pattern.' 

707 } not in validation.results 

708 

709 # Test on list of good values 

710 ep_schema_raw = """ 

711 { 

712 "type": "test", 

713 "tag": "main_source", 

714 "info": { 

715 "type": "test", 

716 "access": { 

717 "type": "test_allowed_values", 

718 "test_details": { 

719 "key_av_12": ["events_query", "orders_query"] 

720 } 

721 }, 

722 "opts": { 

723 } 

724 } 

725 } 

726 """ 

727 

728 ep_schema = json.loads(ep_schema_raw) 

729 validation = SchemaValidation() 

730 schema = validation.validate_schema(ep_schema, validation_type_="endpoint") 

731 

732 assert validation.ttl_errors == 0 

733 assert { 

734 'status': 'ERROR', 

735 'reason': 'BAD_VALUE', 

736 'section': 'endpoint:info:access:test_details', 

737 'note': 'Value "events_query" within list "key_av_12" does not match the given regex pattern. Check allowed regex pattern.' 

738 } not in validation.results 

739 assert { 

740 'status': 'ERROR', 

741 'reason': 'BAD_VALUE', 

742 'section': 'endpoint:info:access:test_details', 

743 'note': 'Value "orders_query" within list "key_av_12" does not match the given regex pattern. Check allowed regex pattern.' 

744 } not in validation.results 

745 

746 

747def test_validation__key__allowed_values__regex__bad_value(): 

748 """ 

749 Ensure that if a key has a set "allowed_values" and a "type" of "regex" that a given value or list of values records 

750 an ERROR and DataSchemaFailedValidation is raised if a value is invalid according to the regex pattern in "criteria". 

751 ##################################################################################################################### 

752 # key_av_11 = {"required": False, "data_type": str, "allowed_values": {"type": "regex", "criteria": "query(?!\.sql)"}} # 

753 # key_av_12 = { # 

754 # "required": False, # 

755 # "data_type": list, # 

756 # "list_value_opts": {"data_type": str}, # 

757 # "allowed_values": {"type": "regex", "criteria": "query(?!\.sql)"} # 

758 # } # 

759 ##################################################################################################################### 

760 """ 

761 

762 # Test on a single bad value 

763 ep_schema_raw = """ 

764 { 

765 "type": "test", 

766 "tag": "main_source", 

767 "info": { 

768 "type": "test", 

769 "access": { 

770 "type": "test_allowed_values", 

771 "test_details": { 

772 "key_av_11": "events_query.sql" 

773 } 

774 }, 

775 "opts": { 

776 } 

777 } 

778 } 

779 """ 

780 

781 ep_schema = json.loads(ep_schema_raw) 

782 validation = SchemaValidation() 

783 

784 with pytest.raises(DataSchemaFailedValidation): 

785 schema = validation.validate_schema(ep_schema, validation_type_="endpoint") 

786 

787 assert { 

788 'status': 'ERROR', 

789 'reason': 'BAD_VALUE', 

790 'section': 'endpoint:info:access:test_details', 

791 'note': '"key_av_11" value of "events_query.sql" does not match the given regex pattern. Check allowed regex pattern.' 

792 } in validation.results 

793 

794 # Test on a list containing a bad value 

795 ep_schema_raw = """ 

796 { 

797 "type": "test", 

798 "tag": "main_source", 

799 "info": { 

800 "type": "test", 

801 "access": { 

802 "type": "test_allowed_values", 

803 "test_details": { 

804 "key_av_12": ["orders_query", "events_query.sql"] 

805 } 

806 }, 

807 "opts": { 

808 } 

809 } 

810 } 

811 """ 

812 

813 ep_schema = json.loads(ep_schema_raw) 

814 validation = SchemaValidation() 

815 

816 with pytest.raises(DataSchemaFailedValidation): 

817 schema = validation.validate_schema(ep_schema, validation_type_="endpoint") 

818 

819 assert { 

820 'status': 'ERROR', 

821 'reason': 'BAD_VALUE', 

822 'section': 'endpoint:info:access:test_details', 

823 'note': 'Value "events_query.sql" within list "key_av_12" does not match the given regex pattern. Check allowed regex pattern.' 

824 } in validation.results 

825 

826 

827def test_validation__key__allowed_values__regex__critical_error(): 

828 """ 

829 Ensure that a DataSchemaCriticalError is raised if any of the following occur when a key has a set "allowed_values" 

830 and a "type" of "regex": 

831 - "criteria" is not a string 

832 - data_type is not a string (or list of strings) 

833 ################################################################################################################### 

834 # key_av_13 = {"required": False, "data_type": str, "allowed_values": {"type": "regex", "criteria": 5}} # 

835 # key_av_14 = {"required": False, "data_type": int, "allowed_values": {"type": "regex", "criteria": "5"}} # 

836 # key_av_15 = { # 

837 # "required": False, # 

838 # "data_type": list, # 

839 # "list_value_opts": {"data_type": int}, # 

840 # "allowed_values": {"type": "regex", "criteria": "5"} # 

841 # } # 

842 ################################################################################################################### 

843 """ 

844 

845 # Test that a non-string "criteria" will raise a critical error 

846 ep_schema_raw = """ 

847 { 

848 "type": "test", 

849 "tag": "main_source", 

850 "info": { 

851 "type": "test", 

852 "access": { 

853 "type": "test_allowed_values", 

854 "test_details": { 

855 "key_av_13": "#55" 

856 } 

857 }, 

858 "opts": { 

859 } 

860 } 

861 } 

862 """ 

863 

864 ep_schema = json.loads(ep_schema_raw) 

865 validation = SchemaValidation() 

866 

867 with pytest.raises(DataSchemaCriticalError): 

868 schema = validation.validate_schema(ep_schema, validation_type_="endpoint") 

869 

870 # Test that a non-string data type will raise a critical error 

871 ep_schema_raw = """ 

872 { 

873 "type": "test", 

874 "tag": "main_source", 

875 "info": { 

876 "type": "test", 

877 "access": { 

878 "type": "test_allowed_values", 

879 "test_details": { 

880 "key_av_14": 55 

881 } 

882 }, 

883 "opts": { 

884 } 

885 } 

886 } 

887 """ 

888 

889 ep_schema = json.loads(ep_schema_raw) 

890 validation = SchemaValidation() 

891 

892 with pytest.raises(DataSchemaCriticalError): 

893 schema = validation.validate_schema(ep_schema, validation_type_="endpoint") 

894 

895 # Test that a non-string "data_type" in "list_value_opts" will raise a critical error 

896 ep_schema_raw = """ 

897 { 

898 "type": "test", 

899 "tag": "main_source", 

900 "info": { 

901 "type": "test", 

902 "access": { 

903 "type": "test_allowed_values", 

904 "test_details": { 

905 "key_av_14": [55, 56] 

906 } 

907 }, 

908 "opts": { 

909 } 

910 } 

911 } 

912 """ 

913 

914 ep_schema = json.loads(ep_schema_raw) 

915 validation = SchemaValidation() 

916 

917 with pytest.raises(DataSchemaCriticalError): 

918 schema = validation.validate_schema(ep_schema, validation_type_="endpoint")