Skip to content
Snippets Groups Projects
Commit 3a070c52 authored by Riccardo Boero's avatar Riccardo Boero :innocent:
Browse files

Restructuring of test files and requests

parent c9a619fc
No related branches found
No related tags found
No related merge requests found
Pipeline #2010 failed
def test_invalid_polygon(client):
# Create a payload with an invalid polygon
payload = {
"polygon_wkt": "INVALIDPOLYGON",
"polygon_crs": "EPSG:4326"
}
# Make a POST request to the service
response = client.post('/band_statistics/', data=json.dumps(payload), content_type='application/json')
# Assert that the response returns an error
assert response.status_code == 500
data = response.get_json()
assert 'error' in data
assert 'Invalid WKT' in data['error'] # Assuming invalid WKT error message
import pytest
from flask import json
from polygonqueryonraster.app import GeoTIFFService
import tempfile
import os
@pytest.fixture
def client():
# Create a temporary directory to simulate the directory structure
with tempfile.TemporaryDirectory() as tempdir:
# Initialize the Flask app with a test directory
service = GeoTIFFService(directory=tempdir)
service.app.config['TESTING'] = True
client = service.app.test_client()
# Yield the test client
yield client
def test_band_statistics(client):
# Create a dummy request payload
payload = {
"polygon_wkt": "POLYGON((0 0, 1 0, 1 1, 0 1, 0 0))",
"polygon_crs": "EPSG:4326"
}
# Make a POST request to the service
response = client.post('/band_statistics/', data=json.dumps(payload), content_type='application/json')
# Assert the response status code and response data
assert response.status_code == 500 # Expecting error as no GeoTIFF data exists in the directory
assert 'error' in response.get_json()
import pytest
import numpy as np
from unittest import mock
from shapely.geometry import Polygon
from polygonqueryonraster.app import GeoTIFFService
def test_process_geotiff():
# Create a dummy polygon for the test
polygon = Polygon([(0, 0), (1, 0), (1, 1), (0, 1)])
polygon_crs = 'EPSG:4326'
# Mock the directory and rasterio interactions
with mock.patch('glob.glob') as mock_glob, \
mock.patch('rasterio.open') as mock_raster:
# Mock glob to return a dummy file path
mock_glob.return_value = ['dummy.tif']
# Mock the rasterio open function
mock_raster.return_value.__enter__.return_value.read.return_value = np.random.rand(1, 10, 10)
mock_raster.return_value.__enter__.return_value.transform = mock.Mock()
mock_raster.return_value.__enter__.return_value.crs = 'EPSG:4326'
# Instantiate the service
service = GeoTIFFService(directory='/dummy/path')
# Call process_geotiff and verify that it returns stats
stats = service.process_geotiff(polygon, polygon_crs, '/dummy/path')
assert 'band_0' in stats
assert 'count' in stats['band_0']
# tests/test_run_service.py
import os
class GeoTIFFService:
# Existing methods...
def setup_routes(self):
# Ensure the directory exists and contains subdirectories
if os.path.exists(self.directory):
subdirs = next(os.walk(self.directory), (None, []))[1]
if not subdirs:
print(f"No subdirectories found in {self.directory}")
else:
for subdir in subdirs:
subdir_path = os.path.join(self.directory, subdir)
self.create_route(f'/{subdir}', subdir_path)
else:
raise ValueError(f"Directory {self.directory} does not exist.")
if __name__ == '__main__':
test_run_service()
import tempfile
import os
import threading
import time
from polygonqueryonraster.app import GeoTIFFService
def test_flask_app():
# Create a temporary directory for testing
with tempfile.TemporaryDirectory() as tempdir:
# Optionally, create a dummy subdirectory in the temporary directory to simulate subdirectories
os.makedirs(os.path.join(tempdir, 'dummy_subdir'))
# Initialize the service with the temporary directory
service = GeoTIFFService(tempdir, port=5001)
# Start the Flask app in a separate thread
thread = threading.Thread(target=service.run)
thread.start()
# Give the server some time to start
time.sleep(2)
try:
# Test that the Flask app responds correctly
import requests
response = requests.get('http://127.0.0.1:5001/api')
assert response.status_code == 200
assert response.json()["message"] == "Hello, this is a simple REST API!"
finally:
# Stop the Flask app
thread.join(timeout=1)
if __name__ == '__main__':
test_flask_app()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment