Skip to content

(fix) Handle booking more 12 places #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions server.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import json
from flask import Flask, render_template, request, redirect, flash, url_for

app = Flask(__name__)
app.secret_key = "something_special"
app.config["COMPETITIONS_FILE"] = "competitions.json"


def loadClubs():
with open("clubs.json") as c:
Expand All @@ -9,14 +13,11 @@ def loadClubs():


def loadCompetitions():
with open("competitions.json") as comps:
with open(app.config["COMPETITIONS_FILE"], "r") as comps:
listOfCompetitions = json.load(comps)["competitions"]
return listOfCompetitions


app = Flask(__name__)
app.secret_key = "something_special"

competitions = loadCompetitions()
clubs = loadClubs()

Expand Down Expand Up @@ -86,6 +87,9 @@ def get_club_from_name(name):
def check_places(places, club):
if not places or int(places) < 1:
return "Places required must be a positive integer"
if int(places) > 12:
return ("Places required must be a positive integer "
"that does not exceed 12")
if int(places) > int(club["points"]):
return "Places required exceed club's total points"

Expand Down Expand Up @@ -127,3 +131,7 @@ def purchasePlaces():
@app.route("/logout")
def logout():
return redirect(url_for("index"))


if __name__ == "__main__":
app.run(debug=True)
2 changes: 1 addition & 1 deletion templates/booking.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ <h2>{{competition['name']}}</h2>
<form action="/purchasePlaces" method="post">
<input type="hidden" name="club" value="{{club['name']}}">
<input type="hidden" name="competition" value="{{competition['name']}}">
<label for="places">How many places?</label><input type="number" name="places" id="places"/>
<label for="places">How many places?</label><input type="number" name="places" id="places" max="12"/>
<button type="submit">Book</button>
</form>
</body>
Expand Down
15 changes: 15 additions & 0 deletions tests/integration_tests/test_clubs_should_not_use_more_than_12.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

def test_purchase_places_exceeds_12(client):
"""
Test purchasing more places than 12.
"""
response = client.post("/purchasePlaces", data={
"competition": "Competition 1",
"club": "Club 1",
"places": "13"
})

assert response.status_code == 302 # Redirect expected
response = client.get(response.headers["Location"]) # Follow the redirect
assert b"Places required must be a positive integer"
b" that does not exceed 12" in response.data
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def test_purchase_places_insufficient_points(client):
response = client.post("/purchasePlaces", data={
"competition": "Competition 1",
"club": "Club 1",
"places": "20" # Club 1 has only 10 points
"places": "11" # Club 1 has only 10 points
})

assert response.status_code == 302 # Redirect expected
Expand Down
57 changes: 57 additions & 0 deletions tests/unit_tests/test_clubs_should_not_use_more_than_12.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from unittest.mock import patch
from .utils import mock_clubs_list, mock_competitions_list


def test_check_places_invalid_more_than_12():
from server import check_places

error = check_places("13", mock_clubs_list[0])
assert error == ("Places required must be a positive"
" integer that does not exceed 12")


# Unit test for booking with error in places
@patch("server.redirect")
@patch("server.url_for")
@patch("server.flash")
@patch("server.check_places")
@patch("server.get_club_from_name")
@patch("server.get_competition_from_name")
def test_purchase_places_invalid_places(
mock_get_competition,
mock_get_club,
mock_check_places,
mock_flash,
mock_url_for,
mock_redirect,
client,
):
# Mock the functions to simulate an error in places
mock_get_competition.return_value = mock_competitions_list[0]
mock_get_club.return_value = mock_clubs_list[0]
mock_check_places.return_value = (
"Places required must be a positive integer that does not exceed 12"
)

# Mock the redirect and url_for to return a redirect response
mock_url_for.return_value = "/book"

# Simulate POST request to the route
client.post(
"/purchasePlaces",
data={"competition": "Competition 1", "club": "Club 1", "places": "20"},
)

# Assert that the necessary functions are called with the correct parameters
mock_get_competition.assert_called_once_with("Competition 1")
mock_get_club.assert_called_once_with("Club 1")
mock_check_places.assert_called_once_with("20", mock_clubs_list[0])
mock_flash.assert_called_once_with(
"Places required must be a positive integer that does not exceed 12"
)
mock_url_for.assert_called_once_with(
"book",
competition=mock_competitions_list[0]["name"],
club=mock_clubs_list[0]["name"],
)
mock_redirect.assert_called_once_with("/book")
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def test_check_places_invalid_negative():
def test_check_places_exceeds_points():
from server import check_places

error = check_places("20", mock_clubs_list[0])
error = check_places("11", mock_clubs_list[0])
assert error == "Places required exceed club's total points"


Expand Down