From 6dfc3f71a6fcece5305cf9139403ee117d0338f1 Mon Sep 17 00:00:00 2001 From: zionhigt Date: Fri, 1 Apr 2022 13:00:48 +0200 Subject: [PATCH] refactoring fixtures --- server.py | 16 ++--- templates/index.html | 12 ++-- tests/unit_test/conftest.py | 65 +++++++++---------- tests/unit_test/mocks_files/fixture_load.json | 4 +- tests/unit_test/test_server.py | 16 +++-- 5 files changed, 52 insertions(+), 61 deletions(-) diff --git a/server.py b/server.py index 55888d639..35216b17b 100644 --- a/server.py +++ b/server.py @@ -1,5 +1,5 @@ import json -from flask import Flask,render_template,request,redirect,flash,url_for +from flask import Flask, render_template, request, redirect, flash, url_for from datetime import datetime @@ -8,13 +8,11 @@ def loadClubs(path='clubs.json'): listOfClubs = json.load(c)['clubs'] return listOfClubs - def loadCompetitions(path='competitions.json'): with open(path) as comps: listOfCompetitions = json.load(comps)['competitions'] return listOfCompetitions - app = Flask(__name__) app.secret_key = 'something_special' @@ -26,26 +24,24 @@ def loadCompetitions(path='competitions.json'): def index(): return render_template('index.html') -@app.route('/showSummary',methods=['POST']) +@app.route('/showSummary', methods=['POST']) def showSummary(): club = [club for club in clubs if club['email'] == request.form['email']] other_clubs = [club for club in clubs if club['email'] != request.form['email']] if len(club): - return render_template('welcome.html',club=club[0],competitions=competitions, clubs=other_clubs), 200 + return render_template('welcome.html', club=club[0], competitions=competitions, clubs=other_clubs), 200 return render_template('index.html'), 401 - @app.route('/book//') -def book(competition,club): +def book(competition, club): 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) + 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) - @app.route('/purchasePlaces',methods=['POST']) def purchasePlaces(): competition = [c for c in competitions if c['name'] == request.form['competition']][0] @@ -70,10 +66,8 @@ def purchasePlaces(): flash('Great-booking complete!') return render_template('welcome.html', club=club, competitions=competitions), 200 - # TODO: Add route for points display - @app.route('/logout') def logout(): return redirect(url_for('index')) \ No newline at end of file diff --git a/templates/index.html b/templates/index.html index 926526b7d..a99c1943d 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,16 +1,12 @@ + + - GUDLFT Registration + Document -

Welcome to the GUDLFT Registration Portal!

- Please enter your secretary email to continue: -
- - - -
+ ul>l*5 \ No newline at end of file diff --git a/tests/unit_test/conftest.py b/tests/unit_test/conftest.py index 48b338df6..f988e327f 100644 --- a/tests/unit_test/conftest.py +++ b/tests/unit_test/conftest.py @@ -3,48 +3,43 @@ import server -clubs_obj = [ - { - "name":"TEST_CLUB_1", - "email":"TEST_CLUB_EMAIL_1", - "points":"10" - }, - { - "name":"TEST_CLUB_2", - "email":"TEST_CLUB_EMAIL_2", - "points":"10" - } - ] +def mocks(): + clubs_obj = [ + { + "name":"TEST_CLUB_1", + "email":"TEST_CLUB_EMAIL_1", + "points":"10" + }, + { + "name":"TEST_CLUB_2", + "email":"TEST_CLUB_EMAIL_2", + "points":"10" + } + ] -competitions_obj = [ - { - "name": "TEST_COMPETITION_1", - "date": "2020-03-27 10:00:00", - "numberOfPlaces": "25" - }, - { - "name": "TEST_COMPETITION_2", - "date": "2025-03-27 10:00:00", - "numberOfPlaces": "25" - } - ] - - -@pytest.fixture(scope="class") -def clubs(request): - request.cls.clubs = clubs_obj - yield - -@pytest.fixture(scope="class") -def competitions(request): - request.cls.competitions = competitions_obj - yield + competitions_obj = [ + { + "name": "TEST_COMPETITION_1", + "date": "2020-03-27 10:00:00", + "numberOfPlaces": "25" + }, + { + "name": "TEST_COMPETITION_2", + "date": "2025-03-27 10:00:00", + "numberOfPlaces": "25" + } + ] + return clubs_obj, competitions_obj @pytest.fixture(scope="class") def client(request): + clubs_obj, competitions_obj = mocks() + print(mocks()) with patch("server.clubs", clubs_obj): with patch("server.competitions", competitions_obj): with server.app.test_client() as client: + request.cls.clubs = clubs_obj + request.cls.competitions = competitions_obj request.cls.client = client request.cls.cost = server.cost yield \ No newline at end of file diff --git a/tests/unit_test/mocks_files/fixture_load.json b/tests/unit_test/mocks_files/fixture_load.json index 3ad675068..8f6c1a290 100644 --- a/tests/unit_test/mocks_files/fixture_load.json +++ b/tests/unit_test/mocks_files/fixture_load.json @@ -3,7 +3,7 @@ { "name":"TEST_CLUB_1", "email":"TEST_CLUB_EMAIL_1", - "points":"7" + "points":"10" }, { "name":"TEST_CLUB_2", @@ -20,7 +20,7 @@ { "name": "TEST_COMPETITION_2", "date": "2025-03-27 10:00:00", - "numberOfPlaces": "24" + "numberOfPlaces": "25" } ] } \ No newline at end of file diff --git a/tests/unit_test/test_server.py b/tests/unit_test/test_server.py index b5ff2258f..466c29c22 100644 --- a/tests/unit_test/test_server.py +++ b/tests/unit_test/test_server.py @@ -1,17 +1,23 @@ from unittest import TestCase -from pytest import mark +from pytest import mark, fixture from server import loadClubs, loadCompetitions +import json +with open("./tests/unit_test/mocks_files/fixture_load.json", 'r') as FILE_CURSOR: + FILE = json.load(FILE_CURSOR) -@mark.usefixtures('client', 'clubs', 'competitions') +@mark.usefixtures('client') class TestServer(TestCase): + def test_loadClubs(self): - self.assertListEqual(loadClubs("./tests/unit_test/mocks_files/fixture_load.json"), self.clubs) + clubs = FILE.get("clubs") + self.assertListEqual(loadClubs("./tests/unit_test/mocks_files/fixture_load.json"), clubs) def test_loadCompetitions(self): - self.assertListEqual(loadCompetitions("./tests/unit_test/mocks_files/fixture_load.json"), self.competitions) + competitions = FILE.get("competitions") + self.assertListEqual(loadCompetitions("./tests/unit_test/mocks_files/fixture_load.json"), competitions) def test_sould_login_in_out(self): response = self.client.get("/logout") @@ -46,7 +52,7 @@ def test_sould_not_purshase_more_than_12_places(self): }) self.assertIn("You cannot required more than 12 places!", response.data.decode()) self.assertEqual(response.status_code, 403) - + def test_sould_not_purshase_booking_if_date_passed(self): response = self.client.post('/purchasePlaces', data={ "club": "TEST_CLUB_1",