From 824f7e06908438466fda3b8a3356e18f2b3112d9 Mon Sep 17 00:00:00 2001 From: Alex Chesters <10558792+AlexChesters@users.noreply.github.com> Date: Thu, 3 Oct 2024 12:49:03 +0100 Subject: [PATCH] general improvements to python exercise (#110) * fix some linting errorrs * handle errors cleaner * fix some linting errors * tweak status code inspection --- election-api-python/src/results_controller.py | 12 +++---- election-api-python/src/server.py | 10 ++++-- election-api-python/src/test_scoreboard.py | 31 +++++++++++++------ 3 files changed, 34 insertions(+), 19 deletions(-) diff --git a/election-api-python/src/results_controller.py b/election-api-python/src/results_controller.py index ee5ee49..de8de8a 100644 --- a/election-api-python/src/results_controller.py +++ b/election-api-python/src/results_controller.py @@ -4,17 +4,17 @@ class ResultsController: def __init__(self) -> None: self.store: ResultStore = ResultStore() - - def get_result(self, id: int) -> dict: - return self.store.get_result(id) - + + def get_result(self, identifier: int) -> dict: + return self.store.get_result(identifier) + def new_result(self, result: dict) -> dict: self.store.new_result(result) return {} - + def reset(self) -> None: self.store.reset() - + def scoreboard(self) -> dict: # Left blank for you to fill in return {} diff --git a/election-api-python/src/server.py b/election-api-python/src/server.py index 4e0ec31..3e5077b 100644 --- a/election-api-python/src/server.py +++ b/election-api-python/src/server.py @@ -5,8 +5,8 @@ controller: ResultsController = ResultsController() @app.route("/result/", methods=["GET"]) -def individual_result(id) -> dict: - return controller.get_result(int(id)) +def individual_result(identifier) -> dict: + return controller.get_result(int(identifier)) @app.route("/result", methods=["POST"]) def add_result() -> dict: @@ -15,4 +15,8 @@ def add_result() -> dict: @app.route("/scoreboard", methods=["GET"]) def scoreboard() -> dict: return controller.scoreboard() - \ No newline at end of file + +@app.errorhandler(Exception) +def all_exception_handler(e): + print(f"[ERROR] - exception ({type(e).__name__}) occurred during request handling: {e}") + return {}, 500 diff --git a/election-api-python/src/test_scoreboard.py b/election-api-python/src/test_scoreboard.py index e8167ac..9982733 100644 --- a/election-api-python/src/test_scoreboard.py +++ b/election-api-python/src/test_scoreboard.py @@ -1,4 +1,7 @@ -import unittest, json, os +import unittest +import json +import os + from server import app, controller class TestScoreboard(unittest.TestCase): @@ -11,7 +14,7 @@ def __init__(self, methodName: str = ...) -> None: def load_and_post_result_file(self, num: str) -> dict: file_number: str = str(num).zfill(3) - with open(f"{self.RESULT_SAMPLE_PATH}/result{file_number}.json", "r") as file: + with open(f"{self.RESULT_SAMPLE_PATH}/result{file_number}.json", "r", encoding="utf-8") as file: result = file.read() return self.server.post("/result", json=json.loads(result)) @@ -20,17 +23,19 @@ def load_results(self, quantity: int) -> list[dict]: for i in range(quantity): results.append(self.load_and_post_result_file(i + 1)) return results - - def fetch_scoreboard(self) -> list[dict]: + + def fetch_scoreboard(self) -> tuple[int, dict]: # returns (status_code, response_body_object) response = self.server.get("/scoreboard") - return [] if response.data == b'{}\n' else json.loads(response.data.decode("utf-8")) + return (response.status_code, json.loads(response.data.decode("utf-8"))) def setUp(self) -> None: controller.reset() def test_first_5(self) -> None: self.load_results(5) - scoreboard: list = self.fetch_scoreboard() + status_code, scoreboard = self.fetch_scoreboard() + + self.assertEqual(status_code, 200, f"non-200 status code received: {status_code}") self.assertNotEqual(len(scoreboard), 0) # assert LD == 1 # assert LAB = 4 @@ -38,7 +43,9 @@ def test_first_5(self) -> None: def test_first_100(self) -> None: self.load_results(100) - scoreboard: list = self.fetch_scoreboard() + status_code, scoreboard = self.fetch_scoreboard() + + self.assertEqual(status_code, 200, f"non-200 status code received: {status_code}") self.assertNotEqual(len(scoreboard), 0) # assert LD == 12 # assert LAB == 56 @@ -47,7 +54,9 @@ def test_first_100(self) -> None: def test_first_554(self) -> None: self.load_results(554) - scoreboard: list = self.fetch_scoreboard() + status_code, scoreboard = self.fetch_scoreboard() + + self.assertEqual(status_code, 200, f"non-200 status code received: {status_code}") self.assertNotEqual(len(scoreboard), 0) # assert LD == 52 # assert LAB = 325 @@ -56,7 +65,9 @@ def test_first_554(self) -> None: def test_all_results(self) -> None: self.load_results(650) - scoreboard: list = self.fetch_scoreboard() + status_code, scoreboard = self.fetch_scoreboard() + + self.assertEqual(status_code, 200, f"non-200 status code received: {status_code}") self.assertNotEqual(len(scoreboard), 0) # assert LD == 62 # assert LAB == 349 @@ -65,4 +76,4 @@ def test_all_results(self) -> None: # assert sum = 650 if __name__ == "__main__": - unittest.main() \ No newline at end of file + unittest.main()