Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use an app factory pattern to enable app testing #495

Merged
merged 12 commits into from
Aug 3, 2023
Merged

Use an app factory pattern to enable app testing #495

merged 12 commits into from
Aug 3, 2023

Conversation

pamelafox
Copy link
Collaborator

Purpose

This PR changes app.py to use an app factory pattern (create_app) along with a Flask Blueprint to organize the code. This makes testing much easier and is a general best practice.

This PR is based off the previous PR that ruff-linted the files, so that needs to be merged first to isolate the changes. Then I'll turn the draft into an actual PR.

Does this introduce a breaking change?

[ ] Yes
[X] No

Pull Request Type

What kind of change does this Pull Request introduce?

[ ] Bugfix
[ ] Feature
[ ] Code style update (formatting, local variables)
[X] Refactoring (no functional changes, no api changes)
[ ] Documentation content changes
[ ] Other... Please describe:

How to Test

  • python3 -m pytest
  • Run start.sh in app
  • Run azd up for prod

@pamelafox pamelafox marked this pull request as ready for review August 2, 2023 22:16
@@ -14,14 +14,23 @@ jobs:
fail-fast: false
matrix:
os: ["ubuntu-20.04"]
python_version: ["3.8", "3.9", "3.10", "3.11"]
python_version: ["3.9", "3.10", "3.11"]
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Removing 3.8 since we don't currently support it (due to our typing syntax). We can add it back if its an issue for customers.

run: |
cd ./app/frontend
npm install
npm run build
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This must be built since the Flask tests try to hit up index.html


@bp.route("/assets/<path:path>")
def assets(path):
return send_from_directory("static/assets", path)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is a security upgrade over the previous route that used send_static_file for arbitrary paths.

def ask():
if not request.json:
return jsonify({"error": "request must be json"}), 400
if not request.is_json:
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This was a bug, caught by the new tests. Calling request.json causes an immediate 415 HTTP error if theres no JSON passed in, thus it's now request.is_json.

@tonybaloney
Copy link
Contributor

Great improvement, this will cause a few merge conflicts with existing PRs, but that can't be avoided

@pamelafox pamelafox merged commit df48d8c into Azure-Samples:main Aug 3, 2023
5 checks passed
@pamelafox pamelafox deleted the create_app branch August 3, 2023 13:29
JohananLM added a commit to JohananLM/azure-search-openai-demo that referenced this pull request Aug 9, 2023
Use an app factory pattern to enable app testing (Azure-Samples#495)
HughRunyan pushed a commit to RMI/RMI_chatbot that referenced this pull request Mar 26, 2024
* Run ruff, add to precommit

* Blueprint

* Change to blueprint, app factory

* Fixing the imports

* Build frontend

* Update startup

* Update startup

* Gunicorn command update

* Revert yaml change
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants