Skip to content

Commit e3e986a

Browse files
committed
(test) handle update points of club
1 parent d3da543 commit e3e986a

File tree

5 files changed

+123
-64
lines changed

5 files changed

+123
-64
lines changed

.flake8

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[flake8]
2+
max-line-length = 80
3+
exclude =
4+
bin/activate.py,
5+
lib,
6+
packages,
7+
migrations,
8+
build,
9+
dist,
10+
*.pyc,
11+
__pycache__,
12+
bin/activate_this.py

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[tool.black]
2+
line-length = 80

requirements.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ itsdangerous==1.1.0
44
Jinja2==2.11.2
55
MarkupSafe==1.1.1
66
Werkzeug==1.0.1
7-
pytest
7+
pytest
8+
black
9+
flake8

server.py

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,83 @@
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
app = Flask(__name__)
6-
app.secret_key = 'something_special'
7-
app.config['CLUBS_FILE'] = 'clubs.json'
6+
app.secret_key = "something_special"
7+
app.config["CLUBS_FILE"] = "clubs.json"
8+
89

910
def loadClubs():
10-
with open(app.config['CLUBS_FILE']) as c:
11-
listOfClubs = json.load(c)['clubs']
11+
with open(app.config["CLUBS_FILE"]) as c:
12+
listOfClubs = json.load(c)["clubs"]
1213
return listOfClubs
1314

1415

15-
1616
def loadCompetitions():
17-
with open('competitions.json') as comps:
18-
listOfCompetitions = json.load(comps)['competitions']
19-
return listOfCompetitions
17+
with open("competitions.json") as comps:
18+
listOfCompetitions = json.load(comps)["competitions"]
19+
return listOfCompetitions
2020

2121

2222
competitions = loadCompetitions()
2323
clubs = loadClubs()
2424

2525

26-
@app.route('/')
26+
@app.route("/")
2727
def index():
28-
return render_template('index.html')
28+
return render_template("index.html")
29+
2930

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

3536

36-
@app.route('/book/<competition>/<club>')
37-
def book(competition,club):
38-
foundClub = [c for c in clubs if c['name'] == club][0]
39-
foundCompetition = [c for c in competitions if c['name'] == competition][0]
37+
@app.route("/book/<competition>/<club>")
38+
def book(competition, club):
39+
foundClub = [c for c in clubs if c["name"] == club][0]
40+
foundCompetition = [c for c in competitions if c["name"] == competition][0]
4041
if foundClub and foundCompetition:
41-
return render_template('booking.html',club=foundClub,competition=foundCompetition)
42+
return render_template(
43+
"booking.html", club=foundClub, competition=foundCompetition
44+
)
4245
else:
4346
flash("Something went wrong-please try again")
44-
return render_template('welcome.html', club=club, competitions=competitions)
47+
return render_template(
48+
"welcome.html", club=club, competitions=competitions
49+
)
4550

4651

47-
@app.route('/purchasePlaces',methods=['POST'])
52+
@app.route("/purchasePlaces", methods=["POST"])
4853
def purchasePlaces():
4954
dict_clubs = {}
50-
competition = [c for c in competitions if c['name'] == request.form['competition']][0]
51-
club, index_club = [(c, clubs.index(c)) for c in clubs if c['name'] == request.form['club']][0]
52-
placesRequired = int(request.form['places'])
53-
competition['numberOfPlaces'] = int(competition['numberOfPlaces'])-placesRequired
54-
club['points'] = int(club['points']) - placesRequired
55-
clubs[index_club]['points'] = str(club['points'])
55+
competition = [
56+
c for c in competitions if c["name"] == request.form["competition"]
57+
][0]
58+
club, index_club = [
59+
(c, clubs.index(c)) for c in clubs if c["name"] == request.form["club"]
60+
][0]
61+
placesRequired = int(request.form["places"])
62+
competition["numberOfPlaces"] = (
63+
int(competition["numberOfPlaces"]) - placesRequired
64+
)
65+
club["points"] = int(club["points"]) - placesRequired
66+
clubs[index_club]["points"] = str(club["points"])
5667
dict_clubs["clubs"] = clubs
5768
try:
58-
with open(app.config['CLUBS_FILE'], 'w') as file_club:
69+
with open(app.config["CLUBS_FILE"], "w") as file_club:
5970
json.dump(dict_clubs, file_club, indent=4)
6071
except FileNotFoundError:
61-
flash('File clubs not found')
62-
return redirect(url_for('book'), competition, club)
63-
flash('Great-booking complete!')
64-
return render_template('welcome.html', club=club, competitions=competitions)
72+
flash("File clubs not found")
73+
return redirect(url_for("book"), competition, club)
74+
flash("Great-booking complete!")
75+
return render_template("welcome.html", club=club, competitions=competitions)
6576

6677

6778
# TODO: Add route for points display
6879

6980

70-
@app.route('/logout')
81+
@app.route("/logout")
7182
def logout():
72-
return redirect(url_for('index'))
83+
return redirect(url_for("index"))

tests/test_endpoints.py

Lines changed: 61 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,70 @@
22
import pytest
33
from server import app
44

5-
@pytest.fixture
6-
def client(tmpdir):
5+
6+
@pytest.fixture(scope="function")
7+
def temp_clubs_file(tmpdir, request):
78
original_clubs_file = tmpdir.join("clubs.json")
8-
original_clubs_file.write(json.dumps({
9-
"clubs": [
10-
{"name": "Simply Lift", "email": "john@simplylift.co", "points": "11"},
11-
{"name": "Iron Temple", "email": "admin@irontemple.com", "points": "4"},
12-
{"name": "She Lifts", "email": "kate@shelifts.co.uk", "points": "12"}
13-
]
14-
}, indent=4))
15-
app.config['CLUBS_FILE'] = str(original_clubs_file)
16-
return app.test_client()
9+
initial_clubs = {
10+
"clubs": [
11+
{
12+
"name": "Simply Lift",
13+
"email": "john@simplylift.co",
14+
"points": "11",
15+
},
16+
{
17+
"name": "Iron Temple",
18+
"email": "admin@irontemple.com",
19+
"points": "2",
20+
},
21+
{
22+
"name": "She Lifts",
23+
"email": "kate@shelifts.co.uk",
24+
"points": "12",
25+
},
26+
]
27+
}
28+
with open(original_clubs_file, "w") as clubs_file:
29+
json.dump(initial_clubs, clubs_file, indent=4)
1730

18-
def test_purchasePlaces_update_points(client):
19-
response = client.post("/purchasePlaces", data = {"places":1,"competition":"Fall Classic","club":"Iron Temple"})
20-
assert response.status_code == 200
21-
assert b'Great-booking complete!' in response.data
22-
# Verify the points and number of places have been updated
23-
with open(app.config['CLUBS_FILE']) as f:
24-
updated_clubs = json.load(f)['clubs']
25-
updated_club = next(c for c in updated_clubs if c['name'] == 'Iron Temple')
26-
assert updated_club['points'] == "3"
31+
app.config["CLUBS_FILE"] = str(original_clubs_file)
32+
33+
# Ensure the clubs are reset for each test
34+
from server import clubs
35+
36+
clubs.clear()
37+
clubs.extend(initial_clubs["clubs"])
2738

28-
def test_purchasePlaces_update_points_file_not_found(client):
29-
app.config['CLUBS_FILE'] = ""
30-
response = client.post("/purchasePlaces", data = {"places":1,"competition":"Fall Classic","club":"Iron Temple"})
31-
assert response.status_code == 500
32-
# assert b'File clubs not found' in response.data
39+
yield initial_clubs
3340

34-
def test_purchasePlaces_update_points_club_not_found(client):
35-
pass
41+
# Cleanup code
42+
try:
43+
tmpdir.remove(ignore_errors=True)
44+
except Exception as e:
45+
print(f"Error during cleanup: {e}")
3646

37-
def test_purchasePlaces_update_points_more_than_available(client):
38-
pass
3947

48+
@pytest.fixture
49+
def client(temp_clubs_file):
50+
with app.test_client() as client:
51+
yield client
52+
53+
54+
def test_purchasePlaces_update_points(client):
55+
response = client.post(
56+
"/purchasePlaces",
57+
data={
58+
"places": 1,
59+
"competition": "Fall Classic",
60+
"club": "Iron Temple",
61+
},
62+
)
63+
assert response.status_code == 200
64+
assert b"Great-booking complete!" in response.data
65+
# Verify the points and number of places have been updated
66+
with open(app.config["CLUBS_FILE"]) as f:
67+
updated_clubs = json.load(f)["clubs"]
68+
updated_club = next(
69+
c for c in updated_clubs if c["name"] == "Iron Temple"
70+
)
71+
assert updated_club["points"] == "1"

0 commit comments

Comments
 (0)