diff --git a/app/server.py b/app/server.py index 1bb5fb229..65c31d551 100644 --- a/app/server.py +++ b/app/server.py @@ -1,5 +1,7 @@ -import json from flask import Flask, render_template, request, redirect, flash, url_for +from app import utils +import json + CLUB_DB_PATH = "clubs.json" COMPETITION_DB_PATH = "competitions.json" @@ -34,7 +36,12 @@ def showSummary(): for club in clubs: if club['email'] == request.form['email']: - return render_template('welcome.html', club=club, competitions=competitions) + return render_template( + 'welcome.html', + club=club, + past_competitions=utils.get_past_competitions(competitions), + future_competitions=utils.get_future_competitions(competitions) + ) return render_template('index.html') @@ -47,7 +54,12 @@ def book(competition, club): 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, + past_competitions=utils.get_past_competitions(competitions), + future_competitions=utils.get_future_competitions(competitions) + ) @app.route('/purchasePlaces', methods=['POST']) @@ -62,7 +74,12 @@ def purchasePlaces(): club['points'] = int(club['points']) - placesRequired flash('Great-booking complete!') - return render_template('welcome.html', club=club, competitions=competitions) + return render_template( + 'welcome.html', + club=club, + past_competitions=utils.get_past_competitions(competitions), + future_competitions=utils.get_future_competitions(competitions) + ) # TODO: Add route for points display diff --git a/app/templates/welcome.html b/app/templates/welcome.html index ff6b261a2..62e08311a 100644 --- a/app/templates/welcome.html +++ b/app/templates/welcome.html @@ -18,10 +18,10 @@

Welcome, {{club['email']}}

Logout Points available: {{club['points']}}

Competitions:

{%endwith%} diff --git a/app/utils.py b/app/utils.py new file mode 100644 index 000000000..432997c7e --- /dev/null +++ b/app/utils.py @@ -0,0 +1,15 @@ +from datetime import datetime + + +def get_past_competitions(competitions_list: list): + return [ + comp for comp in competitions_list if + datetime.fromisoformat(comp['date']) < datetime.now() + ] + + +def get_future_competitions(competitions_list: list): + return [ + comp for comp in competitions_list if + datetime.fromisoformat(comp['date']) > datetime.now() + ] diff --git a/competitions.json b/competitions.json index 039fc61bd..f769bf779 100644 --- a/competitions.json +++ b/competitions.json @@ -9,6 +9,16 @@ "name": "Fall Classic", "date": "2020-10-22 13:30:00", "numberOfPlaces": "13" - } + }, + { + "name": "Fall Festival", + "date": "2099-03-27 10:00:00", + "numberOfPlaces": "25" + }, + { + "name": "Winter Classic", + "date": "2099-10-22 13:30:00", + "numberOfPlaces": "13" + } ] } \ No newline at end of file diff --git a/tests/conftest.py b/tests/conftest.py index 578e3a8eb..663002cdd 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -7,8 +7,59 @@ CLUB_DB_TEST_PATH = os.path.join(os.path.dirname(__file__), "data", "clubs.json") COMPETITION_BD_TEST_PATH = os.path.join(os.path.dirname(__file__), "data", "competitions.json") +TEST_CLUBS = [ + { + "name": "club_1", + "email": "club1@domain.co", + "points": "5" + }, + { + "name": "club_2", + "email": "club2@domain.co", + "points": "15" + }, + { + "name": "club_3", + "email": "club3@domain.co", + "points": "20" + } +] -def club_mock(data: dict): +TEST_COMPETITIONS = [ + { + "name": "competition1", + "date": "2020-03-27 10:00:00", + "numberOfPlaces": "5" + }, + { + "name": "competition2", + "date": "2099-03-27 10:00:00", + "numberOfPlaces": "0" + }, + { + "name": "competition2", + "date": "2099-03-27 10:00:00", + "numberOfPlaces": "10" + }, + { + "name": "competition3", + "date": "2099-03-27 10:00:00", + "numberOfPlaces": "15" + }, +] + + +@pytest.fixture +def variable_clubs_mock(): + return patch('app.server.clubs', TEST_CLUBS) + + +@pytest.fixture +def variable_competitions_mock(): + return patch('app.server.competitions', TEST_COMPETITIONS) + + +def json_clubs_mock(data: dict): # overwrite club datas db_test = open(CLUB_DB_TEST_PATH, "w") db_test.write(json.dumps(data, indent=4)) # type: ignore @@ -18,7 +69,7 @@ def club_mock(data: dict): return patch('app.server.CLUB_DB_PATH', CLUB_DB_TEST_PATH) -def competition_mock(data: dict): +def json_competition_mock(data: dict): # overwrite club datas db_test = open(COMPETITION_BD_TEST_PATH, "w") db_test.write(json.dumps(data, indent=4)) diff --git a/tests/test_database.py b/tests/test_database.py index c37ea0db0..956ea71ab 100644 --- a/tests/test_database.py +++ b/tests/test_database.py @@ -5,7 +5,7 @@ @pytest.fixture def dummy_club(): - return conftest.club_mock(data={ + return conftest.json_clubs_mock(data={ "clubs": [ { "name": "dummy_name", @@ -30,7 +30,7 @@ def test_load_clubs(dummy_club): @pytest.fixture def dummy_competition(): - return conftest.competition_mock(data={ + return conftest.json_competition_mock(data={ "competitions": [ { "name": "dummy_competition", diff --git a/tests/test_purchase.py b/tests/test_purchase.py index b10b86693..585b5f3f2 100644 --- a/tests/test_purchase.py +++ b/tests/test_purchase.py @@ -4,7 +4,7 @@ @pytest.fixture -def clubs_mock(): +def purchase_clubs_mock(): return patch('app.server.clubs', [ { "name": "club_1", @@ -15,19 +15,19 @@ def clubs_mock(): @pytest.fixture -def competitions_mock(): +def purchase_competitions_mock(): return patch('app.server.competitions', [ { "name": "competition1", - "date": "2020-03-27 10:00:00", + "date": "2099-03-27 10:00:00", "numberOfPlaces": "25" }, ]) -def test_purchase_decrements(client: FlaskClient, competitions_mock: Mock, clubs_mock: Mock): +def test_purchase_decrements(client: FlaskClient, purchase_competitions_mock: Mock, purchase_clubs_mock: Mock): - with competitions_mock, clubs_mock: + with purchase_competitions_mock, purchase_clubs_mock: response = client.post( "/purchasePlaces", diff --git a/tests/test_summary.py b/tests/test_summary.py new file mode 100644 index 000000000..4a7ad1096 --- /dev/null +++ b/tests/test_summary.py @@ -0,0 +1,59 @@ +from flask.testing import FlaskClient +from unittest.mock import patch +import pytest + + +@pytest.fixture +def past_competitions_only(): + return patch('app.server.competitions', [ + { + "name": "past_competition", + "date": "2020-03-27 10:00:00", + "numberOfPlaces": "5" + }, + ]) + + +@pytest.fixture +def future_competitions_only(): + return patch('app.server.competitions', [ + { + "name": "future_competition", + "date": "2099-03-27 10:00:00", + "numberOfPlaces": "5" + }, + ]) + + +def test_past_competitions_are_not_bookable(client: FlaskClient, variable_clubs_mock, past_competitions_only): + + with variable_clubs_mock, past_competitions_only: + + response = client.post( + "/showSummary", + data={ + "email": "club1@domain.co" + } + ) + + assert response.status_code == 200 + assert b"past_competition" in response.data + assert b"2020-03-27 10:00:00" in response.data + assert b"Number of Places: 5" not in response.data + + +def test_future_competitions_are_bookable(client: FlaskClient, variable_clubs_mock, future_competitions_only): + + with variable_clubs_mock, future_competitions_only: + + response = client.post( + "/showSummary", + data={ + "email": "club1@domain.co" + } + ) + + assert response.status_code == 200 + assert b"future_competition" in response.data + assert b"2099-03-27 10:00:00" in response.data + assert b"Number of Places: 5" in response.data diff --git a/tests/test_utils.py b/tests/test_utils.py new file mode 100644 index 000000000..4968afcbf --- /dev/null +++ b/tests/test_utils.py @@ -0,0 +1,12 @@ +from app import utils +from tests.conftest import TEST_COMPETITIONS + + +def test_get_past_competitions(): + past_competitions = utils.get_past_competitions(TEST_COMPETITIONS) + assert len(past_competitions) == 1 + + +def test_get_future_competitions(): + future_competitions = utils.get_future_competitions(TEST_COMPETITIONS) + assert len(future_competitions) == 3