diff --git a/server.py b/server.py index 226bfa999..25b909a61 100644 --- a/server.py +++ b/server.py @@ -8,6 +8,12 @@ def loadClubs(): return listOfClubs +def saveClub(clubs): + + with open('clubs.json', 'w') as c: + json.dump({'clubs': clubs}, c) + + def loadCompetitions(): with open('competitions.json') as comps: listOfCompetitions = json.load(comps)['competitions'] @@ -65,8 +71,12 @@ def purchasePlaces(): return render_template('welcome.html', club=club, competitions=competitions) + club['points'] = str(int(club['points']) - placesRequired) + saveClub(clubs) + competition['numberOfPlaces'] = int( competition['numberOfPlaces']) - placesRequired + flash('Great-booking complete!') return render_template('welcome.html', club=club, diff --git a/tests/conftest.py b/tests/conftest.py index ca8562ecd..802b1df5c 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,6 +1,8 @@ +import copy import pytest +import json -from server import app, loadClubs, loadCompetitions +from server import app @pytest.fixture @@ -8,3 +10,15 @@ def client(): app.config['TESTING'] = True with app.test_client() as client: yield client + + +@pytest.fixture +def test_clubs(): + with open('clubs.json') as c: + return json.load(c)['clubs'] + + +@pytest.fixture +def test_competitions(): + with open('competitions.json') as comps: + return json.load(comps)['competitions'] diff --git a/tests/test_server.py b/tests/test_server.py index c16576a54..07e3860c7 100644 --- a/tests/test_server.py +++ b/tests/test_server.py @@ -1,8 +1,8 @@ +import server from server import loadCompetitions, loadClubs def test_purchase_places(client): - test_club = loadClubs()[0] test_competition = loadCompetitions()[0] places_to_purchase = 8 @@ -18,7 +18,6 @@ def test_purchase_places(client): def test_max_purchase_places(client): - test_club = loadClubs()[0] test_competition = loadCompetitions()[0] places_to_purchase = 28 @@ -34,7 +33,6 @@ def test_max_purchase_places(client): def test_has_sufficient_points(client): - test_club = loadClubs()[1] test_competition = loadCompetitions()[0] places_to_purchase = 9 @@ -47,3 +45,24 @@ def test_has_sufficient_points(client): assert response.status_code == 200 assert b'Insufficiant points.' in response.data + + +def test_purchase_places(client, test_clubs, test_competitions, mocker): + + mocker.patch('server.loadClubs', return_value=test_clubs) + mocker.patch('server.loadCompetitions', return_value=test_competitions) + mock_save_club = mocker.patch('server.saveClub') + + mocker.patch.object(server, 'clubs', test_clubs) + mocker.patch.object(server, 'competitions', test_competitions) + + places_to_purchase = 9 + + response = client.post('/purchasePlaces', data={ + 'club': test_clubs[0]['name'], + 'competition': test_competitions[0]['name'], + 'places': str(places_to_purchase) + }) + + assert int(test_clubs[0]['points']) == 4 + assert b'Great-booking complete!' in response.data