-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy pathtest.py
145 lines (131 loc) · 4.48 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
from cerberus import Validator
import pytest
import yelp
import pprint
pp = pprint.PrettyPrinter(indent=4)
# enable scrapfly cache
yelp.BASE_CONFIG["cache"] = False
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}"
)
page_schema = {
"name": {"type": "string"},
"website": {"type": "string"},
"phone": {"type": "string"},
"address": {"type": "string"},
}
review_schema = {
"encid": {"type": "string"},
"text": {
"type": "dict",
"schema": {
"full": {"type": "string"}
}
},
"rating": {"type": "integer"},
"author": {
"type": "dict",
"schema": {
"encid": {"type": "string"},
"displayName": {"type": "string"},
"displayLocation": {"type": "string"},
"reviewCount": {"type": "integer"},
"friendCount": {"type": "integer"},
"businessPhotoCount": {"type": "integer"},
}
},
"business": {
"type": "dict",
"schema": {
"encid": {"type": "string"},
"alias": {"type": "string"},
"name": {"type": "string"}
}
},
"createdAt": {"type": "string"},
"businessPhotos": {
"type": "list",
"schema": {
"type": "dict",
"schema": {
"encid": {"type": "string"},
"photoUrl": {"type": "string"},
"caption": {"type": "string", "nullable": True},
}
}
}
}
search_schema = {
"bizId": {"type": "string"},
"searchResultBusiness": {
"type": "dict",
"schema": {
"name": {"type": "string"},
"rating": {"type": "float", "nullable": True},
"reviewCount": {"type": "integer", "nullable": True},
"phone": {"type": "string", "nullable": True},
"businessAttributes": {
"type": "dict",
"schema": {
"licenses": {
"type": "list",
"schema": {
"type": "dict",
"schema": {
"license_number": {"type": "string"},
"license_expiration_date": {"type": "string", "nullable": True},
"license_verification_url": {"type": "string"},
"license_verification_status": {"type": "string"},
"license_verification_date": {"type": "string"},
"license_issuing_authority": {"type": "string", "nullable": True},
"license_type": {"type": "string"},
"license_source": {"type": "string"},
},
},
"nullable": True,
}
},
},
"alias": {"type": "string"},
},
},
}
@pytest.mark.asyncio
@pytest.mark.flaky(reruns=3, reruns_delay=30)
async def test_review_scraping():
reviews_data = await yelp.scrape_reviews(
url="https://www.yelp.com/biz/vons-1000-spirits-seattle-4",
# each 10 reviews represent a review page (one request)
max_reviews=28,
)
validator = Validator(review_schema, allow_unknown=True)
for item in reviews_data:
validate_or_fail(item, validator)
assert len(reviews_data) >= 10
@pytest.mark.asyncio
@pytest.mark.flaky(reruns=3, reruns_delay=30)
async def test_page_scraping():
business_data = await yelp.scrape_pages(
urls=[
"https://www.yelp.com/biz/vons-1000-spirits-seattle-4",
"https://www.yelp.com/biz/ihop-seattle-4",
"https://www.yelp.com/biz/toulouse-petit-kitchen-and-lounge-seattle",
]
)
validator = Validator(page_schema, allow_unknown=True)
for item in business_data:
validate_or_fail(item, validator)
assert len(business_data) >= 1
@pytest.mark.asyncio
@pytest.mark.flaky(reruns=3, reruns_delay=30)
async def test_search_scraping():
search_data = await yelp.scrape_search(
keyword="plumbers", location="Seattle, WA", max_pages=2
)
validator = Validator(search_schema, allow_unknown=True)
for item in search_data:
validate_or_fail(item, validator)
assert len(search_data) >= 10