-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy pathtest.py
136 lines (127 loc) · 4.47 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
from cerberus import Validator
import pytest
import stockx
import pprint
pp = pprint.PrettyPrinter(indent=4)
# enable cache?
stockx.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
@pytest.mark.flaky(reruns=3, reruns_delay=30)
async def test_product_scraping():
result = await stockx.scrape_product("https://stockx.com/nike-x-stussy-bucket-hat-black")
_market_schema = {
"bidAskData": {
"type": "dict",
"schema": {
"lowestAsk": {"type": "integer", "nullable": True},
"numberOfAsks": {"type": "integer", "nullable": True},
"highestBid": {"type": "integer", "nullable": True},
"numberOfBids": {"type": "integer", "nullable": True},
},
},
"salesInformation": {
"type": "dict",
"schema": {
"lastSale": {"type": "integer"},
"salesLast72Hours": {"type": "integer"},
},
},
"statistics": {
"type": "dict",
"schema": {
"lastSale": {
"type": "dict",
"schema": {
"amount": {"type": "integer"},
"changePercentage": {"type": "float"},
"changeValue": {"type": "integer"},
"sameFees": {"type": "boolean"},
},
},
},
},
}
schema = {
"id": {"type": "string"},
"listingType": {"type": "string"},
"deleted": {"type": "boolean"},
"gender": {"type": "string"},
"title": {"type": "string"},
"brand": {"type": "string"},
"description": {"type": "string"},
"model": {"type": "string"},
"variants": {
"type": "list",
"schema": {
"type": "dict",
"schema": {
"id": {"type": "string"},
"market": {"type": "dict", "schema": _market_schema},
},
},
},
"market": {
"type": "dict",
"schema": _market_schema,
},
"pricing": {
"type": "dict",
"schema": {
"minimumBid": {"type": "float"},
"market": {
"type": "dict",
"schema": {
"state": {
"type": "dict",
"schema": {
"lowestAsk": {
"type": "dict",
"schema": {
"amount": {"type": "float"},
"currency": {"type": "string"},
},
},
"highestBid": {
"type": "dict",
"schema": {
"amount": {"type": "float"},
},
},
"numberOfAsks": {"type": "integer"},
"numberOfBids": {"type": "integer"},
},
}
},
},
"variants": {
"type": "list",
"schema": {
"type": "dict",
"schema": {"id": {"type": "string"}},
},
},
},
},
}
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():
result = await stockx.scrape_search("https://stockx.com/search?s=nike", max_pages=2)
schema = {
"id": {"type": "string"},
"name": {"type": "string"},
"urlKey": {"type": "string"},
"title": {"type": "string"},
"brand": {"type": "string"},
"description": {"type": "string"},
"model": {"type": "string"},
}
validator = Validator(schema, allow_unknown=True)
for item in result:
validate_or_fail(item, validator)