Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/routes/ags_export_by_polygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def ags_export_by_polygon(
except shapely.errors.GEOSException:
raise HTTPException(status_code=422, detail="Invalid polygon")

url = BOREHOLE_INDEX_URL.format(polygon=polygon)
url = BOREHOLE_INDEX_URL.format(polygon=polygon, borehole_export_limit=BOREHOLE_EXPORT_LIMIT)

try:
response = requests.get(url, timeout=10)
Expand Down
2 changes: 1 addition & 1 deletion app/routes/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
BOREHOLE_EXPORT_URL = "https://gwbv.bgs.ac.uk/ags_export?loca_ids={bgs_loca_id}"
BOREHOLE_INDEX_URL = (
"https://ogcapi.bgs.ac.uk/collections/agsboreholeindex/items?f=json"
"&properties=bgs_loca_id&filter=INTERSECTS(shape,{polygon})&limit=50"
"&properties=bgs_loca_id&filter=INTERSECTS(shape,{polygon})&limit={borehole_export_limit}"
)

log_responses = dict(error_responses)
Expand Down
32 changes: 32 additions & 0 deletions test/integration/test_ags_export_by_polygon.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Tests for API responses."""
from io import BytesIO
import re
import zipfile

import pytest
Expand Down Expand Up @@ -48,6 +49,37 @@ def test_get_ags_exporter_by_polygon(client, count_only):
assert f'Project : {bgs_proj_id}' in metadata_text


@pytest.mark.xfail(IN_GITHUB_ACTIONS, reason="Upstream URL not available from Github Actions")
def test_get_ags_exporter_by_polygon_with_more_than_10_polygons(client):
# Arrange
# There should be 28 boreholes in this area, this should pass for a limit of 50,
# and it should fail for a limit of 10
polygon = 'POLYGON((-3.946 56.065,-3.640 56.065,-3.640 55.966,-3.946 55.966,-3.946 56.065))'
query = f'{API_VERSION}/ags_export_by_polygon/?polygon={polygon}'
ags_metadata_file_name = 'FILE/BGSFileSet01/BGS_download_metadata.txt'

# Act
with client as ac:
response = ac.get(query)

# Assert
assert response.status_code == 200
assert response.headers["Content-Disposition"] == 'attachment; filename="boreholes.zip"'
assert response.headers["Content-Type"] == "application/x-zip-compressed"
assert len(response.content) > 0

assert zipfile.is_zipfile(BytesIO(response.content))
with zipfile.ZipFile(BytesIO(response.content)) as ags_zip:
# Check that metadata.txt lists 28 loca IDs
with ags_zip.open(ags_metadata_file_name) as metadata_file:
# find the pattern 20200205093727287902;20200205093727287903;2020...
regex = r'\d+(;\d+)+'
metadata_text = metadata_file.read().decode()
match = re.search(regex, metadata_text)
assert match
assert len(match.group(0).split(';')) == 28


@pytest.mark.parametrize('polygon, count', [
('POLYGON((-3.946 56.061,-3.640 56.061,-3.640 55.966,-3.946 55.966,-3.946 56.061))', 0),
('POLYGON((-3.946 56.063,-3.640 56.063,-3.640 55.966,-3.946 55.966,-3.946 56.063))', 4),
Expand Down