Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
SelHel committed Jun 9, 2022
1 parent fb7a8a5 commit 268a013
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 16 deletions.
20 changes: 16 additions & 4 deletions server.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import json
from flask import Flask, render_template, request, redirect, flash, url_for

app = Flask(__name__)
app.secret_key = 'something_special'
POINTS_FOR_A_PLACE = 1
MAX_PLACES_PER_COMPETITION = 12


def loadClubs():
Expand All @@ -16,6 +16,9 @@ def loadCompetitions():
listOfCompetitions = json.load(comps)['competitions']
return listOfCompetitions


app = Flask(__name__)
app.secret_key = 'something_special'
competitions = loadCompetitions()
clubs = loadClubs()

Expand Down Expand Up @@ -51,8 +54,17 @@ def purchasePlaces():
competition = [c for c in competitions if c['name'] == request.form['competition']][0]
club = [c for c in clubs if c['name'] == request.form['club']][0]
placesRequired = int(request.form['places'])
competition['numberOfPlaces'] = int(competition['numberOfPlaces'])-placesRequired
flash('Great-booking complete!')
places_allowed = int(club["points"]) // POINTS_FOR_A_PLACE
if placesRequired > places_allowed:
flash('You cannot redeem more points than available!')
elif placesRequired > MAX_PLACES_PER_COMPETITION:
flash(f"You cannot book more than {MAX_PLACES_PER_COMPETITION} places per competition!")
elif placesRequired > int(competition['numberOfPlaces']):
flash(f"You cannot reserve more places than are available in the competition!")
else:
competition['numberOfPlaces'] = int(competition['numberOfPlaces']) - placesRequired
club["points"] = places_allowed - placesRequired * POINTS_FOR_A_PLACE
flash('Great-booking complete!')
return render_template('welcome.html', club=club, competitions=competitions)


Expand Down
44 changes: 44 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,47 @@ def client():
server.app.config['TESTING'] = True
client = server.app.test_client()
return client


@pytest.fixture
def mock_clubs(mocker):
clubs = [
{
"name": "Test Club 1",
"email": "club1@test.com",
"points": "13"
},
{
"name": "Test Club 2",
"email": "club2@test.com",
"points": "20"
},
{
"name": "Test Club 3",
"email": "club3@test.com",
"points": "12"
},
]
mocker.patch.object(server, 'clubs', clubs)


@pytest.fixture
def mock_competitions(mocker):
competitions = [
{
"name": "Test Competition 1",
"date": "2022-06-07 10:00:00",
"numberOfPlaces": "25"
},
{
"name": "Test Competition 2",
"date": "2020-03-27 10:00:00",
"numberOfPlaces": "13"
},
{
"name": "Test Competition 3",
"date": "2022-06-09 10:00:00",
"numberOfPlaces": "5"
}
]
mocker.patch.object(server, 'competitions', competitions)
58 changes: 46 additions & 12 deletions tests/tests_unitaires/test_server.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,56 @@

def test_showSummary_with_unknown_email(client):
unknown_email = 'wrong@email.co'
response = client.post('/showSummary', data={'email': unknown_email})
assert response.status_code == 302
from server import POINTS_FOR_A_PLACE, MAX_PLACES_PER_COMPETITION
"""
Bug :
Quand un secrétaire essaie de réserver plus de 12 places dans une compétition,
il reçoit un message de confirmation.
Attendu :
Il ne devrait pas pouvoir réserver plus de 12 places par compétition.
Les points échangés doivent être correctement déduits du total du club.
L'UI doit l'empêcher de réserver plus de 12 places.
Les places doivent être correctement déduites de la compétition.
"""


def test_purchasePlaces_with_valid_data(client, mock_clubs, mock_competitions):
valid_data = {'club': 'Test Club 1',
'competition': 'Test Competition 1',
'places': 12
}
response = client.post('/purchasePlaces', data=valid_data)
assert response.status_code == 200
assert "Great-booking complete!" in response.data.decode()


def test_showSummary_with_known_email(client):
known_email = 'john@simplylift.co'
response = client.post('/showSummary', data={'email': known_email})
def test_purchasePlaces_more_than_twelve_places_in_competition(client, mock_clubs, mock_competitions):
data = {'club': 'Test Club 2',
'competition': 'Test Competition 1',
'places': 13
}
response = client.post('/purchasePlaces', data=data)
assert response.status_code == 200
assert f"You cannot book more than {MAX_PLACES_PER_COMPETITION} places per competition!" in response.data.decode()


def test_club_should_not_be_able_to_use_more_than_their_points_allowed():
pass
def test_purchasePlaces_more_places_than_available_in_competition(client, mock_clubs, mock_competitions):
data = {'club': 'Test Club 2',
'competition': 'Test Competition 3',
'places': 6
}
response = client.post('/purchasePlaces', data=data)
assert response.status_code == 200
assert "You cannot reserve more places than are available in the competition!" in response.data.decode()


def test_points_should_be_deducted_from_the_clubs_total():
pass
def test_purchasePlaces_update_competition_places(client, mock_clubs, mock_competitions):
valid_data = {'club': 'Test Club 1',
'competition': 'Test Competition 1',
'places': 12
}
expected_competition_value = 25 - 13
response = client.post('/purchasePlaces', data=valid_data)
assert response.status_code == 200
assert "Great-booking complete!" in response.data.decode()



Expand Down

0 comments on commit 268a013

Please sign in to comment.