Skip to content

Commit

Permalink
general improvements to python exercise (bbc#110)
Browse files Browse the repository at this point in the history
* fix some linting errorrs

* handle errors cleaner

* fix some linting errors

* tweak status code inspection
  • Loading branch information
AlexChesters authored Oct 3, 2024
1 parent 6c6b8f8 commit 824f7e0
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 19 deletions.
12 changes: 6 additions & 6 deletions election-api-python/src/results_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
10 changes: 7 additions & 3 deletions election-api-python/src/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
controller: ResultsController = ResultsController()

@app.route("/result/<id>", 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:
Expand All @@ -15,4 +15,8 @@ def add_result() -> dict:
@app.route("/scoreboard", methods=["GET"])
def scoreboard() -> dict:
return controller.scoreboard()


@app.errorhandler(Exception)
def all_exception_handler(e):
print(f"[ERROR] - exception ({type(e).__name__}) occurred during request handling: {e}")
return {}, 500
31 changes: 21 additions & 10 deletions election-api-python/src/test_scoreboard.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import unittest, json, os
import unittest
import json
import os

from server import app, controller

class TestScoreboard(unittest.TestCase):
Expand All @@ -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))

Expand All @@ -20,25 +23,29 @@ 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
# assert winner = noone

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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -65,4 +76,4 @@ def test_all_results(self) -> None:
# assert sum = 650

if __name__ == "__main__":
unittest.main()
unittest.main()

0 comments on commit 824f7e0

Please sign in to comment.