From be4e47ad8e58981bd57d92b7f2168f0db15df295 Mon Sep 17 00:00:00 2001 From: andrew0022 Date: Sun, 3 Dec 2023 21:36:04 -0500 Subject: [PATCH 1/7] all test webapp --- .github/workflows/app-test.yml | 43 +++++++++++++++++++++++++++ Pipfile | 4 +++ Pipfile.lock | 53 +++++++++++++++++++++++++++++++++- web-app/app.py | 35 +++++++++++++--------- 4 files changed, 121 insertions(+), 14 deletions(-) create mode 100644 .github/workflows/app-test.yml diff --git a/.github/workflows/app-test.yml b/.github/workflows/app-test.yml new file mode 100644 index 0000000..9ef1c05 --- /dev/null +++ b/.github/workflows/app-test.yml @@ -0,0 +1,43 @@ +name: app tests + +run-name: ${{ github.actor }} + +on: + push: + branches: [main, master] + pull_request: + branches: [main, master] + +jobs: + test: + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + python-version: ["3.11"] + + steps: + - name: Checkout repository + uses: actions/checkout@v2 + + - name: Set up Python + uses: actions/setup-python@v3 + with: + python-version: ${{ matrix.python-version }} + + - name: Install dependencies + run: | + sudo apt-get update + sudo apt-get install -y portaudio19-dev + sudo apt-get install -y ffmpeg + python -m pip install --upgrade pip + python -m pip install pipenv + pipenv shell --fancy --python $(which python) + pipenv install -r requirements.txt + + working-directory: web-app + + - name: Run tests + run: | + pipenv run pytest --cov=app.py test_app.py + working-directory: web-app/app_tests diff --git a/Pipfile b/Pipfile index 3089624..3b0dab9 100644 --- a/Pipfile +++ b/Pipfile @@ -22,6 +22,10 @@ requests = "*" coverage = "*" pytest-flask = "*" pytest-cov = "*" +pytz = "*" +python-dateutil = "*" +build = "*" +gitcommitlogger = "*" [dev-packages] diff --git a/Pipfile.lock b/Pipfile.lock index c351d64..40498e2 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "9965c4d8f357b21858c378618e12d8215581b0735e07076d5acdeae00d641374" + "sha256": "fb3c86dc44179c576d242d24d86d7c5794b509538b2ed65c8ef035b3f988ddcc" }, "pipfile-spec": 6, "requires": { @@ -57,6 +57,15 @@ "markers": "python_version >= '3.8'", "version": "==1.7.0" }, + "build": { + "hashes": [ + "sha256:538aab1b64f9828977f84bc63ae570b060a8ed1be419e7870b8b4fc5e6ea553b", + "sha256:589bf99a67df7c9cf07ec0ac0e5e2ea5d4b37ac63301c4986d1acb126aa83f8f" + ], + "index": "pypi", + "markers": "python_version >= '3.7'", + "version": "==1.0.3" + }, "certifi": { "hashes": [ "sha256:9b469f3a900bf28dc19b8cfbf8019bf47f7fdd1a65a1d4ffb98fc14166beb4d1", @@ -270,6 +279,15 @@ "index": "pypi", "version": "==4.0.0" }, + "gitcommitlogger": { + "hashes": [ + "sha256:222dff0239e5cd1d6aea90f95536483f9b936735b858f8f4980e0a4ce005d5e9", + "sha256:8301962714ee3d9807792e8bcb4ab9c2ce5d4f9bfc1fa92ce2a386582dcbfcd3" + ], + "index": "pypi", + "markers": "python_version >= '3.7'", + "version": "==1.2.3" + }, "idna": { "hashes": [ "sha256:9ecdbbd083b06798ae1e86adcbfe8ab1479cf864e4ee30fe4e46a003d12491ca", @@ -546,6 +564,14 @@ "markers": "python_version >= '3.7'", "version": "==4.6.1" }, + "pyproject-hooks": { + "hashes": [ + "sha256:283c11acd6b928d2f6a7c73fa0d01cb2bdc5f07c57a2eeb6e83d5e56b97976f8", + "sha256:f271b298b97f5955d53fb12b72c1fb1948c22c1a6b70b315c54cedaca0264ef5" + ], + "markers": "python_version >= '3.7'", + "version": "==1.0.0" + }, "pytest": { "hashes": [ "sha256:0d009c083ea859a71b76adf7c1d502e4bc170b80a8ef002da5806527b9591fac", @@ -573,6 +599,15 @@ "markers": "python_version >= '3.7'", "version": "==1.3.0" }, + "python-dateutil": { + "hashes": [ + "sha256:0123cacc1627ae19ddf3c27a5de5bd67ee4586fbdd6440d9748f8abb483d3e86", + "sha256:961d03dc3453ebbc59dbdea9e4e11c5651520a876d0f4db161e8674aae935da9" + ], + "index": "pypi", + "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'", + "version": "==2.8.2" + }, "python-dotenv": { "hashes": [ "sha256:a8df96034aae6d2d50a4ebe8216326c61c3eb64836776504fcca410e5937a3ba", @@ -582,6 +617,14 @@ "markers": "python_version >= '3.8'", "version": "==1.0.0" }, + "pytz": { + "hashes": [ + "sha256:7b4fddbeb94a1eba4b557da24f19fdf9db575192544270a9101d8509f9f43d7b", + "sha256:ce42d816b81b68506614c11e8937d3aa9e41007ceb50bfdcb0749b921bf646c7" + ], + "index": "pypi", + "version": "==2023.3.post1" + }, "requests": { "hashes": [ "sha256:58cd2187c01e70e6e26505bca751777aa9f2ee0b7f4300988b709f44e013003f", @@ -600,6 +643,14 @@ "markers": "python_version >= '3.8'", "version": "==69.0.2" }, + "six": { + "hashes": [ + "sha256:1e61c37477a1626458e36f7b1d82aa5c9b094fa4802892072e49de9c60c4c926", + "sha256:8abb2f1d86890a2dfb989f9a77cfcfd3e47c2a354b01111771326f8aa26e0254" + ], + "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'", + "version": "==1.16.0" + }, "speechrecognition": { "hashes": [ "sha256:14131155e8a8ba00ead1b7b9b1a2fa71c845e4db5f9a5f66a33a1bd6c55c6c35", diff --git a/web-app/app.py b/web-app/app.py index 2764a69..833008c 100644 --- a/web-app/app.py +++ b/web-app/app.py @@ -3,15 +3,23 @@ """ import os -from flask import Flask, render_template, request, jsonify +from flask import Flask, Response, render_template, request, redirect, send_file, jsonify +import sys +import pymongo from pymongo import MongoClient import requests -app = Flask("project4") +app = Flask(__name__, template_folder='templates') -client = MongoClient("mongodb://mongodb:27017/") + +@app.route("/test_render_template") +def test_render_template(): + print("Request received for /test_render_template") + return render_template("root.html") + +client = MongoClient("mongodb://localhost:27017/") db = client["ml_databse"] collection = db["transcription"] @@ -34,9 +42,8 @@ def display_results(): if not my_transcript: return jsonify({"error": "Result not found"}), 404 - return render_template( - "results.html", transcription_result=my_transcript, activePage="results.html" - ) + return render_template("results.html", transcription_result=my_transcript, activePage="results.html") + @app.route("/analyzeData", methods=["POST"]) @@ -49,7 +56,7 @@ def analyze_data(): return jsonify({"status": "error", "message": "No audio file provided"}) audio_file = request.files["audio"] - ml_client_url = "http://backend:5001/analyzeAudio" + ml_client_url = "http://127.0.0.1:5001/analyzeAudio" # Use the converted audio file response = requests.post( ml_client_url, files={"audio": audio_file}, timeout=100 @@ -58,14 +65,16 @@ def analyze_data(): if response.status_code == 200: result = response.json() return jsonify(result) - return ( - jsonify({"error": "Failed to send and process audio. Please try again."}), - 500, - ) + else: + return ( + jsonify( + {"error": "Failed to send and process audio. Please try again."} + ), + 500, + ) except FileNotFoundError as e: return jsonify({"status": "error", "message": f"File not found: {str(e)}"}) - if __name__ == "__main__": - app.run(host="0.0.0.0", port=int(os.getenv("PORT", "5000")), debug=True) + app.run(host="0.0.0.0", port=int(os.getenv("PORT", "5000")), debug=True) \ No newline at end of file From 2a2bc3b8ce9aacc405bf66680919391198afe92c Mon Sep 17 00:00:00 2001 From: andrew0022 Date: Sun, 3 Dec 2023 21:41:53 -0500 Subject: [PATCH 2/7] format --- web-app/app.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/web-app/app.py b/web-app/app.py index 833008c..67a9fd7 100644 --- a/web-app/app.py +++ b/web-app/app.py @@ -3,15 +3,22 @@ """ import os -from flask import Flask, Response, render_template, request, redirect, send_file, jsonify +from flask import ( + Flask, + Response, + render_template, + request, + redirect, + send_file, + jsonify, +) import sys import pymongo from pymongo import MongoClient import requests -app = Flask(__name__, template_folder='templates') - +app = Flask(__name__, template_folder="templates") @app.route("/test_render_template") @@ -19,6 +26,7 @@ def test_render_template(): print("Request received for /test_render_template") return render_template("root.html") + client = MongoClient("mongodb://localhost:27017/") db = client["ml_databse"] collection = db["transcription"] @@ -42,8 +50,9 @@ def display_results(): if not my_transcript: return jsonify({"error": "Result not found"}), 404 - return render_template("results.html", transcription_result=my_transcript, activePage="results.html") - + return render_template( + "results.html", transcription_result=my_transcript, activePage="results.html" + ) @app.route("/analyzeData", methods=["POST"]) @@ -76,5 +85,6 @@ def analyze_data(): except FileNotFoundError as e: return jsonify({"status": "error", "message": f"File not found: {str(e)}"}) + if __name__ == "__main__": - app.run(host="0.0.0.0", port=int(os.getenv("PORT", "5000")), debug=True) \ No newline at end of file + app.run(host="0.0.0.0", port=int(os.getenv("PORT", "5000")), debug=True) From 4028046879659373c0368098a6589113b371f906 Mon Sep 17 00:00:00 2001 From: andrew0022 Date: Sun, 3 Dec 2023 21:45:44 -0500 Subject: [PATCH 3/7] format lint black --- Pipfile | 2 + Pipfile.lock | 128 ++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 128 insertions(+), 2 deletions(-) diff --git a/Pipfile b/Pipfile index 3b0dab9..8fd9aaf 100644 --- a/Pipfile +++ b/Pipfile @@ -28,6 +28,8 @@ build = "*" gitcommitlogger = "*" [dev-packages] +black = "*" +pylint = "*" [requires] python_version = "3" diff --git a/Pipfile.lock b/Pipfile.lock index 40498e2..425ce2d 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "fb3c86dc44179c576d242d24d86d7c5794b509538b2ed65c8ef035b3f988ddcc" + "sha256": "b8fe147312d971ae8b93366a32b492f38fe0ecac77faf061b69cc8adfc046182" }, "pipfile-spec": 6, "requires": { @@ -703,5 +703,129 @@ "version": "==3.0.1" } }, - "develop": {} + "develop": { + "astroid": { + "hashes": [ + "sha256:7d5895c9825e18079c5aeac0572bc2e4c83205c95d416e0b4fee8bc361d2d9ca", + "sha256:86b0bb7d7da0be1a7c4aedb7974e391b32d4ed89e33de6ed6902b4b15c97577e" + ], + "markers": "python_full_version >= '3.8.0'", + "version": "==3.0.1" + }, + "black": { + "hashes": [ + "sha256:250d7e60f323fcfc8ea6c800d5eba12f7967400eb6c2d21ae85ad31c204fb1f4", + "sha256:2a9acad1451632021ee0d146c8765782a0c3846e0e0ea46659d7c4f89d9b212b", + "sha256:412f56bab20ac85927f3a959230331de5614aecda1ede14b373083f62ec24e6f", + "sha256:421f3e44aa67138ab1b9bfbc22ee3780b22fa5b291e4db8ab7eee95200726b07", + "sha256:45aa1d4675964946e53ab81aeec7a37613c1cb71647b5394779e6efb79d6d187", + "sha256:4c44b7211a3a0570cc097e81135faa5f261264f4dfaa22bd5ee2875a4e773bd6", + "sha256:4c68855825ff432d197229846f971bc4d6666ce90492e5b02013bcaca4d9ab05", + "sha256:5133f5507007ba08d8b7b263c7aa0f931af5ba88a29beacc4b2dc23fcefe9c06", + "sha256:54caaa703227c6e0c87b76326d0862184729a69b73d3b7305b6288e1d830067e", + "sha256:58e5f4d08a205b11800332920e285bd25e1a75c54953e05502052738fe16b3b5", + "sha256:698c1e0d5c43354ec5d6f4d914d0d553a9ada56c85415700b81dc90125aac244", + "sha256:6c1cac07e64433f646a9a838cdc00c9768b3c362805afc3fce341af0e6a9ae9f", + "sha256:760415ccc20f9e8747084169110ef75d545f3b0932ee21368f63ac0fee86b221", + "sha256:7f622b6822f02bfaf2a5cd31fdb7cd86fcf33dab6ced5185c35f5db98260b055", + "sha256:cf57719e581cfd48c4efe28543fea3d139c6b6f1238b3f0102a9c73992cbb479", + "sha256:d136ef5b418c81660ad847efe0e55c58c8208b77a57a28a503a5f345ccf01394", + "sha256:dbea0bb8575c6b6303cc65017b46351dc5953eea5c0a59d7b7e3a2d2f433a911", + "sha256:fc7f6a44d52747e65a02558e1d807c82df1d66ffa80a601862040a43ec2e3142" + ], + "index": "pypi", + "markers": "python_version >= '3.8'", + "version": "==23.11.0" + }, + "click": { + "hashes": [ + "sha256:ae74fb96c20a0277a1d615f1e4d73c8414f5a98db8b799a7931d1582f3390c28", + "sha256:ca9853ad459e787e2192211578cc907e7594e294c7ccc834310722b41b9ca6de" + ], + "markers": "python_version >= '3.7'", + "version": "==8.1.7" + }, + "colorama": { + "hashes": [ + "sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44", + "sha256:4f1d9991f5acc0ca119f9d443620b77f9d6b33703e51011c16baf57afb285fc6" + ], + "markers": "sys_platform == 'win32'", + "version": "==0.4.6" + }, + "dill": { + "hashes": [ + "sha256:76b122c08ef4ce2eedcd4d1abd8e641114bfc6c2867f49f3c41facf65bf19f5e", + "sha256:cc1c8b182eb3013e24bd475ff2e9295af86c1a38eb1aff128dac8962a9ce3c03" + ], + "index": "pypi", + "markers": "python_version >= '3.7'", + "version": "==0.3.7" + }, + "isort": { + "hashes": [ + "sha256:8bef7dde241278824a6d83f44a544709b065191b95b6e50894bdc722fcba0504", + "sha256:f84c2818376e66cf843d497486ea8fed8700b340f308f076c6fb1229dff318b6" + ], + "markers": "python_full_version >= '3.8.0'", + "version": "==5.12.0" + }, + "mccabe": { + "hashes": [ + "sha256:348e0240c33b60bbdf4e523192ef919f28cb2c3d7d5c7794f74009290f236325", + "sha256:6c2d30ab6be0e4a46919781807b4f0d834ebdd6c6e3dca0bda5a15f863427b6e" + ], + "markers": "python_version >= '3.6'", + "version": "==0.7.0" + }, + "mypy-extensions": { + "hashes": [ + "sha256:4392f6c0eb8a5668a69e23d168ffa70f0be9ccfd32b5cc2d26a34ae5b844552d", + "sha256:75dbf8955dc00442a438fc4d0666508a9a97b6bd41aa2f0ffe9d2f2725af0782" + ], + "markers": "python_version >= '3.5'", + "version": "==1.0.0" + }, + "packaging": { + "hashes": [ + "sha256:048fb0e9405036518eaaf48a55953c750c11e1a1b68e0dd1a9d62ed0c092cfc5", + "sha256:8c491190033a9af7e1d931d0b5dacc2ef47509b34dd0de67ed209b5203fc88c7" + ], + "markers": "python_version >= '3.7'", + "version": "==23.2" + }, + "pathspec": { + "hashes": [ + "sha256:1d6ed233af05e679efb96b1851550ea95bbb64b7c490b0f5aa52996c11e92a20", + "sha256:e0d8d0ac2f12da61956eb2306b69f9469b42f4deb0f3cb6ed47b9cce9996ced3" + ], + "markers": "python_version >= '3.7'", + "version": "==0.11.2" + }, + "platformdirs": { + "hashes": [ + "sha256:118c954d7e949b35437270383a3f2531e99dd93cf7ce4dc8340d3356d30f173b", + "sha256:cb633b2bcf10c51af60beb0ab06d2f1d69064b43abf4c185ca6b28865f3f9731" + ], + "markers": "python_version >= '3.7'", + "version": "==4.0.0" + }, + "pylint": { + "hashes": [ + "sha256:0d4c286ef6d2f66c8bfb527a7f8a629009e42c99707dec821a03e1b51a4c1496", + "sha256:60ed5f3a9ff8b61839ff0348b3624ceeb9e6c2a92c514d81c9cc273da3b6bcda" + ], + "index": "pypi", + "markers": "python_full_version >= '3.8.0'", + "version": "==3.0.2" + }, + "tomlkit": { + "hashes": [ + "sha256:75baf5012d06501f07bee5bf8e801b9f343e7aac5a92581f20f80ce632e6b5a4", + "sha256:b0a645a9156dc7cb5d3a1f0d4bab66db287fcb8e0430bdd4664a095ea16414ba" + ], + "markers": "python_version >= '3.7'", + "version": "==0.12.3" + } + } } From 6df642aeeb708088d4390c74e88c159da2bd4877 Mon Sep 17 00:00:00 2001 From: andrew0022 Date: Sun, 3 Dec 2023 21:57:08 -0500 Subject: [PATCH 4/7] lint black 2 --- web-app/app.py | 42 ++++++++++++----------------------- web-app/app_tests/test_app.py | 23 ++++++++----------- 2 files changed, 23 insertions(+), 42 deletions(-) diff --git a/web-app/app.py b/web-app/app.py index 67a9fd7..b60889f 100644 --- a/web-app/app.py +++ b/web-app/app.py @@ -3,43 +3,33 @@ """ import os -from flask import ( - Flask, - Response, - render_template, - request, - redirect, - send_file, - jsonify, -) -import sys +from flask import Flask, render_template, request, jsonify import pymongo from pymongo import MongoClient import requests - app = Flask(__name__, template_folder="templates") +# Connect to MongoDB +client = MongoClient("mongodb://localhost:27017/") +db = client["ml_database"] +collection = db["transcription"] @app.route("/test_render_template") def test_render_template(): + """ + Request received for /test_render_template + """ print("Request received for /test_render_template") return render_template("root.html") - -client = MongoClient("mongodb://localhost:27017/") -db = client["ml_databse"] -collection = db["transcription"] - - @app.route("/") def root_page(): """ - template to render root page + Template to render root page """ return render_template("root.html") - @app.route("/results") def display_results(): """ @@ -54,11 +44,10 @@ def display_results(): "results.html", transcription_result=my_transcript, activePage="results.html" ) - @app.route("/analyzeData", methods=["POST"]) def analyze_data(): """ - Function to send genreated audio file to the machine learning client + Function to send generated audio file to the machine learning client """ try: if "audio" not in request.files: @@ -66,25 +55,22 @@ def analyze_data(): audio_file = request.files["audio"] ml_client_url = "http://127.0.0.1:5001/analyzeAudio" + # Use the converted audio file - response = requests.post( - ml_client_url, files={"audio": audio_file}, timeout=100 - ) + response = requests.post(ml_client_url, files={"audio": audio_file}, timeout=100) print("sent over") + if response.status_code == 200: result = response.json() return jsonify(result) else: return ( - jsonify( - {"error": "Failed to send and process audio. Please try again."} - ), + jsonify({"error": "Failed to send and process audio. Please try again."}), 500, ) except FileNotFoundError as e: return jsonify({"status": "error", "message": f"File not found: {str(e)}"}) - if __name__ == "__main__": app.run(host="0.0.0.0", port=int(os.getenv("PORT", "5000")), debug=True) diff --git a/web-app/app_tests/test_app.py b/web-app/app_tests/test_app.py index d3c97c0..63308ff 100644 --- a/web-app/app_tests/test_app.py +++ b/web-app/app_tests/test_app.py @@ -1,21 +1,20 @@ -import sys -import os import tempfile import pytest from flask import jsonify -from flask import Flask -from flask import render_template -from app import app, collection +from app import app -client = app.test_client() +# Remove unused imports +# import sys +# import os +# Rename either the fixture or the variable to avoid confusion +app_client = app.test_client() @pytest.fixture def client(): app.config["TESTING"] = True - with app.test_client() as client: - yield client - + with app.test_client() as test_client: + yield test_client def test_root_page(client): response = client.get("/test_render_template") @@ -23,7 +22,6 @@ def test_root_page(client): print("Response data:", response.data) assert b"Recording audio..." in response.data - def test_analyze_data(client, monkeypatch): def mock_post(*args, **kwargs): class MockResponse: @@ -44,13 +42,11 @@ def status_code(self): assert response.status_code == 200 assert b"mocked" in response.data - def test_analyze_data_no_audio(client): response = client.post("/analyzeData") assert response.status_code == 200 assert b"No audio file provided" in response.data - def test_analyze_data_failed_request(client, monkeypatch): def mock_post(*args, **kwargs): class MockResponse: @@ -71,6 +67,5 @@ def status_code(self): assert response.status_code == 500 assert b"Failed to send and process audio" in response.data - if __name__ == "__main__": - pytest.main(["-v", "test_app.py", "--cov=web-app"]) + pytest.main(["-v", "test_app.py", "--cov=web-app"]) \ No newline at end of file From 5a68c985783c40f19b14671230654b88d1de1a3c Mon Sep 17 00:00:00 2001 From: andrew0022 Date: Sun, 3 Dec 2023 22:31:04 -0500 Subject: [PATCH 5/7] lint fixes --- web-app/app_tests/test_app.py | 58 ++++++++++++++++++++++++++++++----- 1 file changed, 51 insertions(+), 7 deletions(-) diff --git a/web-app/app_tests/test_app.py b/web-app/app_tests/test_app.py index 63308ff..33167be 100644 --- a/web-app/app_tests/test_app.py +++ b/web-app/app_tests/test_app.py @@ -1,36 +1,63 @@ +""" +Tests for the web application. +""" + import tempfile import pytest -from flask import jsonify from app import app -# Remove unused imports -# import sys -# import os - -# Rename either the fixture or the variable to avoid confusion app_client = app.test_client() @pytest.fixture def client(): + """ + Fixture to configure the app for testing and provide a test client. + """ app.config["TESTING"] = True with app.test_client() as test_client: yield test_client +# pylint: disable=W0621 def test_root_page(client): + """ + Test the root page and check if "Recording audio..." is present in the response. + """ response = client.get("/test_render_template") print("Response status code:", response.status_code) print("Response data:", response.data) assert b"Recording audio..." in response.data +# pylint: disable=W0613 def test_analyze_data(client, monkeypatch): + """ + Test the analyze_data route with a mocked response. + """ + # pylint: disable=W0613 def mock_post(*args, **kwargs): class MockResponse: + """ + Mock function for simulating a POST request. + + This function returns a mocked response with a JSON payload. + + Returns: + MockResponse: An object with a `json` method and a `status_code` property. + """ @staticmethod def json(): + """ + Get the JSON representation of the mocked response. + + Returns: + dict: A dictionary representing the mocked JSON response. + """ return {"mocked": "response"} @property def status_code(self): + """ + def status code + """ return 200 return MockResponse() @@ -42,20 +69,37 @@ def status_code(self): assert response.status_code == 200 assert b"mocked" in response.data +# pylint: disable=W0613 def test_analyze_data_no_audio(client): + """ + Test the analyze_data route without providing an audio file. + """ response = client.post("/analyzeData") assert response.status_code == 200 assert b"No audio file provided" in response.data +# pylint: disable=W0613 def test_analyze_data_failed_request(client, monkeypatch): + """ + Test the analyze_data route with a mocked failed response. + """ def mock_post(*args, **kwargs): class MockResponse: + """ + mock response class + """ @staticmethod def json(): + """ + def json + """ return {"error": "Mocked error"} @property def status_code(self): + """ + def status code + """ return 500 return MockResponse() @@ -68,4 +112,4 @@ def status_code(self): assert b"Failed to send and process audio" in response.data if __name__ == "__main__": - pytest.main(["-v", "test_app.py", "--cov=web-app"]) \ No newline at end of file + pytest.main(["-v", "test_app.py", "--cov=web-app"]) From d62924eb4ca8d2c86f514b5161985af45ba0bb9f Mon Sep 17 00:00:00 2001 From: andrew0022 Date: Sun, 3 Dec 2023 22:31:31 -0500 Subject: [PATCH 6/7] lint fixes --- web-app/app.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/web-app/app.py b/web-app/app.py index b60889f..29717b7 100644 --- a/web-app/app.py +++ b/web-app/app.py @@ -4,7 +4,6 @@ import os from flask import Flask, render_template, request, jsonify -import pymongo from pymongo import MongoClient import requests @@ -55,11 +54,11 @@ def analyze_data(): audio_file = request.files["audio"] ml_client_url = "http://127.0.0.1:5001/analyzeAudio" - # Use the converted audio file response = requests.post(ml_client_url, files={"audio": audio_file}, timeout=100) print("sent over") + # pylint: disable=R1705 if response.status_code == 200: result = response.json() return jsonify(result) @@ -67,7 +66,7 @@ def analyze_data(): return ( jsonify({"error": "Failed to send and process audio. Please try again."}), 500, - ) + ) \ except FileNotFoundError as e: return jsonify({"status": "error", "message": f"File not found: {str(e)}"}) From 19327b160ff10ffb52921e83491b5a1309b72702 Mon Sep 17 00:00:00 2001 From: andrew0022 Date: Sun, 3 Dec 2023 22:33:26 -0500 Subject: [PATCH 7/7] black --- web-app/app.py | 16 ++++++++++++---- web-app/app_tests/test_app.py | 20 +++++++++++++++----- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/web-app/app.py b/web-app/app.py index 29717b7..b030d08 100644 --- a/web-app/app.py +++ b/web-app/app.py @@ -14,6 +14,7 @@ db = client["ml_database"] collection = db["transcription"] + @app.route("/test_render_template") def test_render_template(): """ @@ -22,6 +23,7 @@ def test_render_template(): print("Request received for /test_render_template") return render_template("root.html") + @app.route("/") def root_page(): """ @@ -29,6 +31,7 @@ def root_page(): """ return render_template("root.html") + @app.route("/results") def display_results(): """ @@ -43,6 +46,7 @@ def display_results(): "results.html", transcription_result=my_transcript, activePage="results.html" ) + @app.route("/analyzeData", methods=["POST"]) def analyze_data(): """ @@ -55,7 +59,9 @@ def analyze_data(): audio_file = request.files["audio"] ml_client_url = "http://127.0.0.1:5001/analyzeAudio" # Use the converted audio file - response = requests.post(ml_client_url, files={"audio": audio_file}, timeout=100) + response = requests.post( + ml_client_url, files={"audio": audio_file}, timeout=100 + ) print("sent over") # pylint: disable=R1705 @@ -64,12 +70,14 @@ def analyze_data(): return jsonify(result) else: return ( - jsonify({"error": "Failed to send and process audio. Please try again."}), + jsonify( + {"error": "Failed to send and process audio. Please try again."} + ), 500, - ) \ - + ) except FileNotFoundError as e: return jsonify({"status": "error", "message": f"File not found: {str(e)}"}) + if __name__ == "__main__": app.run(host="0.0.0.0", port=int(os.getenv("PORT", "5000")), debug=True) diff --git a/web-app/app_tests/test_app.py b/web-app/app_tests/test_app.py index 33167be..e82d7e5 100644 --- a/web-app/app_tests/test_app.py +++ b/web-app/app_tests/test_app.py @@ -8,6 +8,7 @@ app_client = app.test_client() + @pytest.fixture def client(): """ @@ -17,6 +18,7 @@ def client(): with app.test_client() as test_client: yield test_client + # pylint: disable=W0621 def test_root_page(client): """ @@ -27,22 +29,25 @@ def test_root_page(client): print("Response data:", response.data) assert b"Recording audio..." in response.data + # pylint: disable=W0613 def test_analyze_data(client, monkeypatch): """ Test the analyze_data route with a mocked response. """ + # pylint: disable=W0613 def mock_post(*args, **kwargs): class MockResponse: """ - Mock function for simulating a POST request. + Mock function for simulating a POST request. - This function returns a mocked response with a JSON payload. + This function returns a mocked response with a JSON payload. + + Returns: + MockResponse: An object with a `json` method and a `status_code` property. + """ - Returns: - MockResponse: An object with a `json` method and a `status_code` property. - """ @staticmethod def json(): """ @@ -69,6 +74,7 @@ def status code assert response.status_code == 200 assert b"mocked" in response.data + # pylint: disable=W0613 def test_analyze_data_no_audio(client): """ @@ -78,16 +84,19 @@ def test_analyze_data_no_audio(client): assert response.status_code == 200 assert b"No audio file provided" in response.data + # pylint: disable=W0613 def test_analyze_data_failed_request(client, monkeypatch): """ Test the analyze_data route with a mocked failed response. """ + def mock_post(*args, **kwargs): class MockResponse: """ mock response class """ + @staticmethod def json(): """ @@ -111,5 +120,6 @@ def status code assert response.status_code == 500 assert b"Failed to send and process audio" in response.data + if __name__ == "__main__": pytest.main(["-v", "test_app.py", "--cov=web-app"])