forked from scrapfly/scrapfly-scrapers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
81 lines (72 loc) · 2.94 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import pytest
import pprint as pp
from cerberus import Validator
import tripadvisor
# enable cache?
tripadvisor.BASE_CONFIG["cache"] = True
def validate_or_fail(item, validator):
if not validator.validate(item):
pp.pformat(item)
pytest.fail(
f"Validation failed for item: {pp.pformat(item)}\nErrors: {validator.errors}"
)
@pytest.mark.asyncio
async def test_location_data_scraping():
result_location = await tripadvisor.scrape_location_data(query="Malta")
schema = {
"localizedName": {"type": "string"},
"locationV2": {"type": "dict"},
"placeType": {"type": "string"},
"latitude": {"type": "float"},
"longitude": {"type": "float"},
"isGeo": {"type": "boolean"},
"thumbnail": {"type": "dict"},
"url": {"type": "string", "required": True, "regex": r"/Tourism-.+?\.html"},
"HOTELS_URL": {"type": "string", "required": True, "regex": r"/Hotels-.+?\.html"},
"ATTRACTIONS_URL": {"type": "string", "required": True, "regex": r"/Attractions-.+?\.html"},
"RESTAURANTS_URL": {"type": "string", "required": True, "regex": r"/Restaurants-.+?\.html"},
}
validator = Validator(schema, allow_unknown=True)
assert validator.validate(result_location[0]), {"item": result_location[0], "errors": validator.errors}
@pytest.mark.asyncio
async def test_search_scraping():
result_search = await tripadvisor.scrape_search(query="Malta", max_pages=2)
schema = {
"url": {"type": "string", "regex": r"https://www.tripadvisor.com/Hotel_Review-g.+?\.html"},
"name": {"type": "string", "minlength": 5},
}
validator = Validator(schema, allow_unknown=True)
for item in result_search:
assert validator.validate(item), {"item": item, "errors": validator.errors}
@pytest.mark.asyncio
async def test_hotel_scraping():
result_hotel = await tripadvisor.scrape_hotel(
"https://www.tripadvisor.com/Hotel_Review-g190327-d264936-Reviews-1926_Hotel_Spa-Sliema_Island_of_Malta.html",
max_review_pages=3,
)
# test hotel info
schema = {
"basic_data": {
"type": "dict",
"schema": {
"name": {"type": "string", "required": True},
"url": {"type": "string", "required": True},
"image": {"type": "string", "required": True},
"priceRange": {"type": "string", "required": True},
}
},
"description": {"type": "string", "required": True},
"reviews": {
"type": "list",
"schema": {
"type": "dict",
"schema": {
"title": {"type": "string", "required": True},
"tripDate": {"type": "string", "required": True}
}
}
}
}
validator = Validator(schema, allow_unknown=True)
validate_or_fail(result_hotel, validator)
assert len(result_hotel["reviews"]) >= 10