Skip to content

Commit

Permalink
Improve api testing
Browse files Browse the repository at this point in the history
  • Loading branch information
banesullivan committed Apr 27, 2022
1 parent 13273f5 commit e709d2a
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 41 deletions.
10 changes: 5 additions & 5 deletions project/example/core/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ class CoreConfig(AppConfig):

def ready(self):
if not getattr(settings, 'DEBUG', False):
logging.getLogger("gdal").setLevel(logging.ERROR)
logging.getLogger("large_image").setLevel(logging.ERROR)
logging.getLogger("tifftools").setLevel(logging.ERROR)
logging.getLogger("pyvips").setLevel(logging.ERROR)
logging.getLogger("PIL").setLevel(logging.ERROR)
logging.getLogger('gdal').setLevel(logging.ERROR)
logging.getLogger('large_image').setLevel(logging.ERROR)
logging.getLogger('tifftools').setLevel(logging.ERROR)
logging.getLogger('pyvips').setLevel(logging.ERROR)
logging.getLogger('PIL').setLevel(logging.ERROR)
2 changes: 1 addition & 1 deletion project/example/core/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def s3_image_file_geotiff() -> models.S3ImageFile:


@pytest.fixture
def non_geo_image() -> models.ImageFile:
def png_image() -> models.ImageFile:
return ImageFileFactory(
file__filename='afie_1.jpg',
file__from_path=datastore.fetch('afie_1.jpg'),
Expand Down
21 changes: 11 additions & 10 deletions project/example/core/tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from PIL import Image
import pytest
from rest_framework import status


@pytest.mark.django_db(transaction=True)
Expand All @@ -10,7 +11,7 @@ def test_thumbnail(authenticated_api_client, image_file_geotiff, format):
response = authenticated_api_client.get(
f'/api/image-file/{image_file_geotiff.pk}/thumbnail.{format}'
)
assert response.status_code == 200
assert status.is_success(response.status_code)
assert response['Content-Type'] == f'image/{format}'


Expand All @@ -20,7 +21,7 @@ def test_thumbnail_max_size(authenticated_api_client, image_file_geotiff, format
response = authenticated_api_client.get(
f'/api/image-file/{image_file_geotiff.pk}/thumbnail.{format}?max_width=100'
)
assert response.status_code == 200
assert status.is_success(response.status_code)
assert response['Content-Type'] == f'image/{format}'
# Check width of thumbnail
with Image.open(io.BytesIO(response.content)) as im:
Expand All @@ -29,7 +30,7 @@ def test_thumbnail_max_size(authenticated_api_client, image_file_geotiff, format
response = authenticated_api_client.get(
f'/api/image-file/{image_file_geotiff.pk}/thumbnail.{format}?max_height=100'
)
assert response.status_code == 200
assert status.is_success(response.status_code)
assert response['Content-Type'] == f'image/{format}'
# Check height of thumbnail
with Image.open(io.BytesIO(response.content)) as im:
Expand All @@ -39,13 +40,13 @@ def test_thumbnail_max_size(authenticated_api_client, image_file_geotiff, format
@pytest.mark.django_db(transaction=True)
def test_histogram(authenticated_api_client, image_file_geotiff):
response = authenticated_api_client.get(f'/api/image-file/{image_file_geotiff.pk}/histogram')
assert response.status_code == 200
assert status.is_success(response.status_code)
hist = response.data[0]
assert isinstance(hist, dict)
response = authenticated_api_client.get(
f'/api/image-file/{image_file_geotiff.pk}/histogram?onlyMinMax=true'
)
assert response.status_code == 200
assert status.is_success(response.status_code)
assert 'min' in response.data


Expand All @@ -54,7 +55,7 @@ def test_pixel(authenticated_api_client, image_file_geotiff):
response = authenticated_api_client.get(
f'/api/image-file/{image_file_geotiff.pk}/pixel?left=0&top=0'
)
assert response.status_code == 200
assert status.is_success(response.status_code)
data = response.data
assert 'bands' in data

Expand All @@ -65,7 +66,7 @@ def test_region_pixel(authenticated_api_client, image_file_geotiff, ome_image, f
response = authenticated_api_client.get(
f'/api/image-file/{image_file_geotiff.pk}/region.{format}?left=0&right=10&bottom=10&top=0&units=pixels'
)
assert response.status_code == 200
assert status.is_success(response.status_code)
if format == 'tif':
format = 'tiff' # Change for mime_type
assert response['Content-Type'] == f'image/{format}'
Expand All @@ -76,19 +77,19 @@ def test_region_pixel_out_of_bounds(authenticated_api_client, image_file_geotiff
response = authenticated_api_client.get(
f'/api/image-file/{image_file_geotiff.pk}/region.tif?left=10000000&right=20000000&bottom=20000000&top=10000000&units=pixels'
)
assert response.status_code == 400
assert status.is_client_error(response.status_code)


@pytest.mark.django_db(transaction=True)
def test_region_geo(authenticated_api_client, image_file_geotiff):
response = authenticated_api_client.get(
f'/api/image-file/{image_file_geotiff.pk}/region.tif?units=EPSG:4326&left=-117.4567824262003&right=-117.10373770277764&bottom=32.635234150046514&top=32.964410481130365'
)
assert response.status_code == 200
assert status.is_success(response.status_code)
assert response['Content-Type'] == 'image/tiff'
# Leave units out
response = authenticated_api_client.get(
f'/api/image-file/{image_file_geotiff.pk}/region.tif?left=-117.4567824262003&right=-117.10373770277764&bottom=32.635234150046514&top=32.964410481130365'
)
assert response.status_code == 200
assert status.is_success(response.status_code)
assert response['Content-Type'] == 'image/tiff'
21 changes: 11 additions & 10 deletions project/example/core/tests/test_metadata.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import pytest
from rest_framework import status


@pytest.mark.django_db(transaction=True)
def test_metadata(authenticated_api_client, image_file_geotiff):
response = authenticated_api_client.get(
f'/api/image-file/{image_file_geotiff.pk}/metadata?projection=EPSG:3857'
)
assert response.status_code == 200
assert status.is_success(response.status_code)
metadata = response.data
assert metadata['geospatial']
assert metadata['levels'] == 9
Expand All @@ -20,7 +21,7 @@ def test_metadata_vsi(authenticated_api_client, image_file_geotiff):
response = authenticated_api_client.get(
f'/api/vsi-image-file/{image_file_geotiff.pk}/metadata?projection=EPSG:3857'
)
assert response.status_code == 200
assert status.is_success(response.status_code)
metadata = response.data
assert metadata['geospatial']
assert metadata['levels'] == 9
Expand All @@ -34,7 +35,7 @@ def test_metadata_s3(authenticated_api_client, s3_image_file_geotiff):
response = authenticated_api_client.get(
f'/api/s3-image-file/{s3_image_file_geotiff.pk}/metadata?projection=EPSG:3857'
)
assert response.status_code == 200
assert status.is_success(response.status_code)
metadata = response.data
assert metadata['geospatial']
assert metadata['levels'] == 9
Expand All @@ -48,7 +49,7 @@ def test_metadata_s3_vsi(authenticated_api_client, s3_image_file_geotiff):
response = authenticated_api_client.get(
f'/api/s3-vsi-image-file/{s3_image_file_geotiff.pk}/metadata?projection=EPSG:3857'
)
assert response.status_code == 200
assert status.is_success(response.status_code)
metadata = response.data
assert metadata['geospatial']
assert metadata['levels'] == 9
Expand All @@ -62,7 +63,7 @@ def test_metadata_internal(authenticated_api_client, image_file_geotiff):
response = authenticated_api_client.get(
f'/api/image-file/{image_file_geotiff.pk}/metadata_internal'
)
assert response.status_code == 200
assert status.is_success(response.status_code)
metadata = response.data
assert metadata['geospatial']
assert metadata['driverLongName']
Expand All @@ -71,15 +72,15 @@ def test_metadata_internal(authenticated_api_client, image_file_geotiff):
@pytest.mark.django_db(transaction=True)
def test_bands(authenticated_api_client, image_file_geotiff):
response = authenticated_api_client.get(f'/api/image-file/{image_file_geotiff.pk}/bands')
assert response.status_code == 200
assert status.is_success(response.status_code)
bands = response.data
assert isinstance(bands[1], dict)


@pytest.mark.django_db(transaction=True)
def test_band(authenticated_api_client, image_file_geotiff):
response = authenticated_api_client.get(f'/api/image-file/{image_file_geotiff.pk}/band?band=1')
assert response.status_code == 200
assert status.is_success(response.status_code)
band = response.data
assert band['interpretation']

Expand All @@ -89,7 +90,7 @@ def test_metadata_ome(authenticated_api_client, ome_image):
response = authenticated_api_client.get(
f'/api/image-file/{ome_image.pk}/metadata?source=ometiff'
)
assert response.status_code == 200
assert status.is_success(response.status_code)
metadata = response.data
assert 'frames' in metadata
assert len(metadata['frames'])
Expand All @@ -104,11 +105,11 @@ def test_bad_source(authenticated_api_client, image_file_geotiff):
response = authenticated_api_client.get(
f'/api/image-file/{image_file_geotiff.pk}/metadata?source=foo'
)
assert response.status_code == 400
assert status.is_client_error(response.status_code)


@pytest.mark.django_db(transaction=True)
def test_bad_image_data(authenticated_api_client, lonely_header_file):
# Catches server error safely and returns 500-level APIException
response = authenticated_api_client.get(f'/api/image-file/{lonely_header_file.pk}/metadata')
assert response.status_code == 500
assert status.is_server_error(response.status_code)
11 changes: 6 additions & 5 deletions project/example/core/tests/test_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@
from urllib.parse import quote

import pytest
from rest_framework import status


@pytest.mark.django_db(transaction=True)
def test_style_parameters(authenticated_api_client, image_file_geotiff):
response = authenticated_api_client.get(
f'/api/image-file/{image_file_geotiff.pk}/thumbnail.png?band=1&palette=viridis'
)
assert response.status_code == 200
assert status.is_success(response.status_code)
assert response['Content-Type'] == 'image/png'
response = authenticated_api_client.get(
f'/api/image-file/{image_file_geotiff.pk}/thumbnail.png?band=1&palette=green&scheme=discrete'
)
assert response.status_code == 200
assert status.is_success(response.status_code)
assert response['Content-Type'] == 'image/png'
response = authenticated_api_client.get(
f'/api/image-file/{image_file_geotiff.pk}/thumbnail.png?band=1&min=5&max=100&nodata=0'
)
assert response.status_code == 200
assert status.is_success(response.status_code)
assert response['Content-Type'] == 'image/png'


Expand All @@ -34,7 +35,7 @@ def test_style_encoded(authenticated_api_client, image_file_geotiff):
response = authenticated_api_client.get(
f'/api/image-file/{image_file_geotiff.pk}/thumbnail.png?style={style_encoded}',
)
assert response.status_code == 200
assert status.is_success(response.status_code)
assert response['Content-Type'] == 'image/png'
# TODO: might want to verify style was actually used

Expand All @@ -45,4 +46,4 @@ def test_bad_style_encoded(authenticated_api_client, image_file_geotiff):
response = authenticated_api_client.get(
f'/api/image-file/{image_file_geotiff.pk}/thumbnail.png?style={style_encoded}',
)
assert response.status_code == 400
assert status.is_client_error(response.status_code)
21 changes: 11 additions & 10 deletions project/example/core/tests/test_tiles.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
import pytest
from rest_framework import status


@pytest.mark.django_db(transaction=True)
def test_tile(authenticated_api_client, image_file_geotiff):
response = authenticated_api_client.get(
f'/api/image-file/{image_file_geotiff.pk}/tiles/1/0/0.png?projection=EPSG:3857'
)
assert response.status_code == 200
assert status.is_success(response.status_code)
assert response['Content-Type'] == 'image/png'
response = authenticated_api_client.get(
f'/api/image-file/{image_file_geotiff.pk}/tiles/1/0/0.jpeg?projection=EPSG:3857'
)
assert response.status_code == 200
assert status.is_success(response.status_code)
assert response['Content-Type'] == 'image/jpeg'
response = authenticated_api_client.get(
f'/api/image-file/{image_file_geotiff.pk}/tiles/100/0/0.png'
)
assert response.status_code == 400
assert status.is_client_error(response.status_code)


@pytest.mark.django_db(transaction=True)
def test_tile_corners(authenticated_api_client, image_file_geotiff):
response = authenticated_api_client.get(
f'/api/image-file/{image_file_geotiff.pk}/tiles/1/0/0/corners?projection=EPSG:3857'
)
assert response.status_code == 200
assert status.is_success(response.status_code)
data = response.data
assert data['proj4']

Expand All @@ -34,7 +35,7 @@ def test_tiles_metadata(authenticated_api_client, image_file_geotiff):
response = authenticated_api_client.get(
f'/api/image-file/{image_file_geotiff.pk}/tiles/metadata?projection=EPSG:3857'
)
assert response.status_code == 200
assert status.is_success(response.status_code)
metadata = response.data
assert metadata['geospatial']
assert metadata['levels'] == 9
Expand All @@ -51,19 +52,19 @@ def test_tiles_metadata(authenticated_api_client, image_file_geotiff):


@pytest.mark.django_db(transaction=True)
def test_non_geo_tiles(authenticated_api_client, non_geo_image):
response = authenticated_api_client.get(f'/api/image-file/{non_geo_image.pk}/tiles/0/0/0.png')
assert response.status_code == 200
def test_non_geo_tiles(authenticated_api_client, png_image):
response = authenticated_api_client.get(f'/api/image-file/{png_image.pk}/tiles/0/0/0.png')
assert status.is_success(response.status_code)
assert response['Content-Type'] == 'image/png'


@pytest.mark.django_db(transaction=True)
def test_ome_tiles(authenticated_api_client, ome_image):
response = authenticated_api_client.get(f'/api/image-file/{ome_image.pk}/tiles/0/0/0.png')
assert response.status_code == 200
assert status.is_success(response.status_code)
assert response['Content-Type'] == 'image/png'
response = authenticated_api_client.get(
f'/api/image-file/{ome_image.pk}/tiles/0/0/0.png?source=ometiff'
)
assert response.status_code == 200
assert status.is_success(response.status_code)
assert response['Content-Type'] == 'image/png'

0 comments on commit e709d2a

Please sign in to comment.