Skip to content

Commit

Permalink
Merge pull request #2 from Rossignol-h/bugfix/book-past-competiton/#5
Browse files Browse the repository at this point in the history
Bugfix/book past competiton/#5
  • Loading branch information
Rossignol-h authored Nov 3, 2022
2 parents 5185567 + 9ea0811 commit 97ea339
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 8 deletions.
10 changes: 10 additions & 0 deletions competitions.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@
"name": "Fall Classic",
"date": "2020-10-22 13:30:00",
"numberOfPlaces": "13"
},
{
"name": "Winter Competition",
"date": "2022-12-01 14:00:00",
"numberOfPlaces": "8"
},
{
"name": "College League",
"date": "2023-01-15 11:35:00",
"numberOfPlaces": "78"
}
]
}
22 changes: 19 additions & 3 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,41 @@ def show_summary():
return redirect(url_for('index'))


# ====================================================== ROUTE FOR BOOK PLACES


@app.route('/book/<competition>/<club>')
def book(competition,club):
"""
Displays :
Competition's name and places available
& the booking form
"""
foundClub = [c for c in clubs if c['name'] == club][0]
foundCompetition = [c for c in competitions if c['name'] == competition][0]
if foundClub and foundCompetition:
return render_template('booking.html',club=foundClub,competition=foundCompetition)
else:
flash("Something went wrong-please try again")
return render_template('welcome.html', club=club, competitions=competitions)
return render_template('welcome.html', club=club, competitions=competitions, current_date=current_date)


# ============================================================ ROUTE FOR PURCHASE PLACES


@app.route('/purchasePlaces',methods=['POST'])
def purchasePlaces():
@app.errorhandler(400)
def purchase_places():
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'])

if competition['date'] < current_date:
flash("Sorry, this competition is over !")
return render_template('welcome.html', club=club, competitions=competitions, current_date=current_date), 400
competition['numberOfPlaces'] = int(competition['numberOfPlaces'])-placesRequired
flash('Great-booking complete!')
return render_template('welcome.html', club=club, competitions=competitions)
return render_template('welcome.html', club=club, competitions=competitions, current_date=current_date)


# TODO: Add route for points display
Expand Down
4 changes: 3 additions & 1 deletion templates/welcome.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ <h3>Competitions:</h3>
{{comp['name']}}<br />
Date: {{comp['date']}}</br>
Number of Places: {{comp['numberOfPlaces']}}
{%if comp['numberOfPlaces']|int >0%}
{%if comp['date'] < current_date %}
<p>This competition is over</p>
{%elif comp['numberOfPlaces']|int >0%}
<a href="{{ url_for('book',competition=comp['name'],club=club['name']) }}">Book Places</a>
{%endif%}
</li>
Expand Down
34 changes: 30 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ def client():
yield client


# @pytest.fixture
# def current_date():
# now = datetime.datetime.now()
# return now.strftime("%Y-%m-%d, %H:%M:%S")
@pytest.fixture
def current_date():
now = datetime.datetime.now()
return now.strftime("%Y-%m-%d, %H:%M:%S")


@pytest.fixture
Expand Down Expand Up @@ -58,3 +58,29 @@ def unknown_club():
"points": "16"
}
]


@pytest.fixture
def competitions_fixture():
return [
{
"name": "Aka League",
"date": "2023-02-05 10:00:00",
"numberOfPlaces": "20"
},
{
"name": "Pink Race",
"date": "2021-05-12 09:00:00",
"numberOfPlaces": "4"
},
{
"name": "Champion Road",
"date": "2022-10-13 13:00:00",
"numberOfPlaces": "13"
},
{
"name": "Ice Race",
"date": "2022-12-01 14:00:00",
"numberOfPlaces": "10"
}
]
32 changes: 32 additions & 0 deletions tests/units/test_past_competition.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import server


def test_purchase_futur_competition(client, mocker, clubs_fixture, competitions_fixture):
"""
GIVEN a connected secretary's club wants to book places in a futur competition.
date of this competition = "2023-02-05 10:00:00".
WHEN this secretary types: 5 places
THEN places are pruchased with status code:200.
"""
mocker.patch.object(server, 'clubs', clubs_fixture)
mocker.patch.object(server, 'competitions', competitions_fixture)
club = clubs_fixture[0]['name']
competition = competitions_fixture[0]['name']
response = client.post('/purchasePlaces', data={'competition': competition, 'club': club, 'places': 5})
assert response.status_code == 200


def test_purchase_past_competition(client, mocker, clubs_fixture, competitions_fixture):
"""
GIVEN a connected secretary's club wants to book places in a past competition.
date of this competition = "2021-05-12 09:00:00".
WHEN this secretary types: 5 places
THEN an error message displays, with status code:400 BAD REQUEST.
"""
mocker.patch.object(server, 'clubs', clubs_fixture)
mocker.patch.object(server, 'competitions', competitions_fixture)
club = clubs_fixture[0]['name']
competition = competitions_fixture[1]['name']
response = client.post('/purchasePlaces', data={'competition': competition, 'club': club, 'places': 5})
assert response.status_code == 400
assert b'Sorry, this competition is over !' in response.data

0 comments on commit 97ea339

Please sign in to comment.