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/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 4084baeac..e50c58eca 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,52 @@ 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 + + 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) + + return render_template('welcome.html', club=club, + competitions=competitions) # TODO: Add route for points display @@ -56,4 +73,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/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 new file mode 100644 index 000000000..4200b2789 --- /dev/null +++ b/tests/test_server.py @@ -0,0 +1,39 @@ +from server import loadCompetitions, loadClubs + + +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