SlimG commited on
Commit
9dc3352
·
1 Parent(s): 7ccefb1

stub API calls in tests with mocked responses

Browse files
Files changed (4) hide show
  1. .env.test +3 -8
  2. requirements-dev.txt +2 -1
  3. tests/conftest.py +473 -0
  4. tests/test_utils.py +2 -18
.env.test CHANGED
@@ -1,8 +1,3 @@
1
- API_URL=
2
- # API_URL=https://slimg-tennis-api.hf.space/
3
- # API_URL=http://localhost:7860/
4
- API_KEY=
5
- # API_KEY=je_suis_une_clef_d_api
6
-
7
- # For security
8
- STREAMLIT_PWD=pwd_test
 
1
+ API_URL=https://dummy_url
2
+ API_KEY=dummy_key
3
+ STREAMLIT_PWD=pwd_test
 
 
 
 
 
requirements-dev.txt CHANGED
@@ -1,2 +1,3 @@
1
  pytest==8.3.5
2
- pytest-dotenv==0.5.2
 
 
1
  pytest==8.3.5
2
+ pytest-dotenv==0.5.2
3
+ responses==0.25.7
tests/conftest.py ADDED
@@ -0,0 +1,473 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+ import responses
3
+ from responses.matchers import query_param_matcher
4
+ from dotenv import load_dotenv
5
+ import os
6
+
7
+ load_dotenv()
8
+
9
+ API_URL = os.getenv('API_URL')
10
+
11
+ # -----------------------------------------------------------------
12
+ # Fixtures for requests calls
13
+ # -----------------------------------------------------------------
14
+ @pytest.fixture(autouse=True)
15
+ def mock_responses():
16
+ with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps:
17
+ rsps.add(
18
+ rsps.GET,
19
+ f"{API_URL}/references/courts",
20
+ json=[
21
+ "Indoor",
22
+ "Outdoor"
23
+ ],
24
+ status=200
25
+ )
26
+
27
+ rsps.add(
28
+ rsps.GET,
29
+ f"{API_URL}/references/surfaces",
30
+ json=[
31
+ "Carpet",
32
+ "Clay",
33
+ "Grass",
34
+ "Hard"
35
+ ],
36
+ status=200
37
+ )
38
+
39
+ rsps.add(
40
+ rsps.GET,
41
+ f"{API_URL}/references/series",
42
+ json=[
43
+ "ATP250",
44
+ "ATP500",
45
+ "Grand Slam",
46
+ "International",
47
+ "International Gold",
48
+ "Masters",
49
+ "Masters 1000",
50
+ "Masters Cup"
51
+ ],
52
+ status=200
53
+ )
54
+
55
+ rsps.add(
56
+ rsps.GET,
57
+ f"{API_URL}/tournament/names",
58
+ json=[
59
+ {
60
+ "name": "Abierto Mexicano",
61
+ "first_year": 2001,
62
+ "last_year": 2025
63
+ },
64
+ {
65
+ "name": "Australian Open",
66
+ "first_year": 2001,
67
+ "last_year": 2025
68
+ },
69
+ {
70
+ "name": "Austrian Open",
71
+ "first_year": 2008,
72
+ "last_year": 2009
73
+ },
74
+ {
75
+ "name": "BMW Open",
76
+ "first_year": 2004,
77
+ "last_year": 2025
78
+ },
79
+ {
80
+ "name": "BNP Paribas",
81
+ "first_year": 2004,
82
+ "last_year": 2008
83
+ },
84
+ {
85
+ "name": "BNP Paribas Masters",
86
+ "first_year": 2009,
87
+ "last_year": 2024
88
+ },
89
+ {
90
+ "name": "BNP Paribas Open",
91
+ "first_year": 2009,
92
+ "last_year": 2025
93
+ },
94
+ {
95
+ "name": "Brasil Open",
96
+ "first_year": 2004,
97
+ "last_year": 2019
98
+ },
99
+ {
100
+ "name": "French Open",
101
+ "first_year": 2004,
102
+ "last_year": 2024
103
+ },
104
+ {
105
+ "name": "Halle Open",
106
+ "first_year": 2019,
107
+ "last_year": 2024
108
+ },
109
+ {
110
+ "name": "Madrid Masters",
111
+ "first_year": 2004,
112
+ "last_year": 2008
113
+ },
114
+ {
115
+ "name": "US Open",
116
+ "first_year": 2004,
117
+ "last_year": 2024
118
+ },
119
+ {
120
+ "name": "Wimbledon",
121
+ "first_year": 2004,
122
+ "last_year": 2024
123
+ },
124
+ ],
125
+ status=200
126
+ )
127
+
128
+ rsps.add(
129
+ rsps.GET,
130
+ f"{API_URL}/tournament/matches",
131
+ match=[query_param_matcher(params={
132
+ "name": "BMW Open",
133
+ "year": 2025,
134
+ })],
135
+ json=[
136
+ {
137
+ "id": 40926,
138
+ "date": "2025-04-20",
139
+ "comment": "Completed",
140
+ "winner_rank": 3,
141
+ "winner_points": 7595,
142
+ "loser_rank": 15,
143
+ "loser_points": 2700,
144
+ "tournament_name": "BMW Open",
145
+ "tournament_series": "ATP500",
146
+ "tournament_surface": "Clay",
147
+ "tournament_court": "Outdoor",
148
+ "tournament_round": "The Final",
149
+ "tournament_location": "Munich",
150
+ "fk_winner_id": 666,
151
+ "fk_loser_id": 1049,
152
+ "odds": [
153
+ {
154
+ "id": 218029,
155
+ "bookmaker": "Avg",
156
+ "winner": 1.21,
157
+ "loser": 4.53
158
+ },
159
+ {
160
+ "id": 218027,
161
+ "bookmaker": "B365",
162
+ "winner": 1.2,
163
+ "loser": 4.5
164
+ },
165
+ {
166
+ "id": 218030,
167
+ "bookmaker": "Max",
168
+ "winner": 1.23,
169
+ "loser": 5.19
170
+ },
171
+ {
172
+ "id": 218028,
173
+ "bookmaker": "PS",
174
+ "winner": 1.22,
175
+ "loser": 4.94
176
+ }
177
+ ]
178
+ },
179
+ {
180
+ "id": 40924,
181
+ "date": "2025-04-19",
182
+ "comment": "Completed",
183
+ "winner_rank": 15,
184
+ "winner_points": 2700,
185
+ "loser_rank": 22,
186
+ "loser_points": 2075,
187
+ "tournament_name": "BMW Open",
188
+ "tournament_series": "ATP500",
189
+ "tournament_surface": "Clay",
190
+ "tournament_court": "Outdoor",
191
+ "tournament_round": "Semifinals",
192
+ "tournament_location": "Munich",
193
+ "fk_winner_id": 1049,
194
+ "fk_loser_id": 985,
195
+ "odds": [
196
+ {
197
+ "id": 218021,
198
+ "bookmaker": "Avg",
199
+ "winner": 3.25,
200
+ "loser": 1.35
201
+ },
202
+ {
203
+ "id": 218019,
204
+ "bookmaker": "B365",
205
+ "winner": 3.2,
206
+ "loser": 1.36
207
+ },
208
+ {
209
+ "id": 218022,
210
+ "bookmaker": "Max",
211
+ "winner": 3.5,
212
+ "loser": 1.39
213
+ },
214
+ {
215
+ "id": 218020,
216
+ "bookmaker": "PS",
217
+ "winner": 3.5,
218
+ "loser": 1.35
219
+ }
220
+ ]
221
+ },
222
+ {
223
+ "id": 40925,
224
+ "date": "2025-04-19",
225
+ "comment": "Completed",
226
+ "winner_rank": 3,
227
+ "winner_points": 7595,
228
+ "loser_rank": 77,
229
+ "loser_points": 805,
230
+ "tournament_name": "BMW Open",
231
+ "tournament_series": "ATP500",
232
+ "tournament_surface": "Clay",
233
+ "tournament_court": "Outdoor",
234
+ "tournament_round": "Semifinals",
235
+ "tournament_location": "Munich",
236
+ "fk_winner_id": 666,
237
+ "fk_loser_id": 1097,
238
+ "odds": [
239
+ {
240
+ "id": 218025,
241
+ "bookmaker": "Avg",
242
+ "winner": 1.22,
243
+ "loser": 4.41
244
+ },
245
+ {
246
+ "id": 218023,
247
+ "bookmaker": "B365",
248
+ "winner": 1.2,
249
+ "loser": 4.5
250
+ },
251
+ {
252
+ "id": 218026,
253
+ "bookmaker": "Max",
254
+ "winner": 1.25,
255
+ "loser": 4.95
256
+ },
257
+ {
258
+ "id": 218024,
259
+ "bookmaker": "PS",
260
+ "winner": 1.22,
261
+ "loser": 4.95
262
+ }
263
+ ]
264
+ }
265
+ ],
266
+ )
267
+
268
+ rsps.add(
269
+ rsps.GET,
270
+ f"{API_URL}/players",
271
+ match=[query_param_matcher({
272
+ "ids": ['103', '104']
273
+ })],
274
+ json={
275
+ "103": {
276
+ "id": 103,
277
+ "name": "Nishikori K.",
278
+ "tennis_id": "N552",
279
+ "caracteristics": {
280
+ "id": 42,
281
+ "nationality": "Japan",
282
+ "last_name": "Nishikori",
283
+ "first_name": "Kei",
284
+ "play_hand": "R",
285
+ "back_hand": 2,
286
+ "height_cm": 178,
287
+ "weight_kg": 73,
288
+ "date_of_birth": "1989-12-29",
289
+ "pro_year": 2007
290
+ }
291
+ },
292
+ "104": {
293
+ "id": 104,
294
+ "name": "Reynolds B.",
295
+ "tennis_id": "R0G6",
296
+ "caracteristics": {
297
+ "id": 27,
298
+ "nationality": "United States",
299
+ "last_name": "Reynolds",
300
+ "first_name": "Bob",
301
+ "play_hand": "U",
302
+ "back_hand": 0,
303
+ "height_cm": None,
304
+ "weight_kg": None,
305
+ "date_of_birth": None,
306
+ "pro_year": None
307
+ }
308
+ }
309
+ },
310
+ status=200
311
+ )
312
+
313
+ rsps.add(
314
+ rsps.GET,
315
+ f"{API_URL}/player/103",
316
+ json={
317
+ "id": 103,
318
+ "name": "Nishikori K.",
319
+ "tennis_id": "N552",
320
+ "caracteristics": {
321
+ "id": 42,
322
+ "nationality": "Japan",
323
+ "last_name": "Nishikori",
324
+ "first_name": "Kei",
325
+ "play_hand": "R",
326
+ "back_hand": 2,
327
+ "height_cm": 178,
328
+ "weight_kg": 73,
329
+ "date_of_birth": "1989-12-29",
330
+ "pro_year": 2007
331
+ }
332
+ },
333
+ status=200
334
+ )
335
+
336
+ rsps.add(
337
+ rsps.GET,
338
+ f"{API_URL}/player/104",
339
+ json={
340
+ "id": 104,
341
+ "name": "Reynolds B.",
342
+ "tennis_id": "R0G6",
343
+ "caracteristics": {
344
+ "id": 27,
345
+ "nationality": "United States",
346
+ "last_name": "Reynolds",
347
+ "first_name": "Bob",
348
+ "play_hand": "U",
349
+ "back_hand": 0,
350
+ "height_cm": None,
351
+ "weight_kg": None,
352
+ "date_of_birth": None,
353
+ "pro_year": None
354
+ }
355
+ },
356
+ status=200
357
+ )
358
+
359
+ rsps.add(
360
+ rsps.GET,
361
+ f"{API_URL}/match/40926",
362
+ json={
363
+ "id": 40926,
364
+ "date": "2025-04-20",
365
+ "comment": "Completed",
366
+ "winner_rank": 3,
367
+ "winner_points": 7595,
368
+ "loser_rank": 15,
369
+ "loser_points": 2700,
370
+ "tournament_name": "BMW Open",
371
+ "tournament_series": "ATP500",
372
+ "tournament_surface": "Clay",
373
+ "tournament_court": "Outdoor",
374
+ "tournament_round": "The Final",
375
+ "tournament_location": "Munich",
376
+ "fk_winner_id": 666,
377
+ "fk_loser_id": 1049,
378
+ "odds": [
379
+ {
380
+ "id": 218029,
381
+ "bookmaker": "Avg",
382
+ "winner": 1.21,
383
+ "loser": 4.53
384
+ },
385
+ {
386
+ "id": 218027,
387
+ "bookmaker": "B365",
388
+ "winner": 1.2,
389
+ "loser": 4.5
390
+ },
391
+ {
392
+ "id": 218030,
393
+ "bookmaker": "Max",
394
+ "winner": 1.23,
395
+ "loser": 5.19
396
+ },
397
+ {
398
+ "id": 218028,
399
+ "bookmaker": "PS",
400
+ "winner": 1.22,
401
+ "loser": 4.94
402
+ }
403
+ ],
404
+ "winner": {
405
+ "id": 666,
406
+ "name": "Zverev A.",
407
+ "tennis_id": "Z355",
408
+ "caracteristics": {
409
+ "id": 505,
410
+ "nationality": "Germany",
411
+ "last_name": "Zverev",
412
+ "first_name": "Alexander",
413
+ "play_hand": "R",
414
+ "back_hand": 2,
415
+ "height_cm": 198,
416
+ "weight_kg": 90,
417
+ "date_of_birth": "1997-04-20",
418
+ "pro_year": 2013
419
+ }
420
+ },
421
+ "loser": {
422
+ "id": 1049,
423
+ "name": "Shelton B.",
424
+ "tennis_id": "S0S1",
425
+ "caracteristics": {
426
+ "id": 857,
427
+ "nationality": "United States",
428
+ "last_name": "Shelton",
429
+ "first_name": "Ben",
430
+ "play_hand": "L",
431
+ "back_hand": 2,
432
+ "height_cm": 193,
433
+ "weight_kg": 88,
434
+ "date_of_birth": "2002-10-09",
435
+ "pro_year": 2022
436
+ }
437
+ }
438
+ },
439
+ status=200
440
+ )
441
+
442
+ rsps.add(
443
+ rsps.GET,
444
+ f"{API_URL}/list_available_models",
445
+ json=[
446
+ {
447
+ "name": "LogisticRegression",
448
+ "run_id": "55e44730cce245a99da6f023a2afd54c",
449
+ "version": "1"
450
+ },
451
+ {
452
+ "name": "MLP",
453
+ "run_id": "a0b525d0a8d940f28e8db6b237414e6e",
454
+ "version": "1"
455
+ }
456
+ ],
457
+ status=200
458
+ )
459
+
460
+ rsps.add(
461
+ rsps.GET,
462
+ f"{API_URL}/predict",
463
+ json={
464
+ "result": 1,
465
+ "prob": [
466
+ 0.3512304338942953,
467
+ 0.6487695661057047
468
+ ]
469
+ },
470
+ status=200
471
+ )
472
+
473
+ yield rsps
tests/test_utils.py CHANGED
@@ -1,21 +1,13 @@
1
  from streamlit.testing.v1 import AppTest
2
  import os
3
  from dotenv import load_dotenv
4
- from unittest.mock import patch, Mock
5
 
6
  load_dotenv()
7
 
8
- @patch('app.utils.get_all_models_options')
9
- def test_check_pwd_correct(mock_get_all_models_options: Mock):
10
  """
11
  Test the login flow with a correct password.
12
  """
13
- mock_get_all_models_options.return_value = [{
14
- "name": "Model 1",
15
- "run_id": "12345",
16
- "version": "1",
17
- }]
18
-
19
  STREAMLIT_PWD = os.getenv("STREAMLIT_PWD")
20
  assert STREAMLIT_PWD is not None, "STREAMLIT_PWD environment variable is not set"
21
 
@@ -30,17 +22,10 @@ def test_check_pwd_correct(mock_get_all_models_options: Mock):
30
  assert at.session_state["is_authenticated"] is True, "Authentication should have succeeded"
31
  assert "password" not in at.session_state, "Password should be cleared from session state"
32
 
33
- @patch('app.utils.get_all_models_options')
34
- def test_check_pwd_incorrect(mock_get_all_models_options: Mock):
35
  """
36
  Test the login flow with a correct password.
37
  """
38
- mock_get_all_models_options.return_value = [{
39
- "name": "Model 1",
40
- "run_id": "12345",
41
- "version": "1",
42
- }]
43
-
44
  STREAMLIT_PWD = os.getenv("STREAMLIT_PWD")
45
  assert STREAMLIT_PWD is not None, "STREAMLIT_PWD environment variable is not set"
46
 
@@ -52,4 +37,3 @@ def test_check_pwd_incorrect(mock_get_all_models_options: Mock):
52
 
53
  # Check expected session state updates
54
  assert "is_authenticated" not in at.session_state, "Expected 'is_authenticated' key in session state"
55
- assert len(at.error) == 1, "Expected one error message"
 
1
  from streamlit.testing.v1 import AppTest
2
  import os
3
  from dotenv import load_dotenv
 
4
 
5
  load_dotenv()
6
 
7
+ def test_check_pwd_correct():
 
8
  """
9
  Test the login flow with a correct password.
10
  """
 
 
 
 
 
 
11
  STREAMLIT_PWD = os.getenv("STREAMLIT_PWD")
12
  assert STREAMLIT_PWD is not None, "STREAMLIT_PWD environment variable is not set"
13
 
 
22
  assert at.session_state["is_authenticated"] is True, "Authentication should have succeeded"
23
  assert "password" not in at.session_state, "Password should be cleared from session state"
24
 
25
+ def test_check_pwd_incorrect():
 
26
  """
27
  Test the login flow with a correct password.
28
  """
 
 
 
 
 
 
29
  STREAMLIT_PWD = os.getenv("STREAMLIT_PWD")
30
  assert STREAMLIT_PWD is not None, "STREAMLIT_PWD environment variable is not set"
31
 
 
37
 
38
  # Check expected session state updates
39
  assert "is_authenticated" not in at.session_state, "Expected 'is_authenticated' key in session state"