Project

General

Profile

« Previous | Next » 

Revision 456ad6c0

Added by koszko over 1 year ago

include new schemas in package and in tests

View differences:

setup.cfg
34 34
include_package_data=True
35 35
install_requires =
36 36
    click
37
    jsonschema
37 38
    importlib; python_version == "2.6"
38 39
 # reuse is optional:
39 40
 #    reuse
src/hydrilla/builder/build.py
35 35

  
36 36
from .. import util
37 37

  
38
index_json_schema = util.load_schema('package_source-1.schema.json')
38
index_validator = util.validator_for('package_source-1.schema.json')
39 39

  
40 40
class FileReferenceError(Exception):
41 41
    """
......
275 275

  
276 276
        item_list.append(new_item_obj)
277 277

  
278
        return dict([(prop, new_item_obj[prop])
279
                     for prop in ('type', 'identifier', 'version')])
278
        props_in_ref = ('type', 'identifier', 'version', 'long_name')
279
        return dict([(prop, new_item_obj[prop]) for prop in props_in_ref])
280 280

  
281 281
    def _process_index_json(self, index_obj: dict):
282 282
        """
......
285 285
        files and computed definitions of the source package and items defined
286 286
        in it.
287 287
        """
288
        jsonschema.validate(index_obj, index_json_schema)
288
        index_validator.validate(index_obj)
289 289

  
290 290
        self.source_name = index_obj['source_name']
291 291

  
src/hydrilla/schemas
1
Subproject commit ca1de2ed4a69a71f2f75552ade693d04ea1baa85
1
Subproject commit c72c8438875d20b156d22d975523a19bbb407d95
src/hydrilla/util/__init__.py
5 5
# Available under the terms of Creative Commons Zero v1.0 Universal.
6 6

  
7 7
from ._util import strip_json_comments, normalize_version, parse_version, \
8
    version_string, load_schema
8
    version_string, validator_for
src/hydrilla/util/_util.py
30 30
from pathlib import Path
31 31
from typing import Optional
32 32

  
33
from jsonschema import RefResolver, Draft7Validator
34

  
33 35
here = Path(__file__).resolve().parent
34 36

  
35 37
_strip_comment_re = re.compile(r'''
......
103 105
    """
104 106
    return '.'.join([str(n) for n in ver]) + ('' if rev is None else f'-{rev}')
105 107

  
106
def load_schema(schema_filename: str) -> dict:
107
    """Find schema JSON file in '../schemas' and json.load() it."""
108
    with open(here.parent / 'schemas' / schema_filename, 'rt') as file_handle:
109
        return json.load(file_handle)
108
schemas = {}
109
for path in (here.parent / 'schemas').glob('*-1.schema.json'):
110
    schema = json.loads(path.read_text())
111
    schemas[schema['$id']] = schema
112

  
113
common_schema_filename = 'common_definitions-1.schema.json'
114
common_schema_path = here.parent / "schemas" / common_schema_filename
115

  
116
resolver = RefResolver(
117
    base_uri=f'file://{str(common_schema_path)}',
118
    referrer=f'https://hydrilla.koszko.org/{common_schema_filename}',
119
    store=schemas
120
)
121

  
122
def validator_for(schema_filename: str) -> Draft7Validator:
123
    """
124
    Prepare a validator for one of the schemas in '../schemas'.
125

  
126
    This function is not thread-safe.
127
    """
128
    return Draft7Validator(resolver.resolve(schema_filename)[1],
129
                           resolver=resolver)
src/test/test_hydrilla_builder.py
148 148
            'definitions': [{
149 149
                'type': 'resource',
150 150
                'identifier': 'helloapple',
151
                'long_name': 'Hello Apple',
151 152
                'version': [2021, 11, 10],
152 153
            }, {
153 154
                'type':       'resource',
154 155
                'identifier': 'hello-message',
156
                'long_name': 'Hello Message',
155 157
                'version':     [2021, 11, 10],
156 158
            }, {
157 159
                'type': 'mapping',
158 160
                'identifier': 'helloapple',
161
	        'long_name': 'Hello Apple',
159 162
                'version': [2021, 11, 10],
160 163
            }]
161 164
        }
......
346 349
        with open(subdir / '2021.11.10', 'rt') as file_handle:
347 350
            assert json.load(file_handle) == resource_json
348 351

  
352
        hydrilla_util.validator_for('api_resource_description-1.schema.json')\
353
                     .validate(resource_json)
354

  
349 355
    # Verify files under 'mapping/'
350 356
    mapping_dir = dstdir / 'mapping'
351 357
    assert ['helloapple'] == [path.name for path in mapping_dir.iterdir()]
......
356 362
    with open(subdir / '2021.11.10', 'rt') as file_handle:
357 363
        assert json.load(file_handle) == settings.expected_mapping
358 364

  
365
    hydrilla_util.validator_for('api_mapping_description-1.schema.json')\
366
                 .validate(settings.expected_mapping)
367

  
359 368
    # Verify files under 'source/'
360 369
    source_dir = dstdir / 'source'
361 370
    assert {'hello.json', 'hello.zip'} == \
......
377 386
    with open(source_dir / 'hello.json', 'rt') as file_handle:
378 387
        assert json.load(file_handle) == settings.expected_source_description
379 388

  
389
    hydrilla_util.validator_for('api_source_description-1.schema.json')\
390
                 .validate(settings.expected_source_description)
391

  
380 392
def modify_index_missing_file(dummy: CaseSettings, obj: dict) -> None:
381 393
    """
382 394
    Modify index.json to expect missing report.spdx file and cause an error.

Also available in: Unified diff