Skip to content

Commit 0487eda

Browse files
authoredNov 6, 2022
Test/add integration testing (#8)
* feat(board.html): add template for points display board * feat(server): add route for points display board * feat(board.html): complete template * test(test_points_board): start testing happy path * feat(board.html): add condition if no clubs in json * test(test_points_board): add sad path to test * test(units/): add test_unlimited_points.py * fix(server): add condition to limit purchase * fix(booking.html): add display of flash message * test(units/): test_unlimited_points.py passed * test(units/): change docstrings in test_unlimited_points.py * test(units/): done test_unlimited_points.py * fix(issue): [OpenClassrooms-Student-Center#2] * test(units/): add test_points_not_update.py with TDD approch added test and failed * fix(server): add substraction points after book places * test(units/): run test_points_not_update.py and passed * fix(issue): [OpenClassrooms-Student-Center#6] * test(units/): add test_book_under_12.py with TDD method: started with failed test * fix(server): add condition if book more 12 places * test(units/): complete test_book_under_12.py and run tests passed * fix(issue): [OpenClassrooms-Student-Center#4] * test(functional/): add & complete test_route.py * fix(404.html): add 404 handler and template * fix(500.html): add 500 handler and template * test(functional/): add & complete test_load_json.py * test(integration/): add & complete test of app * fix(server): issue book more than places available * test(integration/): cleanup test_app
1 parent 3dfef18 commit 0487eda

File tree

6 files changed

+162
-24
lines changed

6 files changed

+162
-24
lines changed
 

‎server.py

+11-5
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,13 @@ def book(competition,club):
8484
Competition's name and places available
8585
& the booking form
8686
"""
87-
foundClub = [c for c in clubs if c['name'] == club][0]
88-
foundCompetition = [c for c in competitions if c['name'] == competition][0]
89-
if foundClub and foundCompetition:
87+
try:
88+
foundClub = [c for c in clubs if c['name'] == club][0]
89+
foundCompetition = [c for c in competitions if c['name'] == competition][0]
9090
return render_template('booking.html',club=foundClub,competition=foundCompetition)
91-
else:
92-
flash("Something went wrong-please try again")
91+
92+
except IndexError:
93+
flash("Sorry, this club or competition wasn't found !!")
9394
return render_template('welcome.html', club=club, competitions=competitions, current_date=current_date)
9495

9596

@@ -115,6 +116,7 @@ def purchase_places():
115116
club_points = int(club['points'])
116117
placesRequired = int(request.form['places'])
117118
current_cart = cart[competition["name"]][club["name"]]
119+
current_places_available = int(competition['numberOfPlaces'])
118120

119121
if competition['date'] < current_date:
120122
flash("Sorry, this competition is over !")
@@ -133,6 +135,10 @@ def purchase_places():
133135
and now you have exceeded the limit of {MAX_PLACES} places !""")
134136
return render_template('booking.html', club=club, competition=competition, current_date=current_date), 400
135137

138+
elif placesRequired > current_places_available:
139+
flash("Sorry, you book more places than available !")
140+
return render_template('booking.html', club=club, competition=competition, current_date=current_date), 400
141+
136142
else:
137143
competition['numberOfPlaces'] = int(competition['numberOfPlaces'])-placesRequired
138144

‎templates/404.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<!DOCTYPE html>
22
<html lang="en">
33
<head>
4-
<title>404</title>
4+
<title>404 error page</title>
55
<meta charset="utf-8">
66
<meta name="viewport" content="width=device-width, initial-scale=1">
77
</head>

‎tests/conftest.py

+26-18
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,6 @@ def client():
1212
yield client
1313

1414

15-
# @pytest.fixture
16-
# def current_date():
17-
# now = datetime.datetime.now()
18-
# return now.strftime("%Y-%m-%d, %H:%M:%S")
19-
20-
2115
@pytest.fixture
2216
def clubs_fixture():
2317
return [
@@ -75,8 +69,8 @@ def competitions_fixture():
7569
},
7670
{
7771
"name": "Champion Road",
78-
"date": "2022-10-13 13:00:00",
79-
"numberOfPlaces": "13"
72+
"date": "2023-02-13 13:00:00",
73+
"numberOfPlaces": "8"
8074
},
8175
{
8276
"name": "Ice Race",
@@ -103,26 +97,40 @@ def get(fixture, case , places=0):
10397
fixture[0].patch.object(server, 'competitions', fixture[2])
10498
fixture[0].patch.object(server, 'cart', fixture[3])
10599

106-
if case == "email":
100+
valid_club = fixture[1][0]['name']
101+
valid_competition = fixture[2][0]['name']
107102

103+
if case == "email":
108104
email = fixture[1][0]['email']
109105
data_email = {'email': email}
110106
return data_email
111107

108+
elif case == "wrong_email":
109+
return {"email": "unknown_email@gmail.com"}
110+
112111
elif case == "club_competition":
113-
114-
club = fixture[1][0]['name']
115-
competition = fixture[2][0]['name']
116-
data_book = {'competition': competition, 'club': club}
112+
data_book = {'competition': valid_competition, 'club': valid_club}
117113
return data_book
118114

115+
elif case == "club_unknown-competition":
116+
return {'competition': "Black Race", 'club': valid_club}
117+
119118
elif case == "club_competition_places":
120-
121-
club = fixture[1][0]['name']
122-
competition = fixture[2][0]['name']
123-
data_purchase_places = {'competition': competition, 'club': club, 'places': places}
119+
data_purchase_places = {'competition': valid_competition, 'club': valid_club, 'places': places}
124120
return data_purchase_places
125121

126-
elif case == "board":
122+
elif case == "small_competition_places":
123+
small_competition = fixture[2][2]['name']
124+
data_purchase_places = {'competition': small_competition, 'club': valid_club, 'places': places}
125+
return data_purchase_places
127126

127+
elif case == "past_competition_places":
128+
past_competition = fixture[2][1]['name']
129+
data_purchase_places = {'competition': past_competition, 'club': valid_club, 'places': places}
130+
return data_purchase_places
131+
132+
elif case == "board":
128133
return fixture[1]
134+
135+
elif case == "empty_board":
136+
return fixture[0].patch.object(server, 'clubs', '')

‎tests/integration/__init__.py

Whitespace-only changes.

‎tests/integration/test_app.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from conftest import get
2+
3+
4+
"""
5+
Testing a complete smooth scenario of a user :
6+
* Home page
7+
* Login
8+
* book for a competition
9+
* purchase places
10+
* got to see display board
11+
* Logout
12+
13+
"""
14+
15+
# ================================================== START TESTING
16+
17+
18+
def test_app(client, fixture):
19+
20+
assert client.get('/').status_code == 200
21+
response = client.get('/')
22+
assert b'Welcome to the GUDLFT Registration' in response.data
23+
24+
email = get(fixture, "email")
25+
response = client.post('/showSummary', data=email)
26+
assert response.status_code == 200
27+
28+
data = get(fixture, "club_competition")
29+
response = client.get(f"/book/{data['competition']}/{data['club']}")
30+
assert response.status_code == 200
31+
32+
data = get(fixture, "club_competition_places", 5)
33+
response = client.post('/purchasePlaces', data=data)
34+
assert response.status_code == 200
35+
36+
response = client.get('/board')
37+
clubs = get(fixture, "board")
38+
assert response.status_code == 200
39+
for club in clubs:
40+
assert club['name'] in response.data.decode()
41+
assert str(club["points"]) in response.data.decode()
42+
43+
response = client.get('/logout')
44+
assert response.status_code == 302
45+
assert response.headers["Location"] == "/"

‎tests/integration/test_sad_app.py

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
from conftest import get
2+
import pytest
3+
4+
"""
5+
Testing a complete worst scenario of a user :
6+
* Home page : wrong endpoint (/home') doesn't exist.
7+
* Login : with unknown email
8+
* book for a unknown competition
9+
* purchase places :
10+
* past competition
11+
* more than 12 places,
12+
* more than club's points,
13+
* more than competition's places, (book 12 places, this competition has 8)
14+
15+
* got to see display board with empty data
16+
* Logout successfully ;)
17+
18+
data used for this test :
19+
Mostly :
20+
club = Basic lift, 15 points
21+
competition = Aka League, date [2023-02-05 10:00:00], 20 places.
22+
23+
"""
24+
25+
# ================================================== START TESTING
26+
27+
28+
def test_sad_app(client, fixture):
29+
30+
response = client.get('/home')
31+
assert response.status_code == 404
32+
assert b'404 error page' in response.data
33+
34+
email = get(fixture, "wrong_email")
35+
response = client.post('/showSummary', data=email)
36+
assert response.status_code == 302
37+
with pytest.raises(Exception) as exc_info:
38+
assert str(exc_info.value) == "Sorry, this email wasn't found. Please try again with a correct email !!"
39+
40+
41+
data = get(fixture, "club_unknown-competition")
42+
response = client.get(f"/book/{data['competition']}/{data['club']}")
43+
with pytest.raises(Exception) as exc_info:
44+
assert str(exc_info.value) == "Sorry, this club or competition wasn&#39;t found !!"
45+
46+
data = get(fixture, "past_competition_places", 5)
47+
response = client.post('/purchasePlaces', data=data)
48+
assert response.status_code == 400
49+
assert b'Sorry, this competition is over !' in response.data
50+
51+
52+
data = get(fixture, "club_competition_places", 13)
53+
response = client.post('/purchasePlaces', data=data)
54+
assert response.status_code == 400
55+
assert b'Sorry, you can&#39;t book more than 12 places !' in response.data
56+
57+
58+
data = get(fixture, "small_competition_places", 12)
59+
response = client.post('/purchasePlaces', data=data)
60+
assert response.status_code == 400
61+
assert b'Sorry, you book more places than available !' in response.data
62+
63+
64+
data = get(fixture, "club_competition_places", 10)
65+
response = client.post('/purchasePlaces', data=data)
66+
data = get(fixture, "club_competition_places", 10)
67+
response = client.post('/purchasePlaces', data=data)
68+
assert response.status_code == 400
69+
assert b'Sorry, your club doesn&#39;t have enough points !' in response.data
70+
71+
72+
get(fixture, "empty_board")
73+
response = client.get('/board')
74+
assert b'Sorry, this section has no clubs to display' in response.data
75+
76+
77+
response = client.get('/logout')
78+
assert response.status_code == 302
79+
assert response.headers["Location"] == "/"

0 commit comments

Comments
 (0)