Skip to content

Commit

Permalink
fixed bug with csv export and added more detailed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BernhardKoschicek committed Sep 25, 2023
1 parent 215922c commit 89f65be
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
4 changes: 2 additions & 2 deletions openatlas/api/resources/resolve_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ def resolve_entity(
parser: dict[str, Any]) \
-> Union[Response, dict[str, Any], tuple[Any, int]]:
if parser['export'] == 'csv':
export_entities_csv(entity, entity.name)
return export_entities_csv(entity, entity.name)
if parser['export'] == 'csvNetwork':
export_csv_for_network_analysis([entity], parser)
return export_csv_for_network_analysis([entity], parser)
result = get_entity_formatted(entity, parser)
if parser['format'] in app.config['RDF_FORMATS']: # pragma: nocover
return Response(
Expand Down
39 changes: 28 additions & 11 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,19 +99,25 @@ def test_api(self) -> None:
assert self.get_geom_properties(rv, 'objectName')
assert self.get_geom_properties(rv, 'shapeType')

for rv in [
self.app.get(
url_for('api_03.export_database', format_='xml')),
self.app.get(
url_for('api_03.export_database', format_='json')),
self.app.get(
url_for('api_03.export_database', format_='csv'))]:
assert b'Shire' in rv.data
rv = self.app.get(
url_for('api_03.export_database', format_='xml'))
assert b'Shire' in rv.data
assert 'application/xml' in rv.headers.get('Content-Type')

rv = self.app.get(
url_for('api_03.export_database', format_='json'))
assert b'Shire' in rv.data
assert 'application/json' in rv.headers.get('Content-Type')

rv = self.app.get(url_for('api_03.export_database', format_='csv'))
assert b'Shire' in rv.data
assert 'application/zip' in rv.headers.get('Content-Type')

# ---Entity Endpoints---
# Test Entity
rv = self.app.get(
url_for('api_03.entity', id_=place.id, download=True))
assert 'application/json' in rv.headers.get('Content-Type')
rv = rv.get_json()['features'][0]
assert self.get_bool(rv, '@id')
assert self.get_bool(rv, 'type', 'Feature')
Expand Down Expand Up @@ -159,6 +165,7 @@ def test_api(self) -> None:
# Test entity in GeoJSON format
rv = self.app.get(url_for(
'api_03.entity', id_=place.id, format='geojson'))
assert 'application/json' in rv.headers.get('Content-Type')
rv = rv.get_json()['features'][0]
assert self.get_bool(rv['geometry'], 'type')
assert self.get_bool(rv['geometry'], 'coordinates')
Expand All @@ -176,6 +183,7 @@ def test_api(self) -> None:

rv = self.app.get(url_for(
'api_03.entity', id_=place.id, format='geojson-v2'))
assert 'application/json' in rv.headers.get('Content-Type')
rv = rv.get_json()['features'][0]
assert self.get_bool(rv['geometry'], 'type')
assert self.get_bool(
Expand All @@ -195,6 +203,7 @@ def test_api(self) -> None:
# Test entity in Linked Open Usable Data
rv = self.app.get(
url_for('api_03.entity', id_=place.id, format='loud'))
assert 'application/json' in rv.headers.get('Content-Type')
rv = rv.get_json()
assert bool(rv['type'] == 'PhysicalThing')
assert bool(rv['_label'] == 'Shire')
Expand Down Expand Up @@ -228,15 +237,19 @@ def test_api(self) -> None:
for rv in [
self.app.get(
url_for('api_03.entity', id_=place.id, export='csv')),
self.app.get(url_for(
'api_03.entity', id_=place.id, export='csvNetwork')),
self.app.get(url_for(
'api_03.query',
entities=location.id,
cidoc_classes='E18',
view_classes='artifact',
system_classes='person',
export='csv')),
export='csv'))]:
assert b'Shire' in rv.data
assert 'text/csv' in rv.headers.get('Content-Type')

for rv in [
self.app.get(url_for(
'api_03.entity', id_=place.id, export='csvNetwork')),
self.app.get(url_for(
'api_03.query',
entities=location.id,
Expand All @@ -245,6 +258,7 @@ def test_api(self) -> None:
system_classes='person',
export='csvNetwork'))]:
assert b'Shire' in rv.data
assert 'application/zip' in rv.headers.get('Content-Type')

# Test Entities endpoints
for rv in [
Expand Down Expand Up @@ -296,6 +310,7 @@ def test_api(self) -> None:
column='system_class',
download=True,
actor=place.id))]:
assert 'application/json' in rv.headers.get('Content-Type')
rv = rv.get_json()
rv_results = rv['results'][0]['features'][0]
rv_page = rv['pagination']
Expand Down Expand Up @@ -675,6 +690,7 @@ def test_api(self) -> None:
self.app.get(url_for('api_03.subunits', id_=place.id)),
self.app.get(
url_for('api_03.subunits', id_=place.id, download=True))]:
assert 'application/json' in rv.headers.get('Content-Type')
rv = rv.get_json()[str(place.id)]
for item in rv:
if item['id'] == place.id:
Expand Down Expand Up @@ -739,6 +755,7 @@ def test_api(self) -> None:
centroid=True,
limit=0))]:
assert b'(autogenerated)' in rv.data
assert 'application/json' in rv.headers.get('Content-Type')

# Test Error Handling
for rv in [
Expand Down

0 comments on commit 89f65be

Please sign in to comment.