From 47df6543357d80159969dfc7bc973de9a8e2a72b Mon Sep 17 00:00:00 2001 From: Steve Date: Sat, 13 Jul 2024 21:21:49 +0200 Subject: [PATCH 1/2] first commit : purchase fix --- .gitignore | 5 +++-- server.py | 41 +++++++++++++++++++++++++---------------- tests/__init__.py | 0 tests/test_server.py | 5 +++++ 4 files changed, 33 insertions(+), 18 deletions(-) create mode 100644 tests/__init__.py create mode 100644 tests/test_server.py diff --git a/.gitignore b/.gitignore index 2cba99d87..f944d3e49 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ bin include lib .Python -tests/ .envrc -__pycache__ \ No newline at end of file +__pycache__ +/env +.idea/ \ No newline at end of file diff --git a/server.py b/server.py index 4084baeac..148dea2a6 100644 --- a/server.py +++ b/server.py @@ -1,17 +1,17 @@ import json -from flask import Flask,render_template,request,redirect,flash,url_for +from flask import Flask, render_template, request, redirect, flash, url_for def loadClubs(): with open('clubs.json') as c: - listOfClubs = json.load(c)['clubs'] - return listOfClubs + listOfClubs = json.load(c)['clubs'] + return listOfClubs def loadCompetitions(): with open('competitions.json') as comps: - listOfCompetitions = json.load(comps)['competitions'] - return listOfCompetitions + listOfCompetitions = json.load(comps)['competitions'] + return listOfCompetitions app = Flask(__name__) @@ -20,35 +20,44 @@ def loadCompetitions(): competitions = loadCompetitions() clubs = loadClubs() + @app.route('/') def index(): return render_template('index.html') -@app.route('/showSummary',methods=['POST']) + +@app.route('/showSummary', methods=['POST']) def showSummary(): - club = [club for club in clubs if club['email'] == request.form['email']][0] - return render_template('welcome.html',club=club,competitions=competitions) + club = [club for club in clubs if club['email'] == request.form['email']][ + 0] + return render_template('welcome.html', club=club, + competitions=competitions) @app.route('/book//') -def book(competition,club): +def book(competition, club): foundClub = [c for c in clubs if c['name'] == club][0] foundCompetition = [c for c in competitions if c['name'] == competition][0] if foundClub and foundCompetition: - return render_template('booking.html',club=foundClub,competition=foundCompetition) + return render_template('booking.html', club=foundClub, + competition=foundCompetition) else: flash("Something went wrong-please try again") - return render_template('welcome.html', club=club, competitions=competitions) + return render_template('welcome.html', club=club, + competitions=competitions) -@app.route('/purchasePlaces',methods=['POST']) +@app.route('/purchasePlaces', methods=['POST']) def purchasePlaces(): - competition = [c for c in competitions if c['name'] == request.form['competition']][0] + 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 + competition['numberOfPlaces'] = int( + competition['numberOfPlaces']) - placesRequired flash('Great-booking complete!') - return render_template('welcome.html', club=club, competitions=competitions) + return render_template('welcome.html', club=club, + competitions=competitions) # TODO: Add route for points display @@ -56,4 +65,4 @@ def purchasePlaces(): @app.route('/logout') def logout(): - return redirect(url_for('index')) \ No newline at end of file + return redirect(url_for('index')) diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/test_server.py b/tests/test_server.py new file mode 100644 index 000000000..8d3c5afd8 --- /dev/null +++ b/tests/test_server.py @@ -0,0 +1,5 @@ + + +class PurchasePlaces: + def test_purchase_places(self): + assert False From 6a03829d0cf58a48228d96ad120c20b7f858d680 Mon Sep 17 00:00:00 2001 From: Steve Date: Mon, 15 Jul 2024 13:50:33 +0200 Subject: [PATCH 2/2] Fixed max purchase limit --- pytest.ini | 3 +++ requirements.txt | 2 ++ server.py | 8 ++++++++ tests/conftest.py | 10 ++++++++++ tests/test_server.py | 40 +++++++++++++++++++++++++++++++++++++--- 5 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 pytest.ini create mode 100644 tests/conftest.py diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 000000000..c24fe5bb9 --- /dev/null +++ b/pytest.ini @@ -0,0 +1,3 @@ +[pytest] +filterwarnings = + ignore::DeprecationWarning diff --git a/requirements.txt b/requirements.txt index 139affa05..94218a6e5 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,3 +4,5 @@ itsdangerous==1.1.0 Jinja2==2.11.2 MarkupSafe==1.1.1 Werkzeug==1.0.1 + +pytest~=8.2.2 \ No newline at end of file diff --git a/server.py b/server.py index 148dea2a6..e50c58eca 100644 --- a/server.py +++ b/server.py @@ -52,10 +52,18 @@ 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']) + + if placesRequired > 12: + flash('Max purchase 12.') + 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/conftest.py b/tests/conftest.py new file mode 100644 index 000000000..ca8562ecd --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,10 @@ +import pytest + +from server import app, loadClubs, loadCompetitions + + +@pytest.fixture +def client(): + app.config['TESTING'] = True + with app.test_client() as client: + yield client diff --git a/tests/test_server.py b/tests/test_server.py index 8d3c5afd8..4200b2789 100644 --- a/tests/test_server.py +++ b/tests/test_server.py @@ -1,5 +1,39 @@ +from server import loadCompetitions, loadClubs -class PurchasePlaces: - def test_purchase_places(self): - assert False +def test_purchase_places(client): + + clubs = loadClubs() + competitions = loadCompetitions() + + test_club = clubs[0] + test_competition = competitions[0] + places_to_purchase = 8 + + response = client.post('/purchasePlaces', data={ + 'club': test_club['name'], + 'competition': test_competition['name'], + 'places': places_to_purchase + }) + + assert response.status_code == 200 + assert b'Great-booking complete!' in response.data + + +def test_max_purchase_places(client): + + clubs = loadClubs() + competitions = loadCompetitions() + + test_club = clubs[0] + test_competition = competitions[0] + places_to_purchase = 28 + + response = client.post('/purchasePlaces', data={ + 'club': test_club['name'], + 'competition': test_competition['name'], + 'places': places_to_purchase + }) + + assert response.status_code == 200 + assert b'Max purchase 12.' in response.data