Skip to content
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
10 changes: 8 additions & 2 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,14 @@ def purchasePlaces():
competition = [c for c in competitions if c['name'] == request.form['competition']][0]
club = [c for c in clubs if c['name'] == request.form['club']][0]
placesRequired = int(request.form['places'])
competition['numberOfPlaces'] = int(competition['numberOfPlaces'])-placesRequired
flash('Great-booking complete!')
club_points = int(club['points'])

if placesRequired > club_points:
flash("Cannot book more places than club points.")
return render_template('welcome.html', club=club, competitions=competitions)

competition['numberOfPlaces'] = int(competition['numberOfPlaces']) - placesRequired
flash('Great - booking complete!')
return render_template('welcome.html', club=club, competitions=competitions)


Expand Down
44 changes: 44 additions & 0 deletions tests/unit/test_book_place_with_enough_point.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import server

"""
Unit test file for the place booking feature.
The purpose is to verify that booking places is only possible with sufficient points.

Test 1: the club has 4 points and wants to book 4 places — booking accepted :
- status code 200
- confirmation message
- remaining places updated correctly.
Test 2: the club has 4 points and wants to book 5 places — booking refused :
- status code 200
- error message
- remaining places unchanged
"""


def test_book_places_with_enough_points(client):
server.clubs = [{"name": "Club A", "points": "4"}]
server.competitions = [{"name": "Comp 1", "numberOfPlaces": "5"}]

response = client.post('/purchasePlaces', data={
'competition': 'Comp 1',
'club': 'Club A',
'places': '4'
})

assert response.status_code == 200
assert b"Great - booking complete!" in response.data
assert int(server.competitions[0]['numberOfPlaces']) == 1

def test_book_places_without_enough_points(client):
server.clubs = [{"name": "Club B", "points": "4"}]
server.competitions = [{"name": "Comp 2", "numberOfPlaces": "5"}]

response = client.post('/purchasePlaces', data={
'competition': 'Comp 2',
'club': 'Club B',
'places': '5'
})

assert response.status_code == 200
assert b"Cannot book more places than club points." in response.data
assert int(server.competitions[0]['numberOfPlaces']) == 5