forked from scrapfly/scrapfly-scrapers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
124 lines (113 loc) · 3.73 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
from cerberus import Validator
import pytest
import nordstorm
import pprint
pp = pprint.PrettyPrinter(indent=4)
# enable cache
nordstorm.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}")
product_schema = {
"id": {"type": "integer"},
"title": {"type": "string"},
"type": {"type": "string"},
"typeParent": {"type": "string"},
"brand": {
"type": "dict",
"schema": {
"brandName": {"type": "string"},
"brandUrl": {"type": "string"},
"hasBrandPage": {"type": "boolean"},
"imsBrandId": {"type": "integer"},
},
},
"description": {"type": "string"},
"features": {"type": "list", "schema": {"type": "string"}},
"gender": {"type": "string"},
"media": {
"type": "dict",
"valueschema": {
"type": "dict",
"schema": {
"id": {"type": "integer"},
"colorId": {"type": "string"},
"name": {"type": "string"},
"url": {"type": "string"},
},
},
},
"variants": {
"type": "dict",
"valueschema": {
"type": "dict",
"schema": {
"id": {"type": "string"},
"sizeId": {"type": "string"},
"colorId": {"type": "string"},
"totalQuantityAvailable": {"type": "integer"},
},
},
},
}
search_schema = {
"id": {"type": "integer"},
"brandId": {"type": "integer"},
"brandName": {"type": "string"},
"styleNumber": {"type": "string"},
"colorCount": {"type": "integer"},
"colorDefaultId": {"type": "string"},
"name": {"type": "string"},
"extraNameCopy": {"type": "string"},
"priceCurrencyCode": {"type": "string"},
"priceCountryCode": {"type": "string"},
"price": {
"type": "dict",
"schema": {
"totalPriceRange": {
"type": "dict",
"schema": {
"min": {
"type": "dict",
"schema": {
"currencyCode": {"type": "string"},
"units": {"type": "integer"},
"nanos": {"type": "integer"},
},
},
"max": {
"type": "dict",
"schema": {
"currencyCode": {"type": "string"},
"units": {"type": "integer"},
"nanos": {"type": "integer"},
},
},
},
}
},
},
}
@pytest.mark.asyncio
async def test_product_scraping():
products_data = await nordstorm.scrape_products(
urls=[
"https://www.nordstrom.com/s/nike-air-max-270-sneaker-women/4700177",
"https://www.nordstrom.com/s/nike-sportswear-club-hoodie/6049642",
"https://www.nordstrom.com/s/nike-phoenix-fleece-crewneck-sweatshirt/6665302",
]
)
validator = Validator(product_schema, allow_unknown=True)
for item in products_data:
validate_or_fail(item, validator)
assert len(products_data) >= 1
@pytest.mark.asyncio
async def test_search_scraping():
search_data = await nordstorm.scrape_search(
url="https://www.nordstrom.com/sr?origin=keywordsearch&keyword=indigo", max_pages=3
)
validator = Validator(search_schema, allow_unknown=True)
for item in search_data:
validate_or_fail(item, validator)
assert len(search_data) >= 10