From 8135241e0c17f4bbfd4a9e90dbcfc27b8422abcb Mon Sep 17 00:00:00 2001 From: elof-dev Date: Wed, 22 Oct 2025 09:55:53 +0700 Subject: [PATCH] Fix issue #2 : correct purchasePlaces() and add unit tests - Updated purchasePlaces() to check if the club has enough points before confirming a booking - Added a new test file with 2 unit tests to verify point validation logic --- server.py | 10 ++++- .../unit/test_book_place_with_enough_point.py | 44 +++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 tests/unit/test_book_place_with_enough_point.py diff --git a/server.py b/server.py index 43c1340ce..07932372b 100644 --- a/server.py +++ b/server.py @@ -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) diff --git a/tests/unit/test_book_place_with_enough_point.py b/tests/unit/test_book_place_with_enough_point.py new file mode 100644 index 000000000..efe3df3dd --- /dev/null +++ b/tests/unit/test_book_place_with_enough_point.py @@ -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 \ No newline at end of file