def test_entity_with_view(monkeypatch): before_each_storage_test(monkeypatch) MockEntities.one_entity = None from gobapi.storage import get_entity assert (get_entity('catalog', 'collection1', 'identificatie', 'enhanced') == None) mockEntity = MockEntity('identificatie', 'attribute', 'meta') MockEntities.one_entity = mockEntity assert (get_entity('catalog', 'collection1', 'identificatie', 'enhanced') == { 'attribute': 'attribute', 'identificatie': 'identificatie', 'meta': 'meta' })
def _entity(catalog_name, collection_name, entity_id, view=None): """Returns the entity within the specified collection with the specified id An individual entity is returned. :param catalog_name: e.g. meetbouten :param collection_name: e.g. meting :param entity_id: unique identifier of the entity :param view: the database view that's being used to get the entity, defaults to the entity table :return: """ if GOBModel().get_collection(catalog_name, collection_name): view = request.args.get('view', None) # If a view is requested and doesn't exist return a 404 if view and not GOBViews().get_view(catalog_name, collection_name, view): return not_found( f'{catalog_name}.{collection_name}?view={view} not found') view_name = GOBViews().get_view(catalog_name, collection_name, view)['name'] if view else None result = get_entity(catalog_name, collection_name, entity_id, view_name) return hal_response(result) if result is not None else not_found( f'{catalog_name}.{collection_name}:{entity_id} not found') else: return not_found(f'{catalog_name}.{collection_name} not found')
def _reference_collection(catalog_name, collection_name, entity_id, reference_path): """Returns the (very many) references from an entity within the specified collection with the specified id An list of references is returned. :param catalog_name: e.g. meetbouten :param collection_name: e.g. meting :param entity_id: unique identifier of the entity :param reference: unique identifier of the reference attribute e.g. ligt_in_buurt :param view: the database view that's being used to get the entity, defaults to the entity table :return: """ model = GOBModel() entity_collection = model.get_collection(catalog_name, collection_name) if entity_collection: # Get the reference reference_name = reference_path.replace('-', '_') reference = model.get_collection( catalog_name, collection_name)['references'].get(reference_name) # Check if the source entity exists entity = get_entity(catalog_name, collection_name, entity_id) if entity and reference: page = int(request.args.get('page', 1)) page_size = int(request.args.get('page_size', 100)) stream = request.args.get('stream', None) == "true" ndjson = request.args.get('ndjson', None) == "true" if stream: entities, convert = query_reference_entities( catalog_name, collection_name, reference_name, entity_id) return Response(stream_entities(entities, convert), mimetype='application/json') elif ndjson: entities, convert = query_reference_entities( catalog_name, collection_name, reference_name, entity_id) return Response(ndjson_entities(entities, convert), mimetype='application/x-ndjson') else: result, links = _reference_entities(catalog_name, collection_name, reference_name, entity_id, page, page_size) return hal_response(data=result, links=links) response = not_found(f'{catalog_name}.{collection_name}:{entity_id} not found') \ if not entity else not_found(f'{catalog_name}.{collection_name}:{entity_id}:{reference_name} not found') return response else: return not_found(f'{catalog_name}.{collection_name} not found')
def test_entity(monkeypatch): before_each_storage_test(monkeypatch) from gobapi.storage import get_entity assert (get_entity('catalog', 'collection1', 'identificatie') == None) mockEntity = MockEntity('identificatie', 'attribute', '_private_attribute', 'meta') MockEntities.one_entity = mockEntity # Expect the private attribute to be visible expected = { 'identificatie': 'identificatie', 'attribute': 'attribute', 'meta': 'meta', '_private_attribute': '_private_attribute', '_links': { 'self': { 'href': '/gob/catalog/collection1/1/' } } } assert (get_entity('catalog', 'collection1', 'identificatie') == expected)