sghorbal commited on
Commit
e705bc4
·
1 Parent(s): 5c1452c

add scrapper service tests

Browse files
Files changed (2) hide show
  1. tests/conftest.py +353 -1
  2. tests/service/test_scrapper.py +68 -0
tests/conftest.py CHANGED
@@ -67,7 +67,359 @@ def fake_redis():
67
  # -----------------------------------------------------------------
68
  @pytest.fixture(autouse=True)
69
  def mock_responses():
70
- with responses.RequestsMock() as rsps:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
  yield rsps
72
 
73
  # ----------------------------------------------------------------
 
67
  # -----------------------------------------------------------------
68
  @pytest.fixture(autouse=True)
69
  def mock_responses():
70
+ with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps:
71
+ rsps.add(
72
+ rsps.GET,
73
+ "https://www.atptour.com/en/-/www/players/hero/F324",
74
+ json={
75
+ "LastName": "Federer",
76
+ "FirstName": "Roger",
77
+ "MidInitial": None,
78
+ "BirthCity": "Basel, Switzerland",
79
+ "Residence": None,
80
+ "Coach": "Ivan Ljubicic, Severin Luthi",
81
+ "Pronunciation": None,
82
+ "BirthDate": "1981-08-08T00:00:00",
83
+ "Age": None,
84
+ "NatlId": "SUI",
85
+ "Nationality": "Switzerland",
86
+ "HeightIn": 73,
87
+ "HeightFt": "6'1\"",
88
+ "HeightCm": 185,
89
+ "WeightLb": 187,
90
+ "WeightKg": 85,
91
+ "PlayHand": {
92
+ "Id": "R",
93
+ "Description": "Right-Handed"
94
+ },
95
+ "BackHand": {
96
+ "Id": "1",
97
+ "Description": "One-Handed"
98
+ },
99
+ "ProYear": 1998,
100
+ "Active": {
101
+ "Id": "I",
102
+ "Description": "Inactive"
103
+ },
104
+ "DblSpecialist": False,
105
+ "SglRank": None,
106
+ "SglHiRank": 1,
107
+ "SglRankMove": 0,
108
+ "SglRankTie": False,
109
+ "DblRank": None,
110
+ "DblHiRank": 24,
111
+ "DblRankMove": 0,
112
+ "DblRankTie": False,
113
+ "ScRelativeUrlPlayerProfile": "/en/players/roger-federer/f324/overview",
114
+ "ScRelativeUrlPlayerCountryFlag": "/en/~/media/images/flags/sui.svg",
115
+ "GladiatorImageUrl": None,
116
+ "SglCareerWon": 1251,
117
+ "SglCareerLost": 275,
118
+ "SglYtdWon": 0,
119
+ "SglYtdLost": 0,
120
+ "SglCareerTitles": 103,
121
+ "SglYtdTitles": 0,
122
+ "SglYtdPrizeFormatted": "$0",
123
+ "CareerPrizeFormatted": "$130,594,339",
124
+ "DblCareerWon": 131,
125
+ "DblCareerLost": 93,
126
+ "DblYtdWon": 0,
127
+ "DblYtdLost": 0,
128
+ "DblCareerTitles": 8,
129
+ "DblYtdTitles": 0,
130
+ "DblYtdPrizeFormatted": "$0",
131
+ "IsCarbonTrackerEnabled": False,
132
+ "SocialLinks": [
133
+ {
134
+ "SocialId": "FB",
135
+ "SocialLink": "https://www.facebook.com/Federer"
136
+ },
137
+ {
138
+ "SocialId": "IG",
139
+ "SocialLink": "https://www.instagram.com/rogerfederer/"
140
+ },
141
+ {
142
+ "SocialId": "TW",
143
+ "SocialLink": "https://twitter.com/rogerfederer"
144
+ },
145
+ {
146
+ "SocialId": "Web",
147
+ "SocialLink": "http://www.rogerfederer.com"
148
+ }
149
+ ],
150
+ "CacheTags": None,
151
+ "TopCourtLink": "",
152
+ "SglHiRankDate": "2004-02-02T00:00:00",
153
+ "DblHiRankDate": "2003-06-09T00:00:00"
154
+ },
155
+ status=200
156
+ )
157
+
158
+ rsps.add(
159
+ rsps.GET,
160
+ "https://www.atptour.com/en/-/www/site-search/federer/",
161
+ json={
162
+ "Players": [
163
+ {
164
+ "PlayerId": "F324",
165
+ "LastName": "Federer",
166
+ "FirstName": "Roger",
167
+ "NatlId": "SUI",
168
+ "Active": "I",
169
+ "PlayerHeadshotUrl": "https://www.atptour.com/en/-/ajax/PlayerSearch/HeadshotPhoto?playerId=F324&w=150&h=200",
170
+ "SubCategoryName": "roger-federer-f324"
171
+ }
172
+ ],
173
+ "Tournaments": []
174
+ },
175
+ status=200
176
+ )
177
+
178
+ rsps.add(
179
+ rsps.GET,
180
+ "https://www.atptour.com/en/-/www/site-search/herbert/",
181
+ json={
182
+ "Players": [
183
+ {
184
+ "PlayerId": "BS65",
185
+ "LastName": "Baddeley",
186
+ "FirstName": "Herbert",
187
+ "NatlId": None,
188
+ "Active": "I",
189
+ "PlayerHeadshotUrl": "https://www.atptour.com/en/-/ajax/PlayerSearch/HeadshotPhoto?playerId=BS65&w=150&h=200",
190
+ "SubCategoryName": "herbert-baddeley-bs65"
191
+ },
192
+ {
193
+ "PlayerId": "BO78",
194
+ "LastName": "Behrens",
195
+ "FirstName": "Herbert",
196
+ "NatlId": "USA",
197
+ "Active": "I",
198
+ "PlayerHeadshotUrl": "https://www.atptour.com/en/-/ajax/PlayerSearch/HeadshotPhoto?playerId=BO78&w=150&h=200",
199
+ "SubCategoryName": "herbert-behrens-bo78"
200
+ },
201
+ {
202
+ "PlayerId": "B705",
203
+ "LastName": "Bende",
204
+ "FirstName": "Herbert",
205
+ "NatlId": "SVK",
206
+ "Active": "I",
207
+ "PlayerHeadshotUrl": "https://www.atptour.com/en/-/ajax/PlayerSearch/HeadshotPhoto?playerId=B705&w=150&h=200",
208
+ "SubCategoryName": "herbert-bende-b705"
209
+ },
210
+ {
211
+ "PlayerId": "BP00",
212
+ "LastName": "Bowman",
213
+ "FirstName": "Herbert",
214
+ "NatlId": "USA",
215
+ "Active": "I",
216
+ "PlayerHeadshotUrl": "https://www.atptour.com/en/-/ajax/PlayerSearch/HeadshotPhoto?playerId=BP00&w=150&h=200",
217
+ "SubCategoryName": "herbert-bowman-bp00"
218
+ },
219
+ {
220
+ "PlayerId": "BM64",
221
+ "LastName": "Browne",
222
+ "FirstName": "Herbert",
223
+ "NatlId": "USA",
224
+ "Active": "I",
225
+ "PlayerHeadshotUrl": "https://www.atptour.com/en/-/ajax/PlayerSearch/HeadshotPhoto?playerId=BM64&w=150&h=200",
226
+ "SubCategoryName": "herbert-browne-bm64"
227
+ },
228
+ {
229
+ "PlayerId": "CL93",
230
+ "LastName": "Chipp",
231
+ "FirstName": "Herbert",
232
+ "NatlId": "GBR",
233
+ "Active": "I",
234
+ "PlayerHeadshotUrl": "https://www.atptour.com/en/-/ajax/PlayerSearch/HeadshotPhoto?playerId=CL93&w=150&h=200",
235
+ "SubCategoryName": "herbert-chipp-cl93"
236
+ },
237
+ {
238
+ "PlayerId": "FA25",
239
+ "LastName": "Fischer",
240
+ "FirstName": "Herbert",
241
+ "NatlId": "USA",
242
+ "Active": "I",
243
+ "PlayerHeadshotUrl": "https://www.atptour.com/en/-/ajax/PlayerSearch/HeadshotPhoto?playerId=FA25&w=150&h=200",
244
+ "SubCategoryName": "herbert-fischer-fa25"
245
+ },
246
+ {
247
+ "PlayerId": "H893",
248
+ "LastName": "Herbert",
249
+ "FirstName": "Chris",
250
+ "NatlId": "GBR",
251
+ "Active": "I",
252
+ "PlayerHeadshotUrl": "https://www.atptour.com/en/-/ajax/PlayerSearch/HeadshotPhoto?playerId=H893&w=150&h=200",
253
+ "SubCategoryName": "chris-herbert-h893"
254
+ },
255
+ {
256
+ "PlayerId": "H966",
257
+ "LastName": "Herbert",
258
+ "FirstName": "Justus",
259
+ "NatlId": "GER",
260
+ "Active": "I",
261
+ "PlayerHeadshotUrl": "https://www.atptour.com/en/-/ajax/PlayerSearch/HeadshotPhoto?playerId=H966&w=150&h=200",
262
+ "SubCategoryName": "justus-herbert-h966"
263
+ },
264
+ {
265
+ "PlayerId": "H996",
266
+ "LastName": "Herbert",
267
+ "FirstName": "Pierre-Hugues",
268
+ "NatlId": "FRA",
269
+ "Active": "A",
270
+ "PlayerHeadshotUrl": "https://www.atptour.com/en/-/ajax/PlayerSearch/HeadshotPhoto?playerId=H996&w=150&h=200",
271
+ "SubCategoryName": "pierre-hugues-herbert-h996"
272
+ },
273
+ {
274
+ "PlayerId": "H430",
275
+ "LastName": "Herbert",
276
+ "FirstName": "William",
277
+ "NatlId": "GBR",
278
+ "Active": "I",
279
+ "PlayerHeadshotUrl": "https://www.atptour.com/en/-/ajax/PlayerSearch/HeadshotPhoto?playerId=H430&w=150&h=200",
280
+ "SubCategoryName": "william-herbert-h430"
281
+ },
282
+ {
283
+ "PlayerId": "J385",
284
+ "LastName": "Jerich",
285
+ "FirstName": "Herbert",
286
+ "NatlId": "AUT",
287
+ "Active": "I",
288
+ "PlayerHeadshotUrl": "https://www.atptour.com/en/-/ajax/PlayerSearch/HeadshotPhoto?playerId=J385&w=150&h=200",
289
+ "SubCategoryName": "herbert-jerich-j385"
290
+ },
291
+ {
292
+ "PlayerId": "KG69",
293
+ "LastName": "Kinzl",
294
+ "FirstName": "Herbert",
295
+ "NatlId": "AUT",
296
+ "Active": "I",
297
+ "PlayerHeadshotUrl": "https://www.atptour.com/en/-/ajax/PlayerSearch/HeadshotPhoto?playerId=KG69&w=150&h=200",
298
+ "SubCategoryName": "herbert-kinzl-kg69"
299
+ },
300
+ {
301
+ "PlayerId": "LG94",
302
+ "LastName": "Lawford",
303
+ "FirstName": "Herbert",
304
+ "NatlId": "GBR",
305
+ "Active": "I",
306
+ "PlayerHeadshotUrl": "https://www.atptour.com/en/-/ajax/PlayerSearch/HeadshotPhoto?playerId=LG94&w=150&h=200",
307
+ "SubCategoryName": "herbert-lawford-lg94"
308
+ },
309
+ {
310
+ "PlayerId": "L0E1",
311
+ "LastName": "Loerke",
312
+ "FirstName": "Herbert",
313
+ "NatlId": "GER",
314
+ "Active": "I",
315
+ "PlayerHeadshotUrl": "https://www.atptour.com/en/-/ajax/PlayerSearch/HeadshotPhoto?playerId=L0E1&w=150&h=200",
316
+ "SubCategoryName": None
317
+ },
318
+ {
319
+ "PlayerId": "M0M2",
320
+ "LastName": "Mann",
321
+ "FirstName": "Herbert",
322
+ "NatlId": "AUT",
323
+ "Active": "I",
324
+ "PlayerHeadshotUrl": "https://www.atptour.com/en/-/ajax/PlayerSearch/HeadshotPhoto?playerId=M0M2&w=150&h=200",
325
+ "SubCategoryName": None
326
+ },
327
+ {
328
+ "PlayerId": "R509",
329
+ "LastName": "Rapp",
330
+ "FirstName": "Herbert",
331
+ "NatlId": "USA",
332
+ "Active": "I",
333
+ "PlayerHeadshotUrl": "https://www.atptour.com/en/-/ajax/PlayerSearch/HeadshotPhoto?playerId=R509&w=150&h=200",
334
+ "SubCategoryName": "herbert-rapp-r509"
335
+ },
336
+ {
337
+ "PlayerId": "RF72",
338
+ "LastName": "Roper-Barrett",
339
+ "FirstName": "Herbert",
340
+ "NatlId": "GBR",
341
+ "Active": "I",
342
+ "PlayerHeadshotUrl": "https://www.atptour.com/en/-/ajax/PlayerSearch/HeadshotPhoto?playerId=RF72&w=150&h=200",
343
+ "SubCategoryName": "herbert-roper-barrett-rf72"
344
+ },
345
+ {
346
+ "PlayerId": "S264",
347
+ "LastName": "Sandberg",
348
+ "FirstName": "Herbert",
349
+ "NatlId": "GER",
350
+ "Active": "I",
351
+ "PlayerHeadshotUrl": "https://www.atptour.com/en/-/ajax/PlayerSearch/HeadshotPhoto?playerId=S264&w=150&h=200",
352
+ "SubCategoryName": "herbert-sandberg-s264"
353
+ },
354
+ {
355
+ "PlayerId": "TF94",
356
+ "LastName": "Taylor",
357
+ "FirstName": "Herbert",
358
+ "NatlId": None,
359
+ "Active": "I",
360
+ "PlayerHeadshotUrl": "https://www.atptour.com/en/-/ajax/PlayerSearch/HeadshotPhoto?playerId=TF94&w=150&h=200",
361
+ "SubCategoryName": "herbert-taylor-tf94"
362
+ },
363
+ {
364
+ "PlayerId": "TF28",
365
+ "LastName": "Turner",
366
+ "FirstName": "Herbert",
367
+ "NatlId": "AUS",
368
+ "Active": "I",
369
+ "PlayerHeadshotUrl": "https://www.atptour.com/en/-/ajax/PlayerSearch/HeadshotPhoto?playerId=TF28&w=150&h=200",
370
+ "SubCategoryName": "herbert-turner-tf28"
371
+ },
372
+ {
373
+ "PlayerId": "W487",
374
+ "LastName": "Weirather",
375
+ "FirstName": "Herbert",
376
+ "NatlId": "AUT",
377
+ "Active": "I",
378
+ "PlayerHeadshotUrl": "https://www.atptour.com/en/-/ajax/PlayerSearch/HeadshotPhoto?playerId=W487&w=150&h=200",
379
+ "SubCategoryName": "herbert-weirather-w487"
380
+ },
381
+ {
382
+ "PlayerId": "W858",
383
+ "LastName": "Whitney",
384
+ "FirstName": "Herbert",
385
+ "NatlId": "GBR",
386
+ "Active": "I",
387
+ "PlayerHeadshotUrl": "https://www.atptour.com/en/-/ajax/PlayerSearch/HeadshotPhoto?playerId=W858&w=150&h=200",
388
+ "SubCategoryName": "herbert-whitney-w858"
389
+ },
390
+ {
391
+ "PlayerId": "WA15",
392
+ "LastName": "Wilberforce",
393
+ "FirstName": "Herbert W.W.",
394
+ "NatlId": None,
395
+ "Active": "I",
396
+ "PlayerHeadshotUrl": "https://www.atptour.com/en/-/ajax/PlayerSearch/HeadshotPhoto?playerId=WA15&w=150&h=200",
397
+ "SubCategoryName": "herbert-ww-wilberforce-wa15"
398
+ },
399
+ {
400
+ "PlayerId": "W964",
401
+ "LastName": "Wilson-Fox",
402
+ "FirstName": "Herbert",
403
+ "NatlId": "GBR",
404
+ "Active": "I",
405
+ "PlayerHeadshotUrl": "https://www.atptour.com/en/-/ajax/PlayerSearch/HeadshotPhoto?playerId=W964&w=150&h=200",
406
+ "SubCategoryName": "herbert-wilson-fox-w964"
407
+ },
408
+ {
409
+ "PlayerId": "W200",
410
+ "LastName": "Wiltschnig",
411
+ "FirstName": "Herbert",
412
+ "NatlId": "AUT",
413
+ "Active": "I",
414
+ "PlayerHeadshotUrl": "https://www.atptour.com/en/-/ajax/PlayerSearch/HeadshotPhoto?playerId=W200&w=150&h=200",
415
+ "SubCategoryName": "herbert-wiltschnig-w200"
416
+ }
417
+ ],
418
+ "Tournaments": []
419
+ },
420
+ status=200
421
+ )
422
+
423
  yield rsps
424
 
425
  # ----------------------------------------------------------------
tests/service/test_scrapper.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Dict
2
+ from src.service.scrapper import (
3
+ search_player,
4
+ get_personal_details,
5
+ )
6
+
7
+ def test_search_player_herbert(mock_responses):
8
+ """
9
+ Test the search_player function
10
+ """
11
+ player_list = search_player(raw_name="Herbert P.H.")
12
+
13
+ assert len(player_list) > 0, "No player found"
14
+ player_data = None
15
+
16
+ for player in player_list:
17
+ assert isinstance(player, Dict), "player is not a dictionary"
18
+ if player.get("name") == "Pierre-Hugues Herbert":
19
+ player_data = player
20
+ break
21
+
22
+ assert player_data is not None, "Player data not found"
23
+ assert isinstance(player_data, Dict), "Player data is not a dictionary"
24
+ assert player_data.get("name") == "Pierre-Hugues Herbert", "Player name is not correct"
25
+ assert player_data.get("first_name") == "Pierre-Hugues", "Player first name is not correct"
26
+ assert player_data.get("last_name") == "Herbert", "Player last name is not correct"
27
+ assert player_data.get("country") == "FRA", "Player country is not correct"
28
+ assert player_data.get("active") == "A", "Player active status is not correct"
29
+ assert player_data.get("headshot_url") == "https://www.atptour.com/en/-/ajax/PlayerSearch/HeadshotPhoto?playerId=H996&w=150&h=200", "Player headshot URL is not correct"
30
+ assert player_data.get("sub_category_name") == "pierre-hugues-herbert-h996", "Player sub category name is not correct"
31
+ assert player_data.get("player_id") == "H996", "Player ID is not correct"
32
+
33
+ def test_search_player_federer():
34
+ """
35
+ Test the search_player function
36
+ """
37
+ player_list = search_player(raw_name="Federer R.")
38
+
39
+ assert len(player_list) > 0, "No player found"
40
+ player_data = player_list[0]
41
+
42
+ assert isinstance(player_data, Dict), "Player data is not a dictionary"
43
+ assert player_data.get("name") == "Roger Federer", "Player name is not correct"
44
+ assert player_data.get("first_name") == "Roger", "Player first name is not correct"
45
+ assert player_data.get("last_name") == "Federer", "Player last name is not correct"
46
+ assert player_data.get("country") == "SUI", "Player country is not correct"
47
+ assert player_data.get("active") == "I", "Player active status is not correct"
48
+ assert player_data.get("headshot_url") == "https://www.atptour.com/en/-/ajax/PlayerSearch/HeadshotPhoto?playerId=F324&w=150&h=200", "Player headshot URL is not correct"
49
+ assert player_data.get("sub_category_name") == "roger-federer-f324", "Player sub category name is not correct"
50
+ assert player_data.get("player_id") == "F324", "Player ID is not correct"
51
+
52
+ def test_get_personal_details():
53
+ """
54
+ Test the get_personal_details function
55
+ """
56
+ personal_details = get_personal_details(playerId="F324")
57
+
58
+ assert isinstance(personal_details, Dict), "Personal details is not a dictionary"
59
+ assert personal_details.get("last_name") == "Federer", "Player last name is not correct"
60
+ assert personal_details.get("first_name") == "Roger", "Player first name is not correct"
61
+ assert personal_details.get("nationality") == "Switzerland", "Player nationality is not correct"
62
+ assert personal_details.get("birth_date") == "1981-08-08T00:00:00", "Player birth date is not correct"
63
+ assert personal_details.get("pro_year") == 1998, "Player pro year is not correct"
64
+ assert personal_details.get("height_cm") == 185, "Player height is not correct"
65
+ assert personal_details.get("weight_kg") == 85, "Player weight is not correct"
66
+ assert personal_details.get("play_hand") == "R", "Player play hand is not correct"
67
+ assert personal_details.get("back_hand") == "1", "Player back hand is not correct"
68
+