Skip to content

Commit 0962a9e

Browse files
authored
Merge pull request #4 from br-imen/bug/Clubs-should-not-be-able-to-book-more-than-12-places-per-competition
(fix) Handle booking more 12 places
2 parents a5869b8 + be366be commit 0962a9e

6 files changed

+87
-7
lines changed

server.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import json
22
from flask import Flask, render_template, request, redirect, flash, url_for
33

4+
app = Flask(__name__)
5+
app.secret_key = "something_special"
6+
app.config["COMPETITIONS_FILE"] = "competitions.json"
7+
48

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

1014

1115
def loadCompetitions():
12-
with open("competitions.json") as comps:
16+
with open(app.config["COMPETITIONS_FILE"], "r") as comps:
1317
listOfCompetitions = json.load(comps)["competitions"]
1418
return listOfCompetitions
1519

1620

17-
app = Flask(__name__)
18-
app.secret_key = "something_special"
19-
2021
competitions = loadCompetitions()
2122
clubs = loadClubs()
2223

@@ -86,6 +87,9 @@ def get_club_from_name(name):
8687
def check_places(places, club):
8788
if not places or int(places) < 1:
8889
return "Places required must be a positive integer"
90+
if int(places) > 12:
91+
return ("Places required must be a positive integer "
92+
"that does not exceed 12")
8993
if int(places) > int(club["points"]):
9094
return "Places required exceed club's total points"
9195

@@ -127,3 +131,7 @@ def purchasePlaces():
127131
@app.route("/logout")
128132
def logout():
129133
return redirect(url_for("index"))
134+
135+
136+
if __name__ == "__main__":
137+
app.run(debug=True)

templates/booking.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ <h2>{{competition['name']}}</h2>
2020
<form action="/purchasePlaces" method="post">
2121
<input type="hidden" name="club" value="{{club['name']}}">
2222
<input type="hidden" name="competition" value="{{competition['name']}}">
23-
<label for="places">How many places?</label><input type="number" name="places" id="places"/>
23+
<label for="places">How many places?</label><input type="number" name="places" id="places" max="12"/>
2424
<button type="submit">Book</button>
2525
</form>
2626
</body>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
def test_purchase_places_exceeds_12(client):
3+
"""
4+
Test purchasing more places than 12.
5+
"""
6+
response = client.post("/purchasePlaces", data={
7+
"competition": "Competition 1",
8+
"club": "Club 1",
9+
"places": "13"
10+
})
11+
12+
assert response.status_code == 302 # Redirect expected
13+
response = client.get(response.headers["Location"]) # Follow the redirect
14+
assert b"Places required must be a positive integer"
15+
b" that does not exceed 12" in response.data

tests/integration_tests/test_clubs_should_not_use_more_than_their_points.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def test_purchase_places_insufficient_points(client):
2121
response = client.post("/purchasePlaces", data={
2222
"competition": "Competition 1",
2323
"club": "Club 1",
24-
"places": "20" # Club 1 has only 10 points
24+
"places": "11" # Club 1 has only 10 points
2525
})
2626

2727
assert response.status_code == 302 # Redirect expected
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from unittest.mock import patch
2+
from .utils import mock_clubs_list, mock_competitions_list
3+
4+
5+
def test_check_places_invalid_more_than_12():
6+
from server import check_places
7+
8+
error = check_places("13", mock_clubs_list[0])
9+
assert error == ("Places required must be a positive"
10+
" integer that does not exceed 12")
11+
12+
13+
# Unit test for booking with error in places
14+
@patch("server.redirect")
15+
@patch("server.url_for")
16+
@patch("server.flash")
17+
@patch("server.check_places")
18+
@patch("server.get_club_from_name")
19+
@patch("server.get_competition_from_name")
20+
def test_purchase_places_invalid_places(
21+
mock_get_competition,
22+
mock_get_club,
23+
mock_check_places,
24+
mock_flash,
25+
mock_url_for,
26+
mock_redirect,
27+
client,
28+
):
29+
# Mock the functions to simulate an error in places
30+
mock_get_competition.return_value = mock_competitions_list[0]
31+
mock_get_club.return_value = mock_clubs_list[0]
32+
mock_check_places.return_value = (
33+
"Places required must be a positive integer that does not exceed 12"
34+
)
35+
36+
# Mock the redirect and url_for to return a redirect response
37+
mock_url_for.return_value = "/book"
38+
39+
# Simulate POST request to the route
40+
client.post(
41+
"/purchasePlaces",
42+
data={"competition": "Competition 1", "club": "Club 1", "places": "20"},
43+
)
44+
45+
# Assert that the necessary functions are called with the correct parameters
46+
mock_get_competition.assert_called_once_with("Competition 1")
47+
mock_get_club.assert_called_once_with("Club 1")
48+
mock_check_places.assert_called_once_with("20", mock_clubs_list[0])
49+
mock_flash.assert_called_once_with(
50+
"Places required must be a positive integer that does not exceed 12"
51+
)
52+
mock_url_for.assert_called_once_with(
53+
"book",
54+
competition=mock_competitions_list[0]["name"],
55+
club=mock_clubs_list[0]["name"],
56+
)
57+
mock_redirect.assert_called_once_with("/book")

tests/unit_tests/test_clubs_should_not_use_more_than_their_points.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def test_check_places_invalid_negative():
6363
def test_check_places_exceeds_points():
6464
from server import check_places
6565

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

6969

0 commit comments

Comments
 (0)