Skip to content
This repository has been archived by the owner on Oct 29, 2023. It is now read-only.

Commit

Permalink
fixed (OpenClassrooms-Student-Center#2), Can't make a competition go …
Browse files Browse the repository at this point in the history
…unto a negative number
  • Loading branch information
Madscientiste committed Feb 17, 2022
1 parent 98ab9ba commit 8b7666e
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 17 deletions.
8 changes: 7 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"version": "0.2.0",
"configurations": [
{
"name": "Python: Flask",
"name": "Debug Flask",
"type": "python",
"request": "launch",
"module": "flask",
Expand All @@ -15,6 +15,12 @@
},
"args": ["run", "--no-debugger", "--no-reload"],
"jinja": true
},
{
"name": "Debug Tests",
"type": "python",
"request": "launch",
"module": "pytest"
}
]
}
23 changes: 22 additions & 1 deletion app/data/clubs.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,27 @@
"email": "admin@irontemple.com",
"points": "4"
},
{ "name": "She Lifts", "email": "kate@shelifts.co.uk", "points": "12" }
{
"name": "She Lifts",
"email": "kate@shelifts.co.uk",
"points": "12"
}
],
"test": [
{
"name": "Simply Lift",
"email": "john@simplylift.co",
"points": "2"
},
{
"name": "Iron Temple",
"email": "admin@irontemple.com",
"points": "4"
},
{
"name": "She Lifts",
"email": "kate@shelifts.co.uk",
"points": "16"
}
]
}
22 changes: 22 additions & 0 deletions app/data/competitions.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,27 @@
"date": "2025-08-22 13:30:00",
"seats_available": "7"
}
],
"test": [
{
"name": "Spring Festival",
"date": "2020-03-27 10:00:00",
"seats_available": "25"
},
{
"name": "Fall Classic",
"date": "2020-10-22 13:30:00",
"seats_available": "13"
},
{
"name": "Futuristic Friday",
"date": "2025-10-22 13:30:00",
"seats_available": "36"
},
{
"name": "AYAYA Festival",
"date": "2025-08-22 13:30:00",
"seats_available": "7"
}
]
}
7 changes: 5 additions & 2 deletions app/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import pytest

from app.data import clubs, competitions
from app.utilities import JsonLoader
from server import create_app

clubs = JsonLoader("clubs", testing=True)
competitions = JsonLoader("competitions", testing=True)


@pytest.fixture
def client():
Expand All @@ -13,4 +16,4 @@ def client():

@pytest.fixture
def data():
return {"clubs": clubs, "competitions": competitions}
return {"clubs" : clubs, "competitions" : competitions}
24 changes: 24 additions & 0 deletions app/tests/test_server.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import html


def test_index(client, data):
res = client.get("/")
assert res.status_code == 200
Expand All @@ -12,3 +15,24 @@ def test_login(client, data):

res = client.post("/summary", data={"email": "ayaya@gmail.com"}, follow_redirects=True)
assert res.status_code == 401


def test_booking(client, data):
# shouldn't be able to book more than 12 seats & from whats available
# Shouldn't be able to book if they don't have enough points (1 point = 1 competition)
# Shouldn't book a competition if it's date has passed
club = data["clubs"].get_by("name", "Simply Lift", first=True)
competition = data["competitions"].get_by("name", "Spring Festival", first=True)

res = client.post(
"/confirm_booking",
data={
"competition": competition["name"],
"club": club["name"],
"seats": 999,
},
follow_redirects=True,
)

assert res.status_code == 200, "didn't return 200"
assert b"Sorry, we don't have enough seats available for that competition." in res.data
19 changes: 11 additions & 8 deletions app/utilities/json_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,36 @@
class JsonLoader:
"""Load a json from /data/, and handles basic operations"""

def __init__(self, target):
def __init__(self, target, testing=False):
self.target = target
self.testing = testing
self.values = self._load_json()

def __iter__(self):
return iter(self.values)

def _load_json(self):
with open(str(DATA_FOLDER / self.target) + ".json") as file:
self.values = json.load(file)[self.target]
load_name = "test" if self.testing else self.target
self.values = json.load(file)[load_name]
return self.values

def _update_json(self):
with open(str(DATA_FOLDER / self.target) + ".json", "w") as file:
json.dump(self.values, file)

def get_by(self, key, value, first=False):
"""Find an item by a key and value. Returns if nothing is found
Arguments:
key {str} -- The key to search by
value {str} -- The value to search for
value {str} -- The value to search for, if a function is passed, it will be used to filter the values
Keyword Arguments:
first {bool} -- If True, return the first club found (default: {False})
"""
found = filter(lambda x: x[key] == value, self.values)

if callable(value):
found = iter([v for v in self.values if value(v[key])])
else:
found = filter(lambda x: x[key] == value, self.values)

return next(found, None) if first else list(found)

def get_random(self, key):
Expand Down
17 changes: 12 additions & 5 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,18 @@ def summary():

@app.route("/confirm_booking", methods=["POST"])
def confirm_booking():
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]
seatsRequired = int(request.form["seats"])
competition["seats_available"] = int(competition["seats_available"]) - seatsRequired
flash("Great-booking complete!")
competition = competitions.get_by("name", request.form["competition"], first=True)
club = clubs.get_by("name", request.form["club"], first=True)

requested_seats = int(request.form["seats"])
seats_available = int(competition["seats_available"])

if requested_seats > seats_available:
flash("Sorry, we don't have enough seats available for that competition.")
else:
competition["seats_available"] = seats_available - requested_seats
flash("Great-booking complete!")

return render_template("welcome.html", club=club, competitions=competitions)

@app.route("/book/<competition>/<club>")
Expand Down

0 comments on commit 8b7666e

Please sign in to comment.