def test_lazy_model_immutable_after_load(mock_plugin, model_data, model_type, mock_entity_create_id): from attr.exceptions import FrozenInstanceError from coalaip.models import Model, LazyLoadableModel mock_plugin.load.return_value = model_data model = LazyLoadableModel(ld_type=model_type) model.load(mock_entity_create_id, plugin=mock_plugin) with raises(FrozenInstanceError): model.loaded_model = Model(data={'other': 'other'}, ld_type='other_type')
def test_lazy_model_load_raises_on_model_validation(mock_plugin, work_jsonld, right_jsonld_factory, model_type, mock_entity_create_id): from coalaip.models import LazyLoadableModel from coalaip.model_validators import is_creation_model from coalaip.exceptions import ModelDataError mock_plugin.load.return_value = right_jsonld_factory() model = LazyLoadableModel(ld_type=model_type, validator=is_creation_model) with raises(ModelDataError): model.load(mock_entity_create_id, plugin=mock_plugin)
def test_lazy_model_load_raises_on_type_validation(mock_plugin, model_type, bad_type_data, mock_entity_create_id): from coalaip.models import LazyLoadableModel from coalaip.exceptions import ModelDataError mock_plugin.load.return_value = bad_type_data assert model_type != (bad_type_data.get('@type') or bad_type_data.get('type')) model = LazyLoadableModel(ld_type=model_type) with raises(ModelDataError): model.load(mock_entity_create_id, plugin=mock_plugin)
def test_lazy_model_data_immutable_after_load(mock_plugin, model_data, model_type, mock_entity_create_id): from coalaip.models import LazyLoadableModel mock_plugin.load.return_value = model_data model = LazyLoadableModel(ld_type=model_type) model.load(mock_entity_create_id, plugin=mock_plugin) with raises(TypeError): model.data['new_data'] = 'new_data' assert model.data == model_data
def test_lazy_model_load_raises_on_context_validation(mock_plugin, context_urls_all, model_type, mock_entity_create_id): from coalaip.models import LazyLoadableModel from coalaip.exceptions import ModelDataError bad_context_data = {'@context': 'other_context'} mock_plugin.load.return_value = bad_context_data assert context_urls_all != bad_context_data.get('@context') model = LazyLoadableModel(ld_type=model_type, ld_context=context_urls_all) with raises(ModelDataError): model.load(mock_entity_create_id, plugin=mock_plugin)
def test_lazy_model_init_with_data(mock_plugin, model_data, model_type, mock_entity_create_id): from coalaip.models import Model, LazyLoadableModel model = LazyLoadableModel(data=model_data, ld_type=model_type) assert model.data == model_data assert isinstance(model.loaded_model, Model) assert model.loaded_model.data == model_data assert model.loaded_model.ld_type == model.ld_type assert model.loaded_model.ld_context == model.ld_context assert model.loaded_model.validator == model.validator # If initialized with data, load() becomes a noop model.load(mock_entity_create_id, plugin=mock_plugin) mock_plugin.load.assert_not_called()
def test_lazy_model_load_jsonld(mock_plugin, model_data, model_jsonld, model_type, model_context, mock_entity_create_id): from coalaip.models import Model, LazyLoadableModel mock_plugin.load.return_value = model_jsonld model = LazyLoadableModel(ld_type=model_type, ld_context=model_context) model.load(mock_entity_create_id, plugin=mock_plugin) mock_plugin.load.assert_called_with(mock_entity_create_id) assert model.data == model_data assert isinstance(model.loaded_model, Model) assert model.loaded_model.data == model_data assert model.loaded_model.ld_type == model.ld_type assert model.loaded_model.ld_id == model.ld_id assert model.loaded_model.ld_context == model.ld_context assert model.loaded_model.validator == model.validator # If initialized with data, load() becomes a noop mock_plugin.reset_mock() model.load(mock_entity_create_id, plugin=mock_plugin) mock_plugin.load.assert_not_called()