def test_entities(monkeypatch): before_each_storage_test(monkeypatch) from gobapi.storage import get_entities MockEntities.all_entities = [] assert (get_entities('catalog', 'collection1', 0, 1) == ([], 0)) mockEntity = MockEntity('identificatie', 'attribute') MockEntities.all_entities = [mockEntity] assert (get_entities('catalog', 'collection1', 0, 1) == ([{ 'attribute': 'attribute', 'identificatie': 'identificatie', '_links': { 'self': { 'href': '/gob/catalog/collection1/1/' } } }], 1)) mockEntity = MockEntity('identificatie', 'attribute', 'non_existing_attribute') MockEntities.all_entities = [mockEntity] assert (get_entities('catalog', 'collection1', 0, 1) == ([{ 'attribute': 'attribute', 'identificatie': 'identificatie', '_links': { 'self': { 'href': '/gob/catalog/collection1/1/' } } }], 1))
def test_entities_with_view(monkeypatch): before_each_storage_test(monkeypatch) from gobapi.storage import get_entities MockEntities.all_entities = [] # Views return total_count None to prevent slow count on large tables assert (get_entities('catalog', 'collection1', 0, 1, 'enhanced') == ([], None)) mockEntity = MockEntity('identificatie', 'attribute', '_private_attribute') MockEntities.all_entities = [mockEntity] assert (get_entities('catalog', 'collection1', 0, 1, 'enhanced') == ([{ 'attribute': 'attribute', 'identificatie': 'identificatie' }], None)) mockEntity = MockEntity('identificatie', 'attribute', 'non_existing_attribute') MockEntities.all_entities = [mockEntity] assert (get_entities('catalog', 'collection1', 0, 1, 'enhanced') == ([{ 'attribute': 'attribute', 'identificatie': 'identificatie' }], None)) # Add a reference to the table columns MockTable.columns = [ MockColumn('identificatie'), MockColumn('attribute'), MockColumn('_ref_is_test_tse_tst') ] mockEntity = MockEntity('identificatie', 'attribute', '_ref_is_test_tse_tst') mockEntity._ref_is_test_tse_tst = {FIELD.REFERENCE_ID: '1234'} MockEntities.all_entities = [mockEntity] expect_ref = MagicMock() expect_ref._string = '{"reference": {"%s": "1234"}}' % FIELD.REFERENCE_ID assert (get_entities('catalog', 'collection1', 0, 1, 'enhanced') == ([{ 'attribute': 'attribute', 'identificatie': 'identificatie', '_embedded': { 'is_test': expect_ref } }], None)) # Reset the table columns MockTable.columns = [ MockColumn('identificatie'), MockColumn('attribute'), MockColumn('meta') ]
def _reference_entities(src_catalog_name, src_collection_name, reference_name, src_id, page, page_size): """Returns the entities for a reference with specified source entity The page and page_size are used to calculate the offset and number of entities to return A result, links tuple is returned. Result is an object containing relevant metadata about the result Links contain the references to any next or previous page :param src_catalog_name: e.g. meetbouten :param src_collection_name: e.g. metingen :param reference_name: e.g. ligt_in_buurt :param src_id: e.g. 1234 :param page: any page number, page numbering starts at 1 :param page_size: the number of entities per page :return: (result, links) """ assert (GOBModel().get_collection( src_catalog_name, src_collection_name)['references'].get(reference_name)) assert (page >= 1) assert (page_size >= 1) offset = (page - 1) * page_size entities, total_count = get_entities(src_catalog_name, src_collection_name, offset=offset, limit=page_size, view=None, reference_name=reference_name, src_id=src_id) num_pages = (total_count + page_size - 1) // page_size return { 'total_count': total_count, 'page_size': page_size, 'pages': num_pages, 'results': entities }, { 'next': get_page_ref(page + 1, num_pages), 'previous': get_page_ref(page - 1, num_pages) }
def _entities(catalog_name, collection_name, page, page_size, view=None): """Returns the entities in the specified catalog collection The page and page_size are used to calculate the offset and number of entities to return A result, links tuple is returned. Result is an object containing relevant metadata about the result Links contain the references to any next or previous page :param catalog_name: e.g. meetbouten :param collection_name: e.g. meting :param page: any page number, page numbering starts at 1 :param page_size: the number of entities per page :param view: the database view that's being used to get the entities, defaults to the entity table :return: (result, links) """ assert (GOBModel().get_collection(catalog_name, collection_name)) assert (page >= 1) assert (page_size >= 1) offset = (page - 1) * page_size entities, total_count = get_entities(catalog_name, collection_name, offset=offset, limit=page_size, view=view) if view: # For views always show next page unless no results are returned. Count is slow on large views num_pages = page + 1 if len(entities) > 0 else page else: num_pages = (total_count + page_size - 1) // page_size return { 'total_count': total_count, 'page_size': page_size, 'pages': num_pages, 'results': entities }, { 'next': get_page_ref(page + 1, num_pages), 'previous': get_page_ref(page - 1, num_pages) }
def test_entities_with_verymanyreferences(monkeypatch): before_each_storage_test(monkeypatch) from gobapi.storage import get_entities mockEntity = MockEntity('identificatie', 'attribute') MockEntities.all_entities = [mockEntity] # It should have a reference link to the verymanyreference assert (get_entities('catalog', 'collection3', 0, 1) == ([{ 'attribute': 'attribute', 'identificatie': 'identificatie', '_links': { 'self': { 'href': '/gob/catalog/collection3/1/' }, 'verymanyreference': { 'href': '/gob/catalog/collection3/1/verymanyreference/' } } }], 1))
def test_reference_entities(monkeypatch): before_each_storage_test(monkeypatch) from gobapi.storage import get_entities mockEntity = MockEntity('identificatie', 'attribute') MockEntities.all_entities = [mockEntity] # A list of entities of catalog:collection2 should be returned assert (get_entities('catalog', 'collection3', 0, 1, None, 'verymanyreference', '1') == ([{ 'attribute': 'attribute', 'identificatie': 'identificatie', '_links': { 'self': { 'href': '/gob/catalog/collection2/1/' } }, '_embedded': { 'reference': [{ 'reference': { 'bronwaarde': '1', 'id': '1' } }], 'manyreference': [{ 'reference': { 'id': '1', 'bronwaarde': '1' } }, { 'reference': { 'id': '2', 'bronwaarde': '2' } }] } }], 1))
def test_entities_without_reference_id(monkeypatch): before_each_storage_test(monkeypatch) from gobapi.storage import get_entities mockEntity = MockEntity('identificatie', 'attribute') mockEntity.reference[FIELD.REFERENCE_ID] = None MockEntities.all_entities = [mockEntity] assert (get_entities('catalog', 'collection2', 0, 1) == ([{ 'attribute': 'attribute', 'identificatie': 'identificatie', '_links': { 'self': { 'href': '/gob/catalog/collection2/1/' } }, '_embedded': { 'reference': [{ 'reference': { 'bronwaarde': '1', 'id': None } }], 'manyreference': [{ 'reference': { 'id': '1', 'bronwaarde': '1' } }, { 'reference': { 'id': '2', 'bronwaarde': '2' } }] } }], 1))
def test_entities_with_references(monkeypatch): before_each_storage_test(monkeypatch) from gobapi.storage import get_entities mockEntity = MockEntity('identificatie', 'attribute') MockEntities.all_entities = [mockEntity] # The private reference should't be visible on the entities list assert (get_entities('catalog', 'collection2', 0, 1) == ([{ 'attribute': 'attribute', 'identificatie': 'identificatie', '_links': { 'self': { 'href': '/gob/catalog/collection2/1/' } }, '_embedded': { 'reference': [{ 'reference': { 'bronwaarde': '1', 'id': '1' } }], 'manyreference': [{ 'reference': { 'id': '1', 'bronwaarde': '1' } }, { 'reference': { 'id': '2', 'bronwaarde': '2' } }] } }], 1))