@@ -84,3 +84,146 @@ def test_take_places_invalid():
84
84
"invalid" , mock_clubs_list [0 ], mock_competitions_list [0 ]
85
85
)
86
86
assert result is False
87
+
88
+
89
+ # Unit test for valid booking
90
+ @patch ("server.competitions" , mock_competitions_list )
91
+ @patch ("server.render_template" )
92
+ @patch ("server.take_places" )
93
+ @patch ("server.check_places" )
94
+ @patch ("server.get_club_from_name" )
95
+ @patch ("server.get_competition_from_name" )
96
+ @patch ("server.flash" )
97
+ def test_purchase_places_valid (
98
+ mock_flash ,
99
+ mock_get_competition ,
100
+ mock_get_club ,
101
+ mock_check_places ,
102
+ mock_take_places ,
103
+ mock_render_template ,
104
+ client ,
105
+ ):
106
+ # Mock the functions to return valid data
107
+ mock_get_competition .return_value = mock_competitions_list [0 ]
108
+ mock_get_club .return_value = mock_clubs_list [0 ]
109
+ mock_check_places .return_value = None # No error message
110
+ mock_take_places .return_value = True # Simulate successful booking
111
+
112
+ # Simulate POST request to the route
113
+ response = client .post (
114
+ "/purchasePlaces" ,
115
+ data = {"competition" : "Competition 1" , "club" : "Club 1" , "places" : "5" },
116
+ )
117
+
118
+ # Assert that the necessary functions are called with the correct parameters
119
+ mock_get_competition .assert_called_once_with ("Competition 1" )
120
+ mock_get_club .assert_called_once_with ("Club 1" )
121
+ mock_check_places .assert_called_once_with ("5" , mock_clubs_list [0 ])
122
+ mock_take_places .assert_called_once_with (
123
+ 5 , mock_clubs_list [0 ], mock_competitions_list [0 ]
124
+ )
125
+ mock_flash .assert_called_once_with ("Great-booking complete!" )
126
+ mock_render_template .assert_called_once_with (
127
+ "welcome.html" ,
128
+ club = mock_clubs_list [0 ],
129
+ competitions = mock_competitions_list ,
130
+ )
131
+
132
+ # Check response status code
133
+ assert response .status_code == 200
134
+
135
+
136
+ # Unit test for booking with error in places
137
+ @patch ("server.redirect" )
138
+ @patch ("server.url_for" )
139
+ @patch ("server.flash" )
140
+ @patch ("server.check_places" )
141
+ @patch ("server.get_club_from_name" )
142
+ @patch ("server.get_competition_from_name" )
143
+ def test_purchase_places_invalid_places (
144
+ mock_get_competition ,
145
+ mock_get_club ,
146
+ mock_check_places ,
147
+ mock_flash ,
148
+ mock_url_for ,
149
+ mock_redirect ,
150
+ client ,
151
+ ):
152
+ # Mock the functions to simulate an error in places
153
+ mock_get_competition .return_value = mock_competitions_list [0 ]
154
+ mock_get_club .return_value = mock_clubs_list [0 ]
155
+ mock_check_places .return_value = (
156
+ "Places required exceed club's total points"
157
+ )
158
+
159
+ # Mock the redirect and url_for to return a redirect response
160
+ mock_url_for .return_value = "/book"
161
+
162
+ # Simulate POST request to the route
163
+ client .post (
164
+ "/purchasePlaces" ,
165
+ data = {"competition" : "Competition 1" , "club" : "Club 1" , "places" : "20" },
166
+ )
167
+
168
+ # Assert that the necessary functions are called with the correct parameters
169
+ mock_get_competition .assert_called_once_with ("Competition 1" )
170
+ mock_get_club .assert_called_once_with ("Club 1" )
171
+ mock_check_places .assert_called_once_with ("20" , mock_clubs_list [0 ])
172
+ mock_flash .assert_called_once_with (
173
+ "Places required exceed club's total points"
174
+ )
175
+ mock_url_for .assert_called_once_with (
176
+ "book" ,
177
+ competition = mock_competitions_list [0 ]["name" ],
178
+ club = mock_clubs_list [0 ]["name" ],
179
+ )
180
+ mock_redirect .assert_called_once_with ("/book" )
181
+
182
+
183
+ # Unit test for failed booking due to take_places failure
184
+ @patch ("server.redirect" )
185
+ @patch ("server.url_for" )
186
+ @patch ("server.flash" )
187
+ @patch ("server.take_places" )
188
+ @patch ("server.check_places" )
189
+ @patch ("server.get_club_from_name" )
190
+ @patch ("server.get_competition_from_name" )
191
+ def test_purchase_places_failed_take_places (
192
+ mock_get_competition ,
193
+ mock_get_club ,
194
+ mock_check_places ,
195
+ mock_take_places ,
196
+ mock_flash ,
197
+ mock_url_for ,
198
+ mock_redirect ,
199
+ client ,
200
+ ):
201
+ # Mock the functions to simulate an error in take_places
202
+ mock_get_competition .return_value = mock_competitions_list [0 ]
203
+ mock_get_club .return_value = mock_clubs_list [0 ]
204
+ mock_check_places .return_value = None # No error message
205
+ mock_take_places .return_value = False # Simulate failure in taking places
206
+
207
+ # Mock the redirect and url_for to return a redirect response
208
+ mock_url_for .return_value = "/book"
209
+
210
+ # Simulate POST request to the route
211
+ client .post (
212
+ "/purchasePlaces" ,
213
+ data = {"competition" : "Competition 1" , "club" : "Club 1" , "places" : "5" },
214
+ )
215
+
216
+ # Assert that the necessary functions are called with the correct parameters
217
+ mock_get_competition .assert_called_once_with ("Competition 1" )
218
+ mock_get_club .assert_called_once_with ("Club 1" )
219
+ mock_check_places .assert_called_once_with ("5" , mock_clubs_list [0 ])
220
+ mock_take_places .assert_called_once_with (
221
+ 5 , mock_clubs_list [0 ], mock_competitions_list [0 ]
222
+ )
223
+ mock_flash .assert_called_once_with ("Something went wrong-please try again" )
224
+ mock_url_for .assert_called_once_with (
225
+ "book" ,
226
+ competition = mock_competitions_list [0 ]["name" ],
227
+ club = mock_clubs_list [0 ]["name" ],
228
+ )
229
+ mock_redirect .assert_called_once_with ("/book" )
0 commit comments