|
| 1 | +from unittest.mock import patch |
| 2 | +from datetime import datetime |
| 3 | + |
| 4 | +from server import validate_competition_date |
| 5 | +from .utils import mock_competitions_list, mock_clubs_list |
| 6 | + |
| 7 | +mock_competition_past = { |
| 8 | + "name": "Competition 2", |
| 9 | + "date": "2020-03-27 10:00:00", |
| 10 | + "numberOfPlaces": "15", |
| 11 | +} |
| 12 | +mock_future_competition = { |
| 13 | + "name": "Future Competition", |
| 14 | + "date": "2024-03-27 10:00:00", |
| 15 | +} |
| 16 | + |
| 17 | + |
| 18 | +# Test for a valid future competition date |
| 19 | +@patch("server.datetime") |
| 20 | +def test_validate_competition_date_future(mock_datetime): |
| 21 | + # Mock datetime.now() to return a date before the competition date |
| 22 | + mock_datetime.now.return_value = datetime(2023, 3, 27) |
| 23 | + mock_datetime.strptime.side_effect = datetime.strptime |
| 24 | + |
| 25 | + # Call the function with a future competition |
| 26 | + result = validate_competition_date(mock_future_competition) |
| 27 | + |
| 28 | + # Assert that no error message is returned |
| 29 | + assert result is None |
| 30 | + |
| 31 | + |
| 32 | +# Test for a past competition date |
| 33 | +@patch("server.datetime") |
| 34 | +def test_validate_competition_date_past(mock_datetime): |
| 35 | + # Mock datetime.now() to return a date after the competition date |
| 36 | + mock_datetime.now.return_value = datetime(2023, 3, 27) |
| 37 | + mock_datetime.strptime.side_effect = datetime.strptime |
| 38 | + |
| 39 | + # Call the function with a past competition |
| 40 | + result = validate_competition_date(mock_competition_past) |
| 41 | + |
| 42 | + # Assert that the correct error message is returned |
| 43 | + assert ( |
| 44 | + result == "This competition is already over. You cannot book a place." |
| 45 | + ) |
| 46 | + |
| 47 | + |
| 48 | +# Test for valid club and valid competition (future date) |
| 49 | +@patch("server.render_template") |
| 50 | +@patch("server.validate_competition_date") |
| 51 | +@patch("server.get_club_from_name") |
| 52 | +@patch("server.get_competition_from_name") |
| 53 | +def test_book_valid_competition( |
| 54 | + mock_get_competition, |
| 55 | + mock_get_club, |
| 56 | + mock_validate_date, |
| 57 | + mock_render_template, |
| 58 | + client, |
| 59 | +): |
| 60 | + # Mock valid club and competition |
| 61 | + mock_get_competition.return_value = mock_competitions_list[0] |
| 62 | + mock_get_club.return_value = mock_clubs_list[0] |
| 63 | + mock_validate_date.return_value = None # No validation error |
| 64 | + |
| 65 | + # Simulate GET request to the route |
| 66 | + response = client.get("/book/Competition%201/Club%201") |
| 67 | + |
| 68 | + # Assert that the necessary functions are called with the correct parameters |
| 69 | + mock_get_competition.assert_called_once_with("Competition 1") |
| 70 | + mock_get_club.assert_called_once_with("Club 1") |
| 71 | + mock_validate_date.assert_called_once_with(mock_competitions_list[0]) |
| 72 | + mock_render_template.assert_called_once_with( |
| 73 | + "booking.html", |
| 74 | + club=mock_clubs_list[0], |
| 75 | + competition=mock_competitions_list[0], |
| 76 | + ) |
| 77 | + |
| 78 | + # Check the response status code |
| 79 | + assert response.status_code == 200 |
| 80 | + |
| 81 | + |
| 82 | +# Test for valid club and past competition (competition already over) |
| 83 | +@patch("server.competitions", mock_competitions_list) |
| 84 | +@patch("server.render_template") |
| 85 | +@patch("server.flash") |
| 86 | +@patch("server.get_club_from_name") |
| 87 | +@patch("server.get_competition_from_name") |
| 88 | +@patch("server.validate_competition_date") |
| 89 | +def test_book_past_competition( |
| 90 | + mock_validate_date, |
| 91 | + mock_get_competition, |
| 92 | + mock_get_club, |
| 93 | + mock_flash, |
| 94 | + mock_render_template, |
| 95 | + client, |
| 96 | +): |
| 97 | + # Mock valid club but past competition |
| 98 | + mock_get_competition.return_value = mock_competition_past |
| 99 | + mock_get_club.return_value = mock_clubs_list[0] |
| 100 | + mock_validate_date.return_value = ( |
| 101 | + "This competition is already over. You cannot book a place." |
| 102 | + ) |
| 103 | + |
| 104 | + # Simulate GET request to the route |
| 105 | + response = client.get("/book/Competition%202/Club%201") |
| 106 | + |
| 107 | + # Assert that the necessary functions are called with the correct parameters |
| 108 | + mock_get_competition.assert_called_once_with("Competition 2") |
| 109 | + mock_get_club.assert_called_once_with("Club 1") |
| 110 | + mock_validate_date.assert_called_once_with(mock_competition_past) |
| 111 | + mock_flash.assert_called_once_with( |
| 112 | + "This competition is already over. You cannot book a place." |
| 113 | + ) |
| 114 | + mock_render_template.assert_called_once_with( |
| 115 | + "welcome.html", |
| 116 | + club=mock_clubs_list[0], |
| 117 | + competitions=mock_competitions_list, |
| 118 | + ) |
| 119 | + |
| 120 | + # Check the response status code |
| 121 | + assert response.status_code == 200 |
| 122 | + |
| 123 | + |
| 124 | +# Test for missing club or competition |
| 125 | +@patch("server.competitions", mock_competitions_list) |
| 126 | +@patch("server.render_template") |
| 127 | +@patch("server.flash") |
| 128 | +@patch("server.get_club_from_name") |
| 129 | +@patch("server.get_competition_from_name") |
| 130 | +def test_book_missing_club_or_competition( |
| 131 | + mock_get_competition, |
| 132 | + mock_get_club, |
| 133 | + mock_flash, |
| 134 | + mock_render_template, |
| 135 | + client, |
| 136 | +): |
| 137 | + # Mock a case where the competition is missing |
| 138 | + mock_get_competition.return_value = None # Competition not found |
| 139 | + mock_get_club.return_value = mock_clubs_list[0] # Club found |
| 140 | + |
| 141 | + # Simulate GET request to the route |
| 142 | + response = client.get("/book/Competition%201/Club%201") |
| 143 | + |
| 144 | + # Assert that the necessary functions are called with the correct parameters |
| 145 | + mock_get_competition.assert_called_once_with("Competition 1") |
| 146 | + mock_get_club.assert_called_once_with("Club 1") |
| 147 | + mock_flash.assert_called_once_with("Something went wrong-please try again") |
| 148 | + mock_render_template.assert_called_once_with( |
| 149 | + "welcome.html", |
| 150 | + club=mock_clubs_list[0]["name"], |
| 151 | + competitions=mock_competitions_list, |
| 152 | + ) |
| 153 | + |
| 154 | + # Check the response status code |
| 155 | + assert response.status_code == 200 |
| 156 | + |
| 157 | + |
| 158 | +# Test for invalid club |
| 159 | +@patch("server.competitions", mock_competitions_list) |
| 160 | +@patch("server.render_template") |
| 161 | +@patch("server.flash") |
| 162 | +@patch("server.get_club_from_name") |
| 163 | +@patch("server.get_competition_from_name") |
| 164 | +def test_book_invalid_club( |
| 165 | + mock_get_competition, |
| 166 | + mock_get_club, |
| 167 | + mock_flash, |
| 168 | + mock_render_template, |
| 169 | + client, |
| 170 | +): |
| 171 | + # Mock a case where the club is invalid |
| 172 | + mock_get_competition.return_value = mock_competitions_list[ |
| 173 | + 0 |
| 174 | + ] # Competition found |
| 175 | + mock_get_club.return_value = None # Club not found |
| 176 | + |
| 177 | + # Simulate GET request to the route |
| 178 | + response = client.get("/book/Competition%201/Invalid%20Club") |
| 179 | + |
| 180 | + # Assert that the necessary functions are called with the correct parameters |
| 181 | + mock_get_competition.assert_called_once_with("Competition 1") |
| 182 | + mock_get_club.assert_called_once_with("Invalid Club") |
| 183 | + mock_flash.assert_called_once_with("Something went wrong-please try again") |
| 184 | + mock_render_template.assert_called_once_with( |
| 185 | + "welcome.html", club="Invalid Club", competitions=mock_competitions_list |
| 186 | + ) |
| 187 | + |
| 188 | + # Check the response status code |
| 189 | + assert response.status_code == 200 |
0 commit comments