def test_url_mapping(app, dir_factory, url_scheme): """Test register schema.""" app.config['SERVER_NAME'] = 'example.org' app.config['JSONSCHEMAS_HOST'] = 'inveniosoftware.org' if url_scheme is not None: app.config['JSONSCHEMAS_URL_SCHEME'] = url_scheme else: # test with default url scheme configuration url_scheme = JSONSCHEMAS_URL_SCHEME ext = InvenioJSONSchemas(app, entry_point_group=None) schema_files = build_schemas(1) with dir_factory(schema_files) as directory: ext.register_schemas_dir(directory) with app.app_context(): assert 'sub1/subschema_1.json' == ext.url_to_path( '{0}://inveniosoftware.org/schemas/sub1/subschema_1.json' .format(url_scheme)) assert ext.url_to_path( '{0}://inveniosoftware.org/schemas/invalid.json' .format(url_scheme)) is None assert ext.url_to_path( '{0}://example.org/schemas/sub1/subschema_1.json' .format(url_scheme)) is None assert ( '{0}://inveniosoftware.org/schemas/sub1/subschema_1.json' .format(url_scheme) ) == ext.path_to_url('sub1/subschema_1.json') assert ext.path_to_url('invalid.json') is None
def test_redefine(app, dir_factory): ext = InvenioJSONSchemas(app, entry_point_group=None) schema_files = build_schemas(1) with dir_factory(schema_files) as dir1, \ dir_factory(schema_files) as dir2: ext.register_schemas_dir(dir1) # register schemas from a directory which have the same relative # paths with pytest.raises(JSONSchemaDuplicate) as exc_info: ext.register_schemas_dir(dir2) assert exc_info.value.schema in schema_files.keys()
def test_cache(app, dir_factory): """Test cached schema loading.""" m = mock_open with mock.patch('invenio_jsonschemas.ext.open', m): ext = InvenioJSONSchemas(app, entry_point_group=None) schema_files = build_schemas(1) with dir_factory(schema_files) as directory: ext.register_schemas_dir(directory) assert m.counter == 0 ext.get_schema('rootschema_1.json') assert m.counter == 1 ext.get_schema('rootschema_1.json') ext.get_schema('rootschema_1.json') assert m.counter == 1 ext.get_schema('sub1/subschema_1.json') assert m.counter == 2 ext.get_schema('sub1/subschema_1.json') assert m.counter == 2
def test_api(app, dir_factory): ext = InvenioJSONSchemas(app, entry_point_group=None) schema_files = build_schemas(1) with dir_factory(schema_files) as directory: ext.register_schemas_dir(directory) for path in schema_files.keys(): # test get_schema_dir assert ext.get_schema_dir(path) == directory # test get_schema_path assert ext.get_schema_path(path) == \ os.path.join(directory, path) # test get_schema assert ext.get_schema(path) == json.loads(schema_files[path]) # test list_schemas assert set(schema_files.keys()) == set(ext.list_schemas()) # test failure when asking for non existing schemas fails with pytest.raises(JSONSchemaNotFound) as exc_info: ext.get_schema('not_existing_schema.json') assert exc_info.value.schema == 'not_existing_schema.json' # test failure when asking for non existing schemas' path with pytest.raises(JSONSchemaNotFound) as exc_info: ext.get_schema_path('not_existing_schema.json') assert exc_info.value.schema == 'not_existing_schema.json'