-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathtest.py
93 lines (82 loc) · 3.1 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
from cerberus import Validator
import pytest
import realtorcom
import pprint
pp = pprint.PrettyPrinter(indent=4)
# enable cache?
realtorcom.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_property_scraping():
url = "https://www.realtor.com/realestateandhomes-detail/12355-Attlee-Dr_Houston_TX_77077_M70330-35605"
result = await realtorcom.scrape_property(url)
schema = {
"id": {"type": "string"},
"href": {"type": "string", "regex": "https://www.realtor.com/realestateandhomes-detail/.+?"},
"status": {"type": "string"},
"sold_date": {"type": "string", "regex": "\d+-\d+-\d+", "nullable": True},
"tags": {"type": "list", "schema": {"type": "string"}},
"list_price": {"type": "integer"},
"list_price_last_change": {"type": "integer"},
"details": {
"type": "dict",
"schema": {
"beds": {"type": "integer"},
"baths": {"type": "integer"},
},
},
"flags": {"type": "dict"},
"phones": {"type": "list", "schema": {"type": "dict", "schema": {"number": {"type": "string"}}}},
"location": {
"type": "dict",
"schema": {
"address": {
"type": "dict",
"schema": {
"city": {"type": "string"},
"line": {"type": "string"},
"state": {"type": "string"},
"postal_code": {"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 realtorcom.scrape_search("CA", "San-Francisco", max_pages=2)
schema = {
"property_id": {"type": "string"},
"permalink": {"type": "string"},
"list_price": {"type": "integer", "nullable": True}, # some propreties can have no price
"list_date": {"type": "string"},
"photos": {
"type": "list",
"nullable": True,
"schema": {
"type": "dict",
"schema": {
"href": {"type": "string"}
}
}
},
}
validator = Validator(schema, allow_unknown=True)
for item in result:
validate_or_fail(item, validator)
@pytest.mark.asyncio
@pytest.mark.flaky(reruns=3, reruns_delay=30)
async def test_feed_scraping():
url = "https://www.realtor.com/realestateandhomes-detail/sitemap-rss-price/rss-price-ca.xml"
result_feed = await realtorcom.scrape_feed(url)
assert len(result_feed) > 0
for url, lastmod in result_feed.items():
assert url.startswith("https://www.realtor.com/")
assert lastmod is not None