forked from scrapfly/scrapfly-scrapers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
83 lines (72 loc) · 2.54 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
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}")
@pytest.mark.asyncio
async def test_product_scraping():
url = "https://www.aliexpress.com/item/4000927436411.html"
result = await aliexpress.scrape_product(url)
schema = {
"name": {"type": "string"},
"description_short": {"type": "string"},
"images": {"type": "list", "schema": {"type": "string"}},
"stock": {"type": "integer"},
"variants": {
"type": "list",
"schema": {
"type": "dict",
"schema": {
"sku": {"type": "integer"},
"name": {"type": "string"},
"full_price": {"type": "float"},
"discount_price": {"type": "float", "nullable": True},
},
},
},
"seller": {
"type": "dict",
"schema": {
"name": {"type": "string"},
"id": {"type": "integer"},
},
},
}
validator = Validator(schema, allow_unknown=True)
validate_or_fail(result, validator)
@pytest.mark.asyncio
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
async def test_review_scraping():
result = await aliexpress.scrape_product_reviews("120565", "4000927436411", max_pages=2)
assert len(result) > 10
schema = {
"text": {"type": "string"},
"post_time": {"type": "string"},
"stars": {"type": "float"},
"user": {"type": "string", "nullable": True},
"images": {"type": "list", "schema": {"type": "string"}},
}
validator = Validator(schema, allow_unknown=True)
for review in result:
validate_or_fail(review, validator)