Pau Espin Pedrol | 3063730 | 2020-05-06 21:11:02 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | import _prep |
| 4 | |
| 5 | import sys |
| 6 | import os |
| 7 | import io |
| 8 | import pprint |
| 9 | import copy |
| 10 | |
| 11 | from osmo_gsm_tester.core import config, log, schema |
| 12 | |
| 13 | def val(which, test_schema): |
| 14 | try: |
| 15 | schema.validate(which, test_schema) |
| 16 | print('Validation: OK') |
| 17 | except ValueError: |
| 18 | log.log_exn() |
| 19 | print('Validation: Error') |
| 20 | |
| 21 | def get_case_list(dir): |
| 22 | li = [] |
| 23 | for f in os.listdir(dir): |
| 24 | if f.startswith('schema_case'): |
| 25 | li.append(f) |
| 26 | return sorted(li) |
| 27 | |
Pau Espin Pedrol | d79e719 | 2020-05-21 15:40:57 +0200 | [diff] [blame] | 28 | def test_validator(val): |
| 29 | return val in ('valid_value1', 'valid_value2') |
| 30 | |
| 31 | schema.register_schema_types({'test_type': test_validator, |
| 32 | 'another_type': lambda val: val == 'unique_val_ok'}) |
| 33 | |
| 34 | |
Pau Espin Pedrol | 3063730 | 2020-05-06 21:11:02 +0200 | [diff] [blame] | 35 | print('==== Testing dynamically generated schemas ====') |
| 36 | for f in get_case_list(_prep.script_dir): |
| 37 | print('%s:' % f) |
| 38 | example_config = os.path.join(_prep.script_dir, f) |
| 39 | cfg = config.read(example_config) |
| 40 | try: |
| 41 | schema_def = schema.config_to_schema_def(cfg['schema'], 'foobar.prefix.') |
| 42 | except AssertionError: |
| 43 | schema_def = None |
| 44 | log.log_exn() |
| 45 | print('config2schema: Error') |
| 46 | |
| 47 | if schema_def is not None: |
| 48 | pprint.pprint(schema_def) |
| 49 | i = 0 |
| 50 | for t in cfg['tests']: |
| 51 | print('validating tests[%d]' % i) |
| 52 | val(t, schema_def) |
| 53 | i += 1 |
| 54 | print('----------------------') |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | # vim: expandtab tabstop=4 shiftwidth=4 |