Skip to content

Commit 92e92de

Browse files
committed
Issue OpenClassrooms-Student-Center#4 Fixed Issue OpenClassrooms-Student-Center#6 Fixed Some parts of the tree structure for the tests have been modified.
1 parent 9131296 commit 92e92de

8 files changed

+205
-88
lines changed

internal_functions.py

-13
This file was deleted.

server.py

+59-20
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,91 @@
1-
import json
2-
from flask import Flask,render_template,request,redirect,flash,url_for
1+
from flask import Flask, render_template, request, redirect, flash, url_for, json
32
from http import HTTPStatus
4-
from internal_functions import loadCompetitions, loadClubs
53

4+
5+
########### Internal function ###########
6+
def loadclubs() -> object:
7+
with open(r'D:\Tan\Travail\developpement\python\projetOC\projet11\clubs.json') as c:
8+
listofclubs = json.load(c)['clubs']
9+
return listofclubs
10+
11+
12+
def loadcompetitions():
13+
with open(r'D:\Tan\Travail\developpement\python\projetOC\projet11\competitions.json') as comps:
14+
listofcompetitions = json.load(comps)['competitions']
15+
return listofcompetitions
16+
17+
def valid_reservation(places_required, clubpoint, nb_place_competition):
18+
message = ""
19+
if places_required > 12:
20+
message += "The places required should not be more than 12. "
21+
22+
if places_required > nb_place_competition:
23+
message += "The places required is more than the number of places of the competition. "
24+
25+
if places_required > clubpoint:
26+
message += "The places required is more than the number of places of the club."
27+
28+
if message == "":
29+
message += "Great-booking complete!"
30+
return True, message
31+
else:
32+
return False, message
33+
34+
########### Create app ###########
635
app = Flask(__name__)
736
app.secret_key = 'something_special'
837

9-
competitions = loadCompetitions()
10-
clubs = loadClubs()
38+
competitions = loadcompetitions()
39+
clubs = loadclubs()
1140

41+
########### Endpoints ###########
1242
@app.route('/')
1343
def index():
1444
return render_template('index.html')
1545

16-
@app.route('/showSummary',methods=['POST'])
17-
def showSummary():
46+
47+
@app.route('/showSummary', methods=['POST'])
48+
def showsummary():
1849
try:
1950
club = [club for club in clubs if club['email'] == request.form['email']][0]
20-
return render_template('welcome.html',club=club,competitions=competitions)
51+
return render_template('welcome.html', club=club, competitions=competitions)
2152

2253
except IndexError:
2354
flash("Sorry, that email was not found.")
2455
return render_template('index.html'), HTTPStatus.UNAUTHORIZED
2556

57+
2658
@app.route('/book/<competition>/<club>')
27-
def book(competition,club):
28-
foundClub = [c for c in clubs if c['name'] == club][0]
29-
foundCompetition = [c for c in competitions if c['name'] == competition][0]
30-
if foundClub and foundCompetition:
31-
return render_template('booking.html',club=foundClub,competition=foundCompetition)
59+
def book(competition, club):
60+
foundclub = [c for c in clubs if c['name'] == club][0]
61+
foundcompetition = [c for c in competitions if c['name'] == competition][0]
62+
if foundclub and foundcompetition:
63+
return render_template('booking.html', club=foundclub, competition=foundcompetition)
3264
else:
3365
flash("Something went wrong-please try again")
3466
return render_template('welcome.html', club=club, competitions=competitions)
3567

3668

37-
@app.route('/purchasePlaces',methods=['POST'])
38-
def purchasePlaces():
69+
@app.route('/purchasePlaces', methods=['POST'])
70+
def purchaseplaces():
3971
competition = [c for c in competitions if c['name'] == request.form['competition']][0]
4072
club = [c for c in clubs if c['name'] == request.form['club']][0]
41-
placesRequired = int(request.form['places'])
42-
competition['numberOfPlaces'] = int(competition['numberOfPlaces'])-placesRequired
43-
flash('Great-booking complete!')
44-
return render_template('welcome.html', club=club, competitions=competitions)
73+
placesrequired = int(request.form['places'])
74+
75+
statut, message = valid_reservation(placesrequired, int(club["points"]), int(competition['numberOfPlaces']))
76+
if statut:
77+
competition['numberOfPlaces'] = int(competition['numberOfPlaces']) - placesrequired
78+
club['points'] = int(club['points']) - placesrequired
79+
flash(message)
80+
return render_template('welcome.html', club=club, competitions=competitions)
81+
else:
82+
flash(message)
83+
return render_template('booking.html', club=club, competition=competition),HTTPStatus.BAD_REQUEST
4584

4685

4786
# TODO: Add route for points display
4887

4988

5089
@app.route('/logout')
5190
def logout():
52-
return redirect(url_for('index'))
91+
return redirect(url_for('index'))

templates/booking.html

+10
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,15 @@ <h2>{{competition['name']}}</h2>
1313
<label for="places">How many places?</label><input type="number" name="places" id=""/>
1414
<button type="submit">Book</button>
1515
</form>
16+
17+
{% with messages = get_flashed_messages() %}
18+
{% if messages %}
19+
<ul class=flashes>
20+
{% for message in messages %}
21+
<li>{{ message }}</li>
22+
{% endfor %}
23+
</ul>
24+
{% endif %}
25+
{% endwith %}
1626
</body>
1727
</html>

tests/conftest.py

+13-52
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,24 @@
11
import pytest
2-
32
import server
43
from server import app
4+
from tests.global_var import simu_clubs, simu_competitions
55

6-
7-
def get_clubs_tests():
8-
clubs = [
9-
{
10-
"name": "Simply Lift",
11-
"email": "john@simplylift.com",
12-
"points": "13"
13-
},
14-
{
15-
"name": "Iron Temple",
16-
"email": "admin@irontemple.com",
17-
"points": "4"
18-
},
19-
{
20-
"name": "She Lifts",
21-
"email": "kate@shelifts.co.uk",
22-
"points": "12"
23-
},
24-
{
25-
"name": "Club Test",
26-
"email": "clubtest@gmail.com",
27-
"points": "50"
28-
},
29-
]
30-
return clubs
31-
32-
33-
def get_competitions_tests():
34-
competitions = [
35-
{
36-
"name": "Spring Festival",
37-
"date": "2020-03-27 10:00:00",
38-
"numberOfPlaces": "25"
39-
},
40-
{
41-
"name": "Fall Classic",
42-
"date": "2020-10-22 13:30:00",
43-
"numberOfPlaces": "13"
44-
},
45-
{
46-
"name": "Competition Test",
47-
"date": "2022-10-22 13:30:00",
48-
"numberOfPlaces": "50"
49-
},
50-
]
51-
return competitions
52-
53-
6+
########### Configure Client ###########
547
@pytest.fixture
558
def client():
569
with app.test_client() as client:
5710
yield client
5811

12+
########### Configure DataBase ###########
13+
@pytest.fixture
14+
def competitions_test():
15+
return simu_competitions
16+
17+
@pytest.fixture
18+
def clubs_test():
19+
return simu_clubs
5920

6021
@pytest.fixture
61-
def databasemocker(mocker):
62-
mocker.patch.object(server, "clubs", get_clubs_tests())
63-
mocker.patch.object(server, "competitions", get_competitions_tests())
22+
def data_base_mocker(mocker):
23+
mocker.patch.object(server, "clubs", simu_clubs)
24+
mocker.patch.object(server, "competitions", simu_competitions)

tests/global_var.py

+44-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,44 @@
1-
ENDPOINT_SHOWSUMMARY= "/showSummary"
1+
ENDPOINT_SHOWSUMMARY = "/showSummary"
2+
ENDPOINT_PURCHASE_PLACES = "/purchasePlaces"
3+
MAX_PURCHASE = 12
4+
5+
simu_clubs = [
6+
{
7+
"name": "Simply Lift 333",
8+
"email": "john@simplylift.com",
9+
"points": "13"
10+
},
11+
{
12+
"name": "Iron Temple",
13+
"email": "admin@irontemple.com",
14+
"points": "4"
15+
},
16+
{
17+
"name": "She Lifts",
18+
"email": "kate@shelifts.co.uk",
19+
"points": "12"
20+
},
21+
{
22+
"name": "Club Test",
23+
"email": "clubtest@gmail.com",
24+
"points": "50"
25+
},
26+
]
27+
28+
simu_competitions = [
29+
{
30+
"name": "Spring Festival",
31+
"date": "2020-03-27 10:00:00",
32+
"numberOfPlaces": "25"
33+
},
34+
{
35+
"name": "Fall Classic",
36+
"date": "2020-10-22 13:30:00",
37+
"numberOfPlaces": "13"
38+
},
39+
{
40+
"name": "Competition Test",
41+
"date": "2022-10-22 13:30:00",
42+
"numberOfPlaces": "50"
43+
},
44+
]

tests/integration_test/test_authentication.py tests/integration_test/test_integration_authentication.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class TestAuthentication:
99
({'email': 'clubtest@gmail.com'}, HTTPStatus.OK),
1010
({'email': 'kate@shelifts.co.uk'}, HTTPStatus.OK),
1111
])
12-
def test_good_email(self, email, status_code, client, databasemocker):
12+
def test_good_email(self, email, status_code, client, data_base_mocker):
1313
result = client.post(ENDPOINT_SHOWSUMMARY, data=email)
1414
message = f"Welcome, {email['email']}"
1515

@@ -20,7 +20,7 @@ def test_good_email(self, email, status_code, client, databasemocker):
2020
({'email': 'test@gmail.com'}, HTTPStatus.NOT_FOUND),
2121
({'email': ''}, HTTPStatus.NOT_FOUND),
2222
])
23-
def test_bad_email(self, email, status_code, client, databasemocker):
23+
def test_bad_email(self, email, status_code, client, data_base_mocker):
2424
result = client.post(ENDPOINT_SHOWSUMMARY, data=email)
2525

2626
assert result.status_code == HTTPStatus.UNAUTHORIZED
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import pytest
2+
from http import HTTPStatus
3+
import random
4+
from tests.global_var import ENDPOINT_PURCHASE_PLACES
5+
from tests.global_var import MAX_PURCHASE
6+
7+
@pytest.fixture
8+
def valid_reservation_mocker(mocker):
9+
mocker.patch('server.valid_reservation', return_value=(True, "Valid message"))
10+
11+
12+
@pytest.fixture
13+
def invalid_reservation_mocker(mocker):
14+
mocker.patch('server.valid_reservation', return_value=(False, "Invalid message"))
15+
16+
17+
class TestReservation:
18+
19+
def test_good_purchase(self, client, valid_reservation_mocker, data_base_mocker, clubs_test,
20+
competitions_test):
21+
data = {
22+
'competition': (competitions_test[0])['name'],
23+
'club': (clubs_test[0])['name'],
24+
'places': random.randint(1, MAX_PURCHASE),
25+
}
26+
club_points_before_purchase = (clubs_test[0])['points']
27+
competitions_places_before_purchase = (competitions_test[0])['numberOfPlaces']
28+
result = client.post(ENDPOINT_PURCHASE_PLACES, data=data)
29+
30+
assert result.status_code == HTTPStatus.OK
31+
assert "Valid message" in result.data.decode()
32+
assert int(club_points_before_purchase) - data['places'] == int((clubs_test[0])['points'])
33+
assert int(competitions_places_before_purchase) - data['places'] == \
34+
int((competitions_test[0])['numberOfPlaces'])
35+
36+
def test_bad_purchase(self, client, invalid_reservation_mocker, data_base_mocker, clubs_test,
37+
competitions_test):
38+
data = {
39+
'competition': (competitions_test[0])['name'],
40+
'club': (clubs_test[0])['name'],
41+
'places': 0,
42+
}
43+
club_points_before_purchase = (clubs_test[0])['points']
44+
competitions_places_before_purchase = (competitions_test[0])['numberOfPlaces']
45+
result = client.post(ENDPOINT_PURCHASE_PLACES, data=data)
46+
47+
assert result.status_code == HTTPStatus.BAD_REQUEST
48+
assert "Invalid message" in result.data.decode()
49+
assert int(club_points_before_purchase) == int((clubs_test[0])['points'])
50+
assert int(competitions_places_before_purchase) == int((competitions_test[0])['numberOfPlaces'])
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import pytest
2+
3+
from server import valid_reservation
4+
5+
6+
@pytest.mark.parametrize("places_required, clubpoint,nb_place_competition, expected_value, messages",
7+
[(13, 20, 50, False, ["The places required should not be more than 12"]),
8+
(12, 20, 50, True, ["Great-booking complete!"]),
9+
(11, 20, 50, True, ["Great-booking complete!"]),
10+
(11, 20, 10, False, ["The places required is more than the number of places of the "
11+
"competition."]),
12+
(10, 20, 10, True, ["Great-booking complete!"]),
13+
(9, 20, 10, True, ["Great-booking complete!"]),
14+
(11, 10, 20, False, ["The places required is more than the number of places of the club"]),
15+
(10, 10, 20, True, ["Great-booking complete!"]),
16+
(9, 10, 20, True, ["Great-booking complete!"]),
17+
(13, 30, 12, False, ["The places required should not be more than 12",
18+
"The places required is more than the number of places of the "
19+
"competition"]),
20+
(13, 12, 12, False, ["The places required should not be more than 12",
21+
"The places required is more than the number of places of the "
22+
"competition",
23+
"The places required is more than the number of places of the club"])])
24+
def test_sut_validreservation(places_required, clubpoint, nb_place_competition, expected_value, messages):
25+
assert (valid_reservation(places_required, clubpoint, nb_place_competition))[0] == expected_value
26+
for message in messages:
27+
assert message in (valid_reservation(places_required, clubpoint, nb_place_competition))[1]

0 commit comments

Comments
 (0)