diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 5d452f0..adc3cad 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -1,6 +1,17 @@ { - "name": "Python 3", - "image": "mcr.microsoft.com/devcontainers/python:3.11", - "postCreateCommand": "pip3 install --user -r requirements.txt" + "name": "Python 3", + "image": "mcr.microsoft.com/devcontainers/python:3.11", + "postCreateCommand": "pip3 install --user -r requirements.txt", + "customizations": { + "vscode": { + "extensions": [ + "charliermarsh.ruff", + "GitHub.vscode-pull-request-github", + "ms-python.python", + "ms-python.black-formatter", + "stkb.rewrap", + "DavidAnson.vscode-markdownlint" + ] + } + } } - diff --git a/.github/workflows/build-push-docker.yml b/.github/workflows/github-workflows.yml similarity index 62% rename from .github/workflows/build-push-docker.yml rename to .github/workflows/github-workflows.yml index a5edfb8..7aa3950 100644 --- a/.github/workflows/build-push-docker.yml +++ b/.github/workflows/github-workflows.yml @@ -1,6 +1,6 @@ -name: Docker build and push to ghcr.io/ai-cfia and lint test +name: python-backend-workflows -on: +on: pull_request: types: - opened @@ -12,6 +12,16 @@ jobs: uses: ai-cfia/github-workflows/.github/workflows/workflow-lint-test-python.yml@main secrets: inherit + markdown-check: + uses: ai-cfia/github-workflows/.github/workflows/workflow-markdown-check.yml@main + with: + config-file-path: '.mlc_config.json' + secrets: inherit + + repo-standard: + uses: ai-cfia/github-workflows/.github/workflows/workflow-repo-standards-validation.yml@main + secrets: inherit + deploy: uses: ai-cfia/github-workflows/.github/workflows/workflow-build-push-container-github-registry.yml@63-as-a-devops-i-would-like-to-create-a-workflow-to-push-images-to-this-organisation-docker-registry with: diff --git a/.mlc_config.json b/.mlc_config.json new file mode 100644 index 0000000..aa79d75 --- /dev/null +++ b/.mlc_config.json @@ -0,0 +1,3 @@ +{ + "aliveStatusCodes": [999,200,403] +} diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..d156db8 --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,9 @@ +{ + "recommendations": [ + "stkb.rewrap", + "DavidAnson.vscode-markdownlint" + ], + "unwantedRecommendations": [ + + ] +} diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..39f3fd2 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "editor.rulers": [80], // used for .md files + "files.trimTrailingWhitespace": true, + "files.trimFinalNewlines": true, + "files.insertFinalNewline": true +} diff --git a/TESTING.md b/TESTING.md new file mode 100644 index 0000000..7969daf --- /dev/null +++ b/TESTING.md @@ -0,0 +1,7 @@ +# Testing + +Run: + +```bash +python -m unittest discover -s tests +``` diff --git a/app/blueprints/search.py b/app/blueprints/search.py index b9a7e5e..0ac614c 100644 --- a/app/blueprints/search.py +++ b/app/blueprints/search.py @@ -1,7 +1,7 @@ import logging from flask import Blueprint, current_app, jsonify, request -from index_search import AzureIndexSearchQueryError, search +from index_search import AzureIndexSearchError, search from app.ailab_db import DBError, ailab_db_search from app.blueprints.common import create_error_response @@ -15,7 +15,7 @@ class EmptyQueryError(Exception): """Raised when the search query is empty.""" -@search_blueprint.errorhandler(AzureIndexSearchQueryError) +@search_blueprint.errorhandler(AzureIndexSearchError) def handle_azure_error(error): message = current_app.config["ERROR_AZURE_FAILED"] return create_error_response(error, message, 500) diff --git a/docs/DEPLOYMENT.md b/docs/DEPLOYMENT.md index 91701d4..56b2522 100644 --- a/docs/DEPLOYMENT.md +++ b/docs/DEPLOYMENT.md @@ -1,6 +1,9 @@ # Deployment -Note on the container: Create and set the environment variables based on `.env.template`. Make sure -`FINESSE_BACKEND_DEBUG_MODE` is NOT set to `True` in PRODUCTION builds. +Note on the container: Create and set the environment variables based on +`.env.template`. Make sure `FINESSE_BACKEND_DEBUG_MODE` is NOT set to `True` in +PRODUCTION builds. -Refer to the [infrastructure repository](https://github.com/ai-cfia/infra) for a Kubernetes deployment. The secrets (in this case environment variables) are managed with Hashicorp Vault. \ No newline at end of file +Refer to the [infrastructure repository](https://github.com/ai-cfia/infra) for a +Kubernetes deployment. The secrets (in this case environment variables) are +managed with Hashicorp Vault. diff --git a/docs/INSTALLATION.md b/docs/INSTALLATION.md index d641f43..9095662 100644 --- a/docs/INSTALLATION.md +++ b/docs/INSTALLATION.md @@ -2,9 +2,9 @@ 1. Clone the repository: -```bash -git clone https://github.com/ai-cfia/finesse-backend.git -``` + ```bash + git clone https://github.com/ai-cfia/finesse-backend.git + ``` 2. Open in devcontainer. @@ -12,20 +12,20 @@ Alternatively, 1. Clone the repository: -```bash -git clone https://github.com/ai-cfia/finesse-backend.git -cd finesse-frontend -``` + ```bash + git clone https://github.com/ai-cfia/finesse-backend.git + cd finesse-frontend + ``` 2. Create a virtual environment: -```bash -python -m venv venv -source venv/bin/activate -``` + ```bash + python -m venv venv + source venv/bin/activate + ``` 3. Install the dependencies: -```bash -pip install -r requirements.txt -``` + ```bash + pip install -r requirements.txt + ``` diff --git a/docs/USAGE.md b/docs/USAGE.md index a3b08f4..1354a8e 100644 --- a/docs/USAGE.md +++ b/docs/USAGE.md @@ -2,15 +2,16 @@ ## With your local environment -Create and set the environment variables based on `.env.template`. You will need to create a `.env` file. +Create and set the environment variables based on `.env.template`. You will need +to create a `.env` file. -### Running: +### Running ```bash flask run -h 0.0.0.0 --debug ``` -Alternatively, set the `FINESSE_BACKEND_DEBUG_MODE` environment variable to +Alternatively, set the `FINESSE_BACKEND_DEBUG_MODE` environment variable to `True` and run: ```bash @@ -19,7 +20,8 @@ python run.py ## With docker -Create and set the environment variables based on `.env.template`. You will need to create a `.env` file. +Create and set the environment variables based on `.env.template`. You will need +to create a `.env` file. Build the container: diff --git a/tests/test_azure_search.py b/tests/test_azure_search.py index 4f8ff89..9d0e793 100644 --- a/tests/test_azure_search.py +++ b/tests/test_azure_search.py @@ -2,7 +2,7 @@ from unittest.mock import patch from app.app_creator import create_app -from app.blueprints.search import AzureIndexSearchQueryError +from app.blueprints.search import AzureIndexSearchError from tests.common import TestConfig @@ -25,6 +25,6 @@ def test_search_azure_no_query(self): def test_search_azure_error(self): with patch("app.blueprints.search.search") as mock_search: - mock_search.side_effect = AzureIndexSearchQueryError("Azure search failed") + mock_search.side_effect = AzureIndexSearchError("Azure search failed") response = self.client.post("/search/azure", json={"query": "azure query"}) self.assertEqual(response.status_code, 500) diff --git a/tests/test_search.py b/tests/test_search.py index a763d53..0c3b033 100644 --- a/tests/test_search.py +++ b/tests/test_search.py @@ -2,7 +2,7 @@ from unittest.mock import patch from app.app_creator import create_app -from app.blueprints.search import AzureIndexSearchQueryError +from app.blueprints.search import AzureIndexSearchError from tests.common import TestConfig @@ -50,7 +50,7 @@ def test_search_azure_no_query(self): def test_error_handling_debug_mode(self): self.app.config["DEBUG"] = True with patch("app.blueprints.search.search") as mock_func: - mock_func.side_effect = AzureIndexSearchQueryError("Sample Error") + mock_func.side_effect = AzureIndexSearchError("Sample Error") response = self.client.post("/search/azure", json={"query": "test query"}) self.assertEqual(response.status_code, 500) self.assertIn("trace", response.json) @@ -59,7 +59,7 @@ def test_error_handling_debug_mode(self): def test_error_handling_non_debug_mode(self): self.app.config["DEBUG"] = False with patch("app.blueprints.search.search") as mock_func: - mock_func.side_effect = AzureIndexSearchQueryError("Sample Error") + mock_func.side_effect = AzureIndexSearchError("Sample Error") response = self.client.post("/search/azure", json={"query": "test query"}) self.assertEqual(response.status_code, 500) self.assertNotIn("trace", response.json)