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

Modification of testing to use mocked input directories

parent f96ad342
No related branches found
No related tags found
No related merge requests found
Pipeline #2004 failed
# tests/test_run_service.py # tests/test_run_service.py
from polygonqueryonraster.app import app import os
import requests
import threading
def test_run_service(): class GeoTIFFService:
# Start the Flask development server in a separate thread # Existing methods...
thread = threading.Thread(target=app.run, kwargs={'debug': False, 'port': 5001})
thread.start()
# Wait for the server to start def setup_routes(self):
import time # Ensure the directory exists and contains subdirectories
time.sleep(1) 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.")
try:
# Make a sample GET request
response_get = requests.get('http://127.0.0.1:5001/api')
assert response_get.status_code == 200
assert response_get.json()["message"] == "Hello, this is a simple REST API!"
# Make a sample POST request
response_post = requests.post('http://127.0.0.1:5001/api', json={"message": "Updated message!"})
assert response_post.status_code == 200
assert response_post.json()["message"] == "Updated message!"
finally:
# Shut down the Flask development server
thread.join(timeout=1)
if __name__ == '__main__': if __name__ == '__main__':
test_run_service() test_run_service()
from polygonqueryonraster.app import app import tempfile
import requests
import threading
import time
from polygonqueryonraster.app import GeoTIFFService
def test_flask_app():
# Create a temporary directory for testing
with tempfile.TemporaryDirectory() as tempdir:
# Start the Flask app
service = GeoTIFFService(tempdir, port=5001)
thread = threading.Thread(target=service.run)
thread.start()
# Give it time to start
time.sleep(2)
try:
# Send a request to the Flask API
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 server
thread.join(timeout=1)
if __name__ == '__main__': if __name__ == '__main__':
app.run(debug=True, port=5001) # You can specify the port of your choice 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