-
Notifications
You must be signed in to change notification settings - Fork 84
/
test.py
146 lines (133 loc) · 4.41 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
146
from cerberus import Validator
import pytest
import aliexpress
import pprint
pp = pprint.PrettyPrinter(indent=4)
# enable cache?
# aliexpress.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}")
review_schema = {
"reviews": {
"type": "list",
"schema": {
"type": "dict",
"schema": {
"buyerFeedback": {"type": "string"},
"buyerName": {"type": "string"},
"buyerTranslationFeedback": {"type": "string"},
"evalDate": {"type": "string"},
"evaluationId": {"type": "integer"},
"logistics": {"type": "string"},
},
},
},
"evaluation_stats": {
"type": "dict",
"schema": {
"evarageStar": {"type": "float"},
"evarageStarRage": {"type": "float"},
"fiveStarNum": {"type": "float"},
}
}
}
@pytest.mark.asyncio
@pytest.mark.flaky(reruns=3, reruns_delay=30)
async def test_product_scraping():
url = "https://www.aliexpress.com/item/4000927436411.html"
result = await aliexpress.scrape_product(url)
schema = {
"info": {
"type": "dict",
"schema": {
"name": {"type": "string"},
"productId": {"type": "integer"},
"link": {"type": "string"},
"media": {"type": "list", "schema": {"type": "string"}},
"rate": {"type": "integer"},
"reviews": {"type": "integer"},
"soldCount": {"type": "integer"},
"availableCount": {"type": "integer"}
}
},
"pricing": {
"type": "dict",
"schema": {
"priceCurrency": {"type": "string"},
"price": {"type": "float"},
"originalPrice": {"type": "float"},
"discount": {"type": "string"},
}
},
"specifications": {
"type": "list",
"schema": {
"type": "dict",
"schema": {
"name": {"type": "string"},
"value": {"type": "string"}
}
}
},
"shipping": {
"type": "dict",
"schema": {
"cost": {"type": "float"},
"currency": {"type": "string"},
"delivery": {"type": "string"},
}
},
"faqs": {
"type": "list",
"schema": {
"type": "dict",
"schema": {
"question": {"type": "string"},
"answer": {"type": "string", "nullable": True}
}
}
},
"seller": {
"type": "dict",
"schema": {
"name": {"type": "string"},
"link": {"type": "string"},
"id": {"type": "integer"},
"info": {
"type": "dict",
"schema": {
"positiveFeedback": {"type": "string"},
"followers": {"type": "integer"},
}
},
}
},
}
validator = Validator(schema, allow_unknown=True)
validate_or_fail(result, validator)
@pytest.mark.asyncio
@pytest.mark.flaky(reruns=3, reruns_delay=30)
async def test_search_scraping():
url = "https://www.aliexpress.com/w/wholesale-drills.html?catId=0&SearchText=drills"
result = await aliexpress.scrape_search(url, max_pages=2)
assert len(result) >= 100
schema = {
"id": {"type": "string"},
"type": {"type": "string"},
"thumbnail": {"type": "string"},
"title": {"type": "string"},
"currency": {"type": "string"},
"price": {"type": "float"},
}
validator = Validator(schema, allow_unknown=True)
for product in result:
validate_or_fail(product, validator)
@pytest.mark.asyncio
@pytest.mark.flaky(reruns=3, reruns_delay=30)
async def test_review_scraping():
result = await aliexpress.scrape_product_reviews("1005006717259012", max_scrape_pages=2)
assert len(result["reviews"]) > 30
validator = Validator(review_schema, allow_unknown=True)
validate_or_fail(result, validator)