Skip to content

Commit a530554

Browse files
committed
(fix) Handle updates points after purshase places
1 parent 81e09c8 commit a530554

File tree

4 files changed

+75
-21
lines changed

4 files changed

+75
-21
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ lib
44
.Python
55
tests/
66
.envrc
7-
__pycache__
7+
__pycache__
8+
.DS_Store

clubs.json

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1-
{"clubs":[
2-
{
3-
"name":"Simply Lift",
4-
"email":"john@simplylift.co",
5-
"points":"13"
6-
},
7-
{
8-
"name":"Iron Temple",
9-
"email": "admin@irontemple.com",
10-
"points":"4"
11-
},
12-
{ "name":"She Lifts",
13-
"email": "kate@shelifts.co.uk",
14-
"points":"12"
15-
}
16-
]}
1+
{
2+
"clubs": [
3+
{
4+
"name": "Simply Lift",
5+
"email": "john@simplylift.co",
6+
"points": "11"
7+
},
8+
{
9+
"name": "Iron Temple",
10+
"email": "admin@irontemple.com",
11+
"points": "4"
12+
},
13+
{
14+
"name": "She Lifts",
15+
"email": "kate@shelifts.co.uk",
16+
"points": "12"
17+
}
18+
]
19+
}

server.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33

44

55
def loadClubs():
6-
with open('clubs.json') as c:
7-
listOfClubs = json.load(c)['clubs']
8-
return listOfClubs
6+
with open(app.config['CLUBS_FILE']) as c:
7+
listOfClubs = json.load(c)['clubs']
8+
return listOfClubs
9+
910

1011

1112
def loadCompetitions():
@@ -43,10 +44,20 @@ def book(competition,club):
4344

4445
@app.route('/purchasePlaces',methods=['POST'])
4546
def purchasePlaces():
47+
dict_clubs = {}
4648
competition = [c for c in competitions if c['name'] == request.form['competition']][0]
47-
club = [c for c in clubs if c['name'] == request.form['club']][0]
49+
club, index_club = [(c, clubs.index(c)) for c in clubs if c['name'] == request.form['club']][0]
4850
placesRequired = int(request.form['places'])
4951
competition['numberOfPlaces'] = int(competition['numberOfPlaces'])-placesRequired
52+
club['points'] = int(club['points']) - placesRequired
53+
clubs[index_club]['points'] = str(club['points'])
54+
dict_clubs["clubs"] = clubs
55+
try:
56+
with open(app.config['CLUBS_FILE'], 'w') as file_club:
57+
json.dump(dict_clubs, file_club, indent=4)
58+
except FileNotFoundError:
59+
flash('File clubs not found')
60+
return redirect(url_for('book'), competition, club)
5061
flash('Great-booking complete!')
5162
return render_template('welcome.html', club=club, competitions=competitions)
5263

tests/test_endpoints.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import json
2+
import pytest
3+
from server import app
4+
5+
@pytest.fixture
6+
def client(tmpdir):
7+
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()
17+
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"
27+
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
33+
34+
def test_purchasePlaces_update_points_club_not_found(client):
35+
pass
36+
37+
def test_purchasePlaces_update_points_more_than_available(client):
38+
pass
39+

0 commit comments

Comments
 (0)