Skip to content

Commit 6db6b83

Browse files
Merge pull request #1 from githubstevemas/bugfix/max-purchase-places
Bugfix/max purchase places
2 parents 81e09c8 + 6a03829 commit 6db6b83

File tree

7 files changed

+90
-18
lines changed

7 files changed

+90
-18
lines changed

.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ bin
22
include
33
lib
44
.Python
5-
tests/
65
.envrc
7-
__pycache__
6+
__pycache__
7+
/env
8+
.idea/

pytest.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[pytest]
2+
filterwarnings =
3+
ignore::DeprecationWarning

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ itsdangerous==1.1.0
44
Jinja2==2.11.2
55
MarkupSafe==1.1.1
66
Werkzeug==1.0.1
7+
8+
pytest~=8.2.2

server.py

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import json
2-
from flask import Flask,render_template,request,redirect,flash,url_for
2+
from flask import Flask, render_template, request, redirect, flash, url_for
33

44

55
def loadClubs():
66
with open('clubs.json') as c:
7-
listOfClubs = json.load(c)['clubs']
8-
return listOfClubs
7+
listOfClubs = json.load(c)['clubs']
8+
return listOfClubs
99

1010

1111
def loadCompetitions():
1212
with open('competitions.json') as comps:
13-
listOfCompetitions = json.load(comps)['competitions']
14-
return listOfCompetitions
13+
listOfCompetitions = json.load(comps)['competitions']
14+
return listOfCompetitions
1515

1616

1717
app = Flask(__name__)
@@ -20,40 +20,57 @@ def loadCompetitions():
2020
competitions = loadCompetitions()
2121
clubs = loadClubs()
2222

23+
2324
@app.route('/')
2425
def index():
2526
return render_template('index.html')
2627

27-
@app.route('/showSummary',methods=['POST'])
28+
29+
@app.route('/showSummary', methods=['POST'])
2830
def showSummary():
29-
club = [club for club in clubs if club['email'] == request.form['email']][0]
30-
return render_template('welcome.html',club=club,competitions=competitions)
31+
club = [club for club in clubs if club['email'] == request.form['email']][
32+
0]
33+
return render_template('welcome.html', club=club,
34+
competitions=competitions)
3135

3236

3337
@app.route('/book/<competition>/<club>')
34-
def book(competition,club):
38+
def book(competition, club):
3539
foundClub = [c for c in clubs if c['name'] == club][0]
3640
foundCompetition = [c for c in competitions if c['name'] == competition][0]
3741
if foundClub and foundCompetition:
38-
return render_template('booking.html',club=foundClub,competition=foundCompetition)
42+
return render_template('booking.html', club=foundClub,
43+
competition=foundCompetition)
3944
else:
4045
flash("Something went wrong-please try again")
41-
return render_template('welcome.html', club=club, competitions=competitions)
46+
return render_template('welcome.html', club=club,
47+
competitions=competitions)
4248

4349

44-
@app.route('/purchasePlaces',methods=['POST'])
50+
@app.route('/purchasePlaces', methods=['POST'])
4551
def purchasePlaces():
46-
competition = [c for c in competitions if c['name'] == request.form['competition']][0]
52+
competition = \
53+
[c for c in competitions if c['name'] == request.form['competition']][0]
4754
club = [c for c in clubs if c['name'] == request.form['club']][0]
55+
4856
placesRequired = int(request.form['places'])
49-
competition['numberOfPlaces'] = int(competition['numberOfPlaces'])-placesRequired
57+
58+
if placesRequired > 12:
59+
flash('Max purchase 12.')
60+
return render_template('welcome.html', club=club,
61+
competitions=competitions)
62+
63+
competition['numberOfPlaces'] = int(
64+
competition['numberOfPlaces']) - placesRequired
5065
flash('Great-booking complete!')
51-
return render_template('welcome.html', club=club, competitions=competitions)
66+
67+
return render_template('welcome.html', club=club,
68+
competitions=competitions)
5269

5370

5471
# TODO: Add route for points display
5572

5673

5774
@app.route('/logout')
5875
def logout():
59-
return redirect(url_for('index'))
76+
return redirect(url_for('index'))

tests/__init__.py

Whitespace-only changes.

tests/conftest.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import pytest
2+
3+
from server import app, loadClubs, loadCompetitions
4+
5+
6+
@pytest.fixture
7+
def client():
8+
app.config['TESTING'] = True
9+
with app.test_client() as client:
10+
yield client

tests/test_server.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from server import loadCompetitions, loadClubs
2+
3+
4+
def test_purchase_places(client):
5+
6+
clubs = loadClubs()
7+
competitions = loadCompetitions()
8+
9+
test_club = clubs[0]
10+
test_competition = competitions[0]
11+
places_to_purchase = 8
12+
13+
response = client.post('/purchasePlaces', data={
14+
'club': test_club['name'],
15+
'competition': test_competition['name'],
16+
'places': places_to_purchase
17+
})
18+
19+
assert response.status_code == 200
20+
assert b'Great-booking complete!' in response.data
21+
22+
23+
def test_max_purchase_places(client):
24+
25+
clubs = loadClubs()
26+
competitions = loadCompetitions()
27+
28+
test_club = clubs[0]
29+
test_competition = competitions[0]
30+
places_to_purchase = 28
31+
32+
response = client.post('/purchasePlaces', data={
33+
'club': test_club['name'],
34+
'competition': test_competition['name'],
35+
'places': places_to_purchase
36+
})
37+
38+
assert response.status_code == 200
39+
assert b'Max purchase 12.' in response.data

0 commit comments

Comments
 (0)