Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
VisualDev-FR committed Apr 7, 2023
2 parents 86c6886 + 0d0719b commit 9f0c9ef
Show file tree
Hide file tree
Showing 9 changed files with 189 additions and 17 deletions.
25 changes: 21 additions & 4 deletions app/server.py
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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')

Expand All @@ -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'])
Expand All @@ -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
Expand Down
14 changes: 11 additions & 3 deletions app/templates/welcome.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,25 @@ <h2>Welcome, {{club['email']}} </h2><a href="{{url_for('logout')}}">Logout</a>
Points available: {{club['points']}}
<h3>Competitions:</h3>
<ul>
{% for comp in competitions%}
{% for comp in future_competitions%}
<li>
{{comp['name']}}<br />
Date: {{comp['date']}}</br>
{{comp['name']}}<br>
Date: {{comp['date']}}<br>
Number of Places: {{comp['numberOfPlaces']}}
{%if comp['numberOfPlaces']|int >0%}
<a href="{{ url_for('book',competition=comp['name'],club=club['name']) }}">Book Places</a>
{%endif%}
</li>
<hr />
{% endfor %}

{% for comp in past_competitions%}
<li>
{{comp['name']}}<br>
Date: {{comp['date']}}<br>
</li>
<hr />
{% endfor %}
</ul>
{%endwith%}

Expand Down
15 changes: 15 additions & 0 deletions app/utils.py
Original file line number Diff line number Diff line change
@@ -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()
]
12 changes: 11 additions & 1 deletion 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": "Fall Festival",
"date": "2099-03-27 10:00:00",
"numberOfPlaces": "25"
},
{
"name": "Winter Classic",
"date": "2099-10-22 13:30:00",
"numberOfPlaces": "13"
}
]
}
55 changes: 53 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
Expand Down
4 changes: 2 additions & 2 deletions tests/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

@pytest.fixture
def dummy_club():
return conftest.club_mock(data={
return conftest.json_clubs_mock(data={
"clubs": [
{
"name": "dummy_name",
Expand All @@ -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",
Expand Down
10 changes: 5 additions & 5 deletions tests/test_purchase.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@


@pytest.fixture
def clubs_mock():
def purchase_clubs_mock():
return patch('app.server.clubs', [
{
"name": "club_1",
Expand All @@ -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",
Expand Down
59 changes: 59 additions & 0 deletions tests/test_summary.py
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 9f0c9ef

Please sign in to comment.