diff --git a/app/__init__.py b/app/__init__.py index 7e9ad60..8955201 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -4,17 +4,19 @@ from dotenv import load_dotenv from flask import Flask -app = Flask(__name__) +def create_app(): + """Application factory pattern.""" + # Load the env variables to use here + load_dotenv() -# Use app.config to set config connection details -load_dotenv() -app.config['MONGO_URI'] = os.getenv('MONGO_CONNECTION') -app.config['DB_NAME'] = os.getenv('PROJECT_DATABASE') -app.config['COLLECTION_NAME'] = os.getenv('PROJECT_COLLECTION') + app = Flask(__name__) + # Use app.config to set config connection details + app.config['MONGO_URI'] = os.getenv('MONGO_CONNECTION') + app.config['DB_NAME'] = os.getenv('PROJECT_DATABASE') + app.config['COLLECTION_NAME'] = os.getenv('PROJECT_COLLECTION') -# Import routes — routes can import app safely because it exists -from app.routes import register_routes # pylint: disable=wrong-import-position -register_routes(app) + # Import routes — routes can import app safely because it exists + from app.routes import register_routes # pylint: disable=import-outside-toplevel + register_routes(app) -# Expose `app` for importing -__all__ = ["app"] + return app diff --git a/app/routes.py b/app/routes.py index 4c04a76..37f55c2 100644 --- a/app/routes.py +++ b/app/routes.py @@ -68,7 +68,8 @@ def add_book(): # Send the host and new book_id to the helper function to generate links book_for_response = append_hostname(new_book, host) print("book_for_response", book_for_response) - # Remove MOngoDB's ObjectID value + + # Remove MongoDB's ObjectID value book_for_response.pop('_id', None) return jsonify(book_for_response), 201 diff --git a/app/utils/helper.py b/app/utils/helper.py index a08d6c2..f56b2b6 100644 --- a/app/utils/helper.py +++ b/app/utils/helper.py @@ -1,4 +1,4 @@ -""" Collection of helper fucntions """ +""" Collection of helper functions """ from urllib.parse import urljoin diff --git a/run.py b/run.py new file mode 100644 index 0000000..62858c1 --- /dev/null +++ b/run.py @@ -0,0 +1,8 @@ +"""Entry point for running the Flask application. + +This script imports the Flask application factory from the app package, +creates an instance of the application, and runs it in debug mode. +""" +from app import create_app + +app = create_app() diff --git a/tests/test_app.py b/tests/test_app.py index 93a234f..b9e3c01 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -1,18 +1,17 @@ # pylint: disable=missing-docstring -# Filepath is this: /Users/Shanti.Rai@methods.co.uk/Documents/S_BookAPIV.2/tests/test_app.py - from unittest.mock import patch from pymongo.errors import ServerSelectionTimeoutError import pytest from app.datastore.mongo_db import get_book_collection -from app import app +from app import create_app # Option 1: Rename the fixture to something unique (which I've used) # Option 2: Use a linter plugin that understands pytest @pytest.fixture(name="client") def client_fixture(): + app = create_app() app.config['TESTING'] = True return app.test_client() @@ -561,6 +560,7 @@ def test_get_book_collection_handles_connection_failure(): # Set the side effect to raise a ServerSelectionTimeoutError mock_client.side_effect = ServerSelectionTimeoutError("Mock Connection Timeout") + app = create_app() with app.app_context(): # <-- Push the app context here with pytest.raises(Exception) as exc_info: get_book_collection() diff --git a/tests/test_integration.py b/tests/test_integration.py index 44056d0..15f45d0 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -1,12 +1,13 @@ # pylint: disable=missing-docstring import pytest from pymongo import MongoClient -from app import app +from app import create_app @pytest.fixture(name="client") def client_fixture(): """Provides a test client for making requests to our Flask app.""" + app = create_app() app.config['TESTING'] = True app.config['MONGO_URI'] = 'mongodb://localhost:27017/' app.config['DB_NAME'] = 'test_database' diff --git a/tests/test_mongo_helper.py b/tests/test_mongo_helper.py index d50d1a5..9f073a2 100644 --- a/tests/test_mongo_helper.py +++ b/tests/test_mongo_helper.py @@ -1,6 +1,5 @@ # pylint: disable=missing-docstring -# Filepath: #/Users/Shanti.Rai@methods.co.uk/Documents/S_BookAPIV.2/app/datastore/mongo_helper.p from unittest.mock import MagicMock from app.datastore.mongo_helper import insert_book_to_mongo