Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
159d610
Add init.py to initialize utils/ and move append_hostname there
codesungrape Jun 19, 2025
9a7c90f
Move MongoDB connection logic to datastore.mongo_db module
codesungrape Jun 19, 2025
d5a287c
Fix connection failure test by adding app context
codesungrape Jun 19, 2025
769d378
Move mongo_helper.py to datastore/; update import paths
codesungrape Jun 20, 2025
ac8269f
Restructure app with app/__init__.py, routes module, avoid circulars
codesungrape Jun 20, 2025
6e7f474
Fix tests to import app from package and patch updated paths
codesungrape Jun 20, 2025
b64bd48
Tweak code to resolve pylint warnings and disable select rules
codesungrape Jun 20, 2025
e584c11
Move datastore/ and utils/ into app/ and update imports
codesungrape Jun 20, 2025
54bf43a
Add run.py as app entry point; refactor app/__init__.py to use create…
codesungrape Jun 23, 2025
b55ad4a
Refactor test setup: update imports and initialize app using create_a…
codesungrape Jun 23, 2025
16b3c91
Fix pylint import warning by replacing wrong-import-position with imp…
codesungrape Jun 23, 2025
b531ed8
Remove unused global app exposure in __init__.py; rely on create_app …
codesungrape Jun 23, 2025
543c28b
Remove app.run() block; rely on Flask CLI with create_app factory pat…
codesungrape Jun 23, 2025
e46aba2
Remove app.run() block; rely on Flask CLI with create_app factory pat…
codesungrape Jun 23, 2025
3d26b71
Fix spelling errors and remove unnecessary filepath comments
codesungrape Jun 23, 2025
00c711f
Merge branch 'main' into modularize-app-structure
codesungrape Jun 23, 2025
f46c5dd
Resolve pylint trailingnewline error
codesungrape Jun 23, 2025
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
24 changes: 13 additions & 11 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion app/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link

Copilot AI Jun 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] This looks like a debug print and may clutter production logs. Consider removing or replacing with a proper logger call.

Suggested change
print("book_for_response", book_for_response)
logging.debug("book_for_response: %s", book_for_response)

Copilot uses AI. Check for mistakes.
# Remove MOngoDB's ObjectID value

# Remove MongoDB's ObjectID value
book_for_response.pop('_id', None)

return jsonify(book_for_response), 201
Expand Down
2 changes: 1 addition & 1 deletion app/utils/helper.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
""" Collection of helper fucntions """
""" Collection of helper functions """

from urllib.parse import urljoin

Expand Down
8 changes: 8 additions & 0 deletions run.py
Original file line number Diff line number Diff line change
@@ -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()
6 changes: 3 additions & 3 deletions tests/test_app.py
Original file line number Diff line number Diff line change
@@ -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()

Expand Down Expand Up @@ -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()
Copy link

Copilot AI Jun 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test creates an app but does not configure MONGO_URI and DB_NAME, which get_book_collection depends on. Configure these in the test before pushing app context.

Suggested change
app = create_app()
app = create_app()
app.config['MONGO_URI'] = "mongodb://localhost:27017/test_db"
app.config['DB_NAME'] = "test_db"

Copilot uses AI. Check for mistakes.
with app.app_context(): # <-- Push the app context here
with pytest.raises(Exception) as exc_info:
get_book_collection()
Expand Down
3 changes: 2 additions & 1 deletion tests/test_integration.py
Original file line number Diff line number Diff line change
@@ -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'
Copy link

Copilot AI Jun 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The client fixture does not return an app or test client. Add return app.test_client() so tests receive a valid client instance.

Copilot uses AI. Check for mistakes.
Expand Down
1 change: 0 additions & 1 deletion tests/test_mongo_helper.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down