From f4048d60053702461568af0fd250aed9f017ed7a Mon Sep 17 00:00:00 2001 From: Manon Deloupy Date: Wed, 25 Oct 2023 11:14:37 +0200 Subject: [PATCH 1/7] Initialize Azure functions project --- .funcignore | 8 +++ .gitignore | 135 ++++++++++++++++++++++++++++++++++++++++ .vscode/extensions.json | 6 ++ .vscode/launch.json | 12 ++++ .vscode/settings.json | 8 +++ .vscode/tasks.json | 27 ++++++++ host.json | 15 +++++ requirements.txt | 5 ++ 8 files changed, 216 insertions(+) create mode 100644 .funcignore create mode 100644 .gitignore create mode 100644 .vscode/extensions.json create mode 100644 .vscode/launch.json create mode 100644 .vscode/settings.json create mode 100644 .vscode/tasks.json create mode 100644 host.json create mode 100644 requirements.txt diff --git a/.funcignore b/.funcignore new file mode 100644 index 0000000..9966315 --- /dev/null +++ b/.funcignore @@ -0,0 +1,8 @@ +.git* +.vscode +__azurite_db*__.json +__blobstorage__ +__queuestorage__ +local.settings.json +test +.venv \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7685fc4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,135 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +pip-wheel-metadata/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +.hypothesis/ +.pytest_cache/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +.python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don’t work, or not +# install all needed dependencies. +#Pipfile.lock + +# celery beat schedule file +celerybeat-schedule + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# Azure Functions artifacts +bin +obj +appsettings.json +local.settings.json + +# Azurite artifacts +__blobstorage__ +__queuestorage__ +__azurite_db*__.json +.python_packages \ No newline at end of file diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..3f63eb9 --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,6 @@ +{ + "recommendations": [ + "ms-azuretools.vscode-azurefunctions", + "ms-python.python" + ] +} \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..ea3e0f1 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,12 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "Attach to Python Functions", + "type": "python", + "request": "attach", + "port": 9091, + "preLaunchTask": "func: host start" + } + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..ef4ffb5 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,8 @@ +{ + "azureFunctions.deploySubpath": ".", + "azureFunctions.scmDoBuildDuringDeployment": true, + "azureFunctions.pythonVenv": ".venv", + "azureFunctions.projectLanguage": "Python", + "azureFunctions.projectRuntime": "~4", + "debug.internalConsoleOptions": "neverOpen" +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..0c465f1 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,27 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "type": "func", + "label": "func: host start", + "command": "host start", + "problemMatcher": "$func-python-watch", + "isBackground": true, + "dependsOn": "pip install (functions)" + }, + { + "label": "pip install (functions)", + "type": "shell", + "osx": { + "command": "${config:azureFunctions.pythonVenv}/bin/python -m pip install -r requirements.txt" + }, + "windows": { + "command": "${config:azureFunctions.pythonVenv}\\Scripts\\python -m pip install -r requirements.txt" + }, + "linux": { + "command": "${config:azureFunctions.pythonVenv}/bin/python -m pip install -r requirements.txt" + }, + "problemMatcher": [] + } + ] +} \ No newline at end of file diff --git a/host.json b/host.json new file mode 100644 index 0000000..fd4bee7 --- /dev/null +++ b/host.json @@ -0,0 +1,15 @@ +{ + "version": "2.0", + "logging": { + "applicationInsights": { + "samplingSettings": { + "isEnabled": true, + "excludedTypes": "Request" + } + } + }, + "extensionBundle": { + "id": "Microsoft.Azure.Functions.ExtensionBundle", + "version": "[3.*, 4.0.0)" + } +} \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..bdb8fc5 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,5 @@ +# DO NOT include azure-functions-worker in this file +# The Python Worker is managed by Azure Functions platform +# Manually managing azure-functions-worker may cause unexpected issues + +azure-functions From 3af3c56a7ab9bad4e80b99121ed6053cc8525c1d Mon Sep 17 00:00:00 2001 From: Manon Deloupy Date: Wed, 25 Oct 2023 11:31:19 +0200 Subject: [PATCH 2/7] Add ms-dataverse ORM to requirements.txt --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index bdb8fc5..09dd260 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,3 +3,4 @@ # Manually managing azure-functions-worker may cause unexpected issues azure-functions +ms-dataverse \ No newline at end of file From c14ba74378f5684b0f5f9f9ae556be10506081c7 Mon Sep 17 00:00:00 2001 From: Manon Deloupy Date: Wed, 25 Oct 2023 12:03:05 +0200 Subject: [PATCH 3/7] Update README --- README.md | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8ec4b68..35af7f1 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,21 @@ # Core-App -This will contain the core of the Horseball App - Horseball App is a League Management System for horseball competitions +> This will contain the core of the Horseball App - Horseball App is a League Management System for horseball competitions + +## Installation +This project uses the Python package manager `pip`. To install the required packages, run the following command : + +```bash +pip install -r requirements.txt +``` + +## Dependencies +- Python 3.9+ + +## Usage + +> This section will be populated when adding new features. + +## License + +> This section will be updated soon. + From 604d55b4aec5dac046400bb2e66a2a6e8901eb7c Mon Sep 17 00:00:00 2001 From: Manon Deloupy Date: Wed, 1 Nov 2023 11:48:53 +0100 Subject: [PATCH 4/7] Add LeagueAdd function --- LeagueAdd/__init__.py | 22 ++++++++++++++++++++++ LeagueAdd/function.json | 20 ++++++++++++++++++++ LeagueAdd/sample.dat | 3 +++ 3 files changed, 45 insertions(+) create mode 100644 LeagueAdd/__init__.py create mode 100644 LeagueAdd/function.json create mode 100644 LeagueAdd/sample.dat diff --git a/LeagueAdd/__init__.py b/LeagueAdd/__init__.py new file mode 100644 index 0000000..a39d48f --- /dev/null +++ b/LeagueAdd/__init__.py @@ -0,0 +1,22 @@ +import logging + +import azure.functions as func + + +def main(req: func.HttpRequest) -> func.HttpResponse: + logging.info('Python HTTP trigger function processed a request.') + + name = req.params.get('name') or req.get_json().get('name') + country = req.params.get('country') or req.get_json.get('country') + + if name: + # Insert to Dataverse DB + # league = orm.entity("leagues") + # league.create({"name": name, "country": country}) + + return func.HttpResponse("League successfully inserted. Name : #{name}, country : #{country}.") + else: + return func.HttpResponse( + "Params are missing. Please include at least a 'name' to create a league.", + status_code=422 + ) diff --git a/LeagueAdd/function.json b/LeagueAdd/function.json new file mode 100644 index 0000000..d901965 --- /dev/null +++ b/LeagueAdd/function.json @@ -0,0 +1,20 @@ +{ + "scriptFile": "__init__.py", + "bindings": [ + { + "authLevel": "function", + "type": "httpTrigger", + "direction": "in", + "name": "req", + "methods": [ + "get", + "post" + ] + }, + { + "type": "http", + "direction": "out", + "name": "$return" + } + ] +} \ No newline at end of file diff --git a/LeagueAdd/sample.dat b/LeagueAdd/sample.dat new file mode 100644 index 0000000..2e60943 --- /dev/null +++ b/LeagueAdd/sample.dat @@ -0,0 +1,3 @@ +{ + "name": "Azure" +} \ No newline at end of file From 91627d5c9449a079e7e32d4de178216f3802dc95 Mon Sep 17 00:00:00 2001 From: Manon Deloupy Date: Wed, 1 Nov 2023 11:55:13 +0100 Subject: [PATCH 5/7] Init test file for LeagueAdd --- LeagueAdd/__init__.py | 1 - requirements.txt | 3 ++- tests/__init__.py | 0 tests/league_add_test.py | 9 +++++++++ 4 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 tests/__init__.py create mode 100644 tests/league_add_test.py diff --git a/LeagueAdd/__init__.py b/LeagueAdd/__init__.py index a39d48f..0608522 100644 --- a/LeagueAdd/__init__.py +++ b/LeagueAdd/__init__.py @@ -2,7 +2,6 @@ import azure.functions as func - def main(req: func.HttpRequest) -> func.HttpResponse: logging.info('Python HTTP trigger function processed a request.') diff --git a/requirements.txt b/requirements.txt index 09dd260..6b724b4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,4 +3,5 @@ # Manually managing azure-functions-worker may cause unexpected issues azure-functions -ms-dataverse \ No newline at end of file +ms-dataverse +pytest \ No newline at end of file diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/league_add_test.py b/tests/league_add_test.py new file mode 100644 index 0000000..9e9623d --- /dev/null +++ b/tests/league_add_test.py @@ -0,0 +1,9 @@ +import unittest +import azure.functions as func + +from LeagueAdd import main +from unittest import mock + +class TestLeagueAdd(unittest.TestCase): + def test_league_add_success(self): + assert True == True \ No newline at end of file From 6a0b3211026f8478efe8db5bb64609550fd0c6aa Mon Sep 17 00:00:00 2001 From: Manon Deloupy Date: Wed, 1 Nov 2023 13:47:13 +0100 Subject: [PATCH 6/7] Add success test for LeagueAdd --- LeagueAdd/__init__.py | 4 ++-- LeagueAdd/function.json | 4 ++++ tests/league_add_test.py | 17 ++++++++++++++++- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/LeagueAdd/__init__.py b/LeagueAdd/__init__.py index 0608522..e48e072 100644 --- a/LeagueAdd/__init__.py +++ b/LeagueAdd/__init__.py @@ -6,14 +6,14 @@ def main(req: func.HttpRequest) -> func.HttpResponse: logging.info('Python HTTP trigger function processed a request.') name = req.params.get('name') or req.get_json().get('name') - country = req.params.get('country') or req.get_json.get('country') + country = req.params.get('country') or req.get_json().get('country') if name: # Insert to Dataverse DB # league = orm.entity("leagues") # league.create({"name": name, "country": country}) - return func.HttpResponse("League successfully inserted. Name : #{name}, country : #{country}.") + return func.HttpResponse(f"League successfully created. Name : {name}, country : {country}.") else: return func.HttpResponse( "Params are missing. Please include at least a 'name' to create a league.", diff --git a/LeagueAdd/function.json b/LeagueAdd/function.json index d901965..6826c68 100644 --- a/LeagueAdd/function.json +++ b/LeagueAdd/function.json @@ -15,6 +15,10 @@ "type": "http", "direction": "out", "name": "$return" + }, + { + "name": "league_add", + "path": "/api/leagues/create" } ] } \ No newline at end of file diff --git a/tests/league_add_test.py b/tests/league_add_test.py index 9e9623d..57fb45d 100644 --- a/tests/league_add_test.py +++ b/tests/league_add_test.py @@ -1,9 +1,24 @@ import unittest import azure.functions as func +import json from LeagueAdd import main from unittest import mock class TestLeagueAdd(unittest.TestCase): def test_league_add_success(self): - assert True == True \ No newline at end of file + # Request + request = func.HttpRequest( + method='POST', + url='api/leagues/create', + body=json.dumps({ + 'name': 'Foobar', + 'country': 'can' + }).encode('utf8') + ) + + response = main(request) + + # Assertions + assert response.status_code == 200 + assert "League successfully created. Name : Foobar, country : can." in response.get_body().decode() \ No newline at end of file From 8c25670c728792ad9cf71966c4df87f03b9c3586 Mon Sep 17 00:00:00 2001 From: Manon Deloupy Date: Wed, 1 Nov 2023 13:58:38 +0100 Subject: [PATCH 7/7] Add 422 test --- tests/league_add_test.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/tests/league_add_test.py b/tests/league_add_test.py index 57fb45d..e7d43ab 100644 --- a/tests/league_add_test.py +++ b/tests/league_add_test.py @@ -6,8 +6,8 @@ from unittest import mock class TestLeagueAdd(unittest.TestCase): - def test_league_add_success(self): - # Request + def test_league_add_200(self): + # Setup request = func.HttpRequest( method='POST', url='api/leagues/create', @@ -21,4 +21,18 @@ def test_league_add_success(self): # Assertions assert response.status_code == 200 - assert "League successfully created. Name : Foobar, country : can." in response.get_body().decode() \ No newline at end of file + assert "League successfully created. Name : Foobar, country : can." in response.get_body().decode() + + def test_league_add_422(self): + # Setup + request = func.HttpRequest( + method='POST', + url='api/leagues/create', + body=json.dumps({}).encode('utf8') + ) + + response = main(request) + + # Assertions + assert response.status_code == 422 + assert "Params are missing. Please include at least a 'name' to create a league." in response.get_body().decode()