forked from OpenClassrooms-Student-Center/Python_Testing
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolve OpenClassrooms-Student-Center#2 : Club Points validation
- Loading branch information
Showing
10 changed files
with
217 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,19 @@ | ||
{"clubs":[ | ||
{ | ||
"name":"Simply Lift", | ||
"email":"john@simplylift.co", | ||
"points":"13" | ||
}, | ||
{ | ||
"name":"Iron Temple", | ||
"email": "admin@irontemple.com", | ||
"points":"4" | ||
}, | ||
{ "name":"She Lifts", | ||
"email": "kate@shelifts.co.uk", | ||
"points":"12" | ||
} | ||
]} | ||
{ | ||
"clubs": [ | ||
{ | ||
"name": "Simply pytestLift", | ||
"email": "john@simplylift.co", | ||
"points": 13 | ||
}, | ||
{ | ||
"name": "Iron Temple", | ||
"email": "admin@irontemple.com", | ||
"points": 4 | ||
}, | ||
{ | ||
"name": "She Lifts", | ||
"email": "kate@shelifts.co.uk", | ||
"points": 12 | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import json | ||
import os | ||
|
||
|
||
def load_data(file_name): | ||
""" | ||
Charge les données à partir d'un fichier JSON. | ||
Args: | ||
file_name (str): Le nom du fichier JSON. | ||
Returns: | ||
dict: Les données chargées depuis le fichier JSON. | ||
""" | ||
current_dir = os.path.dirname(__file__) | ||
file_path = os.path.join(current_dir, "data", file_name) | ||
with open(file_path) as f: | ||
return json.load(f) | ||
|
||
|
||
def save_data(data, file_name): | ||
""" | ||
Enregistre les données dans un fichier JSON. | ||
Args: | ||
data (dict): Les données à enregistrer. | ||
file_name (str): Le nom du fichier JSON où enregistrer les données. | ||
""" | ||
current_dir = os.path.dirname(__file__) | ||
file_path = os.path.join(current_dir, "data", file_name) | ||
with open(file_path, "w") as f: | ||
json.dump(data, f) | ||
|
||
|
||
def load_clubs(): | ||
return load_data("clubs.json")["clubs"] | ||
|
||
|
||
def load_competitions(): | ||
return load_data("competitions.json")["competitions"] | ||
|
||
|
||
def save_clubs(clubs): | ||
save_data({"clubs": clubs}, "clubs.json") | ||
|
||
|
||
def save_competitions(competitions): | ||
save_data({"competitions": competitions}, "competitions.json") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
body { | ||
margin-left: 20px; | ||
} | ||
button { | ||
margin-top: 20px; | ||
} | ||
.flash-error { | ||
color: red; | ||
font-weight: 700; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from gudlift_reservation import app, server | ||
from gudlift_reservation.json_handler import save_clubs, save_competitions | ||
|
||
|
||
def setup_class(cls): | ||
""" | ||
Méthode de configuration de classe exécutée une seule fois avant tous les tests. | ||
Initialise un client de test Flask | ||
Charge les données des clubs et des compétitions. | ||
Cree des sauvegardes de clubs.json et competitions.json | ||
""" | ||
cls.client = app.test_client() | ||
cls.clubs = server.load_clubs() | ||
cls.competitions = server.load_competitions() | ||
cls.clubs_save = cls.clubs | ||
cls.competitions_save = cls.competitions | ||
|
||
|
||
def teardown_method(self): | ||
""" | ||
Méthode de configuration executée a la fin des tests | ||
Rétablit les fichiers json d'origine. | ||
""" | ||
save_clubs(self.clubs_save) | ||
save_competitions(self.competitions_save) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import pytest | ||
|
||
from gudlift_reservation.json_handler import load_clubs | ||
|
||
from . import setup_class, teardown_method | ||
|
||
|
||
class TestCalculation: | ||
""" | ||
Classe de tests pour tester les calculs de l'application. | ||
""" | ||
|
||
@classmethod | ||
def setup_class(cls): | ||
setup_class(cls) | ||
|
||
def teardown_method(self): | ||
teardown_method(self) | ||
|
||
@pytest.mark.parametrize( | ||
"club_name, competition_name, places, expected_value", | ||
[ | ||
("Iron Temple", "Spring Festival", 1, "Great-booking complete!"), | ||
("Iron Temple", "Spring Festival", 100, "insufficient number of points"), | ||
("Iron Temple", "Spring Festival", -100, "Number of places required must be positive"), | ||
], | ||
) | ||
def test_club_purchase_places_calculation( | ||
self, club_name, competition_name, places, expected_value | ||
): | ||
""" | ||
Verifie l'utilisation des points d'un club. | ||
Le nombre de points demandés doit etre positif. | ||
Les points du club doivent rester positif ou nul. | ||
Les points utilisés doivent etre déduits des points du club. | ||
""" | ||
|
||
club = next(club for club in self.clubs if club["name"] == club_name) | ||
club_points_before = club["points"] | ||
|
||
rv = self.client.post( | ||
"/purchasePlaces", | ||
data={"competition": competition_name, "club": club_name, "places": places}, | ||
follow_redirects=True | ||
) | ||
|
||
self.clubs = load_clubs() | ||
club = next(club for club in self.clubs if club["name"] == club_name) | ||
club_points_after = club["points"] | ||
expected_points = club_points_before - places | ||
|
||
assert rv.status_code == 200 | ||
assert expected_value.encode("utf-8") in rv.data | ||
assert club_points_after >= 0 | ||
|
||
if expected_value == "Great-booking complete!": | ||
assert club_points_after == expected_points |
Oops, something went wrong.